// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/cram/cram_index.c
// Function: cram_index_query
cram_index *cram_index_query(cram_fd *fd, int refid, hts_pos_t pos,
                             cram_index *from) {
    int i, j, k;
    cram_index *e;

    if (from) {
        // Continue from a previous search.
        // We switch to just scanning the linked list, as the nested
        // lists are typically short.
        if (refid == HTS_IDX_NOCOOR)
            refid = -1;

        e = from->e_next;
        if (e && e->refid == refid && e->start <= pos)
            return e;
        else
            return NULL;
    }

    switch(refid) {
    case HTS_IDX_NONE:
    case HTS_IDX_REST:
        // fail, or already there, dealt with elsewhere.
        return NULL;

    case -1:
    case HTS_IDX_NOCOOR:
        refid = -1;
        pos = 0;
        break;

    case HTS_IDX_START: {
        int64_t min_idx = INT64_MAX;
        for (i = 0, j = -1; i < fd->index_sz; i++) {
            if (fd->index[i].e && fd->index[i].e[0].offset < min_idx) {
                min_idx = fd->index[i].e[0].offset;
                j = i;
            }
        }
        if (j < 0)
            return NULL;
        return fd->index[j].e;
    }

    default:
        if (refid < HTS_IDX_NONE || refid+1 >= fd->index_sz)
            return NULL;
    }

    from = &fd->index[refid+1];

    // Ref with nothing aligned against it.
    if (!from->e)
        return NULL;

    // This sequence is covered by the index, so binary search to find
    // the optimal starting block.
    i = 0, j = fd->index[refid+1].nslice-1;
    for (k = j/2; k != i; k = (j-i)/2 + i) {
        if (from->e[k].refid > refid) {
            j = k;
            continue;
        }

        if (from->e[k].refid < refid) {
            i = k;
            continue;
        }

        if (from->e[k].start >= pos) {
            j = k;
            continue;
        }

        if (from->e[k].start < pos) {
            i = k;
            continue;
        }
    }
    // i==j or i==j-1. Check if j is better.
    if (j >= 0 && from->e[j].start < pos && from->e[j].refid == refid)
        i = j;

    /* The above found *a* bin overlapping, but not necessarily the first */
    while (i > 0 && from->e[i-1].end >= pos)
        i--;

    /* We may be one bin before the optimum, so check */
    while (i+1 < from->nslice &&
           (from->e[i].refid < refid ||
            from->e[i].end < pos))
        i++;

    e = &from->e[i];

    return e;
}

// Assembly:
pushq %rbx
movq %rdx, %r8
testq %rcx, %rcx
je 0x3d63c
movq 0x30(%rcx), %rcx
testq %rcx, %rcx
je 0x3d7a1
cmpl $-0x2, %esi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovnel %esi, %eax
cmpl %eax, 0x10(%rcx)
jne 0x3d7a1
movslq 0x14(%rcx), %rax
cmpq %r8, %rax
jg 0x3d7a1
jmp 0x3d7a3
xorl %ecx, %ecx
leal 0x5(%rsi), %eax
cmpl $0x2, %eax
jb 0x3d7a3
leal 0x2(%rsi), %eax
cmpl $0x2, %eax
jae 0x3d6cf
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movq 0x8878(%rdi), %rax
movslq %esi, %rcx
imulq $0x38, %rcx, %rdx
movq 0x40(%rax,%rdx), %rcx
xorl %r10d, %r10d
testq %rcx, %rcx
je 0x3d7a1
addq %rdx, %rax
movslq 0x38(%rax), %r9
leal -0x1(%r9), %edi
movl $0x2, %r11d
movl %edi, %eax
cltd
idivl %r11d
cmpl %r10d, %eax
je 0x3d72e
movslq %eax, %rdx
imulq $0x38, %rdx, %rbx
leaq (%rcx,%rbx), %rdx
cmpl %esi, 0x10(%rcx,%rbx)
jg 0x3d6c1
jge 0x3d6b3
movl %eax, %r10d
movl %edi, %eax
jmp 0x3d6c1
movslq 0x14(%rdx), %rdx
cmpq %rdx, %r8
cmovgl %eax, %r10d
cmovgl %edi, %eax
movl %eax, %edi
subl %r10d, %eax
cltd
idivl %r11d
addl %r10d, %eax
jmp 0x3d690
cmpl $-0x3, %esi
jne 0x3d773
movl 0x8870(%rdi), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movabsq $0x8, %r8
cmpq %rcx, %rax
je 0x3d789
movq 0x8878(%rdi), %r9
movq (%r9,%r8), %r9
testq %r9, %r9
je 0x3d725
movq 0x28(%r9), %r9
cmpq %rsi, %r9
cmovll %ecx, %edx
cmovlq %r9, %rsi
incq %rcx
addq $0x38, %r8
jmp 0x3d6fe
testl %edi, %edi
js 0x3d74c
movl %edi, %eax
imulq $0x38, %rax, %rax
movslq 0x14(%rcx,%rax), %rdx
cmpq %rdx, %r8
jle 0x3d74c
addq %rcx, %rax
cmpl %esi, 0x10(%rax)
cmovel %edi, %r10d
leaq -0x20(%rcx), %rdx
movl %r10d, %eax
sarl $0x1f, %eax
andl %r10d, %eax
testl %r10d, %r10d
jle 0x3d7ab
movl %r10d, %edi
imulq $0x38, %rdi, %rdi
movslq (%rdx,%rdi), %rdi
cmpq %rdi, %r8
jg 0x3d7a8
decl %r10d
jmp 0x3d759
cmpl $-0x5, %esi
jl 0x3d7a1
leal 0x1(%rsi), %eax
cmpl 0x8870(%rdi), %eax
jl 0x3d65a
jmp 0x3d7a1
testl %edx, %edx
js 0x3d7a1
movq 0x8878(%rdi), %rax
movl %edx, %ecx
imulq $0x38, %rcx, %rcx
movq 0x8(%rax,%rcx), %rcx
jmp 0x3d7a3
xorl %ecx, %ecx
movq %rcx, %rax
popq %rbx
retq
movl %r10d, %eax
movslq %eax, %rdx
movq %rdx, %rax
shlq $0x20, %rax
imulq $0x38, %rdx, %rdi
addq %rcx, %rdi
addq $0x18, %rdi
incq %rdx
movabsq $0x100000000, %r10      # imm = 0x100000000
cmpq %r9, %rdx
jge 0x3d7eb
cmpl %esi, -0x8(%rdi)
jl 0x3d7df
movslq (%rdi), %r11
cmpq %r11, %r8
jle 0x3d7eb
addq %r10, %rax
addq $0x38, %rdi
incq %rdx
jmp 0x3d7cd
sarq $0x20, %rax
imulq $0x38, %rax, %rax
addq %rax, %rcx
jmp 0x3d7a3