// File: /workspace/github_cpp_makefile_star2_O2/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
movq %r8, %rbp
movl %esi, %r14d
movq %rdi, %r13
pushq $0x1
popq %rax
movl %eax, 0x1c(%rsp)
testl %esi, %esi
js 0xf43a
movl %ecx, %r15d
movl %edx, %r12d
xorl %ebx, %ebx
testl %edx, %edx
cmovlel %ebx, %r12d
cmpl %ecx, %r12d
jg 0xf548
cmpl %r14d, 0x14(%r13)
jle 0xf546
movq 0x28(%r13), %rax
movl %r14d, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0xf546
pushq $0x1
popq %rdi
pushq $0x50
popq %rsi
callq 0x4370
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 0xf2d4
movl (%r13), %ebp
cmpl %ebp, %eax
jne 0xf55a
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 0xf3ff
jmp 0xf55f
xorl %r15d, %r15d
addl $0x5, %r14d
cmpl $0x3, %r14d
ja 0xf546
leaq 0x34596(%rip), %rax     # 0x439e8
movslq (%rax,%r14,4), %rcx
addq %rax, %rcx
jmpq *%rcx
pushq $0x3
popq %rax
movl %eax, 0x1c(%rsp)
xorl %r15d, %r15d
jmp 0xf528
movl 0x14(%r13), %r14d
xorl %r12d, %r12d
testl %r14d, %r14d
cmovlel %r12d, %r14d
pushq $-0x1
popq %r15
cmpq %r12, %r14
je 0xf518
movq 0x28(%r13), %rax
movq (%rax,%r12,8), %rbx
movl 0xc(%r13), %esi
incl %esi
movq %rbx, %rdi
callq 0xf2d4
cmpl (%rbx), %eax
je 0xf4b8
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 0xf47c
movl 0x14(%r13), %r14d
xorl %r12d, %r12d
testl %r14d, %r14d
cmovlel %r12d, %r14d
pushq $-0x1
popq %r15
cmpq %r12, %r14
je 0xf518
movq 0x28(%r13), %rax
movq (%rax,%r12,8), %rbx
movl 0xc(%r13), %esi
incl %esi
movq %rbx, %rdi
callq 0xf2d4
cmpl (%rbx), %eax
je 0xf513
movl %eax, %eax
imulq $0x18, %rax, %rax
movq 0x20(%rbx), %rcx
movq 0x10(%rcx,%rax), %rax
movq 0x8(%rax), %rax
cmpq $-0x1, %r15
je 0xf510
cmpq %rax, %r15
cmovbeq %rax, %r15
jmp 0xf513
movq %rax, %r15
incq %r12
jmp 0xf4cf
cmpq $-0x1, %r15
jne 0xf528
xorl %r15d, %r15d
cmpq $0x0, 0x20(%r13)
je 0xf546
pushq $0x1
popq %rdi
pushq $0x50
popq %rsi
callq 0x4370
movq %rax, %rbx
movl 0x1c(%rsp), %eax
movl %eax, (%rbx)
movq %r15, 0x28(%rbx)
movq %rbp, 0x38(%rbx)
jmp 0xf548
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 0xf569
movq %r13, %rdi
xorl %esi, %esi
callq 0xf2d4
xorl %edx, %edx
cmpl %ebp, %eax
jne 0xf574
xorl %r14d, %r14d
jmp 0xf583
movq 0x20(%r13), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
movq 0x8(%rcx,%rax), %r14
cmpl %r15d, %r12d
jge 0xf6a0
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 0xf6a5
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 0xf669
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 0x48c0
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 0xf684
leaq 0x1(%rbp), %rax
movl %eax, 0x40(%rbx)
movl %r13d, (%r12,%rbp,4)
incl %r13d
movq %rax, %rbp
jmp 0xf66c
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 0xf5cb
xorl %r12d, %r12d
jmp 0xf6a8
movl 0x40(%rbx), %edx
xorl %r15d, %r15d
testl %edx, %edx
cmovlel %r15d, %edx
xorl %ebp, %ebp
movq %rdx, 0x8(%rsp)
cmpq %r15, %rdx
je 0xf6e7
movl (%r12,%r15,4), %esi
movq %r13, %rdi
callq 0xf2d4
cmpl (%r13), %eax
je 0xf6dd
movq 0x20(%r13), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
addl 0x4(%rcx,%rax), %ebp
incq %r15
movq 0x8(%rsp), %rdx
jmp 0xf6b8
testl %ebp, %ebp
je 0xf548
movslq %ebp, %rdi
pushq $0x10
popq %rsi
callq 0x4370
movq 0x8(%rsp), %rcx
movq %rax, %r13
xorl %eax, %eax
xorl %r15d, %r15d
movq 0x20(%rsp), %rbp
cmpq %rcx, %rax
je 0xf780
movq %rax, 0x10(%rsp)
movl (%r12,%rax,4), %esi
movq %rbp, %rdi
callq 0xf2d4
cmpl (%rbp), %eax
jne 0xf736
movq 0x10(%rsp), %rax
incq %rax
movq 0x8(%rsp), %rcx
jmp 0xf70c
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 0xf727
movq 0x10(%rax), %rdi
cmpq %r14, (%rdi,%rdx)
jbe 0xf777
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 0xf74e
testl %r15d, %r15d
je 0xf859
movslq %r15d, %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0xdd67
leaq 0x18(%r13), %rcx
xorl %eax, %eax
pushq $0x1
popq %rdi
cmpq %r15, %rdi
jge 0xf7ce
movslq %eax, %rdx
shlq $0x4, %rdx
movq 0x8(%r13,%rdx), %rsi
cmpq (%rcx), %rsi
jae 0xf7c5
leaq -0x8(%rcx), %rsi
incl %eax
movups (%rsi), %xmm0
movups %xmm0, 0x10(%rdx,%r13)
incq %rdi
addq $0x10, %rcx
jmp 0xf7a0
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movq %rax, %rdx
shlq $0x4, %rdx
cmpq %rcx, %rdx
je 0xf7f8
movq 0x10(%r13,%rcx), %rsi
cmpq %rsi, 0x8(%r13,%rcx)
jb 0xf7f2
movq %rsi, 0x8(%r13,%rcx)
addq $0x10, %rcx
jmp 0xf7dc
movl %eax, %eax
shlq $0x4, %rax
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %rdx, %rax
je 0xf84b
movslq %ecx, %rsi
shlq $0x4, %rsi
movq 0x10(%r13,%rdx), %rdi
xorq 0x8(%r13,%rsi), %rdi
cmpq $0xffff, %rdi           # imm = 0xFFFF
ja 0xf832
addq %r13, %rsi
addq $0x8, %rsi
movq 0x18(%r13,%rdx), %rdi
movq %rdi, (%rsi)
jmp 0xf845
leaq (%rdx,%r13), %rdi
addq $0x10, %rdi
incl %ecx
movups (%rdi), %xmm0
movups %xmm0, 0x10(%rsi,%r13)
addq $0x10, %rdx
jmp 0xf802
incl %ecx
movl %ecx, 0x10(%rbx)
movq %r13, 0x30(%rbx)
jmp 0xf548
movq %r13, %rdi
callq 0x4600
jmp 0xf548