// File: /workspace/github_c_makefile_star2_O0/icebert[P]pblat/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];
    int 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);
	}
    }

    /* 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, %d\n", gettid(), id, (int)(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 => %d\n", gettid(), id, e->count+1);
    r->last = e;
    e->count++; 

    return e;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
movl $0x1, -0x24(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, -0x28(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x81920
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x81aca
movq -0x20(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x8192d
jmp 0x8194c
leaq 0x614e3(%rip), %rdi     # 0xe2e17
leaq 0x6134b(%rip), %rsi     # 0xe2c86
movl $0x9ea, %edx            # imm = 0x9EA
leaq 0x614de(%rip), %rcx     # 0xe2e25
callq 0x6450
movq -0x10(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x819c1
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
cmpq $0x0, 0x28(%rax)
jle 0x81968
jmp 0x81987
leaq 0x614e3(%rip), %rdi     # 0xe2e52
leaq 0x61310(%rip), %rsi     # 0xe2c86
movl $0x9f4, %edx            # imm = 0x9F4
leaq 0x614a3(%rip), %rcx     # 0xe2e25
callq 0x6450
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rcx
movq 0x28(%rcx), %rax
addq $-0x1, %rax
movq %rax, 0x28(%rcx)
cmpq $0x0, %rax
jg 0x819bf
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
cmpq $0x0, 0x30(%rax)
je 0x819bd
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x80d70
jmp 0x819bf
jmp 0x819c1
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x6a40
cmpl $0x0, %eax
jne 0x819e6
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x81a53
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x81a12
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x9d380
cmpl $0x0, %eax
je 0x81a10
movq $0x0, -0x8(%rbp)
jmp 0x81aca
jmp 0x81a12
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl -0x18(%rbp), %edx
leaq 0x5e362(%rip), %rsi     # 0xdfd96
callq 0x81ae0
movq -0x10(%rbp), %rcx
movq %rax, 0x28(%rcx)
cmpq $0x0, %rax
jne 0x81a51
movq $0x0, -0x8(%rbp)
jmp 0x81aca
jmp 0x81a53
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %edx
movl -0x28(%rbp), %ecx
callq 0x81bf0
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x81a7e
movq $0x0, -0x8(%rbp)
jmp 0x81aca
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x28(%rax)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x28(%rax)
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)