// File: karel-brinda[P]ococo/ext/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 0x497de
movq $0x0, -0x8(%rbp)
jmp 0x49ddf
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4880
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8854(%rax)
je 0x49809
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 0x49851
movq 0x407b0(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x33718(%rip), %rsi     # 0x7cf45
movb $0x0, %al
callq 0x4830
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4420
movq $0x0, -0x8(%rbp)
jmp 0x49ddf
movq -0x10(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x49873
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x498ab
movq 0x40756(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x336be(%rip), %rsi     # 0x7cf45
movb $0x0, %al
callq 0x4830
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4420
movq $0x0, -0x8(%rbp)
jmp 0x49ddf
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 0x49901
movq 0x40700(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x33668(%rip), %rsi     # 0x7cf45
movb $0x0, %al
callq 0x4830
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4420
movq $0x0, -0x8(%rbp)
jmp 0x49ddf
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x4880
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x499b6
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x28(%rbp), %rdx
callq 0x49df0
cmpl $-0x1, %eax
jne 0x4997f
movq 0x40693(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x33619(%rip), %rsi     # 0x7cf63
movb $0x0, %al
callq 0x4830
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x4420
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4420
movq $0x0, -0x8(%rbp)
jmp 0x49ddf
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 0x499b4
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x48fb0
jmp 0x499b6
cmpl $0x1, -0x1c(%rbp)
jge 0x499c7
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 0x499e0
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, -0x1c(%rbp)
cmpl $0x1, -0x18(%rbp)
jl 0x499e8
jmp 0x49a07
leaq 0x3359c(%rip), %rdi     # 0x7cf8b
leaq 0x33370(%rip), %rsi     # 0x7cd66
movl $0xa73, %edx            # imm = 0xA73
leaq 0x33594(%rip), %rcx     # 0x7cf96
callq 0x4360
movl -0x1c(%rbp), %eax
subl -0x18(%rbp), %eax
cvtsi2sd %eax, %xmm0
movq -0x28(%rbp), %rax
cvtsi2sdq 0x10(%rax), %xmm2
movsd 0x3108d(%rip), %xmm1    # 0x7aab0
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jae 0x49a3a
movq -0x10(%rbp), %rax
cmpl $0x0, 0x220(%rax)
je 0x49a4c
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 0x49a80
movq -0x28(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x49a80
cmpl $0x1, -0x18(%rbp)
jne 0x49bb2
movslq -0x1c(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x49bb2
cmpl $0x0, -0x14(%rbp)
jl 0x49b70
movq -0x28(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x49aa7
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x48fb0
jmp 0x49b15
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x14(%rbp), %esi
movq -0x28(%rbp), %rax
movl 0x40(%rax), %edx
callq 0x491a0
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x49af6
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x4420
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4420
movq $0x0, -0x8(%rbp)
jmp 0x49ddf
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8854(%rax)
je 0x49b13
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x48fb0
jmp 0x49b15
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 0x49b84
movq -0x10(%rbp), %rax
movq $0x0, 0x50(%rax)
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x4420
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4420
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x49ddf
cmpl $0x0, -0x14(%rbp)
jge 0x49c20
movq -0x10(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x49bdc
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x46a0
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 0x4420
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4420
movq $0x0, -0x8(%rbp)
jmp 0x49ddf
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x20(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x4810
cmpl $0x0, %eax
jne 0x49c51
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x28(%rax)
jne 0x49cfa
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x28(%rax)
je 0x49c85
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x28(%rax), %rdi
callq 0x65880
cmpl $0x0, %eax
je 0x49c83
movq $0x0, -0x8(%rbp)
jmp 0x49ddf
jmp 0x49c85
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 0x31d3c(%rip), %rsi     # 0x7b9ef
callq 0x493b0
movq -0x10(%rbp), %rcx
movq 0x48(%rcx), %rcx
movq %rax, 0x28(%rcx)
cmpq $0x0, %rax
jne 0x49cf8
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x4420
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4420
movq $0x0, -0x8(%rbp)
jmp 0x49ddf
jmp 0x49cfa
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 0x494c0
movq -0x10(%rbp), %rcx
movq %rax, 0x50(%rcx)
cmpq $0x0, %rax
jne 0x49d51
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
addq $0x38, %rdi
callq 0x4420
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4420
movq $0x0, -0x8(%rbp)
jmp 0x49ddf
movq -0x10(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x49d69
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x46a0
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 0x4420
movq -0x10(%rbp), %rdi
addq $0x8898, %rdi           # imm = 0x8898
callq 0x4420
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)