// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/htslib/cram/cram_io.c
// Function: cram_get_ref
char *cram_get_ref(cram_fd *fd, int id, int start, int end) {
    ref_entry *r;
    char *seq;
    int ostart = start;

    if (id == -1)
	return NULL;

    /* FIXME: axiomatic query of r->seq being true?
     * Or shortcut for unsorted data where we load once and never free?
     */

    //fd->shared_ref = 1; // hard code for now to simplify things

    pthread_mutex_lock(&fd->ref_lock);

    RP("%d cram_get_ref on fd %p, id %d, range %d..%d\n", gettid(), fd, id, start, end);

    /*
     * Unsorted data implies we want to fetch an entire reference at a time.
     * We just deal with this at the moment by claiming we're sharing
     * references instead, which has the same requirement.
     */
    if (fd->unsorted)
	fd->shared_ref = 1;


    /* Sanity checking: does this ID exist? */
    if (id >= fd->refs->nref) {
	fprintf(stderr, "No reference found for id %d\n", id);
	pthread_mutex_unlock(&fd->ref_lock);
	return NULL;
    }

    if (!fd->refs || !fd->refs->ref_id[id]) {
	fprintf(stderr, "No reference found for id %d\n", id);
	pthread_mutex_unlock(&fd->ref_lock);
	return NULL;
    }

    if (!(r = fd->refs->ref_id[id])) {
	fprintf(stderr, "No reference found for id %d\n", id);
	pthread_mutex_unlock(&fd->ref_lock);
	return NULL;
    }


    /*
     * It has an entry, but may not have been populated yet.
     * Any manually loaded .fai files have their lengths known.
     * A ref entry computed from @SQ lines (M5 or UR field) will have
     * r->length == 0 unless it's been loaded once and verified that we have
     * an on-disk filename for it.
     *
     * 19 Sep 2013: Moved the lock here as the cram_populate_ref code calls
     * open_path_mfile and libcurl, which isn't multi-thread safe unless I
     * rewrite my code to have one curl handle per thread.
     */
    pthread_mutex_lock(&fd->refs->lock);
    if (r->length == 0) {
	if (cram_populate_ref(fd, id, r) == -1) {
	    fprintf(stderr, "Failed to populate reference for id %d\n", id);
	    pthread_mutex_unlock(&fd->refs->lock);
	    pthread_mutex_unlock(&fd->ref_lock);
	    return NULL;
	}
	r = fd->refs->ref_id[id];
	if (fd->unsorted)
	    cram_ref_incr_locked(fd->refs, id);
    }


    /*
     * We now know that we the filename containing the reference, so check
     * for limits. If it's over half the reference we'll load all of it in
     * memory as this will speed up subsequent calls.
     */
    if (end < 1)
	end = r->length;
    if (end >= r->length)
	end  = r->length; 
    assert(start >= 1);

    if (end - start >= 0.5*r->length || fd->shared_ref) {
	start = 1;
	end = r->length;
    }
    
    /*
     * Maybe we have it cached already? If so use it.
     *
     * Alternatively if we don't have the sequence but we're sharing
     * references and/or are asking for the entire length of it, then
     * load the full reference into the refs structure and return
     * a pointer to that one instead.
     */
    if (fd->shared_ref || r->seq || (start == 1 && end == r->length)) {
	char *cp;

	if (id >= 0) {
	    if (r->seq) {
		cram_ref_incr_locked(fd->refs, id);
	    } else {
		ref_entry *e;
		if (!(e = cram_ref_load(fd->refs, id, r->is_md5))) {
		    pthread_mutex_unlock(&fd->refs->lock);
		    pthread_mutex_unlock(&fd->ref_lock);
		    return NULL;
		}

		/* unsorted data implies cache ref indefinitely, to avoid
		 * continually loading and unloading.
		 */
		if (fd->unsorted)
		    cram_ref_incr_locked(fd->refs, id);
	    }	    

	    fd->ref = NULL; /* We never access it directly */
	    fd->ref_start = 1;
	    fd->ref_end   = r->length;
	    fd->ref_id    = id;

	    cp = fd->refs->ref_id[id]->seq + ostart-1;
	} else {
	    fd->ref = NULL;
	    cp = NULL;
	}

	RP("%d cram_get_ref returning for id %d, count %d\n", gettid(), id, (int)r->count);

	pthread_mutex_unlock(&fd->refs->lock);
	pthread_mutex_unlock(&fd->ref_lock);
	return cp;
    }

    /*
     * Otherwise we're not sharing, we don't have a copy of it already and
     * we're only asking for a small portion of it.
     *
     * In this case load up just that segment ourselves, freeing any old
     * small segments in the process.
     */

    /* Unmapped ref ID */
    if (id < 0) {
	if (fd->ref_free) {
	    free(fd->ref_free);
	    fd->ref_free = NULL;
	}
	fd->ref = NULL;
	fd->ref_id = id;
	pthread_mutex_unlock(&fd->refs->lock);
	pthread_mutex_unlock(&fd->ref_lock);
	return NULL;
    }

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

    if (!(fd->ref = load_ref_portion(fd->refs->fp, r, start, end))) {
	pthread_mutex_unlock(&fd->refs->lock);
	pthread_mutex_unlock(&fd->ref_lock);
	return NULL;
    }

    if (fd->ref_free)
	free(fd->ref_free);

    fd->ref_id    = id;
    fd->ref_start = start;
    fd->ref_end   = end;
    fd->ref_free = fd->ref;
    seq = fd->ref;

    pthread_mutex_unlock(&fd->refs->lock);
    pthread_mutex_unlock(&fd->ref_lock);

    return seq + ostart - start;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x30d8, %rsp           # imm = 0x30D8
cmpl $-0x1, %esi
je 0x50148
movl %ecx, %r12d
movl %edx, %r15d
movl %esi, %ebp
movq %rdi, %r14
leaq 0x8898(%rdi), %r13
movq %r13, %rdi
callq 0x6ae0
cmpl $0x0, 0x8854(%r14)
je 0x4f90a
movl $0x1, 0x220(%r14)
movq 0x48(%r14), %rdi
cmpl %ebp, 0x18(%rdi)
jle 0x4f9ab
movq 0x10(%rdi), %rax
movslq %ebp, %rbx
movq (%rax,%rbx,8), %rax
testq %rax, %rax
je 0x4f9ab
addq $0x38, %rdi
movq %rax, 0x20(%rsp)
callq 0x6ae0
movq 0x20(%rsp), %rdx
cmpq $0x0, 0x10(%rdx)
movq %r13, 0x28(%rsp)
jne 0x4ff17
leaq 0x8cfc(%r14), %rcx
movb $0x0, 0x9cfc(%r14)
cmpl $0x0, 0x1fc(%r14)
jne 0x4fa06
leaq 0x88fc(%r14), %rsi
cmpb $0x0, (%rcx)
movl %r15d, 0x8(%rsp)
movq %rbx, 0x38(%rsp)
je 0x4f990
movq %rsi, 0x10(%rsp)
movq %rcx, 0x30(%rsp)
jmp 0x4faa7
leaq 0x3880a(%rip), %rax     # 0x881a1
movq %rax, 0x30(%rsp)
cmpb $0x0, (%rsi)
je 0x4f9cd
movq %rsi, 0x10(%rsp)
jmp 0x4faa7
movq 0x63626(%rip), %rax     # 0xb2fd8
movq (%rax), %rdi
leaq 0x384a9(%rip), %rsi     # 0x87e65
xorl %r15d, %r15d
movl %ebp, %edx
xorl %eax, %eax
callq 0x6a90
jmp 0x4fca7
leaq 0x3885b(%rip), %rdi     # 0x8822f
callq 0x68f0
testq %rax, %rax
je 0x4f9e7
cmpb $0x0, (%rax)
jne 0x4fa6a
leaq 0x3884b(%rip), %rdi     # 0x88239
callq 0x68f0
testq %rax, %rax
je 0x4fa37
cmpb $0x0, (%rax)
je 0x4fa37
leaq 0x3883a(%rip), %r8      # 0x8823e
jmp 0x4fa71
movq 0x635cb(%rip), %rax     # 0xb2fd8
movq (%rax), %rdi
movq %rcx, 0x30(%rsp)
leaq 0x38762(%rip), %rsi     # 0x8817e
movq %r14, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x6a90
movq 0x30(%rsp), %rcx
movq 0x20(%rsp), %rdx
jmp 0x4f96b
leaq 0x329d6(%rip), %rdi     # 0x82414
callq 0x68f0
testq %rax, %rax
je 0x4fa4d
cmpb $0x0, (%rax)
jne 0x4fa6a
leaq 0x387f2(%rip), %rdi     # 0x88246
callq 0x68f0
testq %rax, %rax
je 0x4fa63
cmpb $0x0, (%rax)
jne 0x4fa6a
leaq 0x3298f(%rip), %rax     # 0x823f9
leaq 0x3790c(%rip), %r8      # 0x8737d
leaq 0x38751(%rip), %rdx     # 0x881c9
leaq 0x20d0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rdi, 0x10(%rsp)
movq %rax, %rcx
xorl %eax, %eax
callq 0x6c20
cmpl $0x0, 0x1fc(%r14)
jne 0x501a0
movq 0x20(%rsp), %rdx
movq (%rdx), %rcx
testq %rcx, %rcx
je 0x4fc7d
movq 0x18(%r14), %rdi
leaq 0x384fc(%rip), %rsi     # 0x87fba
leaq 0x384f8(%rip), %rdx     # 0x87fbd
callq 0x58d95
testq %rax, %rax
je 0x4fc7d
movq %rax, %r15
movl %r12d, 0xc(%rsp)
movq 0x18(%r14), %rdi
leaq 0x384da(%rip), %rdx     # 0x87fc0
movq %rax, %rsi
xorl %ecx, %ecx
callq 0x59104
testq %rax, %rax
movq %r14, %r12
je 0x4fb97
movq %rax, %rbx
movq %r12, 0x18(%rsp)
cmpl $0x0, 0x1fc(%r12)
jne 0x4feae
movq 0x10(%rsp), %rsi
cmpb $0x0, (%rsi)
movl 0xc(%rsp), %r12d
je 0x4fb4f
movq 0x8(%rbx), %rdx
addq $0x3, %rdx
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
callq 0x4f2b8
leaq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0x6630
testl %eax, %eax
je 0x4fcaf
movq 0x8(%rbx), %rdi
addq $0x3, %rdi
movq 0x30(%rsp), %rsi
xorl %edx, %edx
callq 0x55fd1
testq %rax, %rax
movq 0x18(%rsp), %r12
je 0x4fb97
movq %rax, %r14
leaq 0x40(%rsp), %rsi
movq %rax, %rdi
callq 0x5579e
movq 0x20(%rsp), %rcx
movq %rax, 0x30(%rcx)
testq %rax, %rax
je 0x4fd3f
xorl %r14d, %r14d
jmp 0x4fd47
movq 0x18(%r12), %rdi
leaq 0x2f72b(%rip), %rdx     # 0x7f2ce
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x59104
testq %rax, %rax
movq %r12, %r14
movl 0xc(%rsp), %r12d
je 0x4fc7d
movq 0x8(%rax), %rbx
movq %r14, %r15
leaq 0x3(%rbx), %r14
leaq 0x38641(%rip), %rsi     # 0x88211
pushq $0x5
popq %rdx
movq %r14, %rdi
callq 0x6330
addq $0x8, %rbx
testl %eax, %eax
cmovneq %r14, %rbx
movq %r15, %r14
movq 0x48(%r15), %rdi
movq 0x28(%rdi), %rax
testq %rax, %rax
je 0x4fc0a
movq %rax, %rdi
callq 0x5d0b0
testl %eax, %eax
jne 0x4fc7d
movq 0x48(%r14), %rdi
andq $0x0, 0x28(%rdi)
movq %rbx, %rsi
xorl %edx, %edx
callq 0x502c1
testq %rax, %rax
je 0x4fc7d
movq %rax, %rbx
movq %r14, %rdi
callq 0x5066f
movq %rbx, 0x48(%r14)
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x4fc43
callq 0x5d0b0
testl %eax, %eax
jne 0x4fc7d
movq 0x48(%r14), %rbx
andq $0x0, 0x28(%rbx)
cmpq $0x0, 0x20(%rbx)
je 0x4fc7d
movq 0x18(%r14), %rsi
movq %rbx, %rdi
callq 0x4ef70
cmpl $-0x1, %eax
je 0x4fc7d
movq 0x48(%r14), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x4fc7d
movq 0x38(%rsp), %rbx
cmpq $0x0, (%rax,%rbx,8)
movl 0x8(%rsp), %r15d
jne 0x4fef4
movq 0x63354(%rip), %rax     # 0xb2fd8
movq (%rax), %rdi
leaq 0x381f5(%rip), %rsi     # 0x87e83
xorl %r15d, %r15d
movl %ebp, %edx
xorl %eax, %eax
callq 0x6a90
movq 0x48(%r14), %rdi
addq $0x38, %rdi
callq 0x64f0
movq %r13, %rdi
jmp 0x4ffeb
leaq 0x35153(%rip), %rsi     # 0x84e09
leaq 0xd0(%rsp), %rdi
callq 0x5bd04
testq %rax, %rax
je 0x4fb4f
movq %rax, 0x10(%rsp)
movq 0x70(%rsp), %rax
movq 0x20(%rsp), %rbx
movq %rax, 0x10(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x18(%rbx)
movq 0x18(%rsp), %r14
movq 0x48(%r14), %rax
movq (%rax), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x5a5b3
movq %rax, 0x8(%rbx)
movq 0x48(%r14), %rcx
movq 0x28(%rcx), %rdi
testq %rdi, %rdi
je 0x4fed3
callq 0x5d0b0
testl %eax, %eax
movl 0x8(%rsp), %r15d
movq 0x38(%rsp), %rbx
jne 0x4fc7d
movq 0x48(%r14), %rcx
movq 0x20(%rsp), %rdx
movq 0x8(%rdx), %rax
jmp 0x4fee0
movq 0x8(%r14), %rax
movq %rax, 0x30(%rcx)
movl 0x8(%rsp), %r15d
movl 0xc(%rsp), %r12d
movq 0x10(%rsp), %rsi
movq %r14, 0x38(%rcx)
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rcx)
movl $0x1, 0x40(%rcx)
cmpb $0x0, (%rsi)
movq 0x18(%rsp), %r14
je 0x4fea7
movl %ebp, 0x10(%rsp)
movq 0x8(%rbx), %rdx
addq $0x3, %rdx
leaq 0xd0(%rsp), %rdi
callq 0x4f2b8
cmpl $0x0, 0x1fc(%r14)
jne 0x50169
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
movl $0x3ff, %esi            # imm = 0x3FF
callq 0x4f3e0
leaq 0x38467(%rip), %r15     # 0x88222
leaq 0x10d0(%rsp), %r12
leaq 0x38462(%rip), %rbp     # 0x8822c
xorl %r13d, %r13d
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x6170
movq %r12, %rdi
movq %rbp, %rsi
callq 0x6880
testq %rax, %rax
jne 0x4fe22
incl %r13d
callq 0x6060
cmpl $0x11, (%rax)
je 0x4fdcd
leaq 0x10d0(%rsp), %rdi
callq 0x6590
movl 0x10(%rsp), %ebp
movq 0x18(%rsp), %r14
movl 0x8(%rsp), %r15d
movl 0xc(%rsp), %r12d
jmp 0x4fea7
movq %rax, %r14
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rbx
movq 0x30(%rax), %rdi
pushq $0x1
popq %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x6e70
cmpq %rax, %rbx
jne 0x5018e
movq %r14, %rdi
callq 0x4edfc
cmpl $-0x1, %eax
movl 0x10(%rsp), %ebp
movl 0x8(%rsp), %r15d
movl 0xc(%rsp), %r12d
leaq 0x10d0(%rsp), %rdi
je 0x4fe8e
movl $0x124, %esi            # imm = 0x124
callq 0x6640
testl %eax, %eax
movq 0x18(%rsp), %r14
leaq 0x10d0(%rsp), %rdi
je 0x4fe9a
callq 0x6320
jmp 0x4fea7
callq 0x6320
movq 0x18(%rsp), %r14
jmp 0x4fea7
leaq 0xd0(%rsp), %rsi
callq 0x6430
movq 0x38(%rsp), %rbx
jmp 0x4fef4
movq 0x63123(%rip), %rax     # 0xb2fd8
movq (%rax), %rdi
movq 0x8(%rbx), %rdx
addq $0x3, %rdx
leaq 0x38339(%rip), %rsi     # 0x88200
xorl %eax, %eax
callq 0x6a90
jmp 0x4fb13
movq %rbx, %rdx
movl 0x8(%rsp), %r15d
movq 0x38(%rsp), %rbx
movq 0x10(%rsp), %rsi
movq %rsi, 0x28(%rcx)
movq %rax, 0x20(%rcx)
movl $0x1, 0x40(%rdx)
movq 0x48(%r14), %rdi
movq 0x10(%rdi), %rax
movq (%rax,%rbx,8), %rdx
cmpl $0x0, 0x8854(%r14)
je 0x4ff17
movq %rdx, %r13
movl %ebp, %esi
callq 0x4f46c
movq %r13, %rdx
testl %r15d, %r15d
jle 0x501ca
movq %rbx, %r13
movq 0x10(%rdx), %rax
testl %r12d, %r12d
cmovlel %eax, %r12d
movslq %r12d, %r12
cmpq %r12, %rax
cmovlel %eax, %r12d
movl %r12d, %ecx
subl %r15d, %ecx
cvtsi2sd %ecx, %xmm0
cvtsi2sd %rax, %xmm1
mulsd 0x2de49(%rip), %xmm1    # 0x7dd98
ucomisd %xmm1, %xmm0
pushq $0x1
popq %rbx
cmovbl %r15d, %ebx
cmovael %eax, %r12d
cmpl $0x0, 0x220(%r14)
jne 0x4ff84
cmpq $0x0, 0x30(%rdx)
jne 0x4ff84
cmpl $0x1, %ebx
jne 0x5001e
movslq %r12d, %rcx
cmpq %rcx, %rax
jne 0x5001e
testl %ebp, %ebp
js 0x4ffd1
cmpq $0x0, 0x30(%rdx)
movq 0x48(%r14), %rdi
je 0x4fff5
movq %rdx, %rbx
movl %ebp, %esi
callq 0x4f46c
andq $0x0, 0x50(%r14)
movl $0x1, 0x64(%r14)
movl 0x10(%rbx), %eax
movl %eax, 0x68(%r14)
movl %ebp, 0x60(%r14)
movq 0x48(%r14), %rdi
movq 0x10(%rdi), %rax
movq (%rax,%r13,8), %rax
movq 0x30(%rax), %rax
movl %r15d, %ecx
leaq (%rax,%rcx), %r15
decq %r15
jmp 0x4ffdd
andq $0x0, 0x50(%r14)
movq 0x48(%r14), %rdi
xorl %r15d, %r15d
addq $0x38, %rdi
callq 0x64f0
movq 0x28(%rsp), %rdi
callq 0x64f0
jmp 0x5014b
movq %rdx, %rbx
movl 0x40(%rdx), %edx
movl %ebp, %esi
callq 0x4f55f
testq %rax, %rax
je 0x50131
cmpl $0x0, 0x8854(%r14)
je 0x4ff9d
movq 0x48(%r14), %rdi
jmp 0x4ff96
testl %ebp, %ebp
js 0x5006c
movl %r15d, 0x8(%rsp)
movq %r14, 0x18(%rsp)
movq 0x48(%r14), %r14
movq 0x20(%r14), %rdi
movq %rdx, %r13
movq 0x8(%rdx), %r15
movq %r15, %rsi
callq 0x6a50
movq 0x28(%r14), %rdi
testl %eax, %eax
je 0x500a2
testq %rdi, %rdi
je 0x500a7
callq 0x5d0b0
testl %eax, %eax
jne 0x50148
movq 0x8(%r13), %r15
movq 0x18(%rsp), %rax
movq 0x48(%rax), %r14
jmp 0x500a7
movq 0x58(%r14), %rdi
testq %rdi, %rdi
movq 0x28(%rsp), %rbx
je 0x50084
callq 0x68a0
andq $0x0, 0x58(%r14)
andq $0x0, 0x50(%r14)
movl %ebp, 0x60(%r14)
movq 0x48(%r14), %rdi
addq $0x38, %rdi
callq 0x64f0
movq %rbx, %rdi
jmp 0x50143
testq %rdi, %rdi
jne 0x500d0
movq %r15, 0x20(%r14)
movl 0x40(%r13), %esi
movq %r15, %rdi
callq 0x4f686
movq %rax, %rdi
movq 0x18(%rsp), %rax
movq 0x48(%rax), %rax
movq %rdi, 0x28(%rax)
testq %rdi, %rdi
je 0x50160
movq %r13, %rsi
movl %ebx, %edx
movl %r12d, %ecx
callq 0x4f759
movq 0x18(%rsp), %r14
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x50131
movq 0x58(%r14), %rdi
callq 0x68a0
movl %ebp, 0x60(%r14)
movl %ebx, 0x64(%r14)
movl %r12d, 0x68(%r14)
movq 0x48(%r14), %rdi
movq %r14, %rax
movq 0x50(%r14), %r14
movq %r14, 0x58(%rax)
addq $0x38, %rdi
callq 0x64f0
movq 0x28(%rsp), %rdi
callq 0x64f0
movl 0x8(%rsp), %r15d
addq %r14, %r15
movl %ebx, %eax
subq %rax, %r15
jmp 0x5014b
movq 0x48(%r14), %rdi
addq $0x38, %rdi
callq 0x64f0
movq 0x28(%rsp), %rdi
callq 0x64f0
xorl %r15d, %r15d
movq %r15, %rax
addq $0x30d8, %rsp           # imm = 0x30D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x38, %rax
movq %rax, %rdi
jmp 0x50139
movq 0x62e68(%rip), %rax     # 0xb2fd8
movq (%rax), %rdi
leaq 0x3809d(%rip), %rsi     # 0x88217
leaq 0xd0(%rsp), %rdx
xorl %eax, %eax
callq 0x6a90
jmp 0x4fd9f
leaq 0xd0(%rsp), %rdi
callq 0x6590
jmp 0x4fe49
movq 0x62e31(%rip), %rax     # 0xb2fd8
movq (%rax), %rdi
leaq 0x38033(%rip), %rsi     # 0x881e4
leaq 0x20d0(%rsp), %rdx
movq %rdx, 0x10(%rsp)
xorl %eax, %eax
callq 0x6a90
jmp 0x4faa2
leaq 0x37cda(%rip), %rdi     # 0x87eab
leaq 0x37aae(%rip), %rsi     # 0x87c86
leaq 0x37cd7(%rip), %rcx     # 0x87eb6
movl $0xa85, %edx            # imm = 0xA85
callq 0x6460