// File: /workspace/github_cpp_makefile_star2_O1/karel-brinda[P]ococo/ext/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:
            /* No-coor reads sort after all of the mapped reads.  The position
               is not stored in the index itself, so need to find the end
               offset for the last mapped read.  A loop is needed here in
               case references at the end of the file have no mapped reads,
               or sequence ids are not ordered sequentially.
               See issue samtools#568 and commits b2aab8, 60c22d and cc207d. */
            for (i = 0; i < idx->n; i++) {
                bidx = idx->bidx[i];
                k = kh_get(bin, bidx, META_BIN(idx));
                if (k != kh_end(bidx)) {
                    if (off0==(uint64_t)-1 || off0 < kh_val(bidx, k).list[0].v) {
                        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 0x12635
movl %ecx, %r12d
movl %edx, %ebp
xorl %ebx, %ebx
testl %edx, %edx
cmovlel %ebx, %ebp
cmpl %ecx, %ebp
jg 0x12884
cmpl %r13d, 0x14(%rdi)
jle 0x12882
movq 0x28(%rdi), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %r15
testq %r15, %r15
je 0x12882
movq %r8, %rbx
movq %rdi, %r14
movl $0x1, %edi
movl $0x50, %esi
callq 0x4360
movl %r13d, 0x4(%rax)
movl %ebp, 0x8(%rax)
movl %r12d, 0xc(%rax)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, 0x14(%rax)
movq %rax, (%rsp)
movq %rbx, 0x38(%rax)
movl 0x4(%r14), %eax
movl 0x8(%r14), %ecx
movq %rcx, 0x8(%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 (%r15), %edx
leal -0x1(%rdx), %edi
testl %edx, %edx
je 0x12598
movl %edi, %r9d
andl %r8d, %r9d
movq 0x10(%r15), %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, %r14d
shrl %cl, %r14d
testb $0x2, %r14b
jne 0x1258e
testb $0x1, %r14b
jne 0x1257d
movq 0x18(%r15), %r14
movl %esi, %r13d
cmpl %r8d, (%r14,%r13,4)
je 0x1258e
addl %r11d, %esi
andl %edi, %esi
incl %r11d
cmpl %r9d, %esi
jne 0x1254f
movl %edx, %esi
jmp 0x1259a
shrl %cl, %ebx
testb $0x3, %bl
cmovnel %edx, %esi
jmp 0x1259a
xorl %esi, %esi
cmpl %edx, %esi
jne 0x125bd
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 0x125ca
testl %r8d, %r8d
jne 0x12538
testl %r8d, %r8d
cmovel %r8d, %esi
testl %edx, %edx
je 0x128a0
testl %r8d, %r8d
jne 0x128a0
movq 0x10(%r15), %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 0x12896
testb $0x1, %r11b
jne 0x12624
movq 0x18(%r15), %r11
movl %esi, %ebx
cmpl $0x0, (%r11,%rbx,4)
je 0x12896
addl %r9d, %esi
incl %r9d
andl %edi, %esi
jne 0x125ee
movl %edx, %esi
jmp 0x128a0
xorl %ebx, %ebx
addl $0x5, %r13d
cmpl $0x3, %r13d
ja 0x12884
movq %r8, (%rsp)
movl $0x1, 0x10(%rsp)
leaq 0x40330(%rip), %rax     # 0x52988
movslq (%rax,%r13,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x3, 0x10(%rsp)
xorl %ebx, %ebx
jmp 0x1284f
movslq 0x14(%rdi), %rax
testq %rax, %rax
movq %rdi, 0x18(%rsp)
jle 0x12833
movq 0x28(%rdi), %rcx
movq %rcx, 0x8(%rsp)
movl 0xc(%rdi), %esi
incl %esi
movq $-0x1, %rbx
xorl %edi, %edi
movq 0x8(%rsp), %rcx
movq (%rcx,%rdi,8), %r8
movl (%r8), %r9d
movl $0x0, %r10d
testl %r9d, %r9d
je 0x1271a
movq %rbx, %rdx
leal -0x1(%r9), %r11d
movl %r11d, %ebx
andl %esi, %ebx
movq 0x10(%r8), %r15
movl $0x1, %r12d
movl %ebx, %r10d
movl %r10d, %ecx
shrl $0x4, %ecx
movl (%r15,%rcx,4), %ebp
leal (%r10,%r10), %ecx
andl $0x1e, %ecx
movl %ebp, %r13d
shrl %cl, %r13d
testb $0x2, %r13b
jne 0x1270d
testb $0x1, %r13b
jne 0x126fa
movq 0x18(%r8), %r13
movl %r10d, %r14d
cmpl %esi, (%r13,%r14,4)
je 0x1270d
addl %r12d, %r10d
andl %r11d, %r10d
incl %r12d
cmpl %ebx, %r10d
jne 0x126c9
movl %r9d, %r10d
jmp 0x12717
shrl %cl, %ebp
testb $0x3, %bpl
cmovnel %r9d, %r10d
movq %rdx, %rbx
cmpl %r9d, %r10d
je 0x12739
movq 0x20(%r8), %rcx
movl %r10d, %r8d
leaq (%r8,%r8,2), %r8
movq 0x10(%rcx,%r8,8), %rcx
movq (%rcx), %rcx
cmpq %rcx, %rbx
cmovaeq %rcx, %rbx
incq %rdi
cmpq %rax, %rdi
jne 0x12699
jmp 0x1283a
movslq 0x14(%rdi), %rax
testq %rax, %rax
movq %rdi, 0x18(%rsp)
jle 0x12833
movq 0x28(%rdi), %rcx
movq %rcx, 0x8(%rsp)
movl 0xc(%rdi), %esi
incl %esi
movq $-0x1, %rbx
xorl %edi, %edi
movq 0x8(%rsp), %rcx
movq (%rcx,%rdi,8), %r8
movl (%r8), %r9d
movl $0x0, %r10d
testl %r9d, %r9d
je 0x127f3
movq %rbx, %rdx
leal -0x1(%r9), %r11d
movl %r11d, %ebx
andl %esi, %ebx
movq 0x10(%r8), %r15
movl $0x1, %r12d
movl %ebx, %r10d
movl %r10d, %ecx
shrl $0x4, %ecx
movl (%r15,%rcx,4), %r13d
leal (%r10,%r10), %ecx
andl $0x1e, %ecx
movl %r13d, %ebp
shrl %cl, %ebp
testb $0x2, %bpl
jne 0x127e5
testb $0x1, %bpl
jne 0x127d2
movq 0x18(%r8), %r14
movl %r10d, %ebp
cmpl %esi, (%r14,%rbp,4)
je 0x127e5
addl %r12d, %r10d
andl %r11d, %r10d
incl %r12d
cmpl %ebx, %r10d
jne 0x127a3
movl %r9d, %r10d
jmp 0x127f0
shrl %cl, %r13d
testb $0x3, %r13b
cmovnel %r9d, %r10d
movq %rdx, %rbx
cmpl %r9d, %r10d
je 0x12825
movl %r10d, %ecx
cmpq $-0x1, %rbx
je 0x12814
movq 0x20(%r8), %r9
leaq (%rcx,%rcx,2), %r10
movq 0x10(%r9,%r10,8), %r9
cmpq 0x8(%r9), %rbx
jae 0x12825
movq 0x20(%r8), %r8
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%r8,%rcx,8), %rcx
movq 0x8(%rcx), %rbx
incq %rdi
cmpq %rax, %rdi
jne 0x12773
jmp 0x1283a
movq $-0x1, %rbx
cmpq $-0x1, %rbx
jne 0x1284f
xorl %ebx, %ebx
movq 0x18(%rsp), %rax
cmpq $0x1, 0x20(%rax)
sbbq %rbx, %rbx
cmpq $-0x1, %rbx
je 0x12882
movl $0x1, %edi
movl $0x50, %esi
callq 0x4360
movl (%rax), %ecx
andl $-0x4, %ecx
movl 0x10(%rsp), %edx
orl %ecx, %edx
movl %edx, (%rax)
movq %rbx, 0x28(%rax)
movq (%rsp), %rcx
movq %rcx, 0x38(%rax)
movq %rax, %rbx
jmp 0x12884
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
shrl %cl, %r10d
testb $0x3, %r10b
cmovnel %edx, %esi
cmpl %edx, %esi
jne 0x128a8
xorl %ebx, %ebx
jmp 0x128b7
movq 0x20(%r15), %rcx
movl %esi, %edx
leaq (%rdx,%rdx,2), %rdx
movq 0x8(%rcx,%rdx,8), %rbx
cmpl %r12d, %ebp
movq %rbx, 0x10(%rsp)
jge 0x12a1a
movslq %r12d, %rdx
movq 0x8(%rsp), %rsi
addl %esi, %eax
leal (%rax,%rsi,2), %r14d
movl $0x1, %edi
movl %r14d, %ecx
shlq %cl, %rdi
cmpq %rdx, %rdi
cmovgeq %rdx, %rdi
testl %esi, %esi
js 0x12a1a
movl %ebp, %r8d
decq %rdi
movq (%rsp), %rax
movl 0x40(%rax), %r9d
movl 0x44(%rax), %r12d
xorl %r10d, %r10d
xorl %r11d, %r11d
movq %rdi, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq (%rsp), %rbx
movq %r8, %rbp
movl %r14d, %ecx
shrq %cl, %rbp
leal (%r11,%rbp), %edx
movq %rdi, %r13
sarq %cl, %r13
leal (%r11,%r13), %esi
movl %esi, %eax
subl %edx, %eax
leal (%rax,%r9), %ecx
incl %ecx
cmpl %r12d, %ecx
jle 0x129b4
addl %r9d, %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, %r12d
sarl $0x10, %r12d
orl %eax, %r12d
movq 0x48(%rbx), %rdi
movslq %r12d, %rax
incl %r12d
movl %esi, 0x20(%rsp)
leaq 0x4(,%rax,4), %rsi
movq %r9, 0x18(%rsp)
movq %r10, 0x40(%rsp)
movq %r11, 0x38(%rsp)
movl %edx, 0x24(%rsp)
callq 0x48d0
movl 0x20(%rsp), %esi
movl 0x24(%rsp), %edx
movq 0x38(%rsp), %r11
movq 0x40(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %rdi
movq %rax, 0x48(%rbx)
cmpl %edx, %esi
jl 0x129e2
movslq %r9d, %rax
shlq $0x2, %rax
movq (%rsp), %rcx
addq 0x48(%rcx), %rax
subl %ebp, %r13d
incl %r13d
movl %edx, %ecx
xorl %edx, %edx
leal (%rcx,%rdx), %esi
movl %esi, (%rax,%rdx,4)
incq %rdx
cmpl %edx, %r13d
jne 0x129d1
addl %edx, %r9d
addl $-0x3, %r14d
leal (%r10,%r10,2), %ecx
movl $0x1, %eax
shll %cl, %eax
addl %r11d, %eax
leal 0x1(%r10), %ecx
movl %eax, %r11d
cmpl 0x8(%rsp), %r10d
movl %ecx, %r10d
jne 0x12913
movq (%rsp), %rax
movl %r9d, 0x40(%rax)
movl %r12d, 0x44(%rax)
movq 0x10(%rsp), %rbx
movq (%rsp), %rax
movslq 0x40(%rax), %rdx
movq %rdx, 0x8(%rsp)
testq %rdx, %rdx
jle 0x12ad6
movq 0x48(%rax), %r12
movl (%r15), %esi
leal -0x1(%rsi), %edi
xorl %r8d, %r8d
xorl %eax, %eax
movl $0x0, %r9d
testl %esi, %esi
je 0x12ab2
movl (%r12,%r8,4), %r10d
movl %edi, %r11d
andl %r10d, %r11d
movq 0x10(%r15), %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, %r13d
shrl %cl, %r13d
testb $0x2, %r13b
jne 0x12aa2
testb $0x1, %r13b
jne 0x12a90
movq 0x18(%r15), %r13
movl %r9d, %edx
cmpl %r10d, (%r13,%rdx,4)
je 0x12aa2
addl %ebp, %r9d
andl %edi, %r9d
incl %ebp
cmpl %r11d, %r9d
jne 0x12a5f
movl %esi, %r9d
jmp 0x12aad
shrl %cl, %r14d
testb $0x3, %r14b
cmovnel %esi, %r9d
movq 0x10(%rsp), %rbx
cmpl %esi, %r9d
je 0x12ac6
movq 0x20(%r15), %rcx
movl %r9d, %edx
leaq (%rdx,%rdx,2), %rdx
addl 0x4(%rcx,%rdx,8), %eax
incq %r8
cmpq 0x8(%rsp), %r8
jne 0x12a3f
jmp 0x12ad8
xorl %eax, %eax
testl %eax, %eax
je 0x12cd0
movslq %eax, %rdi
movl $0x10, %esi
callq 0x4360
movq %rax, %r14
cmpl $0x0, 0x8(%rsp)
jle 0x12be4
movq (%rsp), %rax
movq 0x48(%rax), %rax
movq %rax, 0x18(%rsp)
xorl %edx, %edx
xorl %ebp, %ebp
movl (%r15), %esi
movl $0x0, %edi
testl %esi, %esi
je 0x12b82
movq 0x18(%rsp), %rax
movl (%rax,%rdx,4), %r8d
leal -0x1(%rsi), %r9d
movl %r9d, %r10d
andl %r8d, %r10d
movq 0x10(%r15), %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, %r13d
shrl %cl, %r13d
testb $0x2, %r13b
jne 0x12b76
testb $0x1, %r13b
jne 0x12b66
movq 0x18(%r15), %r13
movl %edi, %r12d
cmpl %r8d, (%r13,%r12,4)
je 0x12b76
addl %ebx, %edi
andl %r9d, %edi
incl %ebx
cmpl %r10d, %edi
jne 0x12b37
movl %esi, %edi
jmp 0x12b7d
shrl %cl, %eax
testb $0x3, %al
cmovnel %esi, %edi
movq 0x10(%rsp), %rbx
cmpl %esi, %edi
je 0x12bd4
movq 0x20(%r15), %rax
movl %edi, %ecx
leaq (%rcx,%rcx,2), %rcx
cmpl $0x0, 0x4(%rax,%rcx,8)
jle 0x12bd4
leaq (%rax,%rcx,8), %rcx
movl $0x8, %esi
xorl %edi, %edi
movq 0x10(%rcx), %rax
cmpq %rbx, (%rax,%rsi)
jbe 0x12bc4
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 0x12ba2
incq %rdx
cmpq 0x8(%rsp), %rdx
jne 0x12b0c
jmp 0x12be6
xorl %ebp, %ebp
testl %ebp, %ebp
je 0x12cc8
movslq %ebp, %rdi
movq %r14, %rsi
callq 0x10a0f
xorl %ecx, %ecx
cmpl $0x2, %ebp
movq (%rsp), %rbx
jl 0x12c46
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 0x12c3d
leaq (%rdx,%rsi), %r8
addq $-0x8, %r8
incl %ecx
movups (%r8), %xmm0
movups %xmm0, 0x10(%rdi,%r14)
addq $0x10, %rsi
cmpq %rsi, %rax
jne 0x12c16
movl %ecx, %eax
testl %ecx, %ecx
jle 0x12c6f
movq %rax, %rdx
shlq $0x4, %rdx
xorl %esi, %esi
movq 0x10(%r14,%rsi), %rdi
cmpq %rdi, 0x8(%r14,%rsi)
jb 0x12c66
movq %rdi, 0x8(%r14,%rsi)
addq $0x10, %rsi
cmpq %rsi, %rdx
jne 0x12c55
testl %ecx, %ecx
jle 0x12cd9
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 0x12cac
addq %r14, %rsi
addq $0x8, %rsi
movq 0x8(%r14,%rdx), %rdi
movq %rdi, (%rsi)
jmp 0x12cbb
leaq (%r14,%rdx), %rdi
incl %ecx
movups (%rdi), %xmm0
movups %xmm0, 0x10(%rsi,%r14)
addq $0x10, %rdx
cmpq %rdx, %rax
jne 0x12c82
incl %ecx
jmp 0x12cde
movq %r14, %rdi
callq 0x4610
movq (%rsp), %rbx
jmp 0x12884
movl $0x1, %ecx
movl %ecx, 0x10(%rbx)
movq %r14, 0x30(%rbx)
jmp 0x12884