// File: /workspace/github_c_makefile_star2_O1/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 %rbx
pushq %rax
movq 0x10(%rdi), %rax
movslq %esi, %rcx
movq (%rax,%rcx,8), %rbx
cmpq $0x0, 0x38(%rbx)
jne 0x5106b
cmpq $0x0, 0x30(%rbx)
jne 0x51079
movl %edx, %ebp
movq %rdi, %r14
movq 0x10(%rbx), %r15
movq 0x60(%rdi), %rdi
testq %rdi, %rdi
je 0x50fe1
movq 0x30(%rdi), %rax
testq %rax, %rax
jle 0x51098
leaq -0x1(%rax), %rcx
movq %rcx, 0x30(%rdi)
cmpq $0x1, %rax
jne 0x50fe1
cmpq $0x0, 0x38(%rdi)
je 0x50fe1
callq 0x508d7
movq 0x20(%r14), %rdi
testq %rdi, %rdi
je 0x51069
movq 0x8(%rbx), %rsi
callq 0x46c0
testl %eax, %eax
je 0x51023
movq 0x28(%r14), %rdi
testq %rdi, %rdi
je 0x51009
callq 0xd4a4
testl %eax, %eax
jne 0x51069
movq 0x8(%rbx), %rdi
movq %rdi, 0x20(%r14)
movl %ebp, %esi
callq 0x510b7
movq %rax, 0x28(%r14)
testq %rax, %rax
jne 0x5102a
jmp 0x51069
cmpq $0x0, 0x28(%r14)
je 0x50ff7
movq 0x28(%r14), %rdi
movl $0x1, %edx
movq %rbx, %rsi
movq %r15, %rcx
callq 0x511b0
testq %rax, %rax
je 0x51069
movq %rax, 0x38(%rbx)
movq $0x0, 0x40(%rbx)
movq 0x30(%rbx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x30(%rbx)
movq %rbx, 0x60(%r14)
addq $0x2, %rax
movq %rax, 0x30(%rbx)
jmp 0x5106b
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4cdca(%rip), %rdi     # 0x9de4a
leaq 0x4cbbb(%rip), %rsi     # 0x9dc42
leaq 0x4cdca(%rip), %rcx     # 0x9de58
movl $0xd00, %edx            # imm = 0xD00
callq 0x42f0
leaq 0x4cde6(%rip), %rdi     # 0x9de85
leaq 0x4cb9c(%rip), %rsi     # 0x9dc42
leaq 0x4cdab(%rip), %rcx     # 0x9de58
movl $0xd0a, %edx            # imm = 0xD0A
callq 0x42f0