// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1108, %rsp           # imm = 0x1108
movq %rcx, %r12
cmpl $-0x1, %esi
sete %al
testq %rdx, %rdx
setle %cl
orb %al, %cl
je 0x41242
xorl %r15d, %r15d
jmp 0x415fb
movq %rdx, %r15
movl %esi, %ebp
movq %rdi, %r14
leaq 0x88f0(%rdi), %rbx
movq %rbx, %rdi
callq 0x4750
cmpl $0x0, 0x88a4(%r14)
je 0x4126e
movl $0x1, 0x248(%r14)
movq 0x50(%r14), %rdi
cmpl %ebp, 0x18(%rdi)
jle 0x4136a
movq 0x10(%rdi), %rax
movslq %ebp, %rcx
movq (%rax,%rcx,8), %r13
testq %r13, %r13
je 0x4136a
movq %rcx, 0x10(%rsp)
addq $0x38, %rdi
callq 0x4750
cmpq $0x0, 0x10(%r13)
movq %rbx, 0x18(%rsp)
je 0x4138c
testq %r12, %r12
movq 0x10(%r13), %rax
cmovleq %rax, %r12
cmpq %rax, %r12
cmovgeq %rax, %r12
movq %r12, %rcx
subq %r15, %rcx
cvtsi2sd %rcx, %xmm0
cvtsi2sd %rax, %xmm1
mulsd 0x33d49(%rip), %xmm1    # 0x75020
ucomisd %xmm1, %xmm0
pushq $0x1
popq %rbx
cmovbq %r15, %rbx
cmovaeq %rax, %r12
cmpl $0x0, 0x248(%r14)
jne 0x4130a
cmpq $0x0, 0x38(%r13)
jne 0x4130a
movq %rbx, %rcx
xorq $0x1, %rcx
xorq %r12, %rax
orq %rcx, %rax
jne 0x4164c
testl %ebp, %ebp
movq 0x18(%rsp), %rbx
js 0x415de
cmpq $0x0, 0x38(%r13)
movq 0x50(%r14), %rdi
je 0x41610
movl %ebp, %esi
callq 0x40d40
andq $0x0, 0x58(%r14)
movq $0x1, 0x70(%r14)
movq 0x10(%r13), %rax
movq %rax, 0x78(%r14)
movl %ebp, 0x68(%r14)
movq 0x50(%r14), %rdi
movq 0x10(%rdi), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x38(%rax), %rax
movslq %r15d, %rcx
leaq (%rax,%rcx), %r15
decq %r15
jmp 0x415ea
leaq 0x3bb1f(%rip), %rsi     # 0x7ce90
leaq 0x3bb25(%rip), %rdx     # 0x7ce9d
xorl %r15d, %r15d
pushq $0x1
popq %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x110c6
jmp 0x415f3
cmpq $0x0, 0x80(%r14)
je 0x413b2
movq (%r13), %rcx
leaq 0x3baef(%rip), %rsi     # 0x7ce90
leaq 0x3bb12(%rip), %rdx     # 0x7ceba
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x110c6
leaq 0x3c09c(%rip), %rdi     # 0x7d455
callq 0x4600
movq %rax, (%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
leaq 0x3c085(%rip), %rdi     # 0x7d45e
callq 0x4600
movq %rax, %rbx
leaq 0x3c080(%rip), %rsi     # 0x7d468
leaq 0x3c08b(%rip), %rdx     # 0x7d47a
pushq $0x4
popq %rdi
movq %r14, %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x110c6
movq (%r13), %rcx
testq %rcx, %rcx
je 0x419b5
movq 0x18(%r14), %rax
movq 0x38(%rax), %rdi
leaq 0x39f57(%rip), %rsi     # 0x7b372
leaq 0x39f53(%rip), %rdx     # 0x7b375
callq 0x6a8fd
testq %rax, %rax
je 0x419b5
movq %rbx, 0x8(%rsp)
leaq 0x39f3c(%rip), %rsi     # 0x7b378
movq %rax, 0x58(%rsp)
movq %rax, %rdi
xorl %edx, %edx
callq 0x6b93e
testq %rax, %rax
je 0x41728
movq %rax, %rbx
movq 0x8(%rax), %rcx
addq $0x3, %rcx
leaq 0x3c002(%rip), %rsi     # 0x7d468
leaq 0x3c037(%rip), %rdx     # 0x7d4a4
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x110c6
cmpq $0x0, 0x8(%rsp)
movq %rbx, 0x20(%rsp)
je 0x414c2
movq 0x8(%rsp), %rax
cmpb $0x0, (%rax)
je 0x414c2
movq 0x8(%rbx), %rdx
addq $0x3, %rdx
leaq 0x100(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x466e4
testl %eax, %eax
jne 0x414c2
leaq 0x100(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x4420
testl %eax, %eax
je 0x41522
movq 0x8(%rbx), %rdi
addq $0x3, %rdi
movq (%rsp), %rsi
callq 0x48f66
testq %rax, %rax
je 0x416e4
leaq 0x38f46(%rip), %rdx     # 0x7a429
leaq 0x100(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rcx
movq %rax, 0x50(%rsp)
xorl %eax, %eax
callq 0x4800
movl %eax, %ebx
movq 0x50(%rsp), %rdi
callq 0x45c0
addl $0xfffff000, %ebx       # imm = 0xFFFFF000
cmpl $0xfffff001, %ebx       # imm = 0xFFFFF001
movq 0x20(%rsp), %rbx
jb 0x416e4
leaq 0x100(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x4420
testl %eax, %eax
jne 0x416df
movl $0xf000, %eax           # imm = 0xF000
andl 0x88(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x416df
leaq 0x38fcc(%rip), %rsi     # 0x7a526
leaq 0x100(%rsp), %rdi
callq 0x9596
testq %rax, %rax
je 0x416df
movq %rax, %rbx
movq 0xa0(%rsp), %rax
movq %rax, 0x10(%r13)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x20(%r13)
movq 0x50(%r14), %rax
movq (%rax), %rdi
leaq 0x100(%rsp), %rsi
callq 0x49393
movq %rax, 0x8(%r13)
movq 0x50(%r14), %rcx
movq 0x28(%rcx), %rdi
testq %rdi, %rdi
je 0x415c3
callq 0xbc96
testl %eax, %eax
jne 0x419b5
movq 0x50(%r14), %rcx
movq 0x8(%r13), %rax
movq %rbx, 0x28(%rcx)
movq %rax, 0x20(%rcx)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x48(%r13)
jmp 0x41b48
andq $0x0, 0x58(%r14)
movq 0x50(%r14), %rdi
xorl %r15d, %r15d
addq $0x38, %rdi
callq 0x4340
movq %rbx, %rdi
callq 0x4340
movq %r15, %rax
addq $0x1108, %rsp           # imm = 0x1108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x48(%r13), %edx
movl %ebp, %esi
callq 0x40e16
testq %rax, %rax
je 0x41637
cmpl $0x0, 0x88a4(%r14)
je 0x4132d
movq 0x50(%r14), %rdi
jmp 0x41326
movq 0x50(%r14), %rdi
addq $0x38, %rdi
callq 0x4340
movq %rbx, %rdi
jmp 0x416d5
testl %ebp, %ebp
js 0x416a7
movq 0x50(%r14), %rax
movq 0x20(%rax), %rdi
testq %rdi, %rdi
je 0x416a7
movq 0x8(%r13), %rsi
movq %rsi, (%rsp)
movq %rax, 0x10(%rsp)
callq 0x46c0
movq 0x10(%rsp), %rcx
movq 0x28(%rcx), %rdi
testl %eax, %eax
je 0x41a0a
testq %rdi, %rdi
je 0x41a0f
callq 0xbc96
testl %eax, %eax
jne 0x4123a
movq 0x8(%r13), %rax
movq %rax, (%rsp)
movq 0x50(%r14), %rcx
jmp 0x41a0f
movq 0x60(%r14), %rdi
testq %rdi, %rdi
je 0x416ba
callq 0x45c0
andq $0x0, 0x60(%r14)
andq $0x0, 0x58(%r14)
movl %ebp, 0x68(%r14)
movq 0x50(%r14), %rdi
addq $0x38, %rdi
callq 0x4340
movq 0x18(%rsp), %rdi
callq 0x4340
jmp 0x4123a
movq 0x20(%rsp), %rbx
leaq 0x2c(%rsp), %rcx
andl $0x0, (%rcx)
movq 0x8(%rbx), %rdi
addq $0x3, %rdi
movq (%rsp), %rsi
xorl %edx, %edx
callq 0x48d79
testq %rax, %rax
je 0x41728
movq %rax, %rbx
leaq 0x70(%rsp), %rsi
movq %rax, %rdi
callq 0x483da
movq %rax, 0x38(%r13)
testq %rax, %rax
je 0x41867
xorl %ebx, %ebx
jmp 0x4186f
leaq 0x3b99d(%rip), %rsi     # 0x7d0cc
movq 0x58(%rsp), %rdi
xorl %edx, %edx
callq 0x6b93e
testq %rax, %rax
je 0x419b5
movq 0x8(%rax), %rax
movq %rax, (%rsp)
leaq 0x3(%rax), %rbx
leaq 0x35d8f(%rip), %rsi     # 0x774e6
movq %rbx, %rdi
callq 0x40e0
testq %rax, %rax
je 0x41797
leaq 0x3bd49(%rip), %rsi     # 0x7d4b4
pushq $0x5
popq %rdx
movq %rbx, %rdi
callq 0x4210
testl %eax, %eax
je 0x41797
leaq 0x3bce7(%rip), %rsi     # 0x7d468
leaq 0x3bd32(%rip), %rdx     # 0x7d4ba
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
jmp 0x419b5
leaq 0x3bd16(%rip), %rsi     # 0x7d4b4
pushq $0x5
popq %rdx
movq %rbx, %rdi
callq 0x4210
movq (%rsp), %rsi
addq $0x8, %rsi
testl %eax, %eax
cmovneq %rbx, %rsi
movq 0x50(%r14), %rdi
movq 0x28(%rdi), %rax
testq %rax, %rax
je 0x417e3
movq %rsi, %rbx
movq %rax, %rdi
callq 0xbc96
testl %eax, %eax
jne 0x419b5
movq 0x50(%r14), %rdi
andq $0x0, 0x28(%rdi)
movq %rbx, %rsi
xorl %edx, %edx
callq 0x41c43
testq %rax, %rax
je 0x419b5
movq %rax, %rbx
movq %r14, %rdi
callq 0x4208d
movq %rbx, 0x50(%r14)
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x41821
callq 0xbc96
testl %eax, %eax
jne 0x419b5
movq 0x50(%r14), %rbx
andq $0x0, 0x28(%rbx)
cmpq $0x0, 0x20(%rbx)
je 0x419b5
movq 0x18(%r14), %rsi
movq %rbx, %rdi
callq 0x40917
cmpl $-0x1, %eax
je 0x419b5
movq 0x50(%r14), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x419b5
movq 0x10(%rsp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x41b4d
jmp 0x419b5
movq 0x8(%rbx), %rax
movq %rax, 0x38(%r13)
xorl %edi, %edi
movq %rbx, 0x40(%r13)
movq 0x70(%rsp), %rax
movq %rax, 0x10(%r13)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x48(%r13)
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
je 0x41b43
cmpl $0x0, 0x2c(%rsp)
jne 0x41b43
cmpb $0x0, (%rsi)
je 0x41a03
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdx
addq $0x3, %rdx
leaq 0x100(%rsp), %rdi
callq 0x466e4
testl %eax, %eax
js 0x41b48
leaq 0x3bb91(%rip), %rsi     # 0x7d468
leaq 0x3bc0f(%rip), %rdx     # 0x7d4ed
pushq $0x4
popq %rdi
leaq 0x100(%rsp), %rbx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x110c6
movq %rbx, %rdi
callq 0x4689c
leaq 0x3728e(%rip), %rsi     # 0x78b90
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
callq 0x127bd
movq %rax, (%rsp)
testq %rax, %rax
je 0x41b34
callq 0x6ecda
testq %rax, %rax
je 0x41998
movq %rax, %rbx
movq 0x10(%r13), %rdx
movq 0x38(%r13), %rsi
movq %rax, %rdi
callq 0x6e2bd
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x6ebc0
movq %rbx, %rdi
callq 0x6ed04
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
callq 0x6ed09
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdi
addq $0x3, %rdi
pushq $0x20
popq %rdx
movq %rbx, %rsi
callq 0x4210
testl %eax, %eax
je 0x41aa0
leaq 0x3bae1(%rip), %rsi     # 0x7d468
leaq 0x3bb77(%rip), %rdx     # 0x7d505
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
movq (%rsp), %rdi
callq 0xd94c
movq 0x40(%rsp), %rdi
callq 0x4200
movq 0x40(%rsp), %rdi
callq 0x45c0
movq (%r13), %rcx
leaq 0x3b4d0(%rip), %rbx     # 0x7ce90
leaq 0x3b522(%rip), %rdx     # 0x7cee9
xorl %r15d, %r15d
pushq $0x3
popq %rbp
movl %ebp, %edi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x110c6
leaq 0x3b52b(%rip), %rdx     # 0x7cf0b
movl %ebp, %edi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x110c6
movq 0x50(%r14), %rdi
addq $0x38, %rdi
callq 0x4340
movq 0x18(%rsp), %rdi
jmp 0x415f6
xorl %edi, %edi
jmp 0x41b43
testq %rdi, %rdi
jne 0x41a30
movq (%rsp), %rdi
movq %rdi, 0x20(%rcx)
movl 0x48(%r13), %esi
callq 0x40f42
movq %rax, %rdi
movq 0x50(%r14), %rax
movq %rdi, 0x28(%rax)
testq %rdi, %rdi
je 0x41a94
movq %r13, %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x41035
movq %rax, 0x58(%r14)
testq %rax, %rax
je 0x416c3
movq 0x60(%r14), %rdi
callq 0x45c0
movl %ebp, 0x68(%r14)
movq %rbx, 0x70(%r14)
movq %r12, 0x78(%r14)
movq 0x50(%r14), %rdi
movq 0x58(%r14), %r12
movq %r12, 0x60(%r14)
addq $0x38, %rdi
callq 0x4340
movq 0x18(%rsp), %rdi
callq 0x4340
movslq %r15d, %r15
addq %r12, %r15
subq %rbx, %r15
testq %r12, %r12
cmoveq %r12, %r15
jmp 0x415fb
addq $0x38, %rax
movq %rax, %rdi
jmp 0x416cb
movq 0x10(%r13), %rdx
movq 0x38(%r13), %rsi
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x3e79d
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
callq 0xd8ee
testl %eax, %eax
js 0x41af9
movq 0x8(%rsp), %rax
cmpq 0x10(%r13), %rax
jne 0x41af9
movq 0x40(%rsp), %rdi
movl $0x124, %esi            # imm = 0x124
callq 0x4430
testl %eax, %eax
js 0x41af9
movq 0x40(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x42c0
testl %eax, %eax
jns 0x41b3e
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x3b95c(%rip), %rsi     # 0x7d468
leaq 0x3ba1e(%rip), %rdx     # 0x7d531
pushq $0x1
popq %rdi
leaq 0x100(%rsp), %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x110c6
movq 0x40(%rsp), %rdi
callq 0x4200
jmp 0x41b3e
movq 0x40(%rsp), %rdi
callq 0x43d0
movq 0x40(%rsp), %rdi
callq 0x45c0
movq 0x10(%rsp), %rcx
movq 0x50(%r14), %rdi
movq 0x10(%rdi), %rax
movq (%rax,%rcx,8), %r13
cmpl $0x0, 0x88a4(%r14)
je 0x412ad
movl %ebp, %esi
callq 0x40d40
jmp 0x412ad