// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/cram/cram_io.c
// Function: cram_ref_load
ref_entry *cram_ref_load(refs_t *r, int id, int is_md5) {
    ref_entry *e = r->ref_id[id];
    hts_pos_t start = 1, end = e->length;
    char *seq;

    if (e->seq) {
        return e;
    }

    assert(e->count == 0);

    if (r->last) {
#ifdef REF_DEBUG
        int idx = 0;
        for (idx = 0; idx < r->nref; idx++)
            if (r->last == r->ref_id[idx])
                break;
        RP("%d cram_ref_load DECR %d\n", gettid(), idx);
#endif
        assert(r->last->count > 0);
        if (--r->last->count <= 0) {
            RP("%d FREE REF %d (%p)\n", gettid(), id, r->ref_id[id]->seq);
            if (r->last->seq)
                ref_entry_free_seq(r->last);
        }
    }

    if (!r->fn)
        return NULL;

    /* Open file if it's not already the current open reference */
    if (strcmp(r->fn, e->fn) || r->fp == NULL) {
        if (r->fp)
            if (bgzf_close(r->fp) != 0)
                return NULL;
        r->fn = e->fn;
        if (!(r->fp = bgzf_open_ref(r->fn, "r", is_md5)))
            return NULL;
    }

    RP("%d Loading ref %d (%d..%d)\n", gettid(), id, start, end);

    if (!(seq = load_ref_portion(r->fp, e, start, end))) {
        return NULL;
    }

    RP("%d Loaded ref %d (%d..%d) = %p\n", gettid(), id, start, end, seq);

    RP("%d INC REF %d, %"PRId64"\n", gettid(), id, (e->count+1));
    e->seq = seq;
    e->mf = NULL;
    e->count++;

    /*
     * Also keep track of last used ref so incr/decr loops on the same
     * sequence don't cause load/free loops.
     */
    RP("%d cram_ref_load INCR %d => %"PRId64"\n", gettid(), id, e->count+1);
    r->last = e;
    e->count++;

    return e;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq 0x10(%rdi), %rax
movslq %esi, %rcx
movq (%rax,%rcx,8), %rbx
cmpq $0x0, 0x38(%rbx)
jne 0x4e6e5
cmpq $0x0, 0x30(%rbx)
jne 0x4e6f1
movl %edx, %ebp
movq %rdi, %r14
movq 0x10(%rbx), %r15
movq 0x60(%rdi), %rdi
testq %rdi, %rdi
je 0x4e660
movq 0x30(%rdi), %rax
testq %rax, %rax
jle 0x4e710
leaq -0x1(%rax), %rcx
movq %rcx, 0x30(%rdi)
cmpq $0x1, %rax
jne 0x4e660
cmpq $0x0, 0x38(%rdi)
je 0x4e660
callq 0x4df60
movq 0x20(%r14), %rdi
testq %rdi, %rdi
je 0x4e6e3
movq 0x8(%rbx), %r12
movq %r12, %rsi
callq 0x46c0
movq 0x28(%r14), %rdi
testl %eax, %eax
je 0x4e691
testq %rdi, %rdi
je 0x4e696
callq 0xd3cd
testl %eax, %eax
jne 0x4e6e3
movq 0x8(%rbx), %r12
jmp 0x4e696
testq %rdi, %rdi
jne 0x4e6b0
movq %r12, 0x20(%r14)
movq %r12, %rdi
movl %ebp, %esi
callq 0x4e72f
movq %rax, %rdi
movq %rax, 0x28(%r14)
testq %rax, %rax
je 0x4e6e3
movl $0x1, %edx
movq %rbx, %rsi
movq %r15, %rcx
callq 0x4e828
testq %rax, %rax
je 0x4e6e3
movq %rax, 0x38(%rbx)
movq $0x0, 0x40(%rbx)
movq 0x30(%rbx), %rax
movq %rbx, 0x60(%r14)
addq $0x2, %rax
movq %rax, 0x30(%rbx)
jmp 0x4e6e5
xorl %ebx, %ebx
movq %rbx, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4c94a(%rip), %rdi     # 0x9b042
leaq 0x4c73b(%rip), %rsi     # 0x9ae3a
leaq 0x4c94a(%rip), %rcx     # 0x9b050
movl $0xd00, %edx            # imm = 0xD00
callq 0x42f0
leaq 0x4c966(%rip), %rdi     # 0x9b07d
leaq 0x4c71c(%rip), %rsi     # 0x9ae3a
leaq 0x4c92b(%rip), %rcx     # 0x9b050
movl $0xd0a, %edx            # imm = 0xD0A
callq 0x42f0