// File: /workspace/github_c_makefile_star2_O2/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 0x40ef8
cmpq $0x0, 0x30(%rbx)
jne 0x40f04
movl %edx, %ebp
movq %rdi, %r14
movq 0x10(%rbx), %r15
movq 0x60(%rdi), %rdi
testq %rdi, %rdi
je 0x40e78
movq 0x30(%rdi), %rax
testq %rax, %rax
jle 0x40f23
leaq -0x1(%rax), %rcx
movq %rcx, 0x30(%rdi)
cmpq $0x1, %rax
jne 0x40e78
cmpq $0x0, 0x38(%rdi)
je 0x40e78
callq 0x408da
movq 0x20(%r14), %rdi
testq %rdi, %rdi
je 0x40ef6
movq 0x8(%rbx), %r12
movq %r12, %rsi
callq 0x46c0
movq 0x28(%r14), %rdi
testl %eax, %eax
je 0x40ea9
testq %rdi, %rdi
je 0x40eae
callq 0xbc96
testl %eax, %eax
jne 0x40ef6
movq 0x8(%rbx), %r12
jmp 0x40eae
testq %rdi, %rdi
jne 0x40ec8
movq %r12, 0x20(%r14)
movq %r12, %rdi
movl %ebp, %esi
callq 0x40f42
movq %rax, %rdi
movq %rax, 0x28(%r14)
testq %rax, %rax
je 0x40ef6
pushq $0x1
popq %rdx
movq %rbx, %rsi
movq %r15, %rcx
callq 0x41035
testq %rax, %rax
je 0x40ef6
movq %rax, 0x38(%rbx)
andq $0x0, 0x40(%rbx)
movq 0x30(%rbx), %rax
movq %rbx, 0x60(%r14)
addq $0x2, %rax
movq %rax, 0x30(%rbx)
jmp 0x40ef8
xorl %ebx, %ebx
movq %rbx, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3bf37(%rip), %rdi     # 0x7ce42
leaq 0x3bd28(%rip), %rsi     # 0x7cc3a
leaq 0x3bf37(%rip), %rcx     # 0x7ce50
movl $0xd00, %edx            # imm = 0xD00
callq 0x42f0
leaq 0x3bf53(%rip), %rdi     # 0x7ce7d
leaq 0x3bd09(%rip), %rsi     # 0x7cc3a
leaq 0x3bf18(%rip), %rcx     # 0x7ce50
movl $0xd0a, %edx            # imm = 0xD0A
callq 0x42f0