// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0x81f0e
movq $0x0, -0x8(%rbp)
jmp 0x8250f
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x6ae0
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8854(%rax)
je 0x81f39
movq -0x10(%rbp), %rax
movl $0x1, 0x220(%rax)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x48(%rcx), %rcx
cmpl 0x18(%rcx), %eax
jl 0x81f81
movq 0x83080(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x60f08(%rip), %rsi     # 0xe2e65
movb $0x0, %al
callq 0x6a80
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x64e0
movq $0x0, -0x8(%rbp)
jmp 0x8250f
movq -0x10(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x81fa3
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x81fdb
movq 0x83026(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x60eae(%rip), %rsi     # 0xe2e65
movb $0x0, %al
callq 0x6a80
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x64e0
movq $0x0, -0x8(%rbp)
jmp 0x8250f
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x82031
movq 0x82fd0(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x60e58(%rip), %rsi     # 0xe2e65
movb $0x0, %al
callq 0x6a80
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x64e0
movq $0x0, -0x8(%rbp)
jmp 0x8250f
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x6ae0
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x820e6
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x28(%rbp), %rdx
callq 0x82520
cmpl $-0x1, %eax
jne 0x820af
movq 0x82f63(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x60e09(%rip), %rsi     # 0xe2e83
movb $0x0, %al
callq 0x6a80
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x64e0
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x64e0
movq $0x0, -0x8(%rbp)
jmp 0x8250f
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8854(%rax)
je 0x820e4
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x816e0
jmp 0x820e6
cmpl $0x1, -0x1c(%rbp)
jge 0x820f7
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, -0x1c(%rbp)
movslq -0x1c(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jl 0x82110
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, -0x1c(%rbp)
cmpl $0x1, -0x18(%rbp)
jl 0x82118
jmp 0x82137
leaq 0x60d8c(%rip), %rdi     # 0xe2eab
leaq 0x60b60(%rip), %rsi     # 0xe2c86
movl $0xa85, %edx            # imm = 0xA85
leaq 0x60d84(%rip), %rcx     # 0xe2eb6
callq 0x6450
movl -0x1c(%rbp), %eax
subl -0x18(%rbp), %eax
cvtsi2sd %eax, %xmm0
movq -0x28(%rbp), %rax
cvtsi2sdq 0x10(%rax), %xmm2
movsd 0x56ae5(%rip), %xmm1    # 0xd8c38
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jae 0x8216a
movq -0x10(%rbp), %rax
cmpl $0x0, 0x220(%rax)
je 0x8217c
movl $0x1, -0x18(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x220(%rax)
jne 0x821b0
movq -0x28(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x821b0
cmpl $0x1, -0x18(%rbp)
jne 0x822e2
movslq -0x1c(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x822e2
cmpl $0x0, -0x14(%rbp)
jl 0x822a0
movq -0x28(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x821d7
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x816e0
jmp 0x82245
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x14(%rbp), %esi
movq -0x28(%rbp), %rax
movl 0x40(%rax), %edx
callq 0x818d0
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x82226
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x64e0
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x64e0
movq $0x0, -0x8(%rbp)
jmp 0x8250f
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8854(%rax)
je 0x82243
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x816e0
jmp 0x82245
movq -0x10(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x64(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x68(%rax)
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x60(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x30(%rax), %rax
movslq -0x34(%rbp), %rcx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x822b4
movq -0x10(%rbp), %rax
movq $0x0, 0x50(%rax)
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x64e0
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x64e0
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x8250f
cmpl $0x0, -0x14(%rbp)
jge 0x82350
movq -0x10(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x8230c
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x6880
movq -0x10(%rbp), %rax
movq $0x0, 0x58(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x50(%rax)
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x60(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x64e0
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x64e0
movq $0x0, -0x8(%rbp)
jmp 0x8250f
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x20(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x6a40
cmpl $0x0, %eax
jne 0x82381
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x28(%rax)
jne 0x8242a
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x28(%rax)
je 0x823b5
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x28(%rax), %rdi
callq 0x9d380
cmpl $0x0, %eax
je 0x823b3
movq $0x0, -0x8(%rbp)
jmp 0x8250f
jmp 0x823b5
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x20(%rax), %rdi
movq -0x28(%rbp), %rax
movl 0x40(%rax), %edx
leaq 0x5d9b3(%rip), %rsi     # 0xdfd96
callq 0x81ae0
movq -0x10(%rbp), %rcx
movq 0x48(%rcx), %rcx
movq %rax, 0x28(%rcx)
cmpq $0x0, %rax
jne 0x82428
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x64e0
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x64e0
movq $0x0, -0x8(%rbp)
jmp 0x8250f
jmp 0x8242a
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x28(%rax), %rdi
movq -0x28(%rbp), %rsi
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %ecx
callq 0x81bf0
movq -0x10(%rbp), %rcx
movq %rax, 0x50(%rcx)
cmpq $0x0, %rax
jne 0x82481
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x64e0
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x64e0
movq $0x0, -0x8(%rbp)
jmp 0x8250f
movq -0x10(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x82499
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x6880
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x60(%rax)
movl -0x18(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x64(%rax)
movl -0x1c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x68(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x64e0
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x64e0
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
addq %rcx, %rax
movslq -0x18(%rbp), %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)