// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
testq %rsi, %rsi
je 0x1b24c
movq %rsi, %rbx
movl (%rsi), %eax
testb $0x2, %al
jne 0x1b24c
movq %rdx, %r14
movq %rdi, %r15
movq 0x38(%rdi), %r12
testb $0x1, %al
jne 0x1b264
movq %r14, 0x18(%rsp)
testb $0x8, %al
jne 0x1ae3c
cmpq $0x0, 0x60(%rbx)
je 0x1b398
xorl %ebp, %ebp
testl %ebp, %ebp
je 0x1af24
movl 0x8(%rbx), %ecx
movslq 0xc(%rbx), %rsi
leal 0x1(%rsi), %edx
cmpl %ecx, %edx
setge %al
jge 0x1aeac
movslq %ecx, %rdi
movq 0x50(%rbx), %r8
movq 0x60(%rbx), %r9
leaq (%rsi,%rsi,2), %r10
leaq (%r9,%r10,8), %r9
addq $0x28, %r9
addq $0x2, %rsi
cmpq -0x8(%r9), %r8
jae 0x1ae97
movq (%r9), %r10
movq %r10, %r11
shrq $0x20, %r11
movslq 0x30(%rbx), %r14
cmpq %r14, %r11
jne 0x1aeac
movl %r10d, %r10d
movslq 0x38(%rbx), %r11
cmpq %r11, %r10
jae 0x1aeac
cmpq %rdi, %rsi
setge %al
incl %edx
addq $0x18, %r9
incq %rsi
cmpl %edx, %ecx
jne 0x1ae72
movl %ecx, %edx
movl %edx, 0xc(%rbx)
movl (%rbx), %ecx
testb $0x4, %cl
sete %sil
orb %al, %sil
movslq %edx, %rdx
testb $0x1, %sil
jne 0x1afaa
movq 0x60(%rbx), %rsi
leaq (%rdx,%rdx,2), %rdi
movq 0x10(%rsi,%rdi,8), %rsi
movq %rsi, %r8
shrq $0x20, %r8
movslq 0x34(%rbx), %r9
movl 0x10(%rbx), %edi
cmpl %edi, %r9d
jge 0x1aefb
movq 0x28(%rbx), %r10
leaq (%r9,%r9,4), %r9
cmpl %r8d, 0x10(%r10,%r9,8)
je 0x1afa7
testl %edi, %edi
jle 0x1af84
movq 0x28(%rbx), %r10
addq $0x10, %r10
xorl %r9d, %r9d
cmpl %r8d, (%r10)
je 0x1af87
incq %r9
addq $0x28, %r10
cmpq %r9, %rdi
jne 0x1af0e
movl %edi, %r9d
jmp 0x1af87
movq 0x50(%rbx), %rax
testq %rax, %rax
je 0x1ae46
movslq 0xc(%rbx), %rcx
cmpl 0x8(%rbx), %ecx
jge 0x1ae46
movq 0x60(%rbx), %rdx
leaq (%rcx,%rcx,2), %rcx
cmpq 0x8(%rdx,%rcx,8), %rax
jae 0x1ae46
leaq (%rdx,%rcx,8), %rax
movq 0x10(%rax), %rax
movq %rax, %rcx
shrq $0x20, %rcx
movslq 0x30(%rbx), %rdx
xorl %r14d, %r14d
cmpq %rdx, %rcx
jne 0x1b0d6
movl %eax, %eax
movslq 0x38(%rbx), %rcx
cmpq %rcx, %rax
jb 0x1ae46
jmp 0x1b0d6
xorl %r9d, %r9d
cmpl %edi, %r9d
je 0x1b24c
movl %r9d, 0x34(%rbx)
movq 0x28(%rbx), %rdi
movl %r9d, %r8d
leaq (%r8,%r8,4), %r8
movl 0x10(%rdi,%r8,8), %edi
movl %edi, 0x30(%rbx)
movl %esi, 0x38(%rbx)
testb $0x1, %al
jne 0x1b2f7
xorl %r14d, %r14d
movq 0x60(%rbx), %rax
leaq (%rdx,%rdx,2), %rdx
movq (%rax,%rdx,8), %rsi
testl %ebp, %ebp
jne 0x1afcf
cmpq %rsi, 0x50(%rbx)
jae 0x1b0d6
movq %rsi, 0x50(%rbx)
testb $0x4, %cl
jne 0x1aff0
movq %r12, %rdi
xorl %edx, %edx
callq *0x70(%rbx)
movl %ebp, %r14d
testl %eax, %eax
jns 0x1b0d6
jmp 0x1b2ce
testl %ebp, %ebp
jne 0x1affe
cmpq 0x20(%rbx), %rsi
jb 0x1b0d6
movq %r12, %rdi
xorl %edx, %edx
callq *0x70(%rbx)
testl %eax, %eax
js 0x1b2ce
movq 0x28(%rbx), %rcx
movslq 0x34(%rbx), %rax
leaq (%rax,%rax,4), %rdx
movl 0x10(%rcx,%rdx,8), %eax
movl %eax, 0x20(%rsp)
movq 0x8(%rcx,%rdx,8), %rsi
movslq 0x38(%rbx), %rcx
shlq $0x4, %rcx
movq (%rsi,%rcx), %rdx
movq %rdx, 0x28(%rsp)
movq 0x8(%rsi,%rcx), %rcx
movq %rcx, 0x30(%rsp)
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 0x1b0b2
leaq (%r8,%r10,8), %r8
addq $0x28, %r8
notl %edi
addl %r9d, %edi
cmpq %rdx, -0x10(%r8)
ja 0x1b0b2
movq (%r8), %r9
movq %r9, %r10
shrq $0x20, %r10
movslq %eax, %r11
cmpq %r11, %r10
jne 0x1b09a
movl %r9d, %r9d
shlq $0x4, %r9
movq 0x8(%rsi,%r9), %r9
cmpq %r9, %rcx
cmovleq %r9, %rcx
jmp 0x1b09f
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
movq -0x8(%r8), %r9
cmpq %r9, %rdx
cmovbeq %r9, %rdx
addq $0x18, %r8
decl %edi
jne 0x1b06d
movl %eax, 0x20(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, 0x20(%rbx)
xorl %r14d, %r14d
movq %r12, %rdi
movl $0x17, %esi
leaq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x55e0e
movq %r12, %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
leaq 0x4(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x8(%rsp), %r9
callq *0x68(%rbx)
movl %eax, %r13d
testl %eax, %eax
js 0x1b1d5
movq %r12, %rdi
callq *0x78(%rbx)
movq %rax, 0x50(%rbx)
movl 0x4(%rsp), %eax
cmpl 0x30(%rbx), %eax
je 0x1b15b
movl %eax, 0x30(%rsp)
movq 0x28(%rbx), %rsi
movslq 0x10(%rbx), %rdx
movl $0x28, %ecx
leaq 0x20(%rsp), %rdi
leaq -0x59d(%rip), %r8       # 0x1ab91
callq 0x4640
testq %rax, %rax
je 0x1b1cd
subq 0x28(%rbx), %rax
shrq $0x3, %rax
imull $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
movl %eax, 0x34(%rbx)
movl 0x4(%rsp), %eax
movl %eax, 0x30(%rbx)
movl $0x0, 0x38(%rbx)
movslq 0x34(%rbx), %rcx
movl 0x38(%rbx), %eax
movq 0x28(%rbx), %rdx
leaq (%rcx,%rcx,4), %rsi
movl 0x14(%rdx,%rsi,8), %ecx
movl %r14d, %ebp
cmpl %ecx, %eax
jae 0x1ae3e
movslq %eax, %r9
leaq (%rdx,%rsi,8), %rsi
movq 0x8(%rsp), %rdx
movq 0x8(%rsi), %r8
movq 0x10(%rsp), %rdi
shlq $0x4, %r9
orq $0x8, %r9
cmpq -0x8(%r8,%r9), %rdx
jle 0x1b1a7
cmpq %rdi, (%r8,%r9)
jg 0x1b2c1
movq 0x8(%rsi), %r10
cmpq (%r10,%r9), %rdi
jle 0x1b1b9
leal 0x1(%rax), %r11d
movl %r11d, 0x38(%rbx)
incl %eax
leaq 0x10(%r9), %r11
cmpq -0x8(%r10,%r9), %rdx
jl 0x1b1cd
movq %r11, %r9
cmpl %eax, %ecx
ja 0x1b196
movl %r14d, %ebp
jmp 0x1ae3e
testb $0x4, (%rbx)
je 0x1b302
movq %r12, %rdi
callq 0x55e07
testl %eax, %eax
je 0x1b302
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, %ebp
cmpl 0x14(%rax,%rsi,8), %edx
jb 0x1ae3e
incq %rcx
movl %ecx, 0x34(%rbx)
cmpl 0x10(%rbx), %ecx
jge 0x1b302
movl $0x0, 0x38(%rbx)
leaq (%rcx,%rcx,4), %rcx
movl 0x10(%rax,%rcx,8), %eax
movl %eax, 0x30(%rbx)
jmp 0x1ae3e
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl %r13d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x50(%rbx), %rsi
testq %rsi, %rsi
je 0x1b281
movq %r12, %rdi
xorl %edx, %edx
callq *0x70(%rbx)
testl %eax, %eax
js 0x1b2ce
movq $0x0, 0x50(%rbx)
leaq 0x4(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x8(%rsp), %r9
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq *0x68(%rbx)
movl %eax, %r13d
testl %eax, %eax
jns 0x1b2a6
orb $0x2, (%rbx)
movl 0x4(%rsp), %eax
movl %eax, 0x30(%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x40(%rbx)
movq 0x8(%rsp), %rax
movq %rax, 0x48(%rbx)
jmp 0x1b252
movq %rdi, 0x40(%rbx)
movq %rdx, 0x48(%rbx)
movl %eax, 0x38(%rbx)
jmp 0x1b252
movq 0x50(%rbx), %rcx
leaq 0x7e548(%rip), %rsi     # 0x99821
leaq 0x7e554(%rip), %rdx     # 0x99834
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
jmp 0x1b252
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testb $0x8, %cl
jne 0x1b30a
orb $0x2, (%rbx)
jmp 0x1b252
movq 0x58(%rbx), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq *0x70(%rbx)
testl %eax, %eax
js 0x1b38f
testb $0x4, (%rbx)
je 0x1b345
movups 0x7d712(%rip), %xmm0    # 0x98a38
leaq 0x20(%rsp), %rdx
movaps %xmm0, (%rdx)
movq $0x0, 0x10(%rdx)
movq %r12, %rdi
movl $0x17, %esi
xorl %eax, %eax
callq 0x55e0e
leaq 0x8(%rsp), %rbp
movq 0x18(%rsp), %r14
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
leaq 0x4(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %rbp, %r9
callq *0x68(%rbx)
movl %eax, %r13d
movl 0x4(%rsp), %eax
testl %eax, %eax
js 0x1b378
testl %r13d, %r13d
jns 0x1b34f
movl %r13d, %ecx
shrl $0x1f, %ecx
incl %ecx
orl %ecx, (%rbx)
movq $0x0, 0x50(%rbx)
jmp 0x1b2aa
movq 0x58(%rbx), %rcx
jmp 0x1b2d2
leaq 0x7e4b0(%rip), %rdi     # 0x9984f
leaq 0x7df5a(%rip), %rsi     # 0x99300
leaq 0x7e4c9(%rip), %rcx     # 0x99876
movl $0x10d6, %edx           # imm = 0x10D6
callq 0x42f0