// File: /workspace/github_c_makefile_star2_O3/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
movl %esi, %r13d
testl %esi, %esi
js 0x510e9
movl %ecx, %r12d
movl %edx, %ebp
xorl %eax, %eax
testl %edx, %edx
cmovlel %eax, %ebp
cmpl %ecx, %ebp
jg 0x5178b
cmpl %r13d, 0x14(%rdi)
jle 0x51789
movq 0x28(%rdi), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %rbx
testq %rbx, %rbx
je 0x51789
movq %rdi, %r15
movl $0x1, %edi
movl $0x50, %esi
movq %r8, %r14
callq 0x6470
movl %r13d, 0x4(%rax)
movl %ebp, 0x8(%rax)
movl %r12d, 0xc(%rax)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, 0x14(%rax)
movq %rax, 0x8(%rsp)
movq %r14, 0x38(%rax)
movl 0x4(%r15), %eax
movl 0x8(%r15), %ecx
movq %rcx, (%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 (%rbx), %edx
leal -0x1(%rdx), %esi
movq %rbx, %r13
testl %edx, %edx
je 0x510ab
movl %esi, %r9d
andl %r8d, %r9d
movq 0x10(%r13), %r10
movl $0x1, %r11d
movl %r9d, %edi
movl %edi, %ecx
shrl $0x4, %ecx
movl (%r10,%rcx,4), %ebx
leal (%rdi,%rdi), %ecx
andl $0x1e, %ecx
movl %ebx, %r14d
shrl %cl, %r14d
testb $0x2, %r14b
jne 0x510a1
testb $0x1, %r14b
jne 0x51092
movq 0x18(%r13), %r14
movl %edi, %r15d
cmpl %r8d, (%r14,%r15,4)
je 0x510a1
addl %r11d, %edi
andl %esi, %edi
incl %r11d
cmpl %r9d, %edi
jne 0x51064
jmp 0x510b5
shrl %cl, %ebx
testb $0x3, %bl
cmovnel %edx, %edi
jmp 0x510ad
xorl %edi, %edi
cmpl %edx, %edi
jne 0x512ac
leal -0x1(%r8), %ecx
movl %ecx, %edi
andl $-0x8, %edi
incl %edi
movl %ecx, %r9d
sarl $0x3, %r9d
cmpl %edi, %r8d
cmovgl %ecx, %r9d
movl %r9d, %r8d
testl %r9d, %r9d
jne 0x5104d
testl %edx, %edx
jne 0x512b9
xorl %edi, %edi
jmp 0x5130c
xorl %r12d, %r12d
addl $0x5, %r13d
cmpl $0x3, %r13d
ja 0x51789
movq %r8, 0x20(%rsp)
leaq 0x47192(%rip), %rax     # 0x98298
movslq (%rax,%r13,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x3, (%rsp)
xorl %r12d, %r12d
jmp 0x51766
movl $0x1, (%rsp)
movslq 0x14(%rdi), %rax
testq %rax, %rax
jle 0x511ff
movq 0x28(%rdi), %rcx
movq %rcx, 0x10(%rsp)
movq %rdi, 0x28(%rsp)
movl 0xc(%rdi), %esi
incl %esi
movq $-0x1, %r12
xorl %edi, %edi
movq 0x10(%rsp), %rcx
movq (%rcx,%rdi,8), %r8
movl (%r8), %r9d
movl $0x0, %r10d
testl %r9d, %r9d
je 0x511c5
leal -0x1(%r9), %r11d
movl %r11d, %ebp
andl %esi, %ebp
movq 0x10(%r8), %r13
movl $0x1, %ebx
movl %ebp, %r10d
movl %r10d, %ecx
shrl $0x4, %ecx
movl (%r13,%rcx,4), %r15d
leal (%r10,%r10), %ecx
andl $0x1e, %ecx
movl %r15d, %r14d
shrl %cl, %r14d
testb $0x2, %r14b
jne 0x511ba
testb $0x1, %r14b
jne 0x511ab
movq 0x18(%r8), %r14
movl %r10d, %edx
cmpl %esi, (%r14,%rdx,4)
je 0x511ba
addl %ebx, %r10d
andl %r11d, %r10d
incl %ebx
cmpl %ebp, %r10d
jne 0x5117a
jmp 0x511e4
shrl %cl, %r15d
testb $0x3, %r15b
cmovnel %r9d, %r10d
cmpl %r9d, %r10d
je 0x511e4
movq 0x20(%r8), %rcx
movl %r10d, %edx
leaq (%rdx,%rdx,2), %rdx
movq 0x10(%rcx,%rdx,8), %rcx
movq (%rcx), %rcx
cmpq %rcx, %r12
cmovaeq %rcx, %r12
incq %rdi
cmpq %rax, %rdi
jne 0x5114e
cmpq $-0x1, %r12
movq 0x28(%rsp), %rdi
jne 0x51766
xorl %r12d, %r12d
cmpq $0x0, 0x20(%rdi)
jne 0x51766
jmp 0x51789
movl $0x1, (%rsp)
movq %rdi, %r13
movslq 0x14(%rdi), %rax
testq %rax, %rax
jle 0x5175c
movq 0x28(%r13), %rcx
movq -0x8(%rcx,%rax,8), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x5173d
movl 0xc(%r13), %edi
incl %edi
leal -0x1(%rdx), %r8d
movl %r8d, %r9d
andl %edi, %r9d
movq 0x10(%rax), %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 0x51733
testb $0x1, %bpl
jne 0x5128d
movq 0x18(%rax), %r14
movl %esi, %r15d
cmpl %edi, (%r14,%r15,4)
je 0x51733
addl %r11d, %esi
andl %r8d, %esi
incl %r11d
cmpl %r9d, %esi
jne 0x51259
jmp 0x5175c
movl $0x1, (%rsp)
jmp 0x51766
testl %r8d, %r8d
cmovel %r8d, %edi
jne 0x5130c
testl %edx, %edx
je 0x5130c
movq 0x10(%r13), %r8
xorl %r14d, %r14d
movl $0x1, %r9d
xorl %edi, %edi
movl %edi, %ecx
shrl $0x4, %ecx
movl (%r8,%rcx,4), %r10d
leal (%rdi,%rdi), %ecx
andl $0x1e, %ecx
movl %r10d, %r11d
shrl %cl, %r11d
testb $0x2, %r11b
jne 0x51302
testb $0x1, %r11b
jne 0x512f6
movq 0x18(%r13), %r11
movl %edi, %ebx
cmpl $0x0, (%r11,%rbx,4)
je 0x51302
addl %r9d, %edi
incl %r9d
andl %esi, %edi
jne 0x512c8
jmp 0x51324
shrl %cl, %r10d
testb $0x3, %r10b
cmovnel %edx, %edi
cmpl %edx, %edi
jne 0x51315
xorl %r14d, %r14d
jmp 0x51324
movq 0x20(%r13), %rcx
movl %edi, %edx
leaq (%rdx,%rdx,2), %rdx
movq 0x8(%rcx,%rdx,8), %r14
cmpl %r12d, %ebp
jge 0x517a2
movslq %r12d, %rdx
movq (%rsp), %rsi
addl %esi, %eax
leal (%rax,%rsi,2), %r15d
movl $0x1, %r9d
movl %r15d, %ecx
shlq %cl, %r9
cmpq %rdx, %r9
cmovgeq %rdx, %r9
testl %esi, %esi
js 0x517a2
movq %r14, 0x18(%rsp)
movl %ebp, %r10d
decq %r9
xorl %edi, %edi
xorl %ebp, %ebp
xorl %r8d, %r8d
xorl %r11d, %r11d
xorl %r14d, %r14d
movq %r13, 0x20(%rsp)
movq %r9, 0x38(%rsp)
movq %r10, %rbx
movl %r15d, %ecx
shrq %cl, %rbx
leal (%r14,%rbx), %r13d
movq %r9, %r12
sarq %cl, %r12
leal (%r14,%r12), %esi
movl %esi, %eax
subl %r13d, %eax
leal (%rax,%r8), %ecx
incl %ecx
cmpl %ebp, %ecx
movq %r8, 0x10(%rsp)
jle 0x5140c
addl %r8d, %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, %ebp
sarl $0x10, %ebp
orl %eax, %ebp
movslq %ebp, %rax
incl %ebp
movl %esi, 0x34(%rsp)
leaq 0x4(,%rax,4), %rsi
movq %r14, 0x28(%rsp)
movq %r10, %r14
movq %r11, 0x40(%rsp)
callq 0x6c80
movl 0x34(%rsp), %esi
movq 0x40(%rsp), %r11
movq %r14, %r10
movq 0x28(%rsp), %r14
movq 0x38(%rsp), %r9
movq %rax, %rdi
movq 0x8(%rsp), %rax
movq %rdi, 0x48(%rax)
cmpl %r13d, %esi
jge 0x5141d
movq 0x20(%rsp), %r13
movq 0x10(%rsp), %r8
jmp 0x5144a
movq 0x10(%rsp), %r8
movslq %r8d, %rax
leaq (%rdi,%rax,4), %rax
subl %ebx, %r12d
incl %r12d
movl %r13d, %ecx
xorl %edx, %edx
leal (%rcx,%rdx), %esi
movl %esi, (%rax,%rdx,4)
incq %rdx
cmpl %edx, %r12d
jne 0x51434
addl %edx, %r8d
movq 0x20(%rsp), %r13
addl $-0x3, %r15d
leal (%r11,%r11,2), %ecx
movl $0x1, %eax
shll %cl, %eax
addl %r14d, %eax
leal 0x1(%r11), %ecx
movl %eax, %r14d
cmpl (%rsp), %r11d
movl %ecx, %r11d
jne 0x51377
movq 0x8(%rsp), %rax
movl %r8d, 0x40(%rax)
movl %ebp, 0x44(%rax)
testl %r8d, %r8d
jle 0x5178b
movq %rdi, (%rsp)
movl (%r13), %eax
leal -0x1(%rax), %edx
movq %r8, 0x10(%rsp)
movl %r8d, %ebp
xorl %edi, %edi
xorl %esi, %esi
movq 0x18(%rsp), %rbx
movl $0x0, %r8d
testl %eax, %eax
je 0x51519
movq (%rsp), %rcx
movl (%rcx,%rdi,4), %r9d
movl %edx, %r10d
andl %r9d, %r10d
movq 0x10(%r13), %r11
movl $0x1, %ebx
movl %r10d, %r8d
movl %r8d, %ecx
shrl $0x4, %ecx
movl (%r11,%rcx,4), %r14d
leal (%r8,%r8), %ecx
andl $0x1e, %ecx
movl %r14d, %r15d
shrl %cl, %r15d
testb $0x2, %r15b
jne 0x51509
testb $0x1, %r15b
jne 0x514f5
movq 0x18(%r13), %r15
movl %r8d, %r12d
cmpl %r9d, (%r15,%r12,4)
je 0x51509
addl %ebx, %r8d
andl %edx, %r8d
incl %ebx
cmpl %r10d, %r8d
jne 0x514c5
movq 0x18(%rsp), %rbx
jmp 0x5152e
shrl %cl, %r14d
testb $0x3, %r14b
cmovnel %eax, %r8d
movq 0x18(%rsp), %rbx
cmpl %eax, %r8d
je 0x5152e
movq 0x20(%r13), %rcx
movl %r8d, %r8d
leaq (%r8,%r8,2), %r8
addl 0x4(%rcx,%r8,8), %esi
incq %rdi
cmpq %rbp, %rdi
jne 0x514a1
testl %esi, %esi
je 0x517a2
movslq %esi, %rdi
movl $0x10, %esi
callq 0x6470
movq %rax, %r15
cmpl $0x0, 0x10(%rsp)
jle 0x5179a
movl (%r13), %eax
leal -0x1(%rax), %edx
xorl %esi, %esi
xorl %r12d, %r12d
movl $0x0, %edi
testl %eax, %eax
je 0x515e1
movq (%rsp), %rcx
movl (%rcx,%rsi,4), %r8d
movl %edx, %r9d
andl %r8d, %r9d
movq 0x10(%r13), %r10
movl $0x1, %r11d
movl %r9d, %edi
movl %edi, %ecx
shrl $0x4, %ecx
movl (%r10,%rcx,4), %ebx
leal (%rdi,%rdi), %ecx
andl $0x1e, %ecx
movl %ebx, %r14d
shrl %cl, %r14d
testb $0x2, %r14b
jne 0x515d4
testb $0x1, %r14b
jne 0x515c0
movq 0x18(%r13), %r14
movl %edi, %r13d
cmpl %r8d, (%r14,%r13,4)
movq 0x20(%rsp), %r13
je 0x515d4
addl %r11d, %edi
andl %edx, %edi
incl %r11d
cmpl %r9d, %edi
jne 0x5158d
movq 0x18(%rsp), %rbx
jmp 0x51630
shrl %cl, %ebx
testb $0x3, %bl
cmovnel %eax, %edi
movq 0x18(%rsp), %rbx
cmpl %eax, %edi
je 0x51630
movq 0x20(%r13), %r8
movl %edi, %ecx
leaq (%rcx,%rcx,2), %rdi
movslq 0x4(%r8,%rdi,8), %rcx
testq %rcx, %rcx
jle 0x51630
leaq (%r8,%rdi,8), %rdi
movq 0x10(%rdi), %rdi
addq $0x8, %rdi
xorl %r8d, %r8d
cmpq %rbx, (%rdi)
jbe 0x51624
leaq -0x8(%rdi), %r9
movslq %r12d, %r10
incl %r12d
shlq $0x4, %r10
movups (%r9), %xmm0
movups %xmm0, (%r15,%r10)
incq %r8
addq $0x10, %rdi
cmpq %rcx, %r8
jb 0x51608
incq %rsi
cmpq %rbp, %rsi
jne 0x51569
testl %r12d, %r12d
je 0x5179a
movslq %r12d, %rdi
movq %r15, %rsi
callq 0x4f3c1
movl $0x1, %eax
cmpl $0x2, %r12d
jl 0x51722
movl %r12d, %ecx
leaq 0x18(%r15), %rsi
shlq $0x4, %rcx
addq $-0x10, %rcx
xorl %edi, %edi
xorl %edx, %edx
movslq %edx, %r8
shlq $0x4, %r8
movq 0x8(%r15,%r8), %r9
cmpq 0x18(%r15,%rdi), %r9
jae 0x51699
leaq (%rsi,%rdi), %r9
addq $-0x8, %r9
incl %edx
movups (%r9), %xmm0
movups %xmm0, 0x10(%r8,%r15)
addq $0x10, %rdi
cmpq %rdi, %rcx
jne 0x51672
testl %edx, %edx
jle 0x51722
movl %edx, %ecx
movq %rcx, %rsi
shlq $0x4, %rsi
xorl %edi, %edi
movq 0x10(%r15,%rdi), %r8
cmpq %r8, 0x8(%r15,%rdi)
jb 0x516c2
movq %r8, 0x8(%r15,%rdi)
addq $0x10, %rdi
cmpq %rdi, %rsi
jne 0x516b1
testl %edx, %edx
jle 0x51722
shlq $0x4, %rcx
addq $0x10, %rcx
xorl %eax, %eax
movl $0x10, %edx
movslq %eax, %rsi
shlq $0x4, %rsi
movq (%r15,%rdx), %rdi
xorq 0x8(%r15,%rsi), %rdi
cmpq $0xffff, %rdi           # imm = 0xFFFF
ja 0x51708
addq %r15, %rsi
addq $0x8, %rsi
movq 0x8(%r15,%rdx), %rdi
movq %rdi, (%rsi)
jmp 0x51717
leaq (%r15,%rdx), %rdi
incl %eax
movups (%rdi), %xmm0
movups %xmm0, 0x10(%rsi,%r15)
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x516de
incl %eax
movq 0x8(%rsp), %rcx
movl %eax, 0x10(%rcx)
movq %rcx, %rax
movq %r15, 0x30(%rcx)
jmp 0x5178b
shrl %cl, %ebx
testb $0x3, %bl
cmovnel %edx, %esi
jmp 0x5173f
xorl %esi, %esi
cmpl %edx, %esi
je 0x5175c
movq 0x20(%rax), %rax
movl %esi, %ecx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %rax
movq 0x8(%rax), %r12
cmpq $-0x1, %r12
jne 0x51766
xorl %r12d, %r12d
cmpq $0x0, 0x20(%r13)
je 0x51789
movl $0x1, %edi
movl $0x50, %esi
callq 0x6470
movl (%rsp), %ecx
movl %ecx, (%rax)
movq %r12, 0x28(%rax)
movq 0x20(%rsp), %rcx
movq %rcx, 0x38(%rax)
jmp 0x5178b
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x68a0
movq 0x8(%rsp), %rax
jmp 0x5178b