// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x60(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x1800b
movl $0x0, -0x64(%rbp)
movq $-0x1, -0x70(%rbp)
movl -0x14(%rbp), %eax
addl $0x5, %eax
movl %eax, %ecx
movq %rcx, -0x90(%rbp)
subl $0x3, %eax
ja 0x17f8f
movq -0x90(%rbp), %rax
leaq 0x62d71(%rip), %rcx     # 0x7ab64
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x17e96
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %esi
addl $0x1, %esi
callq 0x17c30
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x17e4a
jmp 0x17e88
movq -0x70(%rbp), %rax
movq -0x50(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl -0x74(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq 0x10(%rcx), %rcx
cmpq (%rcx), %rax
jbe 0x17e86
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
jmp 0x17e88
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x17e03
cmpq $-0x1, -0x70(%rbp)
jne 0x17eb0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x17eb0
movq $0x0, -0x70(%rbp)
jmp 0x17f9c
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x17f58
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %esi
addl $0x1, %esi
callq 0x17c30
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
je 0x17f48
cmpq $-0x1, -0x70(%rbp)
je 0x17f28
movq -0x70(%rbp), %rax
movq -0x50(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl -0x74(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq 0x10(%rcx), %rcx
cmpq 0x8(%rcx), %rax
jae 0x17f46
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x70(%rbp)
jmp 0x17f48
jmp 0x17f4a
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x17ebc
cmpq $-0x1, -0x70(%rbp)
jne 0x17f72
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x17f72
movq $0x0, -0x70(%rbp)
jmp 0x17f9c
movq $0x0, -0x70(%rbp)
jmp 0x17f9c
movl $0x1, -0x64(%rbp)
movq $0x0, -0x70(%rbp)
jmp 0x17f9c
movq $0x0, -0x8(%rbp)
jmp 0x18523
cmpq $-0x1, -0x70(%rbp)
je 0x17ffe
movl $0x1, %edi
movl $0x50, %esi
callq 0x4370
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movl (%rax), %ecx
andl $-0x2, %ecx
orl $0x1, %ecx
movl %ecx, (%rax)
movl -0x64(%rbp), %edx
movq -0x60(%rbp), %rax
movl (%rax), %ecx
andl $0x1, %edx
shll %edx
andl $-0x3, %ecx
orl %edx, %ecx
movl %ecx, (%rax)
movq -0x70(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x60(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x18523
movq $0x0, -0x8(%rbp)
jmp 0x18523
cmpl $0x0, -0x18(%rbp)
jge 0x18018
movl $0x0, -0x18(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x1802d
movq $0x0, -0x8(%rbp)
jmp 0x18523
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x18053
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
jne 0x18060
movq $0x0, -0x8(%rbp)
jmp 0x18523
movl $0x1, %edi
movl $0x50, %esi
callq 0x4370
movq %rax, -0x60(%rbp)
movl -0x14(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x18(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x1c(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x60(%rbp), %rax
movl $0xffffffff, 0x14(%rax) # imm = 0xFFFFFFFF
movq -0x28(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
shll %ecx
movq -0x10(%rbp), %rax
addl 0x8(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl $0x7, %ecx
cltd
idivl %ecx
movl -0x18(%rbp), %edx
movq -0x10(%rbp), %rcx
movl 0x4(%rcx), %ecx
sarl %cl, %edx
movl %edx, %ecx
addl %ecx, %eax
movl %eax, -0x38(%rbp)
movq -0x50(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x17c30
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
je 0x180f9
jmp 0x18132
movl -0x38(%rbp), %eax
subl $0x1, %eax
sarl $0x3, %eax
shll $0x3, %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x78(%rbp), %eax
jle 0x1811e
movl -0x38(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1812a
movl -0x38(%rbp), %eax
subl $0x1, %eax
sarl $0x3, %eax
movl %eax, -0x38(%rbp)
jmp 0x1812c
cmpl $0x0, -0x38(%rbp)
jne 0x180dd
cmpl $0x0, -0x38(%rbp)
jne 0x18147
movq -0x50(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x17c30
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
je 0x18171
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x98(%rbp)
jmp 0x1817c
xorl %eax, %eax
movq %rax, -0x98(%rbp)
jmp 0x1817c
movq -0x98(%rbp), %rax
movq %rax, -0x58(%rbp)
movslq -0x18(%rbp), %rdi
movslq -0x1c(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x10(%rbp), %rax
movl 0x8(%rax), %r8d
callq 0x18530
movl $0x0, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpl 0x40(%rcx), %eax
jge 0x1820c
movq -0x50(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x48(%rax), %rax
movslq -0x2c(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x17c30
movl %eax, -0x44(%rbp)
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
je 0x181ff
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x18201
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x181b5
cmpl $0x0, -0x30(%rbp)
jne 0x1821f
movq -0x60(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x18523
movslq -0x30(%rbp), %rdi
movl $0x10, %esi
callq 0x4370
movq %rax, -0x40(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpl 0x40(%rcx), %eax
jge 0x18321
movq -0x50(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x48(%rax), %rax
movslq -0x2c(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x17c30
movl %eax, -0x44(%rbp)
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
je 0x18311
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movq -0x88(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x1830f
movq -0x88(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x7c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
cmpq -0x58(%rbp), %rax
jbe 0x18302
movq -0x40(%rbp), %rax
movl -0x30(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x30(%rbp)
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x88(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x7c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
jmp 0x18304
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x18296
jmp 0x18311
jmp 0x18313
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1823f
cmpl $0x0, -0x30(%rbp)
jne 0x1833d
movq -0x40(%rbp), %rdi
callq 0x46a0
movq -0x60(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x18523
movslq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x156c0
movl $0x1, -0x2c(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x183c9
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
jae 0x183bc
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x34(%rbp)
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x40(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
jmp 0x183be
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x18358
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x18444
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jb 0x18437
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x18439
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x183d9
movl $0x1, -0x2c(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x184fc
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shrq $0x10, %rax
movq -0x40(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
shrq $0x10, %rcx
cmpq %rcx, %rax
jne 0x184b8
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x184ec
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x34(%rbp)
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x40(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
jmp 0x184ee
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x18452
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x60(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq