static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)

{

    int64_t ret;

    int n;



    convert_select_part(s, sector_num);



    assert(s->total_sectors > sector_num);

    n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);



    if (s->sector_next_status <= sector_num) {

        BlockDriverState *file;

        ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]),

                                    sector_num - s->src_cur_offset,

                                    n, &n, &file);

        if (ret < 0) {

            return ret;

        }



        if (ret & BDRV_BLOCK_ZERO) {

            s->status = BLK_ZERO;

        } else if (ret & BDRV_BLOCK_DATA) {

            s->status = BLK_DATA;

        } else if (!s->target_has_backing) {

            /* Without a target backing file we must copy over the contents of

             * the backing file as well. */

            /* TODO Check block status of the backing file chain to avoid

             * needlessly reading zeroes and limiting the iteration to the

             * buffer size */

            s->status = BLK_DATA;

        } else {

            s->status = BLK_BACKING_FILE;

        }



        s->sector_next_status = sector_num + n;

    }



    n = MIN(n, s->sector_next_status - sector_num);

    if (s->status == BLK_DATA) {

        n = MIN(n, s->buf_sectors);

    }



    /* We need to write complete clusters for compressed images, so if an

     * unallocated area is shorter than that, we must consider the whole

     * cluster allocated. */

    if (s->compressed) {

        if (n < s->cluster_sectors) {

            n = MIN(s->cluster_sectors, s->total_sectors - sector_num);

            s->status = BLK_DATA;

        } else {

            n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);

        }

    }



    return n;

}

static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)

{

    int64_t ret;

    int n;



    convert_select_part(s, sector_num);



    assert(s->total_sectors > sector_num);

    n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);



    if (s->sector_next_status <= sector_num) {

        BlockDriverState *file;

        ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]),

                                    sector_num - s->src_cur_offset,

                                    n, &n, &file);

        if (ret < 0) {

            return ret;

        }



        if (ret & BDRV_BLOCK_ZERO) {

            s->status = BLK_ZERO;

        } else if (ret & BDRV_BLOCK_DATA) {

            s->status = BLK_DATA;

        } else if (!s->target_has_backing) {

            



            





            s->status = BLK_DATA;

        } else {

            s->status = BLK_BACKING_FILE;

        }



        s->sector_next_status = sector_num + n;

    }



    n = MIN(n, s->sector_next_status - sector_num);

    if (s->status == BLK_DATA) {

        n = MIN(n, s->buf_sectors);

    }



    





    if (s->compressed) {

        if (n < s->cluster_sectors) {

            n = MIN(s->cluster_sectors, s->total_sectors - sector_num);

            s->status = BLK_DATA;

        } else {

            n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);

        }

    }



    return n;

}

static int FUNC_0(ImgConvertState *VAR_0, int64_t VAR_1)

{

    int64_t ret;

    int VAR_2;



    convert_select_part(VAR_0, VAR_1);



    assert(VAR_0->total_sectors > VAR_1);

    VAR_2 = MIN(VAR_0->total_sectors - VAR_1, BDRV_REQUEST_MAX_SECTORS);



    if (VAR_0->sector_next_status <= VAR_1) {

        BlockDriverState *file;

        ret = bdrv_get_block_status(blk_bs(VAR_0->src[VAR_0->src_cur]),

                                    VAR_1 - VAR_0->src_cur_offset,

                                    VAR_2, &VAR_2, &file);

        if (ret < 0) {

            return ret;

        }



        if (ret & BDRV_BLOCK_ZERO) {

            VAR_0->status = BLK_ZERO;

        } else if (ret & BDRV_BLOCK_DATA) {

            VAR_0->status = BLK_DATA;

        } else if (!VAR_0->target_has_backing) {

            



            





            VAR_0->status = BLK_DATA;

        } else {

            VAR_0->status = BLK_BACKING_FILE;

        }



        VAR_0->sector_next_status = VAR_1 + VAR_2;

    }



    VAR_2 = MIN(VAR_2, VAR_0->sector_next_status - VAR_1);

    if (VAR_0->status == BLK_DATA) {

        VAR_2 = MIN(VAR_2, VAR_0->buf_sectors);

    }



    





    if (VAR_0->compressed) {

        if (VAR_2 < VAR_0->cluster_sectors) {

            VAR_2 = MIN(VAR_0->cluster_sectors, VAR_0->total_sectors - VAR_1);

            VAR_0->status = BLK_DATA;

        } else {

            VAR_2 = QEMU_ALIGN_DOWN(VAR_2, VAR_0->cluster_sectors);

        }

    }



    return VAR_2;

}

static int FUNC_0(ImgConvertState *VAR_0, int64_t VAR_1)
{ int64_t ret; int VAR_2; convert_select_part(VAR_0, VAR_1); assert(VAR_0->total_sectors > VAR_1); VAR_2 = MIN(VAR_0->total_sectors - VAR_1, BDRV_REQUEST_MAX_SECTORS); if (VAR_0->sector_next_status <= VAR_1) { BlockDriverState *file; ret = bdrv_get_block_status(blk_bs(VAR_0->src[VAR_0->src_cur]),
VAR_1 - VAR_0->src_cur_offset,
VAR_2, &VAR_2, &file); if (ret < 0) { return ret; } if (ret & BDRV_BLOCK_ZERO) { VAR_0->status = BLK_ZERO; } else if (ret & BDRV_BLOCK_DATA) { VAR_0->status = BLK_DATA; } else if (!VAR_0->target_has_backing) { VAR_0->status = BLK_DATA; } else { VAR_0->status = BLK_BACKING_FILE; } VAR_0->sector_next_status = VAR_1 + VAR_2; } VAR_2 = MIN(VAR_2, VAR_0->sector_next_status - VAR_1); if (VAR_0->status == BLK_DATA) { VAR_2 = MIN(VAR_2, VAR_0->buf_sectors); } if (VAR_0->compressed) { if (VAR_2 < VAR_0->cluster_sectors) { VAR_2 = MIN(VAR_0->cluster_sectors, VAR_0->total_sectors - VAR_1); VAR_0->status = BLK_DATA; } else { VAR_2 = QEMU_ALIGN_DOWN(VAR_2, VAR_0->cluster_sectors); } } return VAR_2; }