// File: /workspace/github_c_makefile_star2_O1/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, 0x10(%rsp)
movl %esi, %r13d
testl %esi, %esi
js 0x50e0f
movl %ecx, %r14d
movl %edx, %ebp
xorl %eax, %eax
testl %edx, %edx
cmovlel %eax, %ebp
cmpl %ecx, %ebp
jg 0x5147d
cmpl %r13d, 0x14(%rdi)
jle 0x5147b
movq 0x28(%rdi), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %r12
testq %r12, %r12
je 0x5147b
movq %rdi, %rbx
movl $0x1, %edi
movl $0x50, %esi
callq 0x6470
movl %r13d, 0x4(%rax)
movl %ebp, 0x8(%rax)
movl %r14d, 0xc(%rax)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, 0x14(%rax)
movq %rax, (%rsp)
movq 0x10(%rsp), %rcx
movq %rcx, 0x38(%rax)
movl 0x4(%rbx), %eax
movl 0x8(%rbx), %ecx
movq %rcx, 0x18(%rsp)
leal (%rcx,%rcx,2), %ecx
shll %cl, %edx
notl %edx
movl $0x92492493, %esi       # imm = 0x92492493
imulq %rdx, %rsi
movl %ebp, %r8d
movl %eax, %ecx
shrl %cl, %r8d
shrq $0x22, %rsi
addl %esi, %r8d
movl (%r12), %edx
leal -0x1(%rdx), %edi
testl %edx, %edx
je 0x50d70
movl %edi, %r9d
andl %r8d, %r9d
movq 0x10(%r12), %r10
movl $0x1, %r11d
movl %r9d, %esi
movl %esi, %ecx
shrl $0x4, %ecx
movl (%r10,%rcx,4), %ebx
leal (%rsi,%rsi), %ecx
andl $0x1e, %ecx
movl %ebx, %r15d
shrl %cl, %r15d
testb $0x2, %r15b
jne 0x50d66
testb $0x1, %r15b
jne 0x50d55
movq 0x18(%r12), %r15
movl %esi, %r13d
cmpl %r8d, (%r15,%r13,4)
je 0x50d66
addl %r11d, %esi
andl %edi, %esi
incl %r11d
cmpl %r9d, %esi
jne 0x50d26
movl %edx, %esi
jmp 0x50d72
shrl %cl, %ebx
testb $0x3, %bl
cmovnel %edx, %esi
jmp 0x50d72
xorl %esi, %esi
cmpl %edx, %esi
jne 0x50d95
leal -0x1(%r8), %ecx
movl %ecx, %r9d
andl $-0x8, %r9d
incl %r9d
movl %ecx, %r10d
sarl $0x3, %r10d
cmpl %r9d, %r8d
cmovgl %ecx, %r10d
movl %r10d, %r8d
cmpl %edx, %esi
jne 0x50da2
testl %r8d, %r8d
jne 0x50d0e
testl %r8d, %r8d
cmovel %r8d, %esi
testl %edx, %edx
je 0x50fb5
testl %r8d, %r8d
jne 0x50fb5
movq 0x10(%r12), %r8
xorl %esi, %esi
movl $0x1, %r9d
movl %esi, %ecx
shrl $0x4, %ecx
movl (%r8,%rcx,4), %r10d
leal (%rsi,%rsi), %ecx
andl $0x1e, %ecx
movl %r10d, %r11d
shrl %cl, %r11d
testb $0x2, %r11b
jne 0x50fab
testb $0x1, %r11b
jne 0x50dfe
movq 0x18(%r12), %r11
movl %esi, %ebx
cmpl $0x0, (%r11,%rbx,4)
je 0x50fab
addl %r9d, %esi
incl %r9d
andl %edi, %esi
jne 0x50dc7
movl %edx, %esi
jmp 0x50fb5
xorl %eax, %eax
addl $0x5, %r13d
cmpl $0x3, %r13d
ja 0x5147d
movl $0x1, (%rsp)
leaq 0x4734b(%rip), %rdx     # 0x98178
movslq (%rdx,%r13,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0x3, (%rsp)
xorl %eax, %eax
jmp 0x51448
movslq 0x14(%rdi), %rdx
testq %rdx, %rdx
movq %rdi, 0x18(%rsp)
jle 0x512f3
movq 0x28(%rdi), %rax
movq %rax, 0x8(%rsp)
movl 0xc(%rdi), %esi
incl %esi
movq $-0x1, %rax
xorl %edi, %edi
movq 0x8(%rsp), %rcx
movq (%rcx,%rdi,8), %r8
movl (%r8), %r9d
movl $0x0, %r10d
testl %r9d, %r9d
je 0x50ee8
leal -0x1(%r9), %r11d
movl %r11d, %ebx
andl %esi, %ebx
movq 0x10(%r8), %r14
movl $0x1, %r12d
movl %ebx, %r10d
movl %r10d, %ecx
shrl $0x4, %ecx
movl (%r14,%rcx,4), %ebp
leal (%r10,%r10), %ecx
andl $0x1e, %ecx
movl %ebp, %r13d
shrl %cl, %r13d
testb $0x2, %r13b
jne 0x50ede
testb $0x1, %r13b
jne 0x50ecb
movq 0x18(%r8), %r13
movl %r10d, %r15d
cmpl %esi, (%r13,%r15,4)
je 0x50ede
addl %r12d, %r10d
andl %r11d, %r10d
incl %r12d
cmpl %ebx, %r10d
jne 0x50e9a
movl %r9d, %r10d
jmp 0x50ee8
shrl %cl, %ebp
testb $0x3, %bpl
cmovnel %r9d, %r10d
cmpl %r9d, %r10d
je 0x50f07
movq 0x20(%r8), %rcx
movl %r10d, %r8d
leaq (%r8,%r8,2), %r8
movq 0x10(%rcx,%r8,8), %rcx
movq (%rcx), %rcx
cmpq %rcx, %rax
cmovaeq %rcx, %rax
incq %rdi
cmpq %rdx, %rdi
jne 0x50e6d
jmp 0x512fa
movslq 0x14(%rdi), %rdx
movq $-0x1, %rax
testq %rdx, %rdx
jle 0x51438
movq 0x28(%rdi), %rcx
movq -0x8(%rcx,%rdx,8), %r12
movl (%r12), %edx
testl %edx, %edx
je 0x5141e
movq %rdi, %r13
movl 0xc(%rdi), %edi
incl %edi
leal -0x1(%rdx), %r8d
movl %r8d, %r9d
andl %edi, %r9d
movq 0x10(%r12), %r10
movl $0x1, %r11d
movl %r9d, %esi
movl %esi, %ecx
shrl $0x4, %ecx
movl (%r10,%rcx,4), %ebx
leal (%rsi,%rsi), %ecx
andl $0x1e, %ecx
movl %ebx, %ebp
shrl %cl, %ebp
testb $0x2, %bpl
jne 0x5140a
testb $0x1, %bpl
jne 0x50f96
movq 0x18(%r12), %r14
movl %esi, %r15d
cmpl %edi, (%r14,%r15,4)
je 0x5140a
addl %r11d, %esi
andl %r8d, %esi
incl %r11d
cmpl %r9d, %esi
jne 0x50f61
movl %edx, %esi
jmp 0x51412
shrl %cl, %r10d
testb $0x3, %r10b
cmovnel %edx, %esi
cmpl %edx, %esi
jne 0x50fbd
xorl %ebx, %ebx
jmp 0x50fcd
movq 0x20(%r12), %rcx
movl %esi, %edx
leaq (%rdx,%rdx,2), %rdx
movq 0x8(%rcx,%rdx,8), %rbx
movq (%rsp), %rdi
cmpl %r14d, %ebp
movq %rbx, 0x10(%rsp)
jge 0x51126
movslq %r14d, %rdx
movq 0x18(%rsp), %rsi
addl %esi, %eax
leal (%rax,%rsi,2), %r14d
movl $0x1, %r8d
movl %r14d, %ecx
shlq %cl, %r8
cmpq %rdx, %r8
cmovgeq %rdx, %r8
testl %esi, %esi
js 0x51126
movl %ebp, %r9d
decq %r8
movl 0x40(%rdi), %r10d
movl 0x44(%rdi), %eax
movl %eax, 0x8(%rsp)
xorl %r11d, %r11d
xorl %ebx, %ebx
movq %r9, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq %r9, %rbp
movl %r14d, %ecx
shrq %cl, %rbp
leal (%rbx,%rbp), %edx
movq %r8, %r15
sarq %cl, %r15
leal (%rbx,%r15), %r13d
movl %r13d, %eax
subl %edx, %eax
leal (%rax,%r10), %ecx
incl %ecx
cmpl 0x8(%rsp), %ecx
jle 0x510c6
addl %r10d, %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
movq (%rsp), %rax
movq 0x48(%rax), %rdi
movslq %ecx, %rax
incl %ecx
movl %ecx, 0x8(%rsp)
leaq 0x4(,%rax,4), %rsi
movq %r10, 0x40(%rsp)
movq %rbx, 0x38(%rsp)
movq %r11, %rbx
movl %edx, 0x24(%rsp)
callq 0x6c80
movl 0x24(%rsp), %edx
movq %rbx, %r11
movq 0x38(%rsp), %rbx
movq 0x40(%rsp), %r10
movq 0x30(%rsp), %r9
movq 0x28(%rsp), %r8
movq (%rsp), %rdi
movq %rax, 0x48(%rdi)
cmpl %edx, %r13d
jl 0x510f1
movslq %r10d, %rax
shlq $0x2, %rax
addq 0x48(%rdi), %rax
subl %ebp, %r15d
incl %r15d
movl %edx, %ecx
xorl %edx, %edx
leal (%rcx,%rdx), %esi
movl %esi, (%rax,%rdx,4)
incq %rdx
cmpl %edx, %r15d
jne 0x510e0
addl %edx, %r10d
addl $-0x3, %r14d
leal (%r11,%r11,2), %ecx
movl $0x1, %eax
shll %cl, %eax
addl %ebx, %eax
leal 0x1(%r11), %ecx
movl %eax, %ebx
cmpl 0x18(%rsp), %r11d
movl %ecx, %r11d
jne 0x51028
movl %r10d, 0x40(%rdi)
movl 0x8(%rsp), %eax
movl %eax, 0x44(%rdi)
movq 0x10(%rsp), %rbx
movslq 0x40(%rdi), %rcx
movq %rcx, 0x8(%rsp)
testq %rcx, %rcx
jle 0x511e2
movq 0x48(%rdi), %r13
movl (%r12), %esi
leal -0x1(%rsi), %edi
xorl %r8d, %r8d
xorl %eax, %eax
movl $0x0, %r9d
testl %esi, %esi
je 0x511bd
movl (%r13,%r8,4), %r10d
movl %edi, %r11d
andl %r10d, %r11d
movq 0x10(%r12), %rbx
movl $0x1, %ebp
movl %r11d, %r9d
movl %r9d, %ecx
shrl $0x4, %ecx
movl (%rbx,%rcx,4), %r14d
leal (%r9,%r9), %ecx
andl $0x1e, %ecx
movl %r14d, %r15d
shrl %cl, %r15d
testb $0x2, %r15b
jne 0x511ad
testb $0x1, %r15b
jne 0x5119b
movq 0x18(%r12), %r15
movl %r9d, %edx
cmpl %r10d, (%r15,%rdx,4)
je 0x511ad
addl %ebp, %r9d
andl %edi, %r9d
incl %ebp
cmpl %r11d, %r9d
jne 0x5116a
movl %esi, %r9d
jmp 0x511b8
shrl %cl, %r14d
testb $0x3, %r14b
cmovnel %esi, %r9d
movq 0x10(%rsp), %rbx
cmpl %esi, %r9d
je 0x511d2
movq 0x20(%r12), %rcx
movl %r9d, %edx
leaq (%rdx,%rdx,2), %rdx
addl 0x4(%rcx,%rdx,8), %eax
incq %r8
cmpq 0x8(%rsp), %r8
jne 0x51148
jmp 0x511e4
xorl %eax, %eax
testl %eax, %eax
je 0x51404
movslq %eax, %rdi
movl $0x10, %esi
callq 0x6470
movq %rax, %r14
cmpl $0x0, 0x8(%rsp)
jle 0x51315
movq (%rsp), %rax
movq 0x48(%rax), %rax
movq %rax, 0x18(%rsp)
xorl %edx, %edx
xorl %ebp, %ebp
movl (%r12), %esi
movl $0x0, %edi
testl %esi, %esi
je 0x51290
movq 0x18(%rsp), %rax
movl (%rax,%rdx,4), %r8d
leal -0x1(%rsi), %r9d
movl %r9d, %r10d
andl %r8d, %r10d
movq 0x10(%r12), %r11
movl $0x1, %ebx
movl %r10d, %edi
movl %edi, %eax
shrl $0x4, %eax
movl (%r11,%rax,4), %eax
leal (%rdi,%rdi), %ecx
andl $0x1e, %ecx
movl %eax, %r15d
shrl %cl, %r15d
testb $0x2, %r15b
jne 0x51284
testb $0x1, %r15b
jne 0x51274
movq 0x18(%r12), %r15
movl %edi, %r13d
cmpl %r8d, (%r15,%r13,4)
je 0x51284
addl %ebx, %edi
andl %r9d, %edi
incl %ebx
cmpl %r10d, %edi
jne 0x51245
movl %esi, %edi
jmp 0x5128b
shrl %cl, %eax
testb $0x3, %al
cmovnel %esi, %edi
movq 0x10(%rsp), %rbx
cmpl %esi, %edi
je 0x512e3
movq 0x20(%r12), %rax
movl %edi, %ecx
leaq (%rcx,%rcx,2), %rcx
cmpl $0x0, 0x4(%rax,%rcx,8)
jle 0x512e3
leaq (%rax,%rcx,8), %rcx
movl $0x8, %esi
xorl %edi, %edi
movq 0x10(%rcx), %rax
cmpq %rbx, (%rax,%rsi)
jbe 0x512d3
addq %rsi, %rax
addq $-0x8, %rax
movslq %ebp, %r8
incl %ebp
shlq $0x4, %r8
movups (%rax), %xmm0
movups %xmm0, (%r14,%r8)
incq %rdi
movslq 0x4(%rcx), %rax
addq $0x10, %rsi
cmpq %rax, %rdi
jl 0x512b1
incq %rdx
cmpq 0x8(%rsp), %rdx
jne 0x51218
jmp 0x51317
movq $-0x1, %rax
cmpq $-0x1, %rax
jne 0x51448
xorl %eax, %eax
movq 0x18(%rsp), %rcx
cmpq $0x1, 0x20(%rcx)
jmp 0x51445
xorl %ebp, %ebp
testl %ebp, %ebp
je 0x513fc
movslq %ebp, %rdi
movq %r14, %rsi
callq 0x4f17b
xorl %ecx, %ecx
cmpl $0x2, %ebp
jl 0x51373
movl %ebp, %eax
leaq 0x18(%r14), %rdx
shlq $0x4, %rax
addq $-0x10, %rax
xorl %esi, %esi
xorl %ecx, %ecx
movslq %ecx, %rdi
shlq $0x4, %rdi
movq 0x8(%r14,%rdi), %r8
cmpq 0x18(%r14,%rsi), %r8
jae 0x5136a
leaq (%rdx,%rsi), %r8
addq $-0x8, %r8
incl %ecx
movups (%r8), %xmm0
movups %xmm0, 0x10(%rdi,%r14)
addq $0x10, %rsi
cmpq %rsi, %rax
jne 0x51343
movl %ecx, %eax
testl %ecx, %ecx
jle 0x5139c
movq %rax, %rdx
shlq $0x4, %rdx
xorl %esi, %esi
movq 0x10(%r14,%rsi), %rdi
cmpq %rdi, 0x8(%r14,%rsi)
jb 0x51393
movq %rdi, 0x8(%r14,%rsi)
addq $0x10, %rsi
cmpq %rsi, %rdx
jne 0x51382
testl %ecx, %ecx
jle 0x5148c
shlq $0x4, %rax
addq $0x10, %rax
xorl %ecx, %ecx
movl $0x10, %edx
movslq %ecx, %rsi
shlq $0x4, %rsi
movq (%r14,%rdx), %rdi
xorq 0x8(%r14,%rsi), %rdi
cmpq $0xffff, %rdi           # imm = 0xFFFF
ja 0x513dd
addq %r14, %rsi
addq $0x8, %rsi
movq 0x8(%r14,%rdx), %rdi
movq %rdi, (%rsi)
jmp 0x513ec
leaq (%r14,%rdx), %rdi
incl %ecx
movups (%rdi), %xmm0
movups %xmm0, 0x10(%rsi,%r14)
addq $0x10, %rdx
cmpq %rdx, %rax
jne 0x513b3
incl %ecx
jmp 0x51491
movq %r14, %rdi
callq 0x68a0
movq (%rsp), %rax
jmp 0x5147d
shrl %cl, %ebx
testb $0x3, %bl
cmovnel %edx, %esi
movq $-0x1, %rax
movq %r13, %rdi
jmp 0x51420
xorl %esi, %esi
cmpl %edx, %esi
je 0x51438
movq 0x20(%r12), %rax
movl %esi, %ecx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %rax
movq 0x8(%rax), %rax
cmpq $-0x1, %rax
jne 0x51448
xorl %eax, %eax
cmpq $0x1, 0x20(%rdi)
sbbq %rax, %rax
cmpq $-0x1, %rax
je 0x5147b
movq %rax, %rbx
movl $0x1, %edi
movl $0x50, %esi
callq 0x6470
movl (%rax), %ecx
andl $-0x4, %ecx
movl (%rsp), %edx
orl %ecx, %edx
movl %edx, (%rax)
movq %rbx, 0x28(%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x38(%rax)
jmp 0x5147d
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %ecx
movq (%rsp), %rax
movl %ecx, 0x10(%rax)
movq %r14, 0x30(%rax)
jmp 0x5147d