int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes)

{

    unsigned int idx;

    unsigned int total_bufs, in_total, out_total;



    idx = vq->last_avail_idx;



    total_bufs = in_total = out_total = 0;

    while (virtqueue_num_heads(vq, idx)) {

        unsigned int max, num_bufs, indirect = 0;

        target_phys_addr_t desc_pa;

        int i;



        max = vq->vring.num;

        num_bufs = total_bufs;

        i = virtqueue_get_head(vq, idx++);

        desc_pa = vq->vring.desc;



        if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {

            if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {

                error_report("Invalid size for indirect buffer table");

                exit(1);

            }



            /* If we've got too many, that implies a descriptor loop. */

            if (num_bufs >= max) {

                error_report("Looped descriptor");

                exit(1);

            }



            /* loop over the indirect descriptor table */

            indirect = 1;

            max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);

            num_bufs = i = 0;

            desc_pa = vring_desc_addr(desc_pa, i);

        }



        do {

            /* If we've got too many, that implies a descriptor loop. */

            if (++num_bufs > max) {

                error_report("Looped descriptor");

                exit(1);

            }



            if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {

                if (in_bytes > 0 &&

                    (in_total += vring_desc_len(desc_pa, i)) >= in_bytes)

                    return 1;

            } else {

                if (out_bytes > 0 &&

                    (out_total += vring_desc_len(desc_pa, i)) >= out_bytes)

                    return 1;

            }

        } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);



        if (!indirect)

            total_bufs = num_bufs;

        else

            total_bufs++;

    }



    return 0;

}

int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes)

{

    unsigned int idx;

    unsigned int total_bufs, in_total, out_total;



    idx = vq->last_avail_idx;



    total_bufs = in_total = out_total = 0;

    while (virtqueue_num_heads(vq, idx)) {

        unsigned int max, num_bufs, indirect = 0;

        target_phys_addr_t desc_pa;

        int i;



        max = vq->vring.num;

        num_bufs = total_bufs;

        i = virtqueue_get_head(vq, idx++);

        desc_pa = vq->vring.desc;



        if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {

            if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {

                error_report("Invalid size for indirect buffer table");

                exit(1);

            }



            

            if (num_bufs >= max) {

                error_report("Looped descriptor");

                exit(1);

            }



            

            indirect = 1;

            max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);

            num_bufs = i = 0;

            desc_pa = vring_desc_addr(desc_pa, i);

        }



        do {

            

            if (++num_bufs > max) {

                error_report("Looped descriptor");

                exit(1);

            }



            if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {

                if (in_bytes > 0 &&

                    (in_total += vring_desc_len(desc_pa, i)) >= in_bytes)

                    return 1;

            } else {

                if (out_bytes > 0 &&

                    (out_total += vring_desc_len(desc_pa, i)) >= out_bytes)

                    return 1;

            }

        } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);



        if (!indirect)

            total_bufs = num_bufs;

        else

            total_bufs++;

    }



    return 0;

}

int FUNC_0(VirtQueue *VAR_0, int VAR_1, int VAR_2)

{

    unsigned int VAR_3;

    unsigned int VAR_4, VAR_5, VAR_6;



    VAR_3 = VAR_0->last_avail_idx;



    VAR_4 = VAR_5 = VAR_6 = 0;

    while (virtqueue_num_heads(VAR_0, VAR_3)) {

        unsigned int VAR_7, VAR_8, VAR_9 = 0;

        target_phys_addr_t desc_pa;

        int VAR_10;



        VAR_7 = VAR_0->vring.num;

        VAR_8 = VAR_4;

        VAR_10 = virtqueue_get_head(VAR_0, VAR_3++);

        desc_pa = VAR_0->vring.desc;



        if (vring_desc_flags(desc_pa, VAR_10) & VRING_DESC_F_INDIRECT) {

            if (vring_desc_len(desc_pa, VAR_10) % sizeof(VRingDesc)) {

                error_report("Invalid size for VAR_9 buffer table");

                exit(1);

            }



            

            if (VAR_8 >= VAR_7) {

                error_report("Looped descriptor");

                exit(1);

            }



            

            VAR_9 = 1;

            VAR_7 = vring_desc_len(desc_pa, VAR_10) / sizeof(VRingDesc);

            VAR_8 = VAR_10 = 0;

            desc_pa = vring_desc_addr(desc_pa, VAR_10);

        }



        do {

            

            if (++VAR_8 > VAR_7) {

                error_report("Looped descriptor");

                exit(1);

            }



            if (vring_desc_flags(desc_pa, VAR_10) & VRING_DESC_F_WRITE) {

                if (VAR_1 > 0 &&

                    (VAR_5 += vring_desc_len(desc_pa, VAR_10)) >= VAR_1)

                    return 1;

            } else {

                if (VAR_2 > 0 &&

                    (VAR_6 += vring_desc_len(desc_pa, VAR_10)) >= VAR_2)

                    return 1;

            }

        } while ((VAR_10 = virtqueue_next_desc(desc_pa, VAR_10, VAR_7)) != VAR_7);



        if (!VAR_9)

            VAR_4 = VAR_8;

        else

            VAR_4++;

    }



    return 0;

}

int FUNC_0(VirtQueue *VAR_0, int VAR_1, int VAR_2)
{ unsigned int VAR_3; unsigned int VAR_4, VAR_5, VAR_6; VAR_3 = VAR_0->last_avail_idx; VAR_4 = VAR_5 = VAR_6 = 0; while (virtqueue_num_heads(VAR_0, VAR_3)) { unsigned int VAR_7, VAR_8, VAR_9 = 0; target_phys_addr_t desc_pa; int VAR_10; VAR_7 = VAR_0->vring.num; VAR_8 = VAR_4; VAR_10 = virtqueue_get_head(VAR_0, VAR_3++); desc_pa = VAR_0->vring.desc; if (vring_desc_flags(desc_pa, VAR_10) & VRING_DESC_F_INDIRECT) { if (vring_desc_len(desc_pa, VAR_10) % sizeof(VRingDesc)) { error_report("Invalid size for VAR_9 buffer table"); exit(1); } if (VAR_8 >= VAR_7) { error_report("Looped descriptor"); exit(1); } VAR_9 = 1; VAR_7 = vring_desc_len(desc_pa, VAR_10) / sizeof(VRingDesc); VAR_8 = VAR_10 = 0; desc_pa = vring_desc_addr(desc_pa, VAR_10); } do { if (++VAR_8 > VAR_7) { error_report("Looped descriptor"); exit(1); } if (vring_desc_flags(desc_pa, VAR_10) & VRING_DESC_F_WRITE) { if (VAR_1 > 0 &&
(VAR_5 += vring_desc_len(desc_pa, VAR_10)) >= VAR_1)
return 1; } else { if (VAR_2 > 0 &&
(VAR_6 += vring_desc_len(desc_pa, VAR_10)) >= VAR_2)
return 1; } } while ((VAR_10 = virtqueue_next_desc(desc_pa, VAR_10, VAR_7)) != VAR_7); if (!VAR_9)
VAR_4 = VAR_8; else
VAR_4++; } return 0; }