static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,

                         CURLAIOCB *acb)

{

    int i;

    size_t end = start + len;



    for (i=0; i<CURL_NUM_STATES; i++) {

        CURLState *state = &s->states[i];

        size_t buf_end = (state->buf_start + state->buf_off);

        size_t buf_fend = (state->buf_start + state->buf_len);



        if (!state->orig_buf)

            continue;

        if (!state->buf_off)

            continue;



        // Does the existing buffer cover our section?

        if ((start >= state->buf_start) &&

            (start <= buf_end) &&

            (end >= state->buf_start) &&

            (end <= buf_end))

        {

            char *buf = state->orig_buf + (start - state->buf_start);



            qemu_iovec_from_buf(acb->qiov, 0, buf, len);

            acb->common.cb(acb->common.opaque, 0);



            return FIND_RET_OK;

        }



        // Wait for unfinished chunks

        if ((start >= state->buf_start) &&

            (start <= buf_fend) &&

            (end >= state->buf_start) &&

            (end <= buf_fend))

        {

            int j;



            acb->start = start - state->buf_start;

            acb->end = acb->start + len;



            for (j=0; j<CURL_NUM_ACB; j++) {

                if (!state->acb[j]) {

                    state->acb[j] = acb;

                    return FIND_RET_WAIT;

                }

            }

        }

    }



    return FIND_RET_NONE;

}

static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,

                         CURLAIOCB *acb)

{

    int i;

    size_t end = start + len;



    for (i=0; i<CURL_NUM_STATES; i++) {

        CURLState *state = &s->states[i];

        size_t buf_end = (state->buf_start + state->buf_off);

        size_t buf_fend = (state->buf_start + state->buf_len);



        if (!state->orig_buf)

            continue;

        if (!state->buf_off)

            continue;



        

        if ((start >= state->buf_start) &&

            (start <= buf_end) &&

            (end >= state->buf_start) &&

            (end <= buf_end))

        {

            char *buf = state->orig_buf + (start - state->buf_start);



            qemu_iovec_from_buf(acb->qiov, 0, buf, len);

            acb->common.cb(acb->common.opaque, 0);



            return FIND_RET_OK;

        }



        

        if ((start >= state->buf_start) &&

            (start <= buf_fend) &&

            (end >= state->buf_start) &&

            (end <= buf_fend))

        {

            int j;



            acb->start = start - state->buf_start;

            acb->end = acb->start + len;



            for (j=0; j<CURL_NUM_ACB; j++) {

                if (!state->acb[j]) {

                    state->acb[j] = acb;

                    return FIND_RET_WAIT;

                }

            }

        }

    }



    return FIND_RET_NONE;

}

static int FUNC_0(BDRVCURLState *VAR_0, size_t VAR_1, size_t VAR_2,

                         CURLAIOCB *VAR_3)

{

    int VAR_4;

    size_t end = VAR_1 + VAR_2;



    for (VAR_4=0; VAR_4<CURL_NUM_STATES; VAR_4++) {

        CURLState *state = &VAR_0->states[VAR_4];

        size_t buf_end = (state->buf_start + state->buf_off);

        size_t buf_fend = (state->buf_start + state->buf_len);



        if (!state->orig_buf)

            continue;

        if (!state->buf_off)

            continue;



        

        if ((VAR_1 >= state->buf_start) &&

            (VAR_1 <= buf_end) &&

            (end >= state->buf_start) &&

            (end <= buf_end))

        {

            char *buf = state->orig_buf + (VAR_1 - state->buf_start);



            qemu_iovec_from_buf(VAR_3->qiov, 0, buf, VAR_2);

            VAR_3->common.cb(VAR_3->common.opaque, 0);



            return FIND_RET_OK;

        }



        

        if ((VAR_1 >= state->buf_start) &&

            (VAR_1 <= buf_fend) &&

            (end >= state->buf_start) &&

            (end <= buf_fend))

        {

            int j;



            VAR_3->VAR_1 = VAR_1 - state->buf_start;

            VAR_3->end = VAR_3->VAR_1 + VAR_2;



            for (j=0; j<CURL_NUM_ACB; j++) {

                if (!state->VAR_3[j]) {

                    state->VAR_3[j] = VAR_3;

                    return FIND_RET_WAIT;

                }

            }

        }

    }



    return FIND_RET_NONE;

}

static int FUNC_0(BDRVCURLState *VAR_0, size_t VAR_1, size_t VAR_2,
CURLAIOCB *VAR_3)
{ int VAR_4; size_t end = VAR_1 + VAR_2; for (VAR_4=0; VAR_4<CURL_NUM_STATES; VAR_4++) { CURLState *state = &VAR_0->states[VAR_4]; size_t buf_end = (state->buf_start + state->buf_off); size_t buf_fend = (state->buf_start + state->buf_len); if (!state->orig_buf)
continue; if (!state->buf_off)
continue; if ((VAR_1 >= state->buf_start) &&
(VAR_1 <= buf_end) &&
(end >= state->buf_start) &&
(end <= buf_end))
{ char *buf = state->orig_buf + (VAR_1 - state->buf_start); qemu_iovec_from_buf(VAR_3->qiov, 0, buf, VAR_2); VAR_3->common.cb(VAR_3->common.opaque, 0); return FIND_RET_OK; } if ((VAR_1 >= state->buf_start) &&
(VAR_1 <= buf_fend) &&
(end >= state->buf_start) &&
(end <= buf_fend))
{ int j; VAR_3->VAR_1 = VAR_1 - state->buf_start; VAR_3->end = VAR_3->VAR_1 + VAR_2; for (j=0; j<CURL_NUM_ACB; j++) { if (!state->VAR_3[j]) { state->VAR_3[j] = VAR_3; return FIND_RET_WAIT; } } } } return FIND_RET_NONE; }