static int archipelago_aio_segmented_rw(BDRVArchipelagoState *s,

                                        size_t count,

                                        off_t offset,

                                        ArchipelagoAIOCB *aio_cb,

                                        int op)

{

    int i, ret, segments_nr, last_segment_size;

    ArchipelagoSegmentedRequest *segreq;



    segreq = g_malloc(sizeof(ArchipelagoSegmentedRequest));



    if (op == ARCHIP_OP_FLUSH) {

        segments_nr = 1;

        segreq->ref = segments_nr;

        segreq->total = count;

        segreq->count = 0;

        segreq->failed = 0;

        ret = archipelago_submit_request(s, 0, count, offset, aio_cb,

                                           segreq, ARCHIP_OP_FLUSH);

        if (ret < 0) {

            goto err_exit;

        }

        return 0;

    }



    segments_nr = (int)(count / MAX_REQUEST_SIZE) + \

                  ((count % MAX_REQUEST_SIZE) ? 1 : 0);

    last_segment_size = (int)(count % MAX_REQUEST_SIZE);



    segreq->ref = segments_nr;

    segreq->total = count;

    segreq->count = 0;

    segreq->failed = 0;



    for (i = 0; i < segments_nr - 1; i++) {

        ret = archipelago_submit_request(s, i * MAX_REQUEST_SIZE,

                                           MAX_REQUEST_SIZE,

                                           offset + i * MAX_REQUEST_SIZE,

                                           aio_cb, segreq, op);



        if (ret < 0) {

            goto err_exit;

        }

    }



    if ((segments_nr > 1) && last_segment_size) {

        ret = archipelago_submit_request(s, i * MAX_REQUEST_SIZE,

                                           last_segment_size,

                                           offset + i * MAX_REQUEST_SIZE,

                                           aio_cb, segreq, op);

    } else if ((segments_nr > 1) && !last_segment_size) {

        ret = archipelago_submit_request(s, i * MAX_REQUEST_SIZE,

                                           MAX_REQUEST_SIZE,

                                           offset + i * MAX_REQUEST_SIZE,

                                           aio_cb, segreq, op);

    } else if (segments_nr == 1) {

            ret = archipelago_submit_request(s, 0, count, offset, aio_cb,

                                               segreq, op);

    }



    if (ret < 0) {

        goto err_exit;

    }



    return 0;



err_exit:

    __sync_add_and_fetch(&segreq->failed, 1);

    if (segments_nr == 1) {

        if (__sync_add_and_fetch(&segreq->ref, -1) == 0) {

            g_free(segreq);

        }

    } else {

        if ((__sync_add_and_fetch(&segreq->ref, -segments_nr + i)) == 0) {

            g_free(segreq);

        }

    }



    return ret;

}

static int archipelago_aio_segmented_rw(BDRVArchipelagoState *s,

                                        size_t count,

                                        off_t offset,

                                        ArchipelagoAIOCB *aio_cb,

                                        int op)

{

    int i, ret, segments_nr, last_segment_size;

    ArchipelagoSegmentedRequest *segreq;



    segreq = g_malloc(sizeof(ArchipelagoSegmentedRequest));



    if (op == ARCHIP_OP_FLUSH) {

        segments_nr = 1;

        segreq->ref = segments_nr;

        segreq->total = count;

        segreq->count = 0;

        segreq->failed = 0;

        ret = archipelago_submit_request(s, 0, count, offset, aio_cb,

                                           segreq, ARCHIP_OP_FLUSH);

        if (ret < 0) {

            goto err_exit;

        }

        return 0;

    }



    segments_nr = (int)(count / MAX_REQUEST_SIZE) + \

                  ((count % MAX_REQUEST_SIZE) ? 1 : 0);

    last_segment_size = (int)(count % MAX_REQUEST_SIZE);



    segreq->ref = segments_nr;

    segreq->total = count;

    segreq->count = 0;

    segreq->failed = 0;



    for (i = 0; i < segments_nr - 1; i++) {

        ret = archipelago_submit_request(s, i * MAX_REQUEST_SIZE,

                                           MAX_REQUEST_SIZE,

                                           offset + i * MAX_REQUEST_SIZE,

                                           aio_cb, segreq, op);



        if (ret < 0) {

            goto err_exit;

        }

    }



    if ((segments_nr > 1) && last_segment_size) {

        ret = archipelago_submit_request(s, i * MAX_REQUEST_SIZE,

                                           last_segment_size,

                                           offset + i * MAX_REQUEST_SIZE,

                                           aio_cb, segreq, op);

    } else if ((segments_nr > 1) && !last_segment_size) {

        ret = archipelago_submit_request(s, i * MAX_REQUEST_SIZE,

                                           MAX_REQUEST_SIZE,

                                           offset + i * MAX_REQUEST_SIZE,

                                           aio_cb, segreq, op);

    } else if (segments_nr == 1) {

            ret = archipelago_submit_request(s, 0, count, offset, aio_cb,

                                               segreq, op);

    }



    if (ret < 0) {

        goto err_exit;

    }



    return 0;



err_exit:

    __sync_add_and_fetch(&segreq->failed, 1);

    if (segments_nr == 1) {

        if (__sync_add_and_fetch(&segreq->ref, -1) == 0) {

            g_free(segreq);

        }

    } else {

        if ((__sync_add_and_fetch(&segreq->ref, -segments_nr + i)) == 0) {

            g_free(segreq);

        }

    }



    return ret;

}

    segreq = g_malloc(sizeof(ArchipelagoSegmentedRequest));
static int FUNC_0(BDRVArchipelagoState *VAR_0,

                                        size_t VAR_1,

                                        off_t VAR_2,

                                        ArchipelagoAIOCB *VAR_3,

                                        int VAR_4)

{

    int VAR_5, VAR_6, VAR_7, VAR_8;

    ArchipelagoSegmentedRequest *segreq;



    segreq = g_malloc(sizeof(ArchipelagoSegmentedRequest));



    if (VAR_4 == ARCHIP_OP_FLUSH) {

        VAR_7 = 1;

        segreq->ref = VAR_7;

        segreq->total = VAR_1;

        segreq->VAR_1 = 0;

        segreq->failed = 0;

        VAR_6 = archipelago_submit_request(VAR_0, 0, VAR_1, VAR_2, VAR_3,

                                           segreq, ARCHIP_OP_FLUSH);

        if (VAR_6 < 0) {

            goto err_exit;

        }

        return 0;

    }



    VAR_7 = (int)(VAR_1 / MAX_REQUEST_SIZE) + \

                  ((VAR_1 % MAX_REQUEST_SIZE) ? 1 : 0);

    VAR_8 = (int)(VAR_1 % MAX_REQUEST_SIZE);



    segreq->ref = VAR_7;

    segreq->total = VAR_1;

    segreq->VAR_1 = 0;

    segreq->failed = 0;



    for (VAR_5 = 0; VAR_5 < VAR_7 - 1; VAR_5++) {

        VAR_6 = archipelago_submit_request(VAR_0, VAR_5 * MAX_REQUEST_SIZE,

                                           MAX_REQUEST_SIZE,

                                           VAR_2 + VAR_5 * MAX_REQUEST_SIZE,

                                           VAR_3, segreq, VAR_4);



        if (VAR_6 < 0) {

            goto err_exit;

        }

    }



    if ((VAR_7 > 1) && VAR_8) {

        VAR_6 = archipelago_submit_request(VAR_0, VAR_5 * MAX_REQUEST_SIZE,

                                           VAR_8,

                                           VAR_2 + VAR_5 * MAX_REQUEST_SIZE,

                                           VAR_3, segreq, VAR_4);

    } else if ((VAR_7 > 1) && !VAR_8) {

        VAR_6 = archipelago_submit_request(VAR_0, VAR_5 * MAX_REQUEST_SIZE,

                                           MAX_REQUEST_SIZE,

                                           VAR_2 + VAR_5 * MAX_REQUEST_SIZE,

                                           VAR_3, segreq, VAR_4);

    } else if (VAR_7 == 1) {

            VAR_6 = archipelago_submit_request(VAR_0, 0, VAR_1, VAR_2, VAR_3,

                                               segreq, VAR_4);

    }



    if (VAR_6 < 0) {

        goto err_exit;

    }



    return 0;



err_exit:

    __sync_add_and_fetch(&segreq->failed, 1);

    if (VAR_7 == 1) {

        if (__sync_add_and_fetch(&segreq->ref, -1) == 0) {

            g_free(segreq);

        }

    } else {

        if ((__sync_add_and_fetch(&segreq->ref, -VAR_7 + VAR_5)) == 0) {

            g_free(segreq);

        }

    }



    return VAR_6;

}

static int FUNC_0(BDRVArchipelagoState *VAR_0,
size_t VAR_1,
off_t VAR_2,
ArchipelagoAIOCB *VAR_3,
int VAR_4)
{ int VAR_5, VAR_6, VAR_7, VAR_8; ArchipelagoSegmentedRequest *segreq; segreq = g_malloc(sizeof(ArchipelagoSegmentedRequest)); if (VAR_4 == ARCHIP_OP_FLUSH) { VAR_7 = 1; segreq->ref = VAR_7; segreq->total = VAR_1; segreq->VAR_1 = 0; segreq->failed = 0; VAR_6 = archipelago_submit_request(VAR_0, 0, VAR_1, VAR_2, VAR_3,
segreq, ARCHIP_OP_FLUSH); if (VAR_6 < 0) { goto err_exit; } return 0; } VAR_7 = (int)(VAR_1 / MAX_REQUEST_SIZE) + \
((VAR_1 % MAX_REQUEST_SIZE) ? 1 : 0); VAR_8 = (int)(VAR_1 % MAX_REQUEST_SIZE); segreq->ref = VAR_7; segreq->total = VAR_1; segreq->VAR_1 = 0; segreq->failed = 0; for (VAR_5 = 0; VAR_5 < VAR_7 - 1; VAR_5++) { VAR_6 = archipelago_submit_request(VAR_0, VAR_5 * MAX_REQUEST_SIZE,
MAX_REQUEST_SIZE,
VAR_2 + VAR_5 * MAX_REQUEST_SIZE,
VAR_3, segreq, VAR_4); if (VAR_6 < 0) { goto err_exit; } } if ((VAR_7 > 1) && VAR_8) { VAR_6 = archipelago_submit_request(VAR_0, VAR_5 * MAX_REQUEST_SIZE,
VAR_8,
VAR_2 + VAR_5 * MAX_REQUEST_SIZE,
VAR_3, segreq, VAR_4); } else if ((VAR_7 > 1) && !VAR_8) { VAR_6 = archipelago_submit_request(VAR_0, VAR_5 * MAX_REQUEST_SIZE,
MAX_REQUEST_SIZE,
VAR_2 + VAR_5 * MAX_REQUEST_SIZE,
VAR_3, segreq, VAR_4); } else if (VAR_7 == 1) { VAR_6 = archipelago_submit_request(VAR_0, 0, VAR_1, VAR_2, VAR_3,
segreq, VAR_4); } if (VAR_6 < 0) { goto err_exit; } return 0; err_exit:
__sync_add_and_fetch(&segreq->failed, 1); if (VAR_7 == 1) { if (__sync_add_and_fetch(&segreq->ref, -1) == 0) { g_free(segreq); } } else { if ((__sync_add_and_fetch(&segreq->ref, -VAR_7 + VAR_5)) == 0) { g_free(segreq); } } return VAR_6; }