// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/hts.c
// Function: hts_itr_multi_cram
int hts_itr_multi_cram(const hts_idx_t *idx, hts_itr_t *iter)
{
    const hts_cram_idx_t *cidx = (const hts_cram_idx_t *) idx;
    int tid, i, n_off = 0;
    uint32_t j;
    hts_pos_t beg, end;
    hts_reglist_t *curr_reg;
    hts_pair32_t *curr_intv;
    hts_pair64_max_t *off = NULL, *tmp;
    cram_index *e = NULL;

    if (!cidx || !iter || !iter->multi)
        return -1;

    iter->is_cram = 1;
    iter->read_rest = 0;
    iter->off = NULL;
    iter->n_off = 0;
    iter->curr_off = 0;
    iter->i = -1;

    for (i=0; i<iter->n_reg; i++) {

        curr_reg = &iter->reg_list[i];
        tid = curr_reg->tid;

        if (tid >= 0) {
            tmp = realloc(off, (n_off + curr_reg->count) * sizeof(*off));
            if (!tmp)
                goto err;
            off = tmp;

            for (j=0; j < curr_reg->count; j++) {
                curr_intv = &curr_reg->intervals[j];
                if (curr_intv->end < curr_intv->beg)
                    continue;

                beg = curr_intv->beg;
                end = curr_intv->end;

/* First, fetch the container overlapping 'beg' and assign its file offset to u, then
 * find the container overlapping 'end' and assign the relative end of the slice to v.
 * The cram_ptell function will adjust with the container offset, which is not stored
 * in the index.
 */
                e = cram_index_query(cidx->cram, tid, beg+1, NULL);
                if (e) {
                    off[n_off].u = e->offset;
                    // hts_pair64_max_t::max is now used to link
                    // file offsets to region list entries.
                    // The iterator can use this to decide if it
                    // can skip some file regions.
                    off[n_off].max = ((uint64_t) tid << 32) | j;

                    if (end >= HTS_POS_MAX) {
                       e = cram_index_last(cidx->cram, tid, NULL);
                    } else {
                       e = cram_index_query_last(cidx->cram, tid, end+1);
                    }

                    if (e) {
                        off[n_off++].v = e->e_next
                            ? e->e_next->offset
                            : e->offset + e->slice + e->len;
                    } else {
                        hts_log_warning("Could not set offset end for region %d:%"PRIhts_pos"-%"PRIhts_pos". Skipping", tid, beg, end);
                    }
                }
            }
        } else {
            switch (tid) {
                case HTS_IDX_NOCOOR:
                    e = cram_index_query(cidx->cram, tid, 1, NULL);
                    if (e) {
                        iter->nocoor = 1;
                        iter->nocoor_off = e->offset;
                    } else {
                        hts_log_warning("No index entry for NOCOOR region");
                    }
                    break;
                case HTS_IDX_START:
                    e = cram_index_query(cidx->cram, tid, 1, NULL);
                    if (e) {
                        iter->read_rest = 1;
                        tmp = realloc(off, sizeof(*off));
                        if (!tmp)
                            goto err;
                        off = tmp;
                        off[0].u = e->offset;
                        off[0].v = 0;
                        n_off=1;
                    } else {
                        hts_log_warning("No index entries");
                    }
                    break;
                case HTS_IDX_REST:
                    break;
                case HTS_IDX_NONE:
                    iter->finished = 1;
                    break;
                default:
                    hts_log_error("Query with tid=%d not implemented for CRAM files", tid);
            }
        }
    }

    if (n_off) {
        ks_introsort(_off_max, n_off, off);
        iter->n_off = n_off; iter->off = off;
    }

    if(!n_off && !iter->nocoor)
        iter->finished = 1;

    return 0;

 err:
    free(off);
    return -1;
}

// Assembly:
testq %rdi, %rdi
sete %dl
testq %rsi, %rsi
sete %cl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
orb %dl, %cl
jne 0x19926
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movl (%rsi), %ecx
testb $0x10, %cl
je 0x19918
andl $-0x6, %ecx
orl $0x4, %ecx
movl %ecx, (%rbx)
xorl %eax, %eax
movq %rax, 0x60(%rbx)
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
movq %rcx, 0x8(%rbx)
movq %rax, 0x50(%rbx)
cmpl $0x0, 0x10(%rbx)
jle 0x198fc
movq %rdi, %rbp
xorl %r13d, %r13d
xorl %r12d, %r12d
movl $0x0, 0x4(%rsp)
movq %rbx, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x28(%rbx), %rax
leaq (,%r13,4), %rdx
addq %r13, %rdx
movl 0x10(%rax,%rdx,8), %ecx
testl %ecx, %ecx
js 0x197de
movq %rcx, 0x8(%rsp)
leaq (%rax,%rdx,8), %r14
movl 0x14(%r14), %eax
addl 0x4(%rsp), %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
movq %r12, %rdi
callq 0x4840
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x1990b
cmpl $0x0, 0x14(%r14)
je 0x19802
movq %r13, 0x28(%rsp)
movq 0x8(%rsp), %rsi
movq %rsi, %rax
shlq $0x20, %rax
movq %rax, 0x30(%rsp)
movl $0x8, %ebx
xorl %r13d, %r13d
movq 0x8(%r14), %rax
movq -0x8(%rax,%rbx), %r15
movq (%rax,%rbx), %r12
cmpq %r15, %r12
jl 0x197b6
movq 0x8(%rbp), %rdi
leaq 0x1(%r15), %rdx
xorl %ecx, %ecx
callq 0x49df6
testq %rax, %rax
je 0x19736
movq 0x28(%rax), %rax
movslq 0x4(%rsp), %rcx
movq %rbp, %rdx
leaq (%rcx,%rcx,2), %rbp
movq 0x10(%rsp), %rcx
movq %rax, (%rcx,%rbp,8)
movq 0x30(%rsp), %rax
addq %r13, %rax
movq %rax, 0x10(%rcx,%rbp,8)
movq 0x8(%rdx), %rdi
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
cmpq %rax, %r12
jl 0x1973d
movq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x49fe5
jmp 0x1974c
movq 0x8(%rsp), %rsi
jmp 0x197b6
leaq 0x1(%r12), %rdx
movq 0x8(%rsp), %rsi
callq 0x4a039
testq %rax, %rax
je 0x19765
movq 0x30(%rax), %rcx
testq %rcx, %rcx
movq 0x8(%rsp), %rsi
je 0x19791
movq 0x28(%rcx), %rax
jmp 0x197a0
movl $0x3, %edi
leaq 0x7ce53(%rip), %rsi     # 0x965c4
leaq 0x7ce5f(%rip), %rdx     # 0x965d7
movq 0x8(%rsp), %rbp
movl %ebp, %ecx
movq %r15, %r8
movq %r12, %r9
xorl %eax, %eax
callq 0x13de5
movq %rbp, %rsi
jmp 0x197b1
movslq 0x20(%rax), %rcx
addq 0x28(%rax), %rcx
movslq 0x24(%rax), %rax
addq %rcx, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rbp,8), %rcx
incl 0x4(%rsp)
movq %rax, 0x8(%rcx)
movq 0x18(%rsp), %rbp
incq %r13
movl 0x14(%r14), %eax
addq $0x10, %rbx
cmpq %rax, %r13
jb 0x196c5
movq 0x10(%rsp), %r12
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %r13
jmp 0x198d0
leal 0x5(%rcx), %eax
cmpl $0x3, %eax
ja 0x19885
leaq 0x7c0ff(%rip), %rcx     # 0x958f0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
orb $0x2, (%rbx)
jmp 0x198d0
movq 0x10(%rsp), %r12
jmp 0x198d0
movq 0x8(%rbp), %rdi
movl $0x1, %edx
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
xorl %ecx, %ecx
callq 0x49df6
testq %rax, %rax
je 0x198a1
movq %rax, %r15
orb $0x1, (%rbx)
movl $0x18, %esi
movq %r12, %rdi
callq 0x4840
testq %rax, %rax
je 0x1990b
movq 0x28(%r15), %rcx
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movl $0x1, 0x4(%rsp)
movq %rax, %r12
jmp 0x198d0
movq 0x8(%rbp), %rdi
movl $0x1, %edx
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
xorl %ecx, %ecx
callq 0x49df6
testq %rax, %rax
je 0x198b6
orb $0x8, (%rbx)
movq 0x28(%rax), %rax
movq %rax, 0x58(%rbx)
jmp 0x198d0
movl $0x1, %edi
leaq 0x7cd33(%rip), %rsi     # 0x965c4
leaq 0x7cdaa(%rip), %rdx     # 0x96642
xorl %eax, %eax
callq 0x13de5
jmp 0x198d0
movl $0x3, %edi
leaq 0x7cd17(%rip), %rsi     # 0x965c4
leaq 0x7cd7d(%rip), %rdx     # 0x96631
jmp 0x198c9
movl $0x3, %edi
leaq 0x7cd02(%rip), %rsi     # 0x965c4
leaq 0x7cd47(%rip), %rdx     # 0x96610
xorl %eax, %eax
callq 0x13de5
incq %r13
movslq 0x10(%rbx), %rax
cmpq %rax, %r13
jl 0x19652
movl 0x4(%rsp), %ebp
testl %ebp, %ebp
je 0x198fc
movslq %ebp, %rdi
movq %r12, %rsi
callq 0x18565
movl %ebp, 0x8(%rbx)
movq %r12, 0x60(%rbx)
jmp 0x19907
movl (%rbx), %eax
testb $0x8, %al
jne 0x19907
orl $0x2, %eax
movl %eax, (%rbx)
xorl %eax, %eax
jmp 0x19918
movq %r12, %rdi
callq 0x45c0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq