static int qed_write_table(BDRVQEDState *s, uint64_t offset, QEDTable *table,

                           unsigned int index, unsigned int n, bool flush)

{

    unsigned int sector_mask = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1;

    unsigned int start, end, i;

    QEDTable *new_table;

    struct iovec iov;

    QEMUIOVector qiov;

    size_t len_bytes;

    int ret;



    trace_qed_write_table(s, offset, table, index, n);



    /* Calculate indices of the first and one after last elements */

    start = index & ~sector_mask;

    end = (index + n + sector_mask) & ~sector_mask;



    len_bytes = (end - start) * sizeof(uint64_t);



    new_table = qemu_blockalign(s->bs, len_bytes);

    iov = (struct iovec) {

        .iov_base = new_table->offsets,

        .iov_len = len_bytes,

    };

    qemu_iovec_init_external(&qiov, &iov, 1);



    /* Byteswap table */

    for (i = start; i < end; i++) {

        uint64_t le_offset = cpu_to_le64(table->offsets[i]);

        new_table->offsets[i - start] = le_offset;

    }



    /* Adjust for offset into table */

    offset += start * sizeof(uint64_t);



    ret = bdrv_pwritev(s->bs->file, offset, &qiov);

    trace_qed_write_table_cb(s, table, flush, ret);

    if (ret < 0) {

        goto out;

    }



    if (flush) {

        qed_acquire(s);

        ret = bdrv_flush(s->bs);

        qed_release(s);

        if (ret < 0) {

            goto out;

        }

    }



    ret = 0;

out:

    qemu_vfree(new_table);

    return ret;

}

static int qed_write_table(BDRVQEDState *s, uint64_t offset, QEDTable *table,

                           unsigned int index, unsigned int n, bool flush)

{

    unsigned int sector_mask = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1;

    unsigned int start, end, i;

    QEDTable *new_table;

    struct iovec iov;

    QEMUIOVector qiov;

    size_t len_bytes;

    int ret;



    trace_qed_write_table(s, offset, table, index, n);



    

    start = index & ~sector_mask;

    end = (index + n + sector_mask) & ~sector_mask;



    len_bytes = (end - start) * sizeof(uint64_t);



    new_table = qemu_blockalign(s->bs, len_bytes);

    iov = (struct iovec) {

        .iov_base = new_table->offsets,

        .iov_len = len_bytes,

    };

    qemu_iovec_init_external(&qiov, &iov, 1);



    

    for (i = start; i < end; i++) {

        uint64_t le_offset = cpu_to_le64(table->offsets[i]);

        new_table->offsets[i - start] = le_offset;

    }



    

    offset += start * sizeof(uint64_t);



    ret = bdrv_pwritev(s->bs->file, offset, &qiov);

    trace_qed_write_table_cb(s, table, flush, ret);

    if (ret < 0) {

        goto out;

    }



    if (flush) {

        qed_acquire(s);

        ret = bdrv_flush(s->bs);

        qed_release(s);

        if (ret < 0) {

            goto out;

        }

    }



    ret = 0;

out:

    qemu_vfree(new_table);

    return ret;

}

static int FUNC_0(BDRVQEDState *VAR_0, uint64_t VAR_1, QEDTable *VAR_2,

                           unsigned int VAR_3, unsigned int VAR_4, bool VAR_5)

{

    unsigned int VAR_6 = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1;

    unsigned int VAR_7, VAR_8, VAR_9;

    QEDTable *new_table;

    struct iovec VAR_10;

    QEMUIOVector qiov;

    size_t len_bytes;

    int VAR_11;



    trace_qed_write_table(VAR_0, VAR_1, VAR_2, VAR_3, VAR_4);



    

    VAR_7 = VAR_3 & ~VAR_6;

    VAR_8 = (VAR_3 + VAR_4 + VAR_6) & ~VAR_6;



    len_bytes = (VAR_8 - VAR_7) * sizeof(uint64_t);



    new_table = qemu_blockalign(VAR_0->bs, len_bytes);

    VAR_10 = (struct iovec) {

        .iov_base = new_table->offsets,

        .iov_len = len_bytes,

    };

    qemu_iovec_init_external(&qiov, &VAR_10, 1);



    

    for (VAR_9 = VAR_7; VAR_9 < VAR_8; VAR_9++) {

        uint64_t le_offset = cpu_to_le64(VAR_2->offsets[VAR_9]);

        new_table->offsets[VAR_9 - VAR_7] = le_offset;

    }



    

    VAR_1 += VAR_7 * sizeof(uint64_t);



    VAR_11 = bdrv_pwritev(VAR_0->bs->file, VAR_1, &qiov);

    trace_qed_write_table_cb(VAR_0, VAR_2, VAR_5, VAR_11);

    if (VAR_11 < 0) {

        goto out;

    }



    if (VAR_5) {

        qed_acquire(VAR_0);

        VAR_11 = bdrv_flush(VAR_0->bs);

        qed_release(VAR_0);

        if (VAR_11 < 0) {

            goto out;

        }

    }



    VAR_11 = 0;

out:

    qemu_vfree(new_table);

    return VAR_11;

}

static int FUNC_0(BDRVQEDState *VAR_0, uint64_t VAR_1, QEDTable *VAR_2,
unsigned int VAR_3, unsigned int VAR_4, bool VAR_5)
{ unsigned int VAR_6 = BDRV_SECTOR_SIZE / sizeof(uint64_t) - 1; unsigned int VAR_7, VAR_8, VAR_9; QEDTable *new_table; struct iovec VAR_10; QEMUIOVector qiov; size_t len_bytes; int VAR_11; trace_qed_write_table(VAR_0, VAR_1, VAR_2, VAR_3, VAR_4); VAR_7 = VAR_3 & ~VAR_6; VAR_8 = (VAR_3 + VAR_4 + VAR_6) & ~VAR_6; len_bytes = (VAR_8 - VAR_7) * sizeof(uint64_t); new_table = qemu_blockalign(VAR_0->bs, len_bytes); VAR_10 = (struct iovec) { .iov_base = new_table->offsets,
.iov_len = len_bytes,
}; qemu_iovec_init_external(&qiov, &VAR_10, 1); for (VAR_9 = VAR_7; VAR_9 < VAR_8; VAR_9++) { uint64_t le_offset = cpu_to_le64(VAR_2->offsets[VAR_9]); new_table->offsets[VAR_9 - VAR_7] = le_offset; } VAR_1 += VAR_7 * sizeof(uint64_t); VAR_11 = bdrv_pwritev(VAR_0->bs->file, VAR_1, &qiov); trace_qed_write_table_cb(VAR_0, VAR_2, VAR_5, VAR_11); if (VAR_11 < 0) { goto out; } if (VAR_5) { qed_acquire(VAR_0); VAR_11 = bdrv_flush(VAR_0->bs); qed_release(VAR_0); if (VAR_11 < 0) { goto out; } } VAR_11 = 0; out:
qemu_vfree(new_table); return VAR_11; }