// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/hts.c
// Function: hts_itr_multi_next
int hts_itr_multi_next(htsFile *fd, hts_itr_t *iter, void *r)
{
    void *fp;
    int ret, tid, i, cr, ci;
    hts_pos_t beg, end;
    hts_reglist_t *found_reg;

    if (iter == NULL || iter->finished) return -1;

    if (iter->is_cram) {
        fp = fd->fp.cram;
    } else {
        fp = fd->fp.bgzf;
    }

    if (iter->read_rest) {
        if (iter->curr_off) { // seek to the start
            if (iter->seek(fp, iter->curr_off, SEEK_SET) < 0) {
                hts_log_error("Seek at offset %" PRIu64 " failed.", iter->curr_off);
                return -2;
            }
            iter->curr_off = 0; // only seek once
        }

        ret = iter->readrec(fp, fd, r, &tid, &beg, &end);
        if (ret < 0)
            iter->finished = 1;

        iter->curr_tid = tid;
        iter->curr_beg = beg;
        iter->curr_end = end;

        return ret;
    }
    // A NULL iter->off should always be accompanied by iter->finished.
    assert(iter->off != NULL || iter->nocoor != 0);

    int next_range = 0;
    for (;;) {
        // Note that due to the way bam indexing works, iter->off may contain
        // file chunks that are not actually needed as they contain data
        // beyond the end of the requested region.  These are filtered out
        // by comparing the tid and index into hts_reglist_t::intervals
        // (packed for reasons of convenience into iter->off[iter->i].max)
        // associated with the file region with iter->curr_tid and
        // iter->curr_intv.

        if (next_range
            || iter->curr_off == 0
            || iter->i >= iter->n_off
            || iter->curr_off >= iter->off[iter->i].v
            || (iter->off[iter->i].max >> 32 == iter->curr_tid
                && (iter->off[iter->i].max & 0xffffffff) < iter->curr_intv)) {

            // Jump to the next chunk.  It may be necessary to skip more
            // than one as the iter->off list can include overlapping entries.
            do {
                iter->i++;
            } while (iter->i < iter->n_off
                     && (iter->curr_off >= iter->off[iter->i].v
                         || (iter->off[iter->i].max >> 32 == iter->curr_tid
                             && (iter->off[iter->i].max & 0xffffffff) < iter->curr_intv)));

            if (iter->is_cram && iter->i < iter->n_off) {
                // Ensure iter->curr_reg is correct.
                //
                // We need this for CRAM as we shortcut some of the later
                // logic by getting an end-of-range and continuing to the
                // next offset.
                //
                // We cannot do this for BAM (and fortunately do not need to
                // either) because in BAM world a query to genomic positions
                // GX and GY leading to a seek offsets PX and PY may have
                // GX > GY and PX < PY.  (This is due to the R-tree and falling
                // between intervals, bumping up to a higher bin.)
                // CRAM strictly follows PX >= PY if GX >= GY, so this logic
                // works.
                int want_tid = iter->off[iter->i].max >> 32;
                if (!(iter->curr_reg < iter->n_reg &&
                      iter->reg_list[iter->curr_reg].tid == want_tid)) {
                    int j;
                    for (j = 0; j < iter->n_reg; j++)
                        if (iter->reg_list[j].tid == want_tid)
                            break;
                    if (j == iter->n_reg)
                        return -1;
                    iter->curr_reg = j;
                    iter->curr_tid = iter->reg_list[iter->curr_reg].tid;
                };
                iter->curr_intv = iter->off[iter->i].max & 0xffffffff;
            }

            if (iter->i >= iter->n_off) { // no more chunks, except NOCOORs
                if (iter->nocoor) {
                    next_range = 0;
                    if (iter->seek(fp, iter->nocoor_off, SEEK_SET) < 0) {
                        hts_log_error("Seek at offset %" PRIu64 " failed.", iter->nocoor_off);
                        return -2;
                    }
                    if (iter->is_cram) {
                        cram_range r = { HTS_IDX_NOCOOR };
                        cram_set_option(fp, CRAM_OPT_RANGE_NOSEEK, &r);
                    }

                    // The first slice covering the unmapped reads might
                    // contain a few mapped reads, so scroll
                    // forward until finding the first unmapped read.
                    do {
                        ret = iter->readrec(fp, fd, r, &tid, &beg, &end);
                    } while (tid >= 0 && ret >=0);

                    if (ret < 0)
                        iter->finished = 1;
                    else
                        iter->read_rest = 1;

                    iter->curr_off = 0; // don't seek any more
                    iter->curr_tid = tid;
                    iter->curr_beg = beg;
                    iter->curr_end = end;

                    return ret;
                } else {
                    ret = -1; break;
                }
            } else if (iter->i < iter->n_off) {
                // New chunk may overlap the last one, so ensure we
                // only seek forwards.
                if (iter->curr_off < iter->off[iter->i].u || next_range) {
                    iter->curr_off = iter->off[iter->i].u;

                    // CRAM has the capability of setting an end location.
                    // This means multi-threaded decodes can stop once they
                    // reach that point, rather than pointlessly decoding
                    // more slices than we'll be using.
                    //
                    // We have to be careful here.  Whenever we set the cram
                    // range we need a corresponding seek in order to ensure
                    // we can safely decode at that offset.  We use next_range
                    // var to ensure this is always true; this is set on
                    // end-of-range condition. It's never modified for BAM.
                    if (iter->is_cram) {
                        // Next offset.[uv] tuple, but it's already been
                        // included in our cram range, so don't seek and don't
                        // reset range so we can efficiently multi-thread.
                        if (next_range || iter->curr_off >= iter->end) {
                            if (iter->seek(fp, iter->curr_off, SEEK_SET) < 0) {
                                hts_log_error("Seek at offset %" PRIu64
                                        " failed.", iter->curr_off);
                                return -2;
                            }

                            // Find the genomic range matching this interval.
                            int j;
                            hts_reglist_t *rl = &iter->reg_list[iter->curr_reg];
                            cram_range r = {
                                    rl->tid,
                                    rl->intervals[iter->curr_intv].beg,
                                    rl->intervals[iter->curr_intv].end
                            };

                            // Expand it up to cover neighbouring intervals.
                            // Note we can only have a single chromosome in a
                            // range, so if we detect our blocks span chromosomes
                            // or we have a multi-ref mode slice, we just use
                            // HTS_IDX_START refid instead.  This doesn't actually
                            // seek (due to CRAM_OPT_RANGE_NOSEEK) and is simply
                            // and indicator of decoding with no end limit.
                            //
                            // That isn't as efficient as it could be, but it's
                            // no poorer than before and it works.
                            int tid = r.refid;
                            int64_t end = r.end;
                            int64_t v = iter->off[iter->i].v;
                            j = iter->i+1;
                            while (j < iter->n_off) {
                                if (iter->off[j].u > v)
                                    break;

                                uint64_t max = iter->off[j].max;
                                if ((max>>32) != tid) {
                                    tid = HTS_IDX_START; // => no range limit
                                } else {
                                    if (end < rl->intervals[max & 0xffffffff].end)
                                        end = rl->intervals[max & 0xffffffff].end;
                                }
                                if (v < iter->off[j].v)
                                    v = iter->off[j].v;
                                j++;
                            }
                            r.refid = tid;
                            r.end = end;

                            // Remember maximum 'v' here so we don't do
                            // unnecessary subsequent seeks for the next
                            // regions.  We can't change curr_off, but
                            // beg/end are used only by single region iterator so
                            // we cache it there to avoid changing the struct.
                            iter->end = v;

                            cram_set_option(fp, CRAM_OPT_RANGE_NOSEEK, &r);
                            next_range = 0;
                        }
                    } else { // Not CRAM
                        if (iter->seek(fp, iter->curr_off, SEEK_SET) < 0) {
                            hts_log_error("Seek at offset %" PRIu64 " failed.",
                                          iter->curr_off);
                            return -2;
                        }
                    }
                }
            }
        }

        ret = iter->readrec(fp, fd, r, &tid, &beg, &end);
        if (ret < 0) {
            if (iter->is_cram && cram_eof(fp)) {
                // Skip to end of range
                //
                // We should never be adjusting curr_off manually unless
                // we also can guarantee we'll be doing a seek after to
                // a new location.  Otherwise we'll be reading wrong offset
                // for the next container.
                //
                // We ensure this by adjusting our CRAM_OPT_RANGE
                // accordingly above, but to double check we also
                // set the skipped_block flag to enforce a seek also.
                iter->curr_off = iter->off[iter->i].v;
                next_range = 1;

                // Next region
                if (++iter->curr_intv >= iter->reg_list[iter->curr_reg].count){
                    if (++iter->curr_reg >= iter->n_reg)
                        break;
                    iter->curr_intv = 0;
                    iter->curr_tid = iter->reg_list[iter->curr_reg].tid;
                }
                continue;
            } else {
                break;
            }
        }

        iter->curr_off = iter->tell(fp);

        if (tid != iter->curr_tid) {
            hts_reglist_t key;
            key.tid = tid;

            found_reg = (hts_reglist_t *)bsearch(&key, iter->reg_list,
                                                 iter->n_reg,
                                                 sizeof(hts_reglist_t),
                                                 compare_regions);
            if (!found_reg)
                continue;

            iter->curr_reg = (found_reg - iter->reg_list);
            iter->curr_tid = tid;
            iter->curr_intv = 0;
        }

        cr = iter->curr_reg;
        ci = iter->curr_intv;

        for (i = ci; i < iter->reg_list[cr].count; i++) {
            if (end > iter->reg_list[cr].intervals[i].beg &&
                iter->reg_list[cr].intervals[i].end > beg) {
                iter->curr_beg = beg;
                iter->curr_end = end;
                iter->curr_intv = i;

                return ret;
            }

            // Check if the read starts beyond intervals[i].end
            // If so, the interval is finished so move on to the next.
            if (beg > iter->reg_list[cr].intervals[i].end)
                iter->curr_intv = i + 1;

            // No need to keep searching if the read ends before intervals[i].beg
            if (end < iter->reg_list[cr].intervals[i].beg)
                break;
        }
    }
    iter->finished = 1;

    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, 0x28(%rsp)
testq %rsi, %rsi
je 0x1aced
movq %rsi, %rbx
movl (%rsi), %eax
testb $0x2, %al
jne 0x1aced
movq %rdi, %r15
movq 0x38(%rdi), %r14
testb $0x1, %al
jne 0x1ad05
movq %r14, 0x10(%rsp)
testb $0x8, %al
jne 0x1a8bd
cmpq $0x0, 0x60(%rbx)
je 0x1ae5c
xorl %r12d, %r12d
movabsq $0x100000000, %r14      # imm = 0x100000000
testl %r12d, %r12d
je 0x1a8d7
movl 0x8(%rbx), %edx
movl 0xc(%rbx), %edi
jmp 0x1a935
movq 0x50(%rbx), %rax
testq %rax, %rax
setne %cl
movl 0x8(%rbx), %edx
movl 0xc(%rbx), %edi
cmpl %edx, %edi
setl %sil
andb %cl, %sil
cmpb $0x1, %sil
jne 0x1a935
movslq %edi, %rsi
movq 0x60(%rbx), %rcx
leaq (%rsi,%rsi,2), %rsi
cmpq 0x8(%rcx,%rsi,8), %rax
jae 0x1a935
leaq (%rcx,%rsi,8), %rax
movq 0x10(%rax), %rax
movq %rax, %rcx
shrq $0x20, %rcx
movslq 0x30(%rbx), %rsi
xorl %ebp, %ebp
cmpq %rsi, %rcx
jne 0x1ab67
movl %eax, %eax
movslq 0x38(%rbx), %rcx
cmpq %rcx, %rax
jae 0x1ab67
leal 0x1(%rdi), %esi
cmpl %edx, %esi
jge 0x1ad58
movq 0x50(%rbx), %rax
movq 0x60(%rbx), %rcx
movslq %edi, %rdi
leaq (%rdi,%rdi,2), %r8
shlq $0x20, %rdi
addq %r14, %rdi
leaq (%rcx,%r8,8), %r8
addq $0x28, %r8
cmpq -0x8(%r8), %rax
jae 0x1a983
movq (%r8), %r9
movq %r9, %r10
shrq $0x20, %r10
movslq 0x30(%rbx), %r11
cmpq %r11, %r10
jne 0x1a995
movl %r9d, %r9d
movslq 0x38(%rbx), %r10
cmpq %r10, %r9
jae 0x1a995
incl %esi
addq %r14, %rdi
addq $0x18, %r8
cmpl %esi, %edx
jne 0x1a95e
jmp 0x1ad56
movl %esi, 0xc(%rbx)
movl (%rbx), %edx
sarq $0x20, %rdi
leaq (%rdi,%rdi,2), %rsi
testb $0x4, %dl
jne 0x1a9c2
movq (%rcx,%rsi,8), %rsi
testl %r12d, %r12d
jne 0x1a9b9
cmpq %rsi, %rax
jae 0x1aa5f
movq %rsi, 0x50(%rbx)
jmp 0x1aa6f
movq 0x10(%rcx,%rsi,8), %rdi
movq %rdi, %r9
shrq $0x20, %r9
movslq 0x34(%rbx), %r10
movl 0x10(%rbx), %r8d
cmpl %r8d, %r10d
jge 0x1aa04
movq 0x28(%rbx), %r11
leaq (%r10,%r10,4), %r10
cmpl %r9d, 0x10(%r11,%r10,8)
jne 0x1aa04
movl %edi, 0x38(%rbx)
movq (%rcx,%rsi,8), %rsi
testl %r12d, %r12d
jne 0x1a9fb
cmpq %rsi, %rax
jae 0x1aa5f
movq %rsi, 0x50(%rbx)
jmp 0x1aa89
testl %r8d, %r8d
jle 0x1aa2a
movq 0x28(%rbx), %r11
addq $0x10, %r11
xorl %r10d, %r10d
cmpl %r9d, (%r11)
je 0x1aa2d
incq %r10
addq $0x28, %r11
cmpq %r10, %r8
jne 0x1aa14
jmp 0x1aced
xorl %r10d, %r10d
cmpl %r8d, %r10d
je 0x1aced
movl %r10d, 0x34(%rbx)
movq 0x28(%rbx), %r8
movl %r10d, %r9d
leaq (%r9,%r9,4), %r9
movl 0x10(%r8,%r9,8), %r8d
movl %r8d, 0x30(%rbx)
movl %edi, 0x38(%rbx)
movq (%rcx,%rsi,8), %rsi
testl %r12d, %r12d
jne 0x1aa66
cmpq %rsi, %rax
jb 0x1aa66
xorl %ebp, %ebp
jmp 0x1ab67
movq %rsi, 0x50(%rbx)
testb $0x4, %dl
jne 0x1aa89
movq 0x10(%rsp), %rdi
xorl %edx, %edx
callq *0x70(%rbx)
movl %r12d, %ebp
testl %eax, %eax
jns 0x1ab67
jmp 0x1ae16
testl %r12d, %r12d
jne 0x1aa94
cmpq 0x20(%rbx), %rsi
jb 0x1aa5f
movq 0x10(%rsp), %r12
movq %r12, %rdi
xorl %edx, %edx
callq *0x70(%rbx)
testl %eax, %eax
js 0x1ae16
movq 0x28(%rbx), %rcx
movslq 0x34(%rbx), %rax
leaq (%rax,%rax,4), %rdx
movl 0x10(%rcx,%rdx,8), %eax
movq 0x8(%rcx,%rdx,8), %rsi
movslq 0x38(%rbx), %rcx
shlq $0x4, %rcx
movq (%rsi,%rcx), %rdx
movq %rdx, 0x38(%rsp)
movq 0x8(%rsi,%rcx), %rcx
movq 0x60(%rbx), %r8
movslq 0xc(%rbx), %rdi
leaq (%rdi,%rdi,2), %r10
movq 0x8(%r8,%r10,8), %rdx
movl 0x8(%rbx), %r9d
leal 0x1(%rdi), %r11d
cmpl %r9d, %r11d
jge 0x1ab44
leaq (%r8,%r10,8), %r8
addq $0x28, %r8
notl %edi
addl %r9d, %edi
cmpq %rdx, -0x10(%r8)
ja 0x1ab44
movq (%r8), %r9
movq %r9, %r10
shrq $0x20, %r10
movslq %eax, %r11
cmpq %r11, %r10
jne 0x1ab2c
movl %r9d, %r9d
shlq $0x4, %r9
movq 0x8(%rsi,%r9), %r9
cmpq %r9, %rcx
cmovleq %r9, %rcx
jmp 0x1ab31
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
movq -0x8(%r8), %r9
cmpq %r9, %rdx
cmovbeq %r9, %rdx
addq $0x18, %r8
decl %edi
jne 0x1aaff
movl %eax, 0x30(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x20(%rbx)
xorl %ebp, %ebp
movq %r12, %rdi
movl $0x17, %esi
leaq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x5348c
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq 0x28(%rsp), %rdx
leaq 0xc(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x18(%rsp), %r9
callq *0x68(%rbx)
movl %eax, %r13d
testl %eax, %eax
js 0x1abb6
movq %r12, %rdi
callq *0x78(%rbx)
movq %rax, 0x50(%rbx)
movl 0xc(%rsp), %eax
cmpl 0x30(%rbx), %eax
jne 0x1ac30
movl 0x34(%rbx), %ecx
movl 0x38(%rbx), %eax
movq 0x28(%rbx), %rdx
jmp 0x1ac7f
testb $0x4, (%rbx)
je 0x1ae55
movq 0x10(%rsp), %rdi
callq 0x53485
testl %eax, %eax
je 0x1ae55
movq 0x60(%rbx), %rax
movslq 0xc(%rbx), %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x8(%rax,%rcx,8), %rax
movq %rax, 0x50(%rbx)
movl 0x38(%rbx), %edx
incl %edx
movl %edx, 0x38(%rbx)
movq 0x28(%rbx), %rax
movslq 0x34(%rbx), %rcx
leaq (%rcx,%rcx,4), %rsi
movl $0x1, %r12d
cmpl 0x14(%rax,%rsi,8), %edx
jb 0x1a8ca
incq %rcx
movl %ecx, 0x34(%rbx)
cmpl 0x10(%rbx), %ecx
jge 0x1ae55
movl $0x0, 0x38(%rbx)
leaq (%rcx,%rcx,4), %rcx
movl 0x10(%rax,%rcx,8), %eax
movl %eax, 0x30(%rbx)
jmp 0x1a8ca
movl %eax, 0x40(%rsp)
movq 0x28(%rbx), %rsi
movslq 0x10(%rbx), %rdx
movl $0x28, %ecx
leaq 0x30(%rsp), %rdi
leaq -0x63e(%rip), %r8       # 0x1a60f
callq 0x4640
testq %rax, %rax
je 0x1ace5
movq 0x28(%rbx), %rdx
subq %rdx, %rax
shrq $0x3, %rax
imull $0xcccccccd, %eax, %ecx # imm = 0xCCCCCCCD
movl %ecx, 0x34(%rbx)
movl 0xc(%rsp), %eax
movl %eax, 0x30(%rbx)
movl $0x0, 0x38(%rbx)
xorl %eax, %eax
movslq %ecx, %rcx
leaq (%rcx,%rcx,4), %rsi
movl 0x14(%rdx,%rsi,8), %ecx
movl %ebp, %r12d
cmpl %ecx, %eax
jae 0x1a8ca
leaq (%rdx,%rsi,8), %rsi
movq 0x18(%rsp), %rdx
movq 0x8(%rsi), %rdi
movq 0x20(%rsp), %rsi
movslq %eax, %r8
shlq $0x4, %r8
addq %r8, %rdi
addq $0x8, %rdi
movq -0x8(%rdi), %r8
cmpq %r8, %rdx
jle 0x1accb
movq (%rdi), %r9
cmpq %rsi, %r9
jle 0x1acce
jmp 0x1ae45
movq (%rdi), %r9
incl %eax
cmpq %r9, %rsi
jle 0x1acd8
movl %eax, 0x38(%rbx)
addq $0x10, %rdi
cmpq %r8, %rdx
jl 0x1ace5
cmpl %eax, %ecx
ja 0x1acb5
movl %ebp, %r12d
jmp 0x1a8ca
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl %r13d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x50(%rbx), %rsi
testq %rsi, %rsi
je 0x1ad26
movq %r14, %rdi
xorl %edx, %edx
callq *0x70(%rbx)
testl %eax, %eax
js 0x1ae16
movq $0x0, 0x50(%rbx)
leaq 0xc(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x18(%rsp), %r9
movq %r14, %rdi
movq %r15, %rsi
movq 0x28(%rsp), %rdx
callq *0x68(%rbx)
movl %eax, %r13d
testl %eax, %eax
jns 0x1ad4d
orb $0x2, (%rbx)
movl 0xc(%rsp), %eax
jmp 0x1adfc
movl %edx, %esi
movl %esi, 0xc(%rbx)
movl (%rbx), %eax
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testb $0x8, %al
movq 0x10(%rsp), %r14
jne 0x1ad73
orl $0x2, %eax
movl %eax, (%rbx)
jmp 0x1acf3
movq 0x58(%rbx), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq *0x70(%rbx)
testl %eax, %eax
js 0x1ae1c
testb $0x4, (%rbx)
je 0x1adb2
movups 0x7ad55(%rip), %xmm0    # 0x95ae8
leaq 0x30(%rsp), %rdx
movaps %xmm0, (%rdx)
movq $0x0, 0x10(%rdx)
movq %r14, %rdi
movl $0x17, %esi
xorl %eax, %eax
callq 0x5348c
leaq 0xc(%rsp), %rbp
leaq 0x20(%rsp), %r12
leaq 0x18(%rsp), %r14
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq 0x28(%rsp), %rdx
movq %rbp, %rcx
movq %r12, %r8
movq %r14, %r9
callq *0x68(%rbx)
movl %eax, %r13d
movl 0xc(%rsp), %eax
testl %eax, %eax
js 0x1adea
testl %r13d, %r13d
jns 0x1adc1
movl %r13d, %ecx
shrl $0x1f, %ecx
incl %ecx
orl %ecx, (%rbx)
movq $0x0, 0x50(%rbx)
movl %eax, 0x30(%rbx)
movq 0x20(%rsp), %rax
movq %rax, 0x40(%rbx)
movq 0x18(%rsp), %rax
movq %rax, 0x48(%rbx)
jmp 0x1acf3
movq 0x50(%rbx), %rcx
jmp 0x1ae20
movq 0x58(%rbx), %rcx
leaq 0x7ba9a(%rip), %rsi     # 0x968c1
leaq 0x7baa6(%rip), %rdx     # 0x968d4
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
jmp 0x1acf3
movq %rsi, 0x40(%rbx)
movq %rdx, 0x48(%rbx)
movl %eax, 0x38(%rbx)
jmp 0x1acf3
movl (%rbx), %eax
jmp 0x1ad6c
leaq 0x7ba8c(%rip), %rdi     # 0x968ef
leaq 0x7b536(%rip), %rsi     # 0x963a0
leaq 0x7baa5(%rip), %rcx     # 0x96916
movl $0x10d6, %edx           # imm = 0x10D6
callq 0x42f0