// File: karel-brinda[P]ococo/ext/htslib/sam.c
// Function: cram_itr_query
static hts_itr_t *cram_itr_query(const hts_idx_t *idx, int tid, int beg, int end, hts_readrec_func *readrec)
{
    const hts_cram_idx_t *cidx = (const hts_cram_idx_t *) idx;
    hts_itr_t *iter = (hts_itr_t *) calloc(1, sizeof(hts_itr_t));
    if (iter == NULL) return NULL;

    // Cons up a dummy iterator for which hts_itr_next() will simply invoke
    // the readrec function:
    iter->read_rest = 1;
    iter->off = NULL;
    iter->bins.a = NULL;
    iter->readrec = readrec;

    if (tid >= 0 || tid == HTS_IDX_NOCOOR) {
        cram_range r = { tid == HTS_IDX_NOCOOR ? -1 : tid, beg+1, end };
        int ret = cram_set_option(cidx->cram, CRAM_OPT_RANGE, &r);

        iter->curr_off = 0;
        // The following fields are not required by hts_itr_next(), but are
        // filled in in case user code wants to look at them.
        iter->tid = tid;
        iter->beg = beg;
        iter->end = end;

        switch (ret) {
        case 0:
            break;

        case -2:
            // No data vs this ref, so mark iterator as completed.
            // Same as HTS_IDX_NONE.
            iter->finished = 1;
            break;

        default:
            free(iter);
            return NULL;
        }
    }
    else switch (tid) {
    case HTS_IDX_REST:
        iter->curr_off = 0;
        break;
    case HTS_IDX_NONE:
        iter->curr_off = 0;
        iter->finished = 1;
        break;
    default:
        fprintf(stderr, "[cram_itr_query] tid=%d not implemented for CRAM files -- Exiting\n", tid);
        abort();
        break;
    }

    return iter;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x1, %edi
movl $0x50, %esi
callq 0x4370
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x1c8d8
movq $0x0, -0x8(%rbp)
jmp 0x1ca3c
movq -0x38(%rbp), %rax
movl (%rax), %ecx
andl $-0x2, %ecx
orl $0x1, %ecx
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x48(%rax)
movq -0x28(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
cmpl $0x0, -0x14(%rbp)
jge 0x1c91a
cmpl $-0x2, -0x14(%rbp)
jne 0x1c9d1
leaq -0x44(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpl $-0x2, -0x14(%rbp)
jne 0x1c932
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x54(%rbp)
jmp 0x1c938
movl -0x14(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x50(%rbp), %rax
movl -0x54(%rbp), %ecx
movl %ecx, (%rax)
movl -0x18(%rbp), %eax
incl %eax
movl %eax, -0x40(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
xorl %eax, %eax
movl $0x5, %esi
leaq -0x44(%rbp), %rdx
callq 0x52a20
movl %eax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq $0x0, 0x28(%rax)
movl -0x14(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x18(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x1c(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x48(%rbp), %eax
movl %eax, -0x58(%rbp)
subl $-0x2, %eax
je 0x1c9ac
jmp 0x1c9a1
movl -0x58(%rbp), %eax
testl %eax, %eax
jne 0x1c9bc
jmp 0x1c9aa
jmp 0x1c9cf
movq -0x38(%rbp), %rax
movl (%rax), %ecx
andl $-0x3, %ecx
orl $0x2, %ecx
movl %ecx, (%rax)
jmp 0x1c9cf
movq -0x38(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x1ca3c
jmp 0x1ca34
movl -0x14(%rbp), %eax
movl %eax, -0x5c(%rbp)
subl $-0x5, %eax
je 0x1c9f6
jmp 0x1c9de
movl -0x5c(%rbp), %eax
subl $-0x4, %eax
jne 0x1ca12
jmp 0x1c9e8
movq -0x38(%rbp), %rax
movq $0x0, 0x28(%rax)
jmp 0x1ca32
movq -0x38(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x38(%rbp), %rax
movl (%rax), %ecx
andl $-0x3, %ecx
orl $0x2, %ecx
movl %ecx, (%rax)
jmp 0x1ca32
movq 0x6d5b7(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x5ef37(%rip), %rsi     # 0x7b95d
movb $0x0, %al
callq 0x4830
callq 0x42e0
jmp 0x1ca34
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)