// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/hts.c
// Function: hts_itr_query
hts_itr_t *hts_itr_query(const hts_idx_t *idx, int tid, hts_pos_t beg, hts_pos_t end, hts_readrec_func *readrec)
{
    int i, n_off, l, bin;
    hts_pair64_max_t *off;
    khint_t k;
    bidx_t *bidx;
    uint64_t min_off, max_off;
    hts_pos_t idx_maxpos;
    hts_itr_t *iter;
    uint32_t unmapped = 0, rel_off;

    // It's possible to call this function with NULL idx iff
    // tid is one of the special values HTS_IDX_REST or HTS_IDX_NONE
    if (!idx && !(tid == HTS_IDX_REST || tid == HTS_IDX_NONE)) {
        errno = EINVAL;
        return NULL;
    }

    iter = (hts_itr_t*)calloc(1, sizeof(hts_itr_t));
    if (iter) {
        if (tid < 0) {
            uint64_t off = hts_itr_off(idx, tid);
            if (off != (uint64_t) -1) {
                iter->read_rest = 1;
                iter->curr_off = off;
                iter->readrec = readrec;
                if (tid == HTS_IDX_NONE)
                    iter->finished = 1;
            } else {
                free(iter);
                iter = NULL;
            }
        } else if (tid >= idx->n || (bidx = idx->bidx[tid]) == NULL) {
            iter->finished = 1;
        } else {
            if (beg < 0) beg = 0;
            if (end < beg) {
              free(iter);
              return NULL;
            }

            k = kh_get(bin, bidx, META_BIN(idx));
            if (k != kh_end(bidx))
                unmapped = kh_val(bidx, k).list[1].v;
            else
                unmapped = 1;

            iter->tid = tid, iter->beg = beg, iter->end = end; iter->i = -1;
            iter->readrec = readrec;

            if ( !kh_size(bidx) ) { iter->finished = 1; return iter; }

            idx_maxpos = hts_idx_maxpos(idx);
            if (beg >= idx_maxpos) { iter->finished = 1; return iter; }

            rel_off = beg>>idx->min_shift;
            // compute min_off
            bin = hts_bin_first(idx->n_lvls) + rel_off;
            do {
                int first;
                k = kh_get(bin, bidx, bin);
                if (k != kh_end(bidx)) break;
                first = (hts_bin_parent(bin)<<3) + 1;
                if (bin > first) --bin;
                else bin = hts_bin_parent(bin);
            } while (bin);
            if (bin == 0) k = kh_get(bin, bidx, bin);
            min_off = k != kh_end(bidx)? kh_val(bidx, k).loff : 0;
            // min_off can be calculated more accurately if the
            // linear index is available
            if (idx->lidx[tid].offset
                && rel_off < idx->lidx[tid].n) {
                if (min_off < idx->lidx[tid].offset[rel_off])
                    min_off = idx->lidx[tid].offset[rel_off];
                if (unmapped) {
                    // unmapped reads are not covered by the linear index,
                    // so search backwards for a smaller offset
                    int tmp_off;
                    for (tmp_off = rel_off-1; tmp_off >= 0; tmp_off--) {
                        if (idx->lidx[tid].offset[tmp_off] < min_off) {
                            min_off = idx->lidx[tid].offset[tmp_off];
                            break;
                        }
                    }
                    // if the search went too far back or no satisfactory entry
                    // was found, revert to the bin index loff value
                    if (k != kh_end(bidx) && (min_off < kh_val(bidx, k).loff || tmp_off < 0))
                        min_off = kh_val(bidx, k).loff;
                }
            } else if (unmapped) { //CSI index
                if (k != kh_end(bidx))
                    min_off = kh_val(bidx, k).loff;
            }

            // compute max_off: a virtual offset from a bin to the right of end
            // First check if end lies within the range of the index (it won't
            // if it's HTS_POS_MAX)
            if (end <= idx_maxpos) {
                bin = hts_bin_first(idx->n_lvls) + ((end-1) >> idx->min_shift) + 1;
                if (bin >= idx->n_bins) bin = 0;
                while (1) {
                    // search for an extant bin by moving right, but moving up to the
                    // parent whenever we get to a first child (which also covers falling
                    // off the RHS, which wraps around and immediately goes up to bin 0)
                    while (bin % 8 == 1) bin = hts_bin_parent(bin);
                    if (bin == 0) { max_off = UINT64_MAX; break; }
                    k = kh_get(bin, bidx, bin);
                    if (k != kh_end(bidx) && kh_val(bidx, k).n > 0) { max_off = kh_val(bidx, k).list[0].u; break; }
                    bin++;
                }
            } else {
                // Searching to end of reference
                max_off = UINT64_MAX;
            }

            // retrieve bins
            if (reg2bins(beg, end, iter, idx->min_shift, idx->n_lvls, bidx) < 0) {
                hts_itr_destroy(iter);
                return NULL;
            }

            for (i = n_off = 0; i < iter->bins.n; ++i)
                if ((k = kh_get(bin, bidx, iter->bins.a[i])) != kh_end(bidx))
                    n_off += kh_value(bidx, k).n;
            if (n_off == 0) {
                // No overlapping bins means the iterator has already finished.
                iter->finished = 1;
                return iter;
            }
            off = calloc(n_off, sizeof(*off));
            for (i = n_off = 0; i < iter->bins.n; ++i) {
                if ((k = kh_get(bin, bidx, iter->bins.a[i])) != kh_end(bidx)) {
                    int j;
                    bins_t *p = &kh_value(bidx, k);
                    for (j = 0; j < p->n; ++j)
                        if (p->list[j].v > min_off && p->list[j].u < max_off) {
                            off[n_off].u = min_off > p->list[j].u
                                ? min_off : p->list[j].u;
                            off[n_off].v = max_off < p->list[j].v
                                ? max_off : p->list[j].v;
                            // hts_pair64_max_t::max is now used to link
                            // file offsets to region list entries.
                            // The iterator can use this to decide if it
                            // can skip some file regions.
                            off[n_off].max = ((uint64_t) tid << 32) | j;
                            n_off++;
                        }
                }
            }

            if (n_off == 0) {
                free(off);
                iter->finished = 1;
                return iter;
            }
            ks_introsort(_off_max, n_off, off);
            // resolve completely contained adjacent blocks
            for (i = 1, l = 0; i < n_off; ++i)
                if (off[l].v < off[i].v) off[++l] = off[i];
            n_off = l + 1;
            // resolve overlaps between adjacent blocks; this may happen due to the merge in indexing
            for (i = 1; i < n_off; ++i)
                if (off[i-1].v >= off[i].u) off[i-1].v = off[i].u;
            // merge adjacent blocks
            for (i = 1, l = 0; i < n_off; ++i) {
                if (off[l].v>>16 == off[i].u>>16) off[l].v = off[i].v;
                else off[++l] = off[i];
            }
            n_off = l + 1;
            iter->n_off = n_off; iter->off = off;
        }
    }

    return iter;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %r15
movl %esi, %r14d
testq %rdi, %rdi
setne %al
leal 0x5(%r14), %ecx
cmpl $0x2, %ecx
setb %cl
orb %al, %cl
je 0x1468c
movq %r8, %r13
movq %rdx, %r12
movq %rdi, %rbp
pushq $0x1
popq %rdi
movl $0x90, %esi
callq 0x4300
testq %rax, %rax
je 0x14697
movq %rax, %rbx
testl %r14d, %r14d
js 0x146ab
movq %r15, 0x8(%rsp)
cmpl %r14d, 0x14(%rbp)
jle 0x14d5c
movq 0x28(%rbp), %rax
movl %r14d, %ecx
movq %rcx, 0x48(%rsp)
movq (%rax,%rcx,8), %r15
testq %r15, %r15
je 0x14d5c
xorl %eax, %eax
testq %r12, %r12
cmovgq %r12, %rax
movq %rax, 0x10(%rsp)
cmpq 0x8(%rsp), %rax
jg 0x146d8
movl 0xc(%rbp), %eax
movq %rax, 0x50(%rsp)
leal 0x1(%rax), %esi
movq %r15, %rdi
callq 0x14465
movl (%r15), %r12d
cmpl %r12d, %eax
jne 0x146e2
xorl %edi, %edi
jmp 0x146f9
callq 0x4050
movl $0x16, (%rax)
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %rdi
movl %r14d, %esi
callq 0x144e3
cmpq $-0x1, %rax
je 0x146d8
movl $0x1, (%rbx)
movq %rax, 0x50(%rbx)
movq %r13, 0x68(%rbx)
cmpl $-0x5, %r14d
jne 0x14699
movl $0x3, (%rbx)
jmp 0x14699
movq %rbx, %rdi
callq 0x45c0
jmp 0x14697
movq 0x20(%r15), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
movq 0x10(%rcx,%rax), %rax
cmpl $0x0, 0x18(%rax)
sete %dil
movl %r14d, 0x4(%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x18(%rbx)
movq 0x8(%rsp), %rcx
movq %rcx, 0x20(%rbx)
orl $-0x1, 0xc(%rbx)
movq %r13, 0x68(%rbx)
movl 0x4(%r15), %ecx
testq %rcx, %rcx
je 0x14d5c
movq %rcx, 0x28(%rsp)
movl 0x4(%rbp), %esi
movl 0x8(%rbp), %ecx
movq %rcx, 0x40(%rsp)
leal (%rcx,%rcx,2), %edx
leal (%rdx,%rsi), %ecx
pushq $0x1
popq %r13
shlq %cl, %r13
cmpq %r13, %rax
jge 0x14d5c
movl %edi, 0x18(%rsp)
movl %ecx, 0x5c(%rsp)
movq %rax, %rdi
movq %rsi, 0x30(%rsp)
movl %esi, %ecx
shrq %cl, %rdi
pushq $-0x1
popq %rax
movl %edx, %ecx
shll %cl, %eax
notl %eax
pushq $0x7
popq %rcx
xorl %edx, %edx
divl %ecx
movq %rax, 0x60(%rsp)
movq %rdi, 0x38(%rsp)
leal (%rax,%rdi), %r14d
movq %r15, %rdi
movl %r14d, %esi
callq 0x14465
cmpl %r12d, %eax
jne 0x147ac
leal -0x1(%r14), %eax
movl %eax, %ecx
andl $-0x8, %ecx
incl %ecx
movl %eax, %edx
sarl $0x3, %edx
cmpl %ecx, %r14d
cmovgl %eax, %edx
movl %edx, %r14d
testl %edx, %edx
jne 0x1477d
jmp 0x147b1
testl %r14d, %r14d
jne 0x147bb
movq %r15, %rdi
xorl %esi, %esi
callq 0x14465
movl %eax, %ecx
movq %r12, 0x20(%rsp)
cmpl %r12d, %eax
jne 0x147cc
xorl %r12d, %r12d
jmp 0x147d9
movq 0x20(%r15), %rdx
imulq $0x18, %rcx, %rsi
movq 0x8(%rdx,%rsi), %r12
movq 0x30(%rbp), %rdi
imulq $0x18, 0x48(%rsp), %rdx
movq 0x10(%rdi,%rdx), %rsi
testq %rsi, %rsi
je 0x1482a
addq %rdx, %rdi
movl 0x38(%rsp), %edx
cmpq (%rdi), %rdx
jge 0x1482a
movq (%rsi,%rdx,8), %rdi
cmpq %rdi, %r12
cmovbeq %rdi, %r12
cmpb $0x0, 0x18(%rsp)
movq 0x10(%rsp), %rbp
jne 0x14873
decq %rdx
testl %edx, %edx
js 0x1484d
movl %edx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq (%rsi,%rdi,8), %rdi
cmpq %r12, %rdi
jae 0x14810
jmp 0x14850
cmpl 0x20(%rsp), %eax
sete %al
movl 0x18(%rsp), %edx
orb %al, %dl
movq 0x10(%rsp), %rbp
jne 0x14873
movq 0x20(%r15), %rax
imulq $0x18, %rcx, %rcx
movq 0x8(%rax,%rcx), %r12
jmp 0x14873
movq %r12, %rdi
cmpl 0x20(%rsp), %eax
je 0x14870
movq 0x20(%r15), %rax
imulq $0x18, %rcx, %rcx
movq 0x8(%rax,%rcx), %rax
cmpq %rax, %rdi
cmovbeq %rax, %rdi
testl %edx, %edx
cmovsq %rax, %rdi
movq %rdi, %r12
pushq $-0x1
popq %r14
movq 0x8(%rsp), %rax
cmpq %rax, %r13
jge 0x14888
movq 0x28(%rsp), %r13
jmp 0x148f5
decq %rax
movq 0x30(%rsp), %rcx
sarq %cl, %rax
movq 0x60(%rsp), %rcx
addl %ecx, %eax
incl %eax
xorl %ebp, %ebp
cmpl 0x50(%rsp), %eax
cmovll %eax, %ebp
movq 0x28(%rsp), %r13
movl %ebp, %eax
andl $0x80000007, %eax       # imm = 0x80000007
cmpl $0x1, %eax
jne 0x148bd
decl %ebp
shrl $0x3, %ebp
jmp 0x148aa
testl %ebp, %ebp
je 0x148f0
movq %r15, %rdi
movl %ebp, %esi
callq 0x14465
cmpl 0x20(%rsp), %eax
je 0x148e2
movq 0x20(%r15), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
cmpl $0x0, 0x4(%rcx,%rax)
jg 0x148e6
incl %ebp
jmp 0x148aa
addq %rax, %rcx
movq 0x10(%rcx), %rax
movq (%rax), %r14
movq 0x10(%rsp), %rbp
movq %r14, 0x50(%rsp)
movq 0x40(%rsp), %rax
movq 0x30(%rsp), %rcx
addl %eax, %ecx
leal (%rcx,%rax,2), %ecx
pushq $0x1
popq %rdx
shlq %cl, %rdx
movq 0x8(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
cmpq %rbp, %rdx
jle 0x14955
decq %rdx
movq %rdx, 0x30(%rsp)
xorl %r14d, %r14d
movl %ecx, 0x18(%rsp)
xorl %eax, %eax
cmpl 0x40(%rsp), %eax
jg 0x14966
movq 0x30(%rsp), %rdx
sarq %cl, %rdx
movq %rbp, %rsi
shrq %cl, %rsi
subq %rsi, %r14
addq %rdx, %r14
incq %r14
addl $-0x3, %ecx
incl %eax
jmp 0x14931
xorl %r13d, %r13d
movq $0x0, 0x8(%rsp)
jmp 0x14b55
cmpq %r13, %r14
cmovbq %r14, %r13
testq %r14, %r14
je 0x1498b
movq %r13, %rax
shrq $0x1f, %rax
je 0x14999
callq 0x4050
movl $0xc, (%rax)
jmp 0x14caf
movq $0x0, 0x8(%rsp)
xorl %r13d, %r13d
jmp 0x149c7
leaq (,%r13,4), %rdi
callq 0x4680
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x14caf
movq 0x8(%rsp), %rax
movq %rax, 0x88(%rbx)
movl %r13d, 0x84(%rbx)
movq %r13, 0x28(%rsp)
cmpq 0x20(%rsp), %r14
jae 0x14a79
xorl %r13d, %r13d
xorl %esi, %esi
xorl %edi, %edi
movl 0x18(%rsp), %ecx
cmpl 0x40(%rsp), %edi
movq 0x30(%rsp), %rdx
jg 0x14b4c
movq %rdi, 0x38(%rsp)
movq %rbp, %rax
shrq %cl, %rax
movq %rdx, %rbp
movl %ecx, 0x18(%rsp)
sarq %cl, %rbp
movl %esi, 0x20(%rsp)
movslq %esi, %rcx
addq %rcx, %rbp
addl %eax, %ecx
movslq %ecx, %r14
cmpq %r14, %rbp
jl 0x14a53
movq %r15, %rdi
movl %r14d, %esi
callq 0x14465
cmpl (%r15), %eax
je 0x14a4e
cmpl 0x28(%rsp), %r13d
jge 0x14d67
movslq %r13d, %rax
leal 0x1(%r13), %ecx
movl %ecx, 0x80(%rbx)
movq 0x8(%rsp), %rdx
movl %r14d, (%rdx,%rax,4)
movl %ecx, %r13d
incq %r14
jmp 0x14a15
movq 0x38(%rsp), %rdi
leal (%rdi,%rdi,2), %ecx
pushq $0x1
popq %rax
shll %cl, %eax
movl 0x18(%rsp), %ecx
addl $-0x3, %ecx
movl 0x20(%rsp), %esi
addl %eax, %esi
incl %edi
movq 0x10(%rsp), %rbp
jmp 0x149e2
movslq 0x5c(%rsp), %rax
movq %rax, 0x38(%rsp)
xorl %edx, %edx
xorl %r13d, %r13d
xorl %r14d, %r14d
cmpl 0x20(%rsp), %r14d
je 0x14b4c
movq 0x10(%r15), %rax
movl %r14d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %edx, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x14b41
movl %edx, 0x10(%rsp)
movq 0x18(%r15), %rax
movl %r14d, %ecx
movl (%rax,%rcx,4), %edi
movq %rdi, 0x18(%rsp)
callq 0x184dd
cmpl 0x40(%rsp), %eax
jg 0x14b3d
leal (%rax,%rax,2), %ecx
pushq $-0x1
popq %rax
shll %cl, %eax
movslq %ecx, %rdx
movq 0x38(%rsp), %rcx
subq %rdx, %rcx
movq %rbp, %rsi
shrq %cl, %rsi
notl %eax
xorl %edx, %edx
pushq $0x7
popq %rdi
divl %edi
addq %rax, %rsi
movq 0x18(%rsp), %rdx
cmpq %rdx, %rsi
ja 0x14b3d
movq %rdx, %rsi
movq 0x30(%rsp), %rdx
sarq %cl, %rdx
addq %rax, %rdx
cmpq %rsi, %rdx
jl 0x14b3d
cmpl 0x28(%rsp), %r13d
jge 0x14d86
movslq %r13d, %rax
leal 0x1(%r13), %ecx
movl %ecx, 0x80(%rbx)
movq 0x8(%rsp), %rdx
movl %esi, (%rdx,%rax,4)
movl (%r15), %eax
movq %rax, 0x20(%rsp)
movl %ecx, %r13d
movl 0x10(%rsp), %edx
incl %r14d
addl $0x2, %edx
jmp 0x14a8b
testl %r13d, %r13d
js 0x14caf
movl %r13d, %ebp
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpq %r14, %rbp
je 0x14b8d
movq 0x8(%rsp), %rax
movl (%rax,%r14,4), %esi
movq %r15, %rdi
callq 0x14465
cmpl (%r15), %eax
je 0x14b88
movq 0x20(%r15), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
addl 0x4(%rcx,%rax), %r13d
incq %r14
jmp 0x14b5e
testl %r13d, %r13d
je 0x14d5c
movq %rbp, 0x28(%rsp)
movslq %r13d, %rdi
pushq $0x18
popq %rsi
callq 0x4300
movq %rax, %r13
shlq $0x20, 0x48(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpq 0x28(%rsp), %rbp
je 0x14c50
movq 0x8(%rsp), %rax
movl (%rax,%rbp,4), %esi
movq %r15, %rdi
callq 0x14465
cmpl (%r15), %eax
jne 0x14bd9
incq %rbp
jmp 0x14bb4
movq 0x20(%r15), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rdx
movl 0x4(%rcx,%rdx), %eax
testl %eax, %eax
movl $0x0, %esi
cmovlel %esi, %eax
addq %rdx, %rcx
shlq $0x4, %rax
movq 0x48(%rsp), %rdx
xorl %esi, %esi
cmpq %rsi, %rax
je 0x14bd4
movq 0x10(%rcx), %r8
movq 0x8(%r8,%rsi), %rdi
cmpq %r12, %rdi
jbe 0x14c47
movq (%r8,%rsi), %r8
movq 0x50(%rsp), %r10
cmpq %r10, %r8
jae 0x14c47
cmpq %r8, %r12
cmovaq %r12, %r8
movslq %r14d, %r14
imulq $0x18, %r14, %r9
cmpq %rdi, %r10
cmovbq %r10, %rdi
movq %r8, (%r13,%r9)
movq %rdi, 0x8(%r13,%r9)
movq %rdx, 0x10(%r13,%r9)
incl %r14d
addq $0x10, %rsi
incq %rdx
jmp 0x14bff
testl %r14d, %r14d
je 0x14d54
movslq %r14d, %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x14de0
leaq 0x20(%r13), %rcx
xorl %eax, %eax
pushq $0x1
popq %r8
cmpq %r14, %r8
jge 0x14cbc
movslq %eax, %rdx
imulq $0x18, %rdx, %rdx
movq 0x8(%r13,%rdx), %rdx
cmpq (%rcx), %rdx
jae 0x14ca6
leaq -0x8(%rcx), %rdx
incl %eax
movslq %eax, %rsi
imulq $0x18, %rsi, %rsi
movq 0x10(%rdx), %rdi
movq %rdi, 0x10(%r13,%rsi)
movups (%rdx), %xmm0
movups %xmm0, (%r13,%rsi)
incq %r8
addq $0x18, %rcx
jmp 0x14c71
movq %rbx, %rdi
callq 0x14da5
jmp 0x14697
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
imulq $0x18, %rax, %rdx
cmpq %rcx, %rdx
je 0x14ce3
movq 0x18(%r13,%rcx), %rsi
cmpq %rsi, 0x8(%r13,%rcx)
jb 0x14cdd
movq %rsi, 0x8(%r13,%rcx)
addq $0x18, %rcx
jmp 0x14cc7
movl %eax, %eax
imulq $0x18, %rax, %rax
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %rdx, %rax
je 0x14d46
movslq %ecx, %rsi
imulq $0x18, %rsi, %rsi
movq 0x18(%r13,%rdx), %rdi
xorq 0x8(%r13,%rsi), %rdi
cmpq $0xffff, %rdi           # imm = 0xFFFF
ja 0x14d1d
addq %r13, %rsi
addq $0x8, %rsi
movq 0x20(%r13,%rdx), %rdi
movq %rdi, (%rsi)
jmp 0x14d40
leaq (%rdx,%r13), %rsi
addq $0x18, %rsi
incl %ecx
movslq %ecx, %rdi
imulq $0x18, %rdi, %rdi
movq 0x10(%rsi), %r8
movq %r8, 0x10(%r13,%rdi)
movups (%rsi), %xmm0
movups %xmm0, (%r13,%rdi)
addq $0x18, %rdx
jmp 0x14ced
incl %ecx
movl %ecx, 0x8(%rbx)
movq %r13, 0x60(%rbx)
jmp 0x14699
movq %r13, %rdi
callq 0x45c0
movl $0x2, (%rbx)
jmp 0x14699
leaq 0x63cbb(%rip), %rdi     # 0x78a29
leaq 0x6361c(%rip), %rsi     # 0x78391
leaq 0x63cc7(%rip), %rcx     # 0x78a43
movl $0xc22, %edx            # imm = 0xC22
callq 0x42f0
leaq 0x63c9c(%rip), %rdi     # 0x78a29
leaq 0x635fd(%rip), %rsi     # 0x78391
leaq 0x63cef(%rip), %rcx     # 0x78a8a
movl $0xc39, %edx            # imm = 0xC39
callq 0x42f0