// File: karel-brinda[P]ococo/ext/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 0x491f0
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x4939a
movq -0x20(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x491fd
jmp 0x4921c
leaq 0x33cf3(%rip), %rdi     # 0x7cef7
leaq 0x33b5b(%rip), %rsi     # 0x7cd66
movl $0x9d8, %edx            # imm = 0x9D8
leaq 0x33cee(%rip), %rcx     # 0x7cf05
callq 0x4360
movq -0x10(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x49291
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
cmpq $0x0, 0x28(%rax)
jle 0x49238
jmp 0x49257
leaq 0x33cf3(%rip), %rdi     # 0x7cf32
leaq 0x33b20(%rip), %rsi     # 0x7cd66
movl $0x9e2, %edx            # imm = 0x9E2
leaq 0x33cb3(%rip), %rcx     # 0x7cf05
callq 0x4360
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rcx
movq 0x28(%rcx), %rax
addq $-0x1, %rax
movq %rax, 0x28(%rcx)
cmpq $0x0, %rax
jg 0x4928f
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
cmpq $0x0, 0x30(%rax)
je 0x4928d
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x48640
jmp 0x4928f
jmp 0x49291
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x4810
cmpl $0x0, %eax
jne 0x492b6
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x49323
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x492e2
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x65880
cmpl $0x0, %eax
je 0x492e0
movq $0x0, -0x8(%rbp)
jmp 0x4939a
jmp 0x492e2
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 0x326eb(%rip), %rsi     # 0x7b9ef
callq 0x493b0
movq -0x10(%rbp), %rcx
movq %rax, 0x28(%rcx)
cmpq $0x0, %rax
jne 0x49321
movq $0x0, -0x8(%rbp)
jmp 0x4939a
jmp 0x49323
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %edx
movl -0x28(%rbp), %ecx
callq 0x494c0
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x4934e
movq $0x0, -0x8(%rbp)
jmp 0x4939a
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)