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

    if (id == -1 || start < 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) {
        hts_log_error("No reference found for id %d", id);
        pthread_mutex_unlock(&fd->ref_lock);
        return NULL;
    }

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

    if (!(r = fd->refs->ref_id[id])) {
        hts_log_error("No reference found for id %d", 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 (fd->ref_fn)
            hts_log_warning("Reference file given, but ref '%s' not present",
                            r->name);
        if (cram_populate_ref(fd, id, r) == -1) {
            hts_log_warning("Failed to populate reference \"%s\"",
                            r->name);
            hts_log_warning("See https://www.htslib.org/doc/reference_seqs.html for further suggestions");
            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;

    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 || !fd->refs->fn) {
        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 ? seq + ostart - start : NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl %eax, -0x3c(%rbp)
cmpl $-0x1, -0x14(%rbp)
je 0x72b0b
cmpq $0x1, -0x20(%rbp)
jge 0x72b18
movq $0x0, -0x8(%rbp)
jmp 0x7317b
movq -0x10(%rbp), %rdi
addq $0x88f0, %rdi           # imm = 0x88F0
callq 0x4730
movq -0x10(%rbp), %rax
cmpl $0x0, 0x88a4(%rax)
je 0x72b43
movq -0x10(%rbp), %rax
movl $0x1, 0x248(%rax)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x50(%rcx), %rcx
cmpl 0x18(%rcx), %eax
jl 0x72b8d
movl -0x14(%rbp), %ecx
movl $0x1, %edi
leaq 0x82d06(%rip), %rsi     # 0xf5868
leaq 0x82d0c(%rip), %rdx     # 0xf5875
movb $0x0, %al
callq 0x1b030
movq -0x10(%rbp), %rdi
addq $0x88f0, %rdi           # imm = 0x88F0
callq 0x4330
movq $0x0, -0x8(%rbp)
jmp 0x7317b
movq -0x10(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x72baf
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x72be9
movl -0x14(%rbp), %ecx
movl $0x1, %edi
leaq 0x82caa(%rip), %rsi     # 0xf5868
leaq 0x82cb0(%rip), %rdx     # 0xf5875
movb $0x0, %al
callq 0x1b030
movq -0x10(%rbp), %rdi
addq $0x88f0, %rdi           # imm = 0x88F0
callq 0x4330
movq $0x0, -0x8(%rbp)
jmp 0x7317b
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x72c41
movl -0x14(%rbp), %ecx
movl $0x1, %edi
leaq 0x82c52(%rip), %rsi     # 0xf5868
leaq 0x82c58(%rip), %rdx     # 0xf5875
movb $0x0, %al
callq 0x1b030
movq -0x10(%rbp), %rdi
addq $0x88f0, %rdi           # imm = 0x88F0
callq 0x4330
movq $0x0, -0x8(%rbp)
jmp 0x7317b
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x38, %rdi
callq 0x4730
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x72d45
movq -0x10(%rbp), %rax
cmpq $0x0, 0x80(%rax)
je 0x72c90
movq -0x30(%rbp), %rax
movq (%rax), %rcx
movl $0x3, %edi
leaq 0x82be6(%rip), %rsi     # 0xf5868
leaq 0x82c09(%rip), %rdx     # 0xf5892
movb $0x0, %al
callq 0x1b030
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x30(%rbp), %rdx
callq 0x73190
cmpl $-0x1, %eax
jne 0x72d0e
movq -0x30(%rbp), %rax
movq (%rax), %rcx
movl $0x3, %edi
leaq 0x82bb0(%rip), %rsi     # 0xf5868
leaq 0x82c02(%rip), %rdx     # 0xf58c1
movb $0x0, %al
callq 0x1b030
movl $0x3, %edi
leaq 0x82b96(%rip), %rsi     # 0xf5868
leaq 0x82c0a(%rip), %rdx     # 0xf58e3
movb $0x0, %al
callq 0x1b030
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x38, %rdi
callq 0x4330
movq -0x10(%rbp), %rdi
addq $0x88f0, %rdi           # imm = 0x88F0
callq 0x4330
movq $0x0, -0x8(%rbp)
jmp 0x7317b
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x88a4(%rax)
je 0x72d43
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x72120
jmp 0x72d45
cmpq $0x1, -0x28(%rbp)
jge 0x72d58
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jl 0x72d72
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
subq -0x20(%rbp), %rax
cvtsi2sd %rax, %xmm0
movq -0x30(%rbp), %rax
cvtsi2sdq 0x10(%rax), %xmm2
movsd 0x7a287(%rip), %xmm1    # 0xed018
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jae 0x72da8
movq -0x10(%rbp), %rax
cmpl $0x0, 0x248(%rax)
je 0x72dbc
movq $0x1, -0x20(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x248(%rax)
jne 0x72df1
movq -0x30(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x72df1
cmpq $0x1, -0x20(%rbp)
jne 0x72f23
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x72f23
cmpl $0x0, -0x14(%rbp)
jl 0x72ee1
movq -0x30(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x72e18
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x72120
jmp 0x72e86
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movl -0x14(%rbp), %esi
movq -0x30(%rbp), %rax
movl 0x48(%rax), %edx
callq 0x72330
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
jne 0x72e67
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x38, %rdi
callq 0x4330
movq -0x10(%rbp), %rdi
addq $0x88f0, %rdi           # imm = 0x88F0
callq 0x4330
movq $0x0, -0x8(%rbp)
jmp 0x7317b
movq -0x10(%rbp), %rax
cmpl $0x0, 0x88a4(%rax)
je 0x72e84
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x72120
jmp 0x72e86
movq -0x10(%rbp), %rax
movq $0x0, 0x58(%rax)
movq -0x10(%rbp), %rax
movq $0x1, 0x70(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x68(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x38(%rax), %rax
movslq -0x3c(%rbp), %rcx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x72ef5
movq -0x10(%rbp), %rax
movq $0x0, 0x58(%rax)
movq $0x0, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x38, %rdi
callq 0x4330
movq -0x10(%rbp), %rdi
addq $0x88f0, %rdi           # imm = 0x88F0
callq 0x4330
movq -0x48(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x7317b
cmpl $0x0, -0x14(%rbp)
jl 0x72f38
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, 0x20(%rax)
jne 0x72fa0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x72f5c
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x45a0
movq -0x10(%rbp), %rax
movq $0x0, 0x60(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x58(%rax)
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x68(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x38, %rdi
callq 0x4330
movq -0x10(%rbp), %rdi
addq $0x88f0, %rdi           # imm = 0x88F0
callq 0x4330
movq $0x0, -0x8(%rbp)
jmp 0x7317b
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x20(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x4690
cmpl $0x0, %eax
jne 0x72fd1
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, 0x28(%rax)
jne 0x7307a
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, 0x28(%rax)
je 0x73005
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x28(%rax), %rdi
callq 0x103d0
cmpl $0x0, %eax
je 0x73003
movq $0x0, -0x8(%rbp)
jmp 0x7317b
jmp 0x73005
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x20(%rax), %rdi
movq -0x30(%rbp), %rax
movl 0x48(%rax), %edx
leaq 0x7fabe(%rip), %rsi     # 0xf2af1
callq 0x72550
movq -0x10(%rbp), %rcx
movq 0x50(%rcx), %rcx
movq %rax, 0x28(%rcx)
cmpq $0x0, %rax
jne 0x73078
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x38, %rdi
callq 0x4330
movq -0x10(%rbp), %rdi
addq $0x88f0, %rdi           # imm = 0x88F0
callq 0x4330
movq $0x0, -0x8(%rbp)
jmp 0x7317b
jmp 0x7307a
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x28(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x72690
movq -0x10(%rbp), %rcx
movq %rax, 0x58(%rcx)
cmpq $0x0, %rax
jne 0x730d3
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x38, %rdi
callq 0x4330
movq -0x10(%rbp), %rdi
addq $0x88f0, %rdi           # imm = 0x88F0
callq 0x4330
movq $0x0, -0x8(%rbp)
jmp 0x7317b
movq -0x10(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x730eb
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x45a0
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x68(%rax)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x38, %rdi
callq 0x4330
movq -0x10(%rbp), %rdi
addq $0x88f0, %rdi           # imm = 0x88F0
callq 0x4330
cmpq $0x0, -0x38(%rbp)
je 0x7316b
movq -0x38(%rbp), %rax
movslq -0x3c(%rbp), %rcx
addq %rcx, %rax
xorl %ecx, %ecx
subq -0x20(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
jmp 0x73173
xorl %eax, %eax
movq %rax, -0x58(%rbp)
jmp 0x73173
movq -0x58(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)