// File: /workspace/github_c_makefile_star2_O2/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)
pushq $-0x1
popq %r13
testq %rsi, %rsi
je 0x16f38
movq %rsi, %rbx
movl (%rsi), %eax
testb $0x2, %al
jne 0x16f38
movq %rdi, %r15
movq 0x38(%rdi), %r14
testb $0x1, %al
jne 0x16e10
movq %r14, 0x10(%rsp)
testb $0x8, %al
jne 0x16a1e
cmpq $0x0, 0x60(%rbx)
je 0x16f62
xorl %ebp, %ebp
movabsq $0x100000000, %r12      # imm = 0x100000000
testl %ebp, %ebp
je 0x16a36
movl 0x8(%rbx), %eax
movl 0xc(%rbx), %ecx
jmp 0x16a95
movq 0x50(%rbx), %rdx
testq %rdx, %rdx
setne %sil
movl 0x8(%rbx), %eax
movl 0xc(%rbx), %ecx
cmpl %eax, %ecx
setl %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0x16a95
movslq %ecx, %rdi
movq 0x60(%rbx), %rsi
imulq $0x18, %rdi, %rdi
cmpq 0x8(%rsi,%rdi), %rdx
jae 0x16a95
addq %rdi, %rsi
movq 0x10(%rsi), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
movslq 0x30(%rbx), %rdi
xorl %r14d, %r14d
cmpq %rdi, %rsi
jne 0x16c9c
movl %edx, %edx
movslq 0x38(%rbx), %rsi
cmpq %rsi, %rdx
jae 0x16c9c
movslq %ecx, %rsi
movslq %eax, %rdi
movq %rsi, %rax
shlq $0x20, %rax
addq %r12, %rax
imulq $0x18, %rsi, %r8
addq $0x28, %r8
incq %rsi
cmpq %rdi, %rsi
jge 0x16e5e
movq 0x50(%rbx), %rcx
movq 0x60(%rbx), %rdx
cmpq -0x8(%rdx,%r8), %rcx
jae 0x16ae8
movq (%rdx,%r8), %r9
movq %r9, %r10
shrq $0x20, %r10
movslq 0x30(%rbx), %r11
cmpq %r11, %r10
jne 0x16af4
movl %r9d, %r9d
movslq 0x38(%rbx), %r10
cmpq %r10, %r9
jae 0x16af4
incq %rsi
addq %r12, %rax
addq $0x18, %r8
jmp 0x16ab0
movl %esi, 0xc(%rbx)
movl (%rbx), %edi
sarq $0x20, %rax
testb $0x4, %dil
je 0x16b87
imulq $0x18, %rax, %rsi
movq 0x10(%rdx,%rsi), %rsi
movq %rsi, %r9
shrq $0x20, %r9
movslq 0x34(%rbx), %r10
movl 0x10(%rbx), %r8d
cmpl %r8d, %r10d
jge 0x16b33
movq 0x28(%rbx), %r11
imulq $0x28, %r10, %r10
cmpl %r9d, 0x10(%r11,%r10)
je 0x16b84
testl %r8d, %r8d
movl $0x0, %r10d
cmovgl %r8d, %r10d
pushq $0x10
popq %r14
xorl %r11d, %r11d
cmpq %r11, %r10
je 0x16b63
movq 0x28(%rbx), %r13
cmpl %r9d, (%r13,%r14)
je 0x16b60
incq %r11
addq $0x28, %r14
jmp 0x16b47
movl %r11d, %r10d
cmpl %r8d, %r10d
je 0x16f5e
movl %r10d, 0x34(%rbx)
movq 0x28(%rbx), %r8
movl %r10d, %r9d
imulq $0x28, %r9, %r9
movl 0x10(%r8,%r9), %r8d
movl %r8d, 0x30(%rbx)
movl %esi, 0x38(%rbx)
imulq $0x18, %rax, %rax
xorl %r14d, %r14d
movq (%rdx,%rax), %rsi
testl %ebp, %ebp
jne 0x16b9f
cmpq %rsi, %rcx
jae 0x16c9c
movq %rsi, 0x50(%rbx)
testb $0x4, %dil
jne 0x16bc3
movq 0x10(%rsp), %rdi
xorl %edx, %edx
callq *0x70(%rbx)
movl %ebp, %r14d
testl %eax, %eax
jns 0x16c9c
jmp 0x16f12
testl %ebp, %ebp
jne 0x16bd1
cmpq 0x20(%rbx), %rsi
jb 0x16c9c
movq 0x10(%rsp), %r13
movq %r13, %rdi
xorl %edx, %edx
callq *0x70(%rbx)
testl %eax, %eax
js 0x16f12
movq 0x28(%rbx), %rcx
movslq 0x34(%rbx), %rax
imulq $0x28, %rax, %rdx
movl 0x10(%rcx,%rdx), %eax
movq 0x8(%rcx,%rdx), %rcx
movslq 0x38(%rbx), %rdx
shlq $0x4, %rdx
movq (%rcx,%rdx), %rsi
movq %rsi, 0x38(%rsp)
movq 0x8(%rcx,%rdx), %rdx
movq 0x60(%rbx), %rsi
movslq 0xc(%rbx), %rdi
imulq $0x18, %rdi, %r8
addq %rsi, %r8
addq $0x28, %r8
movq -0x20(%r8), %rsi
movslq 0x8(%rbx), %r9
incq %rdi
pushq $-0x3
popq %rbp
cmpq %r9, %rdi
jge 0x16c7a
cmpq %rsi, -0x10(%r8)
ja 0x16c7a
movq (%r8), %r10
movq %r10, %r11
shrq $0x20, %r11
movslq %eax, %r14
cmpq %r14, %r11
jne 0x16c64
movl %r10d, %r10d
shlq $0x4, %r10
movq 0x8(%rcx,%r10), %r10
cmpq %r10, %rdx
cmovleq %r10, %rdx
jmp 0x16c66
movl %ebp, %eax
movq -0x8(%r8), %r10
cmpq %r10, %rsi
cmovbeq %r10, %rsi
incq %rdi
addq $0x18, %r8
jmp 0x16c32
movl %eax, 0x30(%rsp)
movq %rdx, 0x40(%rsp)
movq %rsi, 0x20(%rbx)
xorl %r14d, %r14d
movq %r13, %rdi
pushq $0x17
popq %rsi
leaq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x451f7
movq 0x10(%rsp), %rbp
movq %rbp, %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 0x16cec
movq %rbp, %rdi
callq *0x78(%rbx)
movq %rax, 0x50(%rbx)
movl 0xc(%rsp), %eax
cmpl 0x30(%rbx), %eax
jne 0x16d65
movl 0x34(%rbx), %eax
movslq 0x38(%rbx), %rcx
movq 0x28(%rbx), %rsi
jmp 0x16da9
testb $0x4, (%rbx)
je 0x16f57
movq %rbp, %rdi
callq 0x451f0
testl %eax, %eax
je 0x16f57
movq 0x60(%rbx), %rax
movslq 0xc(%rbx), %rcx
imulq $0x18, %rcx, %rcx
movq 0x8(%rax,%rcx), %rax
movq %rax, 0x50(%rbx)
movl 0x38(%rbx), %edx
incl %edx
movl %edx, 0x38(%rbx)
movq 0x28(%rbx), %rax
movslq 0x34(%rbx), %rcx
imulq $0x28, %rcx, %rsi
pushq $0x1
popq %rdi
movl %edi, %ebp
cmpl 0x14(%rax,%rsi), %edx
jb 0x16a2a
incq %rcx
movl %ecx, 0x34(%rbx)
cmpl 0x10(%rbx), %ecx
jge 0x16f57
andl $0x0, 0x38(%rbx)
imulq $0x28, %rcx, %rcx
movl 0x10(%rax,%rcx), %eax
movl %eax, 0x30(%rbx)
pushq $0x1
popq %rax
movl %eax, %ebp
jmp 0x16a2a
movl %eax, 0x40(%rsp)
movq 0x28(%rbx), %rsi
movslq 0x10(%rbx), %rdx
leaq 0x30(%rsp), %rdi
pushq $0x28
popq %rbp
movq %rbp, %rcx
leaq -0x5f7(%rip), %r8       # 0x1678c
callq 0x4640
testq %rax, %rax
je 0x16e08
movq 0x28(%rbx), %rsi
subq %rsi, %rax
cqto
idivq %rbp
movl %eax, 0x34(%rbx)
movl 0xc(%rsp), %ecx
movl %ecx, 0x30(%rbx)
andl $0x0, 0x38(%rbx)
xorl %ecx, %ecx
cltq
imulq $0x28, %rax, %rax
leaq (%rsi,%rax), %rdx
movl 0x14(%rsi,%rax), %esi
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rdi
movq %rcx, %r8
shlq $0x4, %r8
orq $0x8, %r8
cmpl %ecx, %esi
jbe 0x16e08
movq 0x8(%rdx), %r10
movq -0x8(%r10,%r8), %r9
cmpq %r9, %rax
jle 0x16dec
movq (%r10,%r8), %r10
cmpq %rdi, %r10
jle 0x16df0
jmp 0x16f4a
movq (%r10,%r8), %r10
cmpq %r10, %rdi
jle 0x16dfd
leal 0x1(%rcx), %r10d
movl %r10d, 0x38(%rbx)
incl %ecx
addq $0x10, %r8
cmpq %r9, %rax
jge 0x16dcc
movl %r14d, %ebp
jmp 0x16a2a
movq 0x50(%rbx), %rsi
testq %rsi, %rsi
je 0x16e2e
movq %r14, %rdi
xorl %edx, %edx
callq *0x70(%rbx)
testl %eax, %eax
js 0x16f12
andq $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 0x16e55
orb $0x2, (%rbx)
movl 0xc(%rsp), %eax
jmp 0x16efb
movl %esi, 0xc(%rbx)
movl (%rbx), %eax
testb $0x8, %al
jne 0x16e75
pushq $-0x1
popq %r13
orl $0x2, %eax
movl %eax, (%rbx)
jmp 0x16f38
movq 0x58(%rbx), %rsi
movq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq *0x70(%rbx)
testl %eax, %eax
js 0x16f18
testb $0x4, (%rbx)
je 0x16eb4
movups 0x60bfe(%rip), %xmm0    # 0x77a98
leaq 0x30(%rsp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
pushq $0x17
popq %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x451f7
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 0x16eec
testl %r13d, %r13d
jns 0x16ec3
movl %r13d, %ecx
shrl $0x1f, %ecx
incl %ecx
orl %ecx, (%rbx)
andq $0x0, 0x50(%rbx)
movl %eax, 0x30(%rbx)
movq 0x20(%rsp), %rax
movq %rax, 0x40(%rbx)
movq 0x18(%rsp), %rax
movq %rax, 0x48(%rbx)
jmp 0x16f38
movq 0x50(%rbx), %rcx
jmp 0x16f1c
movq 0x58(%rbx), %rcx
leaq 0x6198f(%rip), %rsi     # 0x788b2
leaq 0x6199b(%rip), %rdx     # 0x788c5
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
pushq $-0x2
popq %r13
movl %r13d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, 0x40(%rbx)
movq %rax, 0x48(%rbx)
movl %ecx, 0x38(%rbx)
jmp 0x16f38
movl (%rbx), %eax
jmp 0x16e6b
pushq $-0x1
jmp 0x16f36
leaq 0x61977(%rip), %rdi     # 0x788e0
leaq 0x61421(%rip), %rsi     # 0x78391
leaq 0x61990(%rip), %rcx     # 0x78907
movl $0x10d6, %edx           # imm = 0x10D6
callq 0x42f0