// File: /workspace/github_cpp_makefile_star2_O3/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 0x126b7
movl %ecx, %r12d
movl %edx, %ebp
xorl %eax, %eax
testl %edx, %edx
cmovlel %eax, %ebp
cmpl %ecx, %ebp
jg 0x12d7e
cmpl %r13d, 0x14(%rdi)
jle 0x128b6
movq 0x28(%rdi), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %rbx
testq %rbx, %rbx
je 0x128b6
movq %r8, %r14
movq %rdi, %r15
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, 0x10(%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 0x12679
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 0x1266f
testb $0x1, %r14b
jne 0x12660
movq 0x18(%r13), %r14
movl %edi, %r15d
cmpl %r8d, (%r14,%r15,4)
je 0x1266f
addl %r11d, %edi
andl %esi, %edi
incl %r11d
cmpl %r9d, %edi
jne 0x12632
jmp 0x12683
shrl %cl, %ebx
testb $0x3, %bl
cmovnel %edx, %edi
jmp 0x1267b
xorl %edi, %edi
cmpl %edx, %edi
jne 0x128ea
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 0x1261b
testl %edx, %edx
jne 0x128f7
xorl %edi, %edi
jmp 0x1294a
xorl %r12d, %r12d
addl $0x5, %r13d
cmpl $0x3, %r13d
ja 0x128b6
movq %r8, 0x28(%rsp)
leaq 0x3e314(%rip), %rax     # 0x509e8
movslq (%rax,%r13,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x3, (%rsp)
xorl %r12d, %r12d
jmp 0x128c4
movl $0x1, (%rsp)
movslq 0x14(%rdi), %rax
testq %rax, %rax
jle 0x128ac
movq 0x28(%rdi), %rcx
movq %rcx, 0x8(%rsp)
movq %rdi, 0x20(%rsp)
movl 0xc(%rdi), %esi
incl %esi
movq $-0x1, %r12
xorl %edi, %edi
movq 0x8(%rsp), %rcx
movq (%rcx,%rdi,8), %r8
movl (%r8), %r9d
movl $0x0, %r10d
testl %r9d, %r9d
je 0x12793
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 0x12788
testb $0x1, %r14b
jne 0x12779
movq 0x18(%r8), %r14
movl %r10d, %edx
cmpl %esi, (%r14,%rdx,4)
je 0x12788
addl %ebx, %r10d
andl %r11d, %r10d
incl %ebx
cmpl %ebp, %r10d
jne 0x12748
jmp 0x127b2
shrl %cl, %r15d
testb $0x3, %r15b
cmovnel %r9d, %r10d
cmpl %r9d, %r10d
je 0x127b2
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 0x1271c
jmp 0x128a1
movl $0x1, (%rsp)
movslq 0x14(%rdi), %rax
testq %rax, %rax
jle 0x128ac
movq 0x28(%rdi), %rcx
movq %rcx, 0x8(%rsp)
movq %rdi, 0x20(%rsp)
movl 0xc(%rdi), %esi
incl %esi
movq $-0x1, %r12
xorl %edi, %edi
movq 0x8(%rsp), %rcx
movq (%rcx,%rdi,8), %r8
movl (%r8), %r9d
movl $0x0, %r10d
testl %r9d, %r9d
je 0x1286a
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), %r14d
leal (%r10,%r10), %ecx
andl $0x1e, %ecx
movl %r14d, %r15d
shrl %cl, %r15d
testb $0x2, %r15b
jne 0x1285f
testb $0x1, %r15b
jne 0x12850
movq 0x18(%r8), %r15
movl %r10d, %edx
cmpl %esi, (%r15,%rdx,4)
je 0x1285f
addl %ebx, %r10d
andl %r11d, %r10d
incl %ebx
cmpl %ebp, %r10d
jne 0x1281f
jmp 0x12895
shrl %cl, %r14d
testb $0x3, %r14b
cmovnel %r9d, %r10d
cmpl %r9d, %r10d
je 0x12895
movq 0x20(%r8), %rcx
movl %r10d, %edx
leaq (%rdx,%rdx,2), %rdx
movq 0x10(%rcx,%rdx,8), %rcx
movq 0x8(%rcx), %rcx
cmpq $-0x1, %r12
je 0x12892
cmpq %rcx, %r12
cmovbeq %rcx, %r12
jmp 0x12895
movq %rcx, %r12
incq %rdi
cmpq %rax, %rdi
jne 0x127f3
cmpq $-0x1, %r12
movq 0x20(%rsp), %rdi
jne 0x128c4
xorl %r12d, %r12d
cmpq $0x0, 0x20(%rdi)
jne 0x128c4
xorl %eax, %eax
jmp 0x12d7e
movl $0x1, (%rsp)
movl $0x1, %edi
movl $0x50, %esi
callq 0x4360
movl (%rsp), %ecx
movl %ecx, (%rax)
movq %r12, 0x28(%rax)
movq 0x28(%rsp), %rcx
movq %rcx, 0x38(%rax)
jmp 0x12d7e
testl %r8d, %r8d
cmovel %r8d, %edi
jne 0x1294a
testl %edx, %edx
je 0x1294a
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 0x12940
testb $0x1, %r11b
jne 0x12934
movq 0x18(%r13), %r11
movl %edi, %ebx
cmpl $0x0, (%r11,%rbx,4)
je 0x12940
addl %r9d, %edi
incl %r9d
andl %esi, %edi
jne 0x12906
jmp 0x12962
shrl %cl, %r10d
testb $0x3, %r10b
cmovnel %edx, %edi
cmpl %edx, %edi
jne 0x12953
xorl %r14d, %r14d
jmp 0x12962
movq 0x20(%r13), %rcx
movl %edi, %edx
leaq (%rdx,%rdx,2), %rdx
movq 0x8(%rcx,%rdx,8), %r14
cmpl %r12d, %ebp
jge 0x12d79
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 0x12d79
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, 0x28(%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, 0x8(%rsp)
jle 0x12a4a
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, 0x20(%rsp)
movq %r10, %r14
movq %r11, 0x40(%rsp)
callq 0x48d0
movl 0x34(%rsp), %esi
movq 0x40(%rsp), %r11
movq %r14, %r10
movq 0x20(%rsp), %r14
movq 0x38(%rsp), %r9
movq %rax, %rdi
movq 0x10(%rsp), %rax
movq %rdi, 0x48(%rax)
cmpl %r13d, %esi
jge 0x12a5b
movq 0x28(%rsp), %r13
movq 0x8(%rsp), %r8
jmp 0x12a88
movq 0x8(%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 0x12a72
addl %edx, %r8d
movq 0x28(%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 0x129b5
movq 0x10(%rsp), %rax
movl %r8d, 0x40(%rax)
movl %ebp, 0x44(%rax)
testl %r8d, %r8d
jle 0x12d7e
movq %rdi, (%rsp)
movl (%r13), %eax
leal -0x1(%rax), %edx
movq %r8, 0x8(%rsp)
movl %r8d, %ebp
xorl %edi, %edi
xorl %esi, %esi
movq 0x18(%rsp), %rbx
movl $0x0, %r8d
testl %eax, %eax
je 0x12b57
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 0x12b47
testb $0x1, %r15b
jne 0x12b33
movq 0x18(%r13), %r15
movl %r8d, %r12d
cmpl %r9d, (%r15,%r12,4)
je 0x12b47
addl %ebx, %r8d
andl %edx, %r8d
incl %ebx
cmpl %r10d, %r8d
jne 0x12b03
movq 0x18(%rsp), %rbx
jmp 0x12b6c
shrl %cl, %r14d
testb $0x3, %r14b
cmovnel %eax, %r8d
movq 0x18(%rsp), %rbx
cmpl %eax, %r8d
je 0x12b6c
movq 0x20(%r13), %rcx
movl %r8d, %r8d
leaq (%r8,%r8,2), %r8
addl 0x4(%rcx,%r8,8), %esi
incq %rdi
cmpq %rbp, %rdi
jne 0x12adf
testl %esi, %esi
je 0x12d79
movslq %esi, %rdi
movl $0x10, %esi
callq 0x4360
movq %rax, %r15
cmpl $0x0, 0x8(%rsp)
jle 0x12d71
movl (%r13), %eax
leal -0x1(%rax), %edx
xorl %esi, %esi
xorl %r12d, %r12d
movl $0x0, %edi
testl %eax, %eax
je 0x12c1f
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 0x12c12
testb $0x1, %r14b
jne 0x12bfe
movq 0x18(%r13), %r14
movl %edi, %r13d
cmpl %r8d, (%r14,%r13,4)
movq 0x28(%rsp), %r13
je 0x12c12
addl %r11d, %edi
andl %edx, %edi
incl %r11d
cmpl %r9d, %edi
jne 0x12bcb
movq 0x18(%rsp), %rbx
jmp 0x12c6e
shrl %cl, %ebx
testb $0x3, %bl
cmovnel %eax, %edi
movq 0x18(%rsp), %rbx
cmpl %eax, %edi
je 0x12c6e
movq 0x20(%r13), %r8
movl %edi, %ecx
leaq (%rcx,%rcx,2), %rdi
movslq 0x4(%r8,%rdi,8), %rcx
testq %rcx, %rcx
jle 0x12c6e
leaq (%r8,%rdi,8), %rdi
movq 0x10(%rdi), %rdi
addq $0x8, %rdi
xorl %r8d, %r8d
cmpq %rbx, (%rdi)
jbe 0x12c62
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 0x12c46
incq %rsi
cmpq %rbp, %rsi
jne 0x12ba7
testl %r12d, %r12d
je 0x12d71
movslq %r12d, %rdi
movq %r15, %rsi
callq 0x109a4
movl $0x1, %eax
cmpl $0x2, %r12d
jl 0x12d60
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 0x12cd7
leaq (%rsi,%rdi), %r9
addq $-0x8, %r9
incl %edx
movups (%r9), %xmm0
movups %xmm0, 0x10(%r8,%r15)
addq $0x10, %rdi
cmpq %rdi, %rcx
jne 0x12cb0
testl %edx, %edx
jle 0x12d60
movl %edx, %ecx
movq %rcx, %rsi
shlq $0x4, %rsi
xorl %edi, %edi
movq 0x10(%r15,%rdi), %r8
cmpq %r8, 0x8(%r15,%rdi)
jb 0x12d00
movq %r8, 0x8(%r15,%rdi)
addq $0x10, %rdi
cmpq %rdi, %rsi
jne 0x12cef
testl %edx, %edx
jle 0x12d60
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 0x12d46
addq %r15, %rsi
addq $0x8, %rsi
movq 0x8(%r15,%rdx), %rdi
movq %rdi, (%rsi)
jmp 0x12d55
leaq (%r15,%rdx), %rdi
incl %eax
movups (%rdi), %xmm0
movups %xmm0, 0x10(%rsi,%r15)
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x12d1c
incl %eax
movq 0x10(%rsp), %rcx
movl %eax, 0x10(%rcx)
movq %rcx, %rax
movq %r15, 0x30(%rcx)
jmp 0x12d7e
movq %r15, %rdi
callq 0x4610
movq 0x10(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq