// File: /workspace/github_c_makefile_star2_O1/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 0x19e85
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 0x19e77
andl $-0x6, %ecx
orl $0x4, %ecx
movl %ecx, (%rbx)
xorl %r13d, %r13d
movq %r13, 0x60(%rbx)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x8(%rbx)
movq %r13, 0x50(%rbx)
cmpl $0x0, 0x10(%rbx)
movl $0x0, %r12d
jle 0x19e3e
movq %rdi, %rbp
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %rbx, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
movq 0x28(%rbx), %rax
leaq (%r14,%r14,4), %rcx
movl 0x10(%rax,%rcx,8), %r8d
testl %r8d, %r8d
js 0x19d2d
movq %r8, (%rsp)
movq %r14, 0x18(%rsp)
leaq (%rax,%rcx,8), %r14
movl 0x14(%r14), %eax
addl %r13d, %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
movq %r12, %rdi
callq 0x4840
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x19e6a
cmpl $0x0, 0x14(%r14)
je 0x19d4f
movq %r13, 0x10(%rsp)
movq (%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 0x19d08
movq 0x8(%rbp), %rdi
leaq 0x1(%r15), %rdx
xorl %ecx, %ecx
callq 0x4c618
testq %rax, %rax
je 0x19c84
movq 0x28(%rax), %rax
movslq 0x10(%rsp), %rcx
movq %rbp, %rdx
leaq (%rcx,%rcx,2), %rbp
movq 0x8(%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 0x19c8a
movq (%rsp), %rsi
xorl %edx, %edx
callq 0x4c7fb
jmp 0x19c98
movq (%rsp), %rsi
jmp 0x19d08
leaq 0x1(%r12), %rdx
movq (%rsp), %rsi
callq 0x4c84f
testq %rax, %rax
je 0x19cb0
movq 0x30(%rax), %rcx
testq %rcx, %rcx
movq (%rsp), %rsi
je 0x19cdb
movq 0x28(%rcx), %rax
jmp 0x19cea
movl $0x3, %edi
leaq 0x7f868(%rip), %rsi     # 0x99524
leaq 0x7f874(%rip), %rdx     # 0x99537
movq (%rsp), %rbp
movl %ebp, %ecx
movq %r15, %r8
movq %r12, %r9
xorl %eax, %eax
callq 0x14378
movq %rbp, %rsi
jmp 0x19d03
movslq 0x20(%rax), %rcx
addq 0x28(%rax), %rcx
movslq 0x24(%rax), %rax
addq %rcx, %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rbp,8), %rcx
movq 0x10(%rsp), %rdx
incl %edx
movq %rdx, 0x10(%rsp)
movq %rax, 0x8(%rcx)
movq 0x20(%rsp), %rbp
incq %r13
movl 0x14(%r14), %eax
addq $0x10, %rbx
cmpq %rax, %r13
jb 0x19c14
movq 0x8(%rsp), %r12
movq 0x28(%rsp), %rbx
movq 0x10(%rsp), %r13
jmp 0x19d54
leal 0x5(%r8), %eax
cmpl $0x3, %eax
ja 0x19de8
leaq 0x7eaff(%rip), %rcx     # 0x98840
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
orb $0x2, (%rbx)
jmp 0x19d59
movq 0x8(%rsp), %r12
movq 0x18(%rsp), %r14
incq %r14
movslq 0x10(%rbx), %rax
cmpq %rax, %r14
jl 0x19ba4
jmp 0x19e3e
movq 0x8(%rbp), %rdi
movl $0x1, %edx
movl %r8d, %esi
xorl %ecx, %ecx
callq 0x4c618
testq %rax, %rax
je 0x19e0a
movq %rax, %r15
orb $0x1, (%rbx)
movl $0x18, %esi
movq %r12, %rdi
callq 0x4840
testq %rax, %rax
je 0x19e6a
movq 0x28(%r15), %rcx
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movl $0x1, %r13d
movq %rax, %r12
jmp 0x19d59
movq 0x8(%rbp), %rdi
movl $0x1, %edx
movl %r8d, %esi
xorl %ecx, %ecx
callq 0x4c618
testq %rax, %rax
je 0x19e1f
orb $0x8, (%rbx)
movq 0x28(%rax), %rax
movq %rax, 0x58(%rbx)
jmp 0x19d59
movl $0x1, %edi
leaq 0x7f730(%rip), %rsi     # 0x99524
leaq 0x7f7a7(%rip), %rdx     # 0x995a2
movl %r8d, %ecx
xorl %eax, %eax
callq 0x14378
jmp 0x19d59
movl $0x3, %edi
leaq 0x7f70e(%rip), %rsi     # 0x99524
leaq 0x7f774(%rip), %rdx     # 0x99591
jmp 0x19e32
movl $0x3, %edi
leaq 0x7f6f9(%rip), %rsi     # 0x99524
leaq 0x7f73e(%rip), %rdx     # 0x99570
xorl %eax, %eax
callq 0x14378
jmp 0x19d59
testl %r13d, %r13d
je 0x19e5a
movslq %r13d, %rdi
movq %r12, %rsi
callq 0x18a93
movl %r13d, 0x8(%rbx)
movq %r12, 0x60(%rbx)
xorl %eax, %eax
jmp 0x19e77
movl (%rbx), %ecx
xorl %eax, %eax
testb $0x8, %cl
jne 0x19e77
orl $0x2, %ecx
movl %ecx, (%rbx)
jmp 0x19e77
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