// File: /workspace/github_c_makefile_star2_O3/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
testq %rcx, %rcx
je 0x49e2f
movq 0x30(%rcx), %rax
testq %rax, %rax
je 0x49fe1
cmpl $-0x2, %esi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmovnel %esi, %ecx
cmpl %ecx, 0x10(%rax)
jne 0x49fe1
movslq 0x14(%rax), %rcx
cmpq %rdx, %rcx
jg 0x49fe1
jmp 0x49fe3
xorl %ecx, %ecx
leal 0x5(%rsi), %eax
cmpl $0x2, %eax
jb 0x49fe1
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
leal 0x2(%rsi), %eax
cmpl $0x2, %eax
jae 0x49f5e
movq 0x8878(%rdi), %rsi
movslq %r8d, %rax
imulq $0x38, %rax, %rdi
movq 0x40(%rsi,%rdi), %rax
xorl %edx, %edx
testq %rax, %rax
je 0x49fe1
addq %rdi, %rsi
movl 0x38(%rsi), %esi
leal -0x1(%rsi), %edi
movl %edi, %r9d
cmpl $0x3, %esi
jb 0x49ed5
movl %edi, %r10d
shrl $0x1f, %r10d
addl %edi, %r10d
sarl %r10d
xorl %edx, %edx
movl %edi, %r9d
movslq %r10d, %r11
imulq $0x38, %r11, %rbx
leaq (%rax,%rbx), %r11
cmpl %r8d, 0x10(%rax,%rbx)
jg 0x49eb3
jl 0x49ead
movslq 0x14(%r11), %r11
cmpq %r11, %rcx
jle 0x49eb3
movl %r10d, %edx
movl %r9d, %r10d
movl %r10d, %r9d
movl %r10d, %r11d
subl %edx, %r11d
movl %r11d, %r10d
shrl $0x1f, %r10d
addl %r11d, %r10d
sarl %r10d
addl %edx, %r10d
incl %r11d
cmpl $0x2, %r11d
ja 0x49e90
testl %r9d, %r9d
js 0x49ef6
movl %r9d, %r10d
imulq $0x38, %r10, %r10
movslq 0x14(%rax,%r10), %r11
cmpq %r11, %rcx
jle 0x49ef6
addq %rax, %r10
cmpl %r8d, 0x10(%r10)
cmovel %r9d, %edx
testl %edx, %edx
jle 0x49f1c
leaq -0x20(%rax), %r9
movl %edx, %r10d
imulq $0x38, %r10, %r10
movslq (%r9,%r10), %r10
cmpq %r10, %rcx
jg 0x49f1c
leal -0x1(%rdx), %r10d
cmpl $0x1, %edx
movl %r10d, %edx
jg 0x49efe
xorl %edx, %edx
leal 0x1(%rdx), %r9d
cmpl %esi, %r9d
jge 0x49f4f
movslq %edx, %r9
imulq $0x38, %r9, %r9
addq %rax, %r9
addq $0x18, %r9
decl %esi
cmpl %r8d, -0x8(%r9)
jl 0x49f43
movslq (%r9), %r10
cmpq %r10, %rcx
jle 0x49f4f
incl %edx
addq $0x38, %r9
cmpl %edx, %esi
jne 0x49f35
movl %edi, %edx
movslq %edx, %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
jmp 0x49fe3
cmpl $-0x3, %esi
jne 0x49fc7
movslq 0x8870(%rdi), %rax
testq %rax, %rax
jle 0x49fe1
movq 0x8878(%rdi), %rdx
addq $0x8, %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
xorl %r8d, %r8d
movq (%rdx), %r9
testq %r9, %r9
je 0x49fa3
movq 0x28(%r9), %r9
cmpq %rsi, %r9
cmovll %r8d, %ecx
cmovlq %r9, %rsi
incq %r8
addq $0x38, %rdx
cmpq %r8, %rax
jne 0x49f8c
testl %ecx, %ecx
js 0x49fe1
movq 0x8878(%rdi), %rax
movl %ecx, %ecx
imulq $0x38, %rcx, %rcx
movq 0x8(%rax,%rcx), %rax
jmp 0x49fe3
cmpl $-0x5, %esi
jl 0x49fe1
leal 0x1(%rsi), %eax
movl %esi, %r8d
movq %rdx, %rcx
cmpl 0x8870(%rdi), %eax
jl 0x49e4f
xorl %eax, %eax
popq %rbx
retq