int bdrv_write(BlockDriverState *bs, int64_t sector_num,
               const uint8_t *buf, int nb_sectors)
{
    BlockDriver *drv = bs->drv;
    if (!bs->drv)
        return -ENOMEDIUM;
    if (bs->read_only)
        return -EACCES;
    if (drv->bdrv_pwrite) {
        int ret, len, count = 0;
        len = nb_sectors * 512;
        do {
            ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len - count);
            if (ret < 0) {
                printf("bdrv_write ret=%d\n", ret);
                return ret;
            }
            count += ret;
            buf += ret;
        } while (count != len);
        bs->wr_bytes += (unsigned) len;
        bs->wr_ops ++;
        return 0;
    }
    return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
}
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
               const uint8_t *buf, int nb_sectors)
{
    BlockDriver *drv = bs->drv;
    if (!bs->drv)
        return -ENOMEDIUM;
    if (bs->read_only)
        return -EACCES;
    if (drv->bdrv_pwrite) {
        int ret, len, count = 0;
        len = nb_sectors * 512;
        do {
            ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len - count);
            if (ret < 0) {
                printf("bdrv_write ret=%d\n", ret);
                return ret;
            }
            count += ret;
            buf += ret;
        } while (count != len);
        bs->wr_bytes += (unsigned) len;
        bs->wr_ops ++;
        return 0;
    }
    return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
}
int FUNC_0(BlockDriverState *VAR_0, int64_t VAR_1,
               const uint8_t *VAR_2, int VAR_3)
{
    BlockDriver *drv = VAR_0->drv;
    if (!VAR_0->drv)
        return -ENOMEDIUM;
    if (VAR_0->read_only)
        return -EACCES;
    if (drv->bdrv_pwrite) {
        int VAR_4, VAR_5, VAR_6 = 0;
        VAR_5 = VAR_3 * 512;
        do {
            VAR_4 = drv->bdrv_pwrite(VAR_0, VAR_1 * 512, VAR_2, VAR_5 - VAR_6);
            if (VAR_4 < 0) {
                printf("FUNC_0 VAR_4=%d\n", VAR_4);
                return VAR_4;
            }
            VAR_6 += VAR_4;
            VAR_2 += VAR_4;
        } while (VAR_6 != VAR_5);
        VAR_0->wr_bytes += (unsigned) VAR_5;
        VAR_0->wr_ops ++;
        return 0;
    }
    return drv->FUNC_0(VAR_0, VAR_1, VAR_2, VAR_3);
}
int FUNC_0(BlockDriverState *VAR_0, int64_t VAR_1,
const uint8_t *VAR_2, int VAR_3)
{ BlockDriver *drv = VAR_0->drv; if (!VAR_0->drv)
return -ENOMEDIUM; if (VAR_0->read_only)
return -EACCES; if (drv->bdrv_pwrite) { int VAR_4, VAR_5, VAR_6 = 0; VAR_5 = VAR_3 * 512; do { VAR_4 = drv->bdrv_pwrite(VAR_0, VAR_1 * 512, VAR_2, VAR_5 - VAR_6); if (VAR_4 < 0) { printf("FUNC_0 VAR_4=%d\n", VAR_4); return VAR_4; } VAR_6 += VAR_4; VAR_2 += VAR_4; } while (VAR_6 != VAR_5); VAR_0->wr_bytes += (unsigned) VAR_5; VAR_0->wr_ops ++; return 0; } return drv->FUNC_0(VAR_0, VAR_1, VAR_2, VAR_3); }