// File: /workspace/github_c_makefile_star2_O1/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:
testq %rcx, %rcx
je 0x4c64c
movq 0x30(%rcx), %rax
testq %rax, %rax
je 0x4c7f8
cmpl $-0x2, %esi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmovnel %esi, %ecx
cmpl %ecx, 0x10(%rax)
jne 0x4c7f8
movslq 0x14(%rax), %rcx
cmpq %rdx, %rcx
jg 0x4c7f8
retq
xorl %ecx, %ecx
leal 0x5(%rsi), %eax
cmpl $0x2, %eax
jb 0x4c7f8
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
leal 0x2(%rsi), %eax
cmpl $0x2, %eax
jae 0x4c76d
movq 0x8878(%rdi), %rsi
movslq %r8d, %rax
imulq $0x38, %rax, %rdi
movq 0x40(%rsi,%rdi), %rax
xorl %edx, %edx
testq %rax, %rax
je 0x4c7f8
addq %rdi, %rsi
movl 0x38(%rsi), %esi
leal -0x1(%rsi), %edi
cmpl $0x3, %esi
jb 0x4c6ec
movl %edi, %r9d
shrl $0x1f, %r9d
addl %edi, %r9d
sarl %r9d
xorl %edx, %edx
movslq %r9d, %r10
imulq $0x38, %r10, %r11
leaq (%rax,%r11), %r10
cmpl %r8d, 0x10(%rax,%r11)
jg 0x4c6ca
jl 0x4c6c4
movslq 0x14(%r10), %r10
cmpq %r10, %rcx
jle 0x4c6ca
movl %r9d, %edx
movl %edi, %r9d
movl %r9d, %edi
movl %r9d, %r10d
subl %edx, %r10d
movl %r10d, %r9d
shrl $0x1f, %r9d
addl %r10d, %r9d
sarl %r9d
addl %edx, %r9d
incl %r10d
cmpl $0x2, %r10d
ja 0x4c6a7
testl %edi, %edi
js 0x4c70b
movl %edi, %r9d
imulq $0x38, %r9, %r9
movslq 0x14(%rax,%r9), %r10
cmpq %r10, %rcx
jle 0x4c70b
addq %rax, %r9
cmpl %r8d, 0x10(%r9)
cmovel %edi, %edx
testl %edx, %edx
jle 0x4c731
leaq -0x20(%rax), %rdi
movl %edx, %r9d
imulq $0x38, %r9, %r9
movslq (%rdi,%r9), %r9
cmpq %r9, %rcx
jg 0x4c731
leal -0x1(%rdx), %r9d
cmpl $0x1, %edx
movl %r9d, %edx
jg 0x4c713
xorl %edx, %edx
leal 0x1(%rdx), %edi
cmpl %esi, %edi
jge 0x4c762
movslq %edx, %rdi
decl %esi
imulq $0x38, %rdi, %rdi
addq %rax, %rdi
addq $0x18, %rdi
cmpl %r8d, -0x8(%rdi)
jl 0x4c756
movslq (%rdi), %r9
cmpq %r9, %rcx
jle 0x4c762
incl %edx
addq $0x38, %rdi
cmpl %edx, %esi
jne 0x4c748
movl %esi, %edx
movslq %edx, %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
retq
cmpl $-0x3, %esi
jne 0x4c7c0
movslq 0x8870(%rdi), %rcx
testq %rcx, %rcx
jle 0x4c7dc
movq 0x8878(%rdi), %rdx
addq $0x8, %rdx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
xorl %r8d, %r8d
movq (%rdx), %r9
testq %r9, %r9
je 0x4c7b2
movq 0x28(%r9), %r9
cmpq %rsi, %r9
cmovll %r8d, %eax
cmovlq %r9, %rsi
incq %r8
addq $0x38, %rdx
cmpq %r8, %rcx
jne 0x4c79b
jmp 0x4c7e1
cmpl $-0x5, %esi
jl 0x4c7f8
leal 0x1(%rsi), %eax
movl %esi, %r8d
movq %rdx, %rcx
cmpl 0x8870(%rdi), %eax
jl 0x4c66c
jmp 0x4c7f8
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x4c7f8
movq 0x8878(%rdi), %rcx
movl %eax, %eax
imulq $0x38, %rax, %rax
movq 0x8(%rcx,%rax), %rax
retq
xorl %eax, %eax
retq