// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x6b13e
cmpl $-0x2, -0x14(%rbp)
jne 0x6b0f7
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x6b131
movq -0x40(%rbp), %rax
movl 0x10(%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x6b131
movq -0x40(%rbp), %rax
movslq 0x14(%rax), %rax
cmpq -0x20(%rbp), %rax
jg 0x6b131
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x6b4ca
movq $0x0, -0x8(%rbp)
jmp 0x6b4ca
movl -0x14(%rbp), %eax
movl %eax, -0x4c(%rbp)
addl $0x5, %eax
subl $0x2, %eax
jb 0x6b168
jmp 0x6b14e
movl -0x4c(%rbp), %eax
subl $-0x3, %eax
je 0x6b189
jmp 0x6b158
movl -0x4c(%rbp), %eax
addl $0x2, %eax
subl $0x2, %eax
jb 0x6b175
jmp 0x6b263
movq $0x0, -0x8(%rbp)
jmp 0x6b4ca
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x20(%rbp)
jmp 0x6b28a
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
movq %rax, -0x48(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8870(%rcx), %eax
jge 0x6b22d
movq -0x10(%rbp), %rax
movq 0x8878(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x6b21d
movq -0x10(%rbp), %rax
movq 0x8878(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq 0x28(%rax), %rax
cmpq -0x48(%rbp), %rax
jge 0x6b21d
movq -0x10(%rbp), %rax
movq 0x8878(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0x48(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x6b21f
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x6b1a5
cmpl $0x0, -0x30(%rbp)
jge 0x6b240
movq $0x0, -0x8(%rbp)
jmp 0x6b4ca
movq -0x10(%rbp), %rax
movq 0x8878(%rax), %rax
movslq -0x30(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x6b4ca
cmpl $-0x5, -0x14(%rbp)
jl 0x6b27b
movl -0x14(%rbp), %eax
addl $0x1, %eax
movq -0x10(%rbp), %rcx
cmpl 0x8870(%rcx), %eax
jl 0x6b288
movq $0x0, -0x8(%rbp)
jmp 0x6b4ca
jmp 0x6b28a
movq -0x10(%rbp), %rax
movq 0x8878(%rax), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x6b2c1
movq $0x0, -0x8(%rbp)
jmp 0x6b4ca
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x8878(%rax), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
subl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x2c(%rbp), %eax
je 0x6b3b0
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x34(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
cmpl -0x14(%rbp), %eax
jle 0x6b328
movl -0x34(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x6b397
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x34(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
cmpl -0x14(%rbp), %eax
jge 0x6b34b
movl -0x34(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x6b397
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x34(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movslq 0x14(%rax), %rax
cmpq -0x20(%rbp), %rax
jl 0x6b370
movl -0x34(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x6b397
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x34(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movslq 0x14(%rax), %rax
cmpq -0x20(%rbp), %rax
jge 0x6b395
movl -0x34(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x6b397
jmp 0x6b397
movl -0x30(%rbp), %eax
subl -0x2c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
addl -0x2c(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x6b2f9
cmpl $0x0, -0x30(%rbp)
jl 0x6b3f4
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x30(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movslq 0x14(%rax), %rax
cmpq -0x20(%rbp), %rax
jge 0x6b3f4
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x30(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x6b3f4
movl -0x30(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x6b3f6
xorl %eax, %eax
cmpl $0x0, -0x2c(%rbp)
movb %al, -0x4d(%rbp)
jle 0x6b427
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movslq 0x18(%rax), %rax
cmpq -0x20(%rbp), %rax
setge %al
movb %al, -0x4d(%rbp)
movb -0x4d(%rbp), %al
testb $0x1, %al
jne 0x6b430
jmp 0x6b43b
movl -0x2c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x6b3f6
jmp 0x6b43d
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
movq -0x28(%rbp), %rdx
xorl %eax, %eax
cmpl (%rdx), %ecx
movb %al, -0x4e(%rbp)
jge 0x6b497
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %ecx
movb $0x1, %al
cmpl -0x14(%rbp), %ecx
movb %al, -0x4f(%rbp)
jl 0x6b491
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movslq 0x18(%rax), %rax
cmpq -0x20(%rbp), %rax
setl %al
movb %al, -0x4f(%rbp)
movb -0x4f(%rbp), %al
movb %al, -0x4e(%rbp)
movb -0x4e(%rbp), %al
testb $0x1, %al
jne 0x6b4a0
jmp 0x6b4ab
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x6b43d
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
popq %rbp
retq