// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/hts.c
// Function: hts_itr_multi_bam
int hts_itr_multi_bam(const hts_idx_t *idx, hts_itr_t *iter)
{
    int i, j, bin;
    khint_t k;
    bidx_t *bidx;
    uint64_t min_off, max_off, t_off = (uint64_t)-1;
    int tid;
    hts_pos_t beg, end, idx_maxpos;
    hts_reglist_t *curr_reg;
    uint32_t unmapped = 0, rel_off;

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

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

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

        if (tid < 0) {
            t_off = hts_itr_off(idx, tid);
            if (t_off != (uint64_t)-1) {
                switch (tid) {
                case HTS_IDX_NONE:
                    iter->finished = 1;
                    // fall through
                case HTS_IDX_START:
                case HTS_IDX_REST:
                    iter->curr_off = t_off;
                    iter->n_reg = 0;
                    iter->reg_list = NULL;
                    iter->read_rest = 1;
                    return 0;
                case HTS_IDX_NOCOOR:
                    iter->nocoor = 1;
                    iter->nocoor_off = t_off;
                }
            }
        } else {
            if (tid >= idx->n || (bidx = idx->bidx[tid]) == NULL || !kh_size(bidx))
                continue;

            k = kh_get(bin, bidx, META_BIN(idx));
            if (k != kh_end(bidx))
                unmapped = kh_val(bidx, k).list[1].v;
            else
                unmapped = 1;

            idx_maxpos = hts_idx_maxpos(idx);

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

                beg = curr_intv->beg;
                end = curr_intv->end;
                if (beg >= idx_maxpos)
                    continue;
                rel_off = beg>>idx->min_shift;

                /* Compute 'min_off' by searching the lowest level bin containing 'beg'.
                       If the computed bin is not in the index, try the next bin to the
                       left, belonging to the same parent. If it is the first sibling bin,
                       try the parent bin. */
                bin = hts_bin_first(idx->n_lvls) + rel_off;
                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;
                // min_off can be calculated more accurately if the
                // linear index is available
                if (idx->lidx[tid].offset
                    && rel_off < idx->lidx[tid].n) {
                    if (min_off < idx->lidx[tid].offset[rel_off])
                        min_off = idx->lidx[tid].offset[rel_off];
                    if (unmapped) {
                        int tmp_off;
                        for (tmp_off = rel_off-1; tmp_off >= 0; tmp_off--) {
                            if (idx->lidx[tid].offset[tmp_off] < min_off) {
                                min_off = idx->lidx[tid].offset[tmp_off];
                                break;
                            }
                        }

                        if (k != kh_end(bidx) && (min_off < kh_val(bidx, k).loff || tmp_off < 0))
                            min_off = kh_val(bidx, k).loff;
                    }
                } else if (unmapped) { //CSI index
                    if (k != kh_end(bidx))
                        min_off = kh_val(bidx, k).loff;
                }

                // compute max_off: a virtual offset from a bin to the right of end
                // First check if end lies within the range of the index (it
                // won't if it's HTS_POS_MAX)
                if (end <= idx_maxpos) {
                    bin = hts_bin_first(idx->n_lvls) + ((end-1) >> idx->min_shift) + 1;
                    if (bin >= idx->n_bins) bin = 0;
                    while (1) {
                        // search for an extant bin by moving right, but moving up to the
                        // parent whenever we get to a first child (which also covers falling
                        // off the RHS, which wraps around and immediately goes up to bin 0)
                        while (bin % 8 == 1) bin = hts_bin_parent(bin);
                        if (bin == 0) { max_off = UINT64_MAX; break; }
                        k = kh_get(bin, bidx, bin);
                        if (k != kh_end(bidx) && kh_val(bidx, k).n > 0) {
                            max_off = kh_val(bidx, k).list[0].u;
                            break;
                        }
                        bin++;
                    }
                } else {
                    // Searching to end of reference
                    max_off = UINT64_MAX;
                }

                //convert coordinates to file offsets
                if (reg2intervals(iter, idx, tid, beg, end, j,
                                  min_off, max_off,
                                  idx->min_shift, idx->n_lvls) < 0) {
                    return -1;
                }
            }
        }
    }

    if (iter->n_off > 1)
        ks_introsort(_off_max, iter->n_off, iter->off);

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

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
pushq $-0x1
popq %r12
orb %al, %cl
jne 0x15879
movq %rsi, %r15
testb $0x10, (%rsi)
je 0x15879
movq %rdi, %r14
orl $-0x1, 0xc(%r15)
xorl %r13d, %r13d
movslq 0x10(%r15), %rax
cmpq %rax, %r13
jge 0x1588e
movq 0x28(%r15), %rcx
imulq $0x28, %r13, %rbx
movslq 0x10(%rcx,%rbx), %rsi
testq %rsi, %rsi
js 0x15314
cmpl 0x14(%r14), %esi
jge 0x1534e
movq 0x28(%r14), %rax
movq (%rax,%rsi,8), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x1534e
movq 0x8(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x1534e
movq %rcx, 0x78(%rsp)
movq %rsi, 0x38(%rsp)
movl 0xc(%r14), %esi
incl %esi
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x14465
cmpl (%rbp), %eax
jne 0x15356
movl $0x0, 0x14(%rsp)
jmp 0x15370
movq %r14, %rdi
movq %rsi, %rbx
callq 0x144e3
cmpq $-0x1, %rax
je 0x1534e
movq %rbx, %rcx
cmpl $-0x2, %ecx
je 0x15346
movq %rcx, %rdx
addl $0x4, %ecx
cmpl $0x2, %ecx
jb 0x158b9
cmpl $-0x5, %edx
jne 0x1534e
jmp 0x158be
orb $0x8, (%r15)
movq %rax, 0x58(%r15)
incq %r13
jmp 0x152a2
movq 0x20(%rbp), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
movq 0x10(%rcx,%rax), %rax
cmpl $0x0, 0x18(%rax)
sete %al
movl %eax, 0x14(%rsp)
addq %rbx, 0x78(%rsp)
imull $0x3, 0x8(%r14), %ecx
addl 0x4(%r14), %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
movq %rax, 0x70(%rsp)
xorl %edx, %edx
movq %r15, 0x50(%rsp)
movq %r14, 0x40(%rsp)
movq %r13, 0x80(%rsp)
movq 0x78(%rsp), %rcx
movl 0x14(%rcx), %eax
cmpq %rax, %rdx
jae 0x1534e
movq 0x8(%rcx), %rax
movq %rdx, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rbx
movq 0x8(%rax,%rcx), %rax
movq %rax, 0x20(%rsp)
cmpq %rbx, %rax
jl 0x1586d
cmpq 0x70(%rsp), %rbx
jge 0x1586d
movq %rdx, 0x58(%rsp)
movl 0x4(%r14), %ecx
movq %rbx, 0x28(%rsp)
movq %rcx, 0x68(%rsp)
sarq %cl, %rbx
movl 0x8(%r14), %eax
movq %rax, 0x48(%rsp)
leal (%rax,%rax,2), %ecx
movl %r12d, %eax
movl %ecx, 0x18(%rsp)
shll %cl, %eax
notl %eax
xorl %edx, %edx
pushq $0x7
popq %rcx
divl %ecx
movl %eax, %ebp
leal (%rbx,%rbp), %r14d
movq 0x8(%rsp), %r13
movq %r13, %rdi
movl %r14d, %esi
callq 0x14465
movl (%r13), %r13d
cmpl %r13d, %eax
jne 0x1544c
leal -0x1(%r14), %ecx
movl %ecx, %edx
andl $-0x8, %edx
incl %edx
movl %ecx, %esi
sarl $0x3, %esi
cmpl %edx, %r14d
cmovgl %ecx, %esi
movl %esi, %r14d
movl %eax, %r13d
testl %esi, %esi
jne 0x15411
jmp 0x15451
testl %r14d, %r14d
jne 0x1545d
movq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x14465
movl %eax, %ecx
cmpl %r13d, %eax
jne 0x1546e
movq $0x0, (%rsp)
jmp 0x15484
movq 0x8(%rsp), %rdx
movq 0x20(%rdx), %rdx
imulq $0x18, %rcx, %rsi
movq 0x8(%rdx,%rsi), %rdx
movq %rdx, (%rsp)
movq 0x40(%rsp), %r14
movq 0x30(%r14), %rdi
imulq $0x18, 0x38(%rsp), %rdx
movq 0x10(%rdi,%rdx), %rsi
testq %rsi, %rsi
je 0x154c8
addq %rdx, %rdi
movl %ebx, %edx
cmpq (%rdi), %rdx
jge 0x154c8
movq (%rsi,%rdx,8), %rdi
movq (%rsp), %r8
cmpq %rdi, %r8
cmovbeq %rdi, %r8
movq %r8, (%rsp)
cmpb $0x0, 0x14(%rsp)
je 0x154ec
movq 0x8(%rsp), %rbx
jmp 0x15533
cmpl %r13d, %eax
sete %al
orb 0x14(%rsp), %al
movq 0x8(%rsp), %rbx
jne 0x15533
movq 0x20(%rbx), %rax
imulq $0x18, %rcx, %rcx
movq 0x8(%rax,%rcx), %rax
movq %rax, (%rsp)
jmp 0x15533
movq 0x8(%rsp), %rbx
decq %rdx
testl %edx, %edx
js 0x1550c
movl %edx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq (%rsi,%rdi,8), %rdi
cmpq (%rsp), %rdi
jae 0x154f1
jmp 0x15510
movq (%rsp), %rdi
cmpl %r13d, %eax
je 0x1552f
movq 0x20(%rbx), %rax
imulq $0x18, %rcx, %rcx
movq 0x8(%rax,%rcx), %rax
cmpq %rax, %rdi
cmovbeq %rax, %rdi
testl %edx, %edx
cmovsq %rax, %rdi
movq %rdi, (%rsp)
pushq $-0x1
popq %rax
movq %rax, 0x30(%rsp)
movq 0x20(%rsp), %rax
cmpq 0x70(%rsp), %rax
jg 0x155b6
movq 0x20(%rsp), %rax
decq %rax
movq 0x68(%rsp), %rcx
sarq %cl, %rax
addl %eax, %ebp
incl %ebp
cmpl 0xc(%r14), %ebp
movl $0x0, %eax
cmovgel %eax, %ebp
movl %ebp, %eax
andl $0x80000007, %eax       # imm = 0x80000007
cmpl $0x1, %eax
jne 0x1557a
decl %ebp
shrl $0x3, %ebp
jmp 0x15567
testl %ebp, %ebp
je 0x155a2
movq %rbx, %rdi
movl %ebp, %esi
callq 0x14465
cmpl %r13d, %eax
je 0x1559e
movq 0x20(%rbx), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
cmpl $0x0, 0x4(%rcx,%rax)
jg 0x155a7
incl %ebp
jmp 0x15567
pushq $-0x1
popq %rax
jmp 0x155b1
addq %rax, %rcx
movq 0x10(%rcx), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x28(%r14), %rax
movq 0x38(%rsp), %rcx
movq (%rax,%rcx,8), %r13
testq %r13, %r13
je 0x15879
movq 0x48(%rsp), %r9
movq 0x68(%rsp), %r10
leal (%r9,%r10), %eax
leal (%rax,%r9,2), %ecx
pushq $0x1
popq %r14
shlq %cl, %r14
movq 0x20(%rsp), %rax
cmpq %rax, %r14
cmovgeq %rax, %r14
movl (%r13), %r11d
decq %r14
xorl %edx, %edx
xorl %eax, %eax
movq 0x28(%rsp), %r8
cmpl %r9d, %edx
jg 0x15622
movq %r14, %rsi
sarq %cl, %rsi
movq %r8, %rdi
sarq %cl, %rdi
subq %rdi, %rsi
addq %rsi, %rax
incq %rax
addl $-0x3, %ecx
incl %edx
jmp 0x15601
movl 0x8(%r15), %ecx
movl %ecx, 0x64(%rsp)
movl 0x18(%rsp), %ecx
addl %r10d, %ecx
cmpq %r11, %rax
jae 0x156cb
xorl %ebx, %ebx
xorl %r15d, %r15d
cmpl %r9d, %r15d
jg 0x157ad
movq 0x28(%rsp), %rbp
sarq %cl, %rbp
movslq %ebx, %rax
addq %rax, %rbp
movq %r14, %r12
movl %ecx, 0x18(%rsp)
sarq %cl, %r12
addq %rax, %r12
cmpq %r12, %rbp
jg 0x156ac
movq %r13, %rdi
movl %ebp, %esi
callq 0x14465
cmpl (%r13), %eax
je 0x156a7
movl %eax, %eax
imulq $0x18, %rax, %rsi
addq 0x20(%r13), %rsi
movq 0x50(%rsp), %rdi
movq 0x38(%rsp), %rdx
movq 0x58(%rsp), %rcx
movq (%rsp), %r8
movq 0x30(%rsp), %r9
callq 0x18918
testl %eax, %eax
js 0x15875
incq %rbp
jmp 0x15663
leal (%r15,%r15,2), %ecx
pushq $0x1
popq %rax
shll %cl, %eax
movl 0x18(%rsp), %ecx
addl $-0x3, %ecx
addl %eax, %ebx
incl %r15d
movq 0x48(%rsp), %r9
jmp 0x1563f
movslq %ecx, %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
movq 0x28(%rsp), %rax
testq %rax, %rax
cmovleq %rbx, %rax
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
cmpl %r11d, %r12d
je 0x157ad
movq 0x10(%r13), %rax
movl %r12d, %ecx
shrl $0x4, %ecx
movl (%rax,%rcx,4), %eax
movl %ebx, %ecx
andb $0x1e, %cl
shrl %cl, %eax
testb $0x3, %al
jne 0x157a2
movq %r11, %r15
movq 0x18(%r13), %rax
movl %r12d, %ecx
movq %rcx, 0x20(%rsp)
movl (%rax,%rcx,4), %ebp
movl %ebp, %edi
callq 0x184dd
cmpl 0x48(%rsp), %eax
jle 0x15732
movq %r15, %r11
jmp 0x157a2
leal (%rax,%rax,2), %ecx
pushq $-0x1
popq %rax
shll %cl, %eax
movslq %ecx, %rdx
movq 0x18(%rsp), %rcx
subq %rdx, %rcx
movq 0x28(%rsp), %rsi
shrq %cl, %rsi
notl %eax
xorl %edx, %edx
pushq $0x7
popq %rdi
divl %edi
addq %rax, %rsi
cmpq %rbp, %rsi
movq %r15, %r11
ja 0x157a2
movq %r14, %rdx
sarq %cl, %rdx
addq %rax, %rdx
cmpq %rbp, %rdx
jl 0x157a2
imulq $0x18, 0x20(%rsp), %rsi
addq 0x20(%r13), %rsi
movq 0x50(%rsp), %rdi
movq 0x38(%rsp), %rdx
movq 0x58(%rsp), %rcx
movq (%rsp), %r8
movq 0x30(%rsp), %r9
callq 0x18918
testl %eax, %eax
js 0x15875
movl (%r13), %r11d
incl %r12d
addl $0x2, %ebx
jmp 0x156e9
movq 0x50(%rsp), %r15
movl 0x8(%r15), %eax
movl %eax, %ecx
movl 0x64(%rsp), %ebp
subl %ebp, %ecx
cmpl $0x2, %ecx
pushq $-0x1
popq %r12
movq 0x40(%rsp), %r14
movq 0x80(%rsp), %r13
jl 0x15864
movslq %ebp, %rbx
movl %ecx, %edi
imulq $0x18, %rbx, %rsi
addq 0x60(%r15), %rsi
callq 0x14de0
movslq 0x8(%r15), %rdx
movslq %ebp, %rcx
imulq $0x18, %rbx, %rax
addq $0x18, %rax
movq %rbx, %r8
leaq 0x1(%r8), %rdi
cmpq %rdx, %rdi
jge 0x1585c
movq 0x60(%r15), %rsi
imulq $0x18, %rcx, %r9
movq 0x8(%rsi,%r9), %r10
cmpq (%rsi,%rax), %r10
jb 0x15835
movq 0x8(%rsi,%rax), %r8
cmpq %r8, %r10
jae 0x1582c
addq %r9, %rsi
addq $0x8, %rsi
movq %r8, (%rsi)
addq $0x18, %rax
movq %rdi, %r8
jmp 0x157fc
incl %ebp
leaq 0x1(%r8), %rbx
cmpq %rcx, %r8
jle 0x157ea
addq %rsi, %rax
movslq %ebp, %rcx
imulq $0x18, %rcx, %rcx
movq 0x10(%rax), %rdx
movq %rdx, 0x10(%rsi,%rcx)
movups (%rax), %xmm0
movups %xmm0, (%rsi,%rcx)
jmp 0x157ea
incl %ebp
movl %ebp, 0x8(%r15)
movl %ebp, %eax
testl %eax, %eax
movq 0x58(%rsp), %rdx
js 0x15879
incq %rdx
jmp 0x1539d
pushq $-0x1
popq %r12
movl %r12d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq 0x8(%r15), %rdi
cmpq $0x2, %rdi
jl 0x158a5
movq 0x60(%r15), %rsi
callq 0x14de0
movl 0x8(%r15), %edi
xorl %r12d, %r12d
testl %edi, %edi
jne 0x15879
movl (%r15), %ecx
testb $0x8, %cl
jne 0x15879
orl $0x2, %ecx
jmp 0x158d5
movl (%r15), %ecx
jmp 0x158c4
movl (%r15), %ecx
orl $0x2, %ecx
movq %rax, 0x50(%r15)
andl $0x0, 0x10(%r15)
andq $0x0, 0x28(%r15)
orl $0x1, %ecx
movl %ecx, (%r15)
xorl %r12d, %r12d
jmp 0x15879