// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/cram/cram_index.c
// Function: cram_seek_to_refpos
int cram_seek_to_refpos(cram_fd *fd, cram_range *r) {
    int ret = 0;
    cram_index *e;

    if (r->refid == HTS_IDX_NONE) {
        ret = -2; goto err;
    }

    // Ideally use an index, so see if we have one.
    if ((e = cram_index_query(fd, r->refid, r->start, NULL))) {
        if (0 != cram_seek(fd, e->offset, SEEK_SET)) {
            ret = -1; goto err;
        }
    } else {
        // Absent from index, but this most likely means it simply has no data.
        ret = -2; goto err;
    }

    pthread_mutex_lock(&fd->range_lock);
    fd->range = *r;
    if (r->refid == HTS_IDX_NOCOOR) {
        fd->range.refid = -1;
        fd->range.start = 0;
    } else if (r->refid == HTS_IDX_START || r->refid == HTS_IDX_REST) {
        fd->range.refid = -2; // special case in cram_next_slice
    }
    pthread_mutex_unlock(&fd->range_lock);

    if (fd->ctr) {
        cram_free_container(fd->ctr);
        if (fd->ctr_mt && fd->ctr_mt != fd->ctr)
            cram_free_container(fd->ctr_mt);
        fd->ctr = NULL;
        fd->ctr_mt = NULL;
        fd->ooc = 0;
        fd->eof = 0;
    }

    return 0;

 err:
    // It's unlikely fd->range will be accessed after EOF or error,
    // but this maintains identical behaviour to the previous code.
    pthread_mutex_lock(&fd->range_lock);
    fd->range = *r;
    pthread_mutex_unlock(&fd->range_lock);
    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x18(%rbp), %rax
cmpl $-0x5, (%rax)
jne 0x6b68c
movl $0xfffffffe, -0x1c(%rbp) # imm = 0xFFFFFFFE
jmp 0x6b7f7
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %esi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x6b0d0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x6b6da
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rsi
xorl %edx, %edx
callq 0x79840
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x6b6d8
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
jmp 0x6b7f7
jmp 0x6b6e6
movl $0xfffffffe, -0x1c(%rbp) # imm = 0xFFFFFFFE
jmp 0x6b7f7
movq -0x10(%rbp), %rdi
addq $0x8918, %rdi           # imm = 0x8918
callq 0x4730
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x258(%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x260(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x268(%rax)
movq -0x18(%rbp), %rax
cmpl $-0x2, (%rax)
jne 0x6b746
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x258(%rax) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movq $0x0, 0x260(%rax)
jmp 0x6b768
movq -0x18(%rbp), %rax
cmpl $-0x3, (%rax)
je 0x6b758
movq -0x18(%rbp), %rax
cmpl $-0x4, (%rax)
jne 0x6b766
movq -0x10(%rbp), %rax
movl $0xfffffffe, 0x258(%rax) # imm = 0xFFFFFFFE
jmp 0x6b768
movq -0x10(%rbp), %rdi
addq $0x8918, %rdi           # imm = 0x8918
callq 0x4330
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x6b7ee
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x74a70
movq -0x10(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x6b7ba
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x38(%rcx), %rax
je 0x6b7ba
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x74a70
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x8978(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x8890(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x6b845
movq -0x10(%rbp), %rdi
addq $0x8918, %rdi           # imm = 0x8918
callq 0x4730
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x258(%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x260(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x268(%rax)
movq -0x10(%rbp), %rdi
addq $0x8918, %rdi           # imm = 0x8918
callq 0x4330
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop