// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/htslib/hts.c
// Function: hts_itr_query
hts_itr_t *hts_itr_query(const hts_idx_t *idx, int tid, int beg, int end, hts_readrec_func *readrec)
{
    int i, n_off, l, bin;
    hts_pair64_t *off;
    khint_t k;
    bidx_t *bidx;
    uint64_t min_off;
    hts_itr_t *iter = 0;
    if (tid < 0) {
        int finished0 = 0;
        uint64_t off0 = (uint64_t)-1;
        khint_t k;
        switch (tid) {
        case HTS_IDX_START:
            // Find the smallest offset, note that sequence ids may not be ordered sequentially
            for (i=0; i<idx->n; i++)
            {
                bidx = idx->bidx[i];
                k = kh_get(bin, bidx, META_BIN(idx));
                if (k == kh_end(bidx)) continue;
                if ( off0 > kh_val(bidx, k).list[0].u ) off0 = kh_val(bidx, k).list[0].u;
            }
            if ( off0==(uint64_t)-1 && idx->n_no_coor ) off0 = 0; // only no-coor reads in this bam
            break;

        case HTS_IDX_NOCOOR:
            if ( idx->n>0 )
            {
                bidx = idx->bidx[idx->n - 1];
                k = kh_get(bin, bidx, META_BIN(idx));
                if (k != kh_end(bidx)) off0 = kh_val(bidx, k).list[0].v;
            }
            if ( off0==(uint64_t)-1 && idx->n_no_coor ) off0 = 0; // only no-coor reads in this bam
            break;

        case HTS_IDX_REST:
            off0 = 0;
            break;

        case HTS_IDX_NONE:
            finished0 = 1;
            off0 = 0;
            break;

        default:
            return 0;
        }
        if (off0 != (uint64_t)-1) {
            iter = (hts_itr_t*)calloc(1, sizeof(hts_itr_t));
            iter->read_rest = 1;
            iter->finished = finished0;
            iter->curr_off = off0;
            iter->readrec = readrec;
            return iter;
        } else return 0;
    }

    if (beg < 0) beg = 0;
    if (end < beg) return 0;
    if (tid >= idx->n || (bidx = idx->bidx[tid]) == NULL) return 0;

    iter = (hts_itr_t*)calloc(1, sizeof(hts_itr_t));
    iter->tid = tid, iter->beg = beg, iter->end = end; iter->i = -1;
    iter->readrec = readrec;

    // compute min_off
    bin = hts_bin_first(idx->n_lvls) + (beg>>idx->min_shift);
    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;
    // retrieve bins
    reg2bins(beg, end, iter, idx->min_shift, idx->n_lvls);
    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) return iter;
    off = (hts_pair64_t*)calloc(n_off, sizeof(hts_pair64_t));
    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) off[n_off++] = p->list[j];
        }
    }
    if (n_off == 0) {
        free(off); return iter;
    }
    ks_introsort(_off, 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 $0x48, %rsp
movq %r8, %rbp
movl %esi, %r14d
movq %rdi, %r13
pushq $0x1
popq %rax
movl %eax, 0x1c(%rsp)
testl %esi, %esi
js 0x49a63
movl %ecx, %r15d
movl %edx, %r12d
xorl %ebx, %ebx
testl %edx, %edx
cmovlel %ebx, %r12d
cmpl %ecx, %r12d
jg 0x49b49
cmpl %r14d, 0x14(%r13)
jle 0x49b47
movq 0x28(%r13), %rax
movl %r14d, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x49b47
pushq $0x1
popq %rdi
pushq $0x50
popq %rsi
callq 0x6470
movq %rax, %rbx
movl %r14d, 0x4(%rax)
movl %r12d, 0x8(%rax)
movl %r15d, 0xc(%rax)
orl $-0x1, 0x14(%rax)
movq %rbp, 0x38(%rax)
movl 0x4(%r13), %edx
movl 0x8(%r13), %eax
movq %rax, 0x8(%rsp)
leal (%rax,%rax,2), %ecx
pushq $-0x1
popq %rax
shll %cl, %eax
notl %eax
pushq $0x7
popq %rsi
movl %r12d, %r14d
movq %rdx, 0x10(%rsp)
movl %edx, %ecx
shrl %cl, %r14d
xorl %edx, %edx
divl %esi
addl %eax, %r14d
movq 0x20(%rsp), %r13
movq %r13, %rdi
movl %r14d, %esi
callq 0x498fd
movl (%r13), %ebp
cmpl %ebp, %eax
jne 0x49b5b
leal -0x1(%r14), %ecx
movl %ecx, %edx
andl $-0x8, %edx
incl %edx
movl %ecx, %esi
sarl $0x3, %esi
cmpl %edx, %r14d
cmovgl %ecx, %esi
movl %esi, %r14d
movl %eax, %ebp
testl %esi, %esi
jne 0x49a28
jmp 0x49b60
xorl %r15d, %r15d
addl $0x5, %r14d
cmpl $0x3, %r14d
ja 0x49b47
leaq 0x3d70d(%rip), %rax     # 0x87188
movslq (%rax,%r14,4), %rcx
addq %rax, %rcx
jmpq *%rcx
pushq $0x3
popq %rax
movl %eax, 0x1c(%rsp)
xorl %r15d, %r15d
jmp 0x49b29
movl 0x14(%r13), %r14d
xorl %r12d, %r12d
testl %r14d, %r14d
cmovlel %r12d, %r14d
pushq $-0x1
popq %r15
cmpq %r12, %r14
je 0x49b19
movq 0x28(%r13), %rax
movq (%rax,%r12,8), %rbx
movl 0xc(%r13), %esi
incl %esi
movq %rbx, %rdi
callq 0x498fd
cmpl (%rbx), %eax
je 0x49add
movq 0x20(%rbx), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
movq 0x10(%rcx,%rax), %rax
movq (%rax), %rax
cmpq %rax, %r15
cmovaeq %rax, %r15
incq %r12
jmp 0x49aa5
movslq 0x14(%r13), %rax
testq %rax, %rax
jle 0x49b1f
movq 0x28(%r13), %rcx
movq -0x8(%rcx,%rax,8), %rbx
movl 0xc(%r13), %esi
incl %esi
movq %rbx, %rdi
callq 0x498fd
cmpl (%rbx), %eax
je 0x49b1f
movq 0x20(%rbx), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
movq 0x10(%rcx,%rax), %rax
movq 0x8(%rax), %r15
cmpq $-0x1, %r15
jne 0x49b29
xorl %r15d, %r15d
cmpq $0x0, 0x20(%r13)
je 0x49b47
pushq $0x1
popq %rdi
pushq $0x50
popq %rsi
callq 0x6470
movq %rax, %rbx
movl 0x1c(%rsp), %eax
movl %eax, (%rbx)
movq %r15, 0x28(%rbx)
movq %rbp, 0x38(%rbx)
jmp 0x49b49
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r14d, %r14d
jne 0x49b6a
movq %r13, %rdi
xorl %esi, %esi
callq 0x498fd
xorl %edx, %edx
cmpl %ebp, %eax
jne 0x49b75
xorl %r14d, %r14d
jmp 0x49b84
movq 0x20(%r13), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
movq 0x8(%rcx,%rax), %r14
cmpl %r15d, %r12d
jge 0x49ca1
movl %r12d, %edx
movslq %r15d, %rax
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rsi
addl %ecx, %esi
leal (%rsi,%rcx,2), %esi
pushq $0x1
popq %rdi
movl %esi, %ecx
shlq %cl, %rdi
cmpq %rax, %rdi
cmovgeq %rax, %rdi
decq %rdi
xorl %r12d, %r12d
xorl %r10d, %r10d
xorl %ebp, %ebp
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %rdi, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
cmpl 0x8(%rsp), %r9d
jg 0x49ca6
movq %rdx, %r13
movl %esi, %ecx
shrq %cl, %r13
addl %r8d, %r13d
movq %rdi, %r15
sarq %cl, %r15
addl %r8d, %r15d
movl %r15d, %eax
subl %r13d, %eax
leal (%rax,%rbp), %ecx
incl %ecx
cmpl %r10d, %ecx
jle 0x49c6a
addl %ebp, %eax
movl %eax, %ecx
sarl %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
sarl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x8, %eax
orl %ecx, %eax
movl %eax, %ecx
sarl $0x10, %ecx
orl %eax, %ecx
leal 0x1(%rcx), %eax
movl %eax, 0x2c(%rsp)
movl %eax, 0x44(%rbx)
movslq %ecx, %rax
movl %esi, 0x10(%rsp)
leaq 0x4(,%rax,4), %rsi
movq %r12, %rdi
movl %r8d, %r12d
movq %r9, 0x40(%rsp)
callq 0x6c80
movl 0x2c(%rsp), %r10d
movq 0x40(%rsp), %r9
movl %r12d, %r8d
movq 0x38(%rsp), %rdi
movl 0x10(%rsp), %esi
movq 0x30(%rsp), %rdx
movq %rax, %r12
movq %rax, 0x48(%rbx)
movslq %ebp, %rbp
cmpl %r15d, %r13d
jg 0x49c85
leaq 0x1(%rbp), %rax
movl %eax, 0x40(%rbx)
movl %r13d, (%r12,%rbp,4)
incl %r13d
movq %rax, %rbp
jmp 0x49c6d
leal (%r9,%r9,2), %ecx
pushq $0x1
popq %rax
shll %cl, %eax
addl $-0x3, %esi
addl %eax, %r8d
incl %r9d
movq 0x20(%rsp), %r13
jmp 0x49bcc
xorl %r12d, %r12d
jmp 0x49ca9
movl 0x40(%rbx), %edx
xorl %r15d, %r15d
testl %edx, %edx
cmovlel %r15d, %edx
xorl %ebp, %ebp
movq %rdx, 0x8(%rsp)
cmpq %r15, %rdx
je 0x49ce8
movl (%r12,%r15,4), %esi
movq %r13, %rdi
callq 0x498fd
cmpl (%r13), %eax
je 0x49cde
movq 0x20(%r13), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
addl 0x4(%rcx,%rax), %ebp
incq %r15
movq 0x8(%rsp), %rdx
jmp 0x49cb9
testl %ebp, %ebp
je 0x49b49
movslq %ebp, %rdi
pushq $0x10
popq %rsi
callq 0x6470
movq 0x8(%rsp), %rcx
movq %rax, %r13
xorl %eax, %eax
xorl %r15d, %r15d
movq 0x20(%rsp), %rbp
cmpq %rcx, %rax
je 0x49d81
movq %rax, 0x10(%rsp)
movl (%r12,%rax,4), %esi
movq %rbp, %rdi
callq 0x498fd
cmpl (%rbp), %eax
jne 0x49d37
movq 0x10(%rsp), %rax
incq %rax
movq 0x8(%rsp), %rcx
jmp 0x49d0d
movq 0x20(%rbp), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rdx
leaq (%rcx,%rdx), %rax
movslq 0x4(%rcx,%rdx), %rcx
pushq $0x8
popq %rdx
xorl %esi, %esi
cmpq %rcx, %rsi
jge 0x49d28
movq 0x10(%rax), %rdi
cmpq %r14, (%rdi,%rdx)
jbe 0x49d78
addq %rdx, %rdi
addq $-0x8, %rdi
movslq %r15d, %r8
incl %r15d
shlq $0x4, %r8
movups (%rdi), %xmm0
movups %xmm0, (%r13,%r8)
incq %rsi
addq $0x10, %rdx
jmp 0x49d4f
testl %r15d, %r15d
je 0x49e5a
movslq %r15d, %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x483c1
leaq 0x18(%r13), %rcx
xorl %eax, %eax
pushq $0x1
popq %rdi
cmpq %r15, %rdi
jge 0x49dcf
movslq %eax, %rdx
shlq $0x4, %rdx
movq 0x8(%r13,%rdx), %rsi
cmpq (%rcx), %rsi
jae 0x49dc6
leaq -0x8(%rcx), %rsi
incl %eax
movups (%rsi), %xmm0
movups %xmm0, 0x10(%rdx,%r13)
incq %rdi
addq $0x10, %rcx
jmp 0x49da1
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movq %rax, %rdx
shlq $0x4, %rdx
cmpq %rcx, %rdx
je 0x49df9
movq 0x10(%r13,%rcx), %rsi
cmpq %rsi, 0x8(%r13,%rcx)
jb 0x49df3
movq %rsi, 0x8(%r13,%rcx)
addq $0x10, %rcx
jmp 0x49ddd
movl %eax, %eax
shlq $0x4, %rax
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %rdx, %rax
je 0x49e4c
movslq %ecx, %rsi
shlq $0x4, %rsi
movq 0x10(%r13,%rdx), %rdi
xorq 0x8(%r13,%rsi), %rdi
cmpq $0xffff, %rdi           # imm = 0xFFFF
ja 0x49e33
addq %r13, %rsi
addq $0x8, %rsi
movq 0x18(%r13,%rdx), %rdi
movq %rdi, (%rsi)
jmp 0x49e46
leaq (%rdx,%r13), %rdi
addq $0x10, %rdi
incl %ecx
movups (%rdi), %xmm0
movups %xmm0, 0x10(%rsi,%r13)
addq $0x10, %rdx
jmp 0x49e03
incl %ecx
movl %ecx, 0x10(%rbx)
movq %r13, 0x30(%rbx)
jmp 0x49b49
movq %r13, %rdi
callq 0x68a0
jmp 0x49b49