// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/cram/cram_io.c
// Function: load_ref_portion
static char *load_ref_portion(BGZF *fp, ref_entry *e,
                              hts_pos_t start, hts_pos_t end) {
    off_t offset, len;
    char *seq;

    if (end < start)
        end = start;

    /*
     * Compute locations in file. This is trivial for the MD5 files, but
     * is still necessary for the fasta variants.
     *
     * Note the offset here, as with faidx, has the assumption that white-
     * space (the diff between line_length and bases_per_line) only occurs
     * at the end of a line of text.
     */
    offset = e->line_length
        ? e->offset + (start-1)/e->bases_per_line * e->line_length +
          (start-1) % e->bases_per_line
        : start-1;

    len = (e->line_length
           ? e->offset + (end-1)/e->bases_per_line * e->line_length +
             (end-1) % e->bases_per_line
           : end-1) - offset + 1;

    if (bgzf_useek(fp, offset, SEEK_SET) < 0) {
        perror("bgzf_useek() on reference file");
        return NULL;
    }

    if (len == 0 || !(seq = malloc(len))) {
        return NULL;
    }

    if (len != bgzf_read(fp, seq, len)) {
        perror("bgzf_read() on reference file");
        free(seq);
        return NULL;
    }

    /* Strip white-space if required. */
    if (len != end-start+1) {
        hts_pos_t i, j;
        char *cp = seq;
        char *cp_to;

        // Copy up to the first white-space, and then repeatedly just copy
        // bases_per_line verbatim, and use the slow method to end again.
        //
        // This may seem excessive, but this code can be a significant
        // portion of total CRAM decode CPU time for shallow data sets.
        for (i = j = 0; i < len; i++) {
            if (!isspace_c(cp[i]))
                cp[j++] = cp[i] & ~0x20;
            else
                break;
        }
        while (i < len && isspace_c(cp[i]))
            i++;
        while (i < len - e->line_length) {
            hts_pos_t j_end = j + e->bases_per_line;
            while (j < j_end)
                cp[j++] = cp[i++] & ~0x20; // toupper equiv
            i += e->line_length - e->bases_per_line;
        }
        for (; i < len; i++) {
            if (!isspace_c(cp[i]))
                cp[j++] = cp[i] & ~0x20;
        }

        cp_to = cp+j;

        if (cp_to - seq != end-start+1) {
            hts_log_error("Malformed reference file");
            free(seq);
            return NULL;
        }
    } else {
        int i;
        for (i = 0; i < len; i++) {
            seq[i] = toupper_c(seq[i]);
        }
    }

    return seq;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rdx, %rbp
movq %rdi, %r13
cmpq %rdx, %rcx
cmovleq %rdx, %rbx
movslq 0x2c(%rsi), %r9
testq %r9, %r9
movq %rsi, (%rsp)
je 0x4e889
movq 0x20(%rsi), %rdi
leaq -0x1(%rbp), %rax
movslq 0x28(%rsi), %r8
cqto
idivq %r8
movq %rax, %rcx
movq %rdx, %r14
leaq -0x1(%rbx), %rax
cqto
idivq %r8
movq %rdx, %r15
imulq %r9, %rcx
addq %rdi, %r14
addq %rcx, %r14
imulq %r9, %rax
addq %rdi, %r15
addq %rax, %r15
jmp 0x4e891
leaq -0x1(%rbp), %r14
leaq -0x1(%rbx), %r15
movq %r13, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xe4bd
testl %eax, %eax
js 0x4ea09
subq %r14, %r15
movq %r15, %r12
incq %r12
je 0x4ea15
movq %r12, %rdi
callq 0x4680
testq %rax, %rax
je 0x4ea15
movq %rax, %r14
movq %r13, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0xbefc
cmpq %rax, %r12
jne 0x4ea2a
subq %rbp, %rbx
cmpq %rbx, %r15
jne 0x4e914
testq %r15, %r15
js 0x4ea18
xorl %ebx, %ebx
callq 0x40a0
movzbl (%r14,%rbx), %ecx
movq (%rax), %rdx
movb (%rdx,%rcx,4), %cl
movb %cl, (%r14,%rbx)
incq %rbx
cmpq %rbx, %r12
jne 0x4e8f8
jmp 0x4ea18
xorl %r13d, %r13d
testq %r15, %r15
js 0x4e942
callq 0x4510
movzbl (%r14,%r13), %ecx
movq (%rax), %rdx
testb $0x20, 0x1(%rdx,%rcx,2)
jne 0x4e942
andb $-0x21, %cl
movb %cl, (%r14,%r13)
incq %r13
cmpq %r13, %r12
jne 0x4e921
movq %r12, %r13
movq %r13, %rbp
cmpq %r15, %r13
jg 0x4e970
callq 0x4510
movq (%rax), %rax
movq %r13, %rbp
movzbl (%r14,%rbp), %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x4e970
leaq 0x1(%rbp), %rcx
cmpq %r15, %rbp
movq %rcx, %rbp
jl 0x4e955
movq %r12, %rbp
movq (%rsp), %rcx
movslq 0x2c(%rcx), %rax
subq %rax, %r12
cmpq %r12, %rbp
jge 0x4e9ae
movslq 0x28(%rcx), %rcx
subq %rcx, %rax
testl %ecx, %ecx
jle 0x4e9a6
leaq (%rcx,%r13), %rdx
movb (%r14,%rbp), %sil
incq %rbp
andb $-0x21, %sil
movb %sil, (%r14,%r13)
incq %r13
cmpq %rdx, %r13
jl 0x4e98f
addq %rax, %rbp
cmpq %r12, %rbp
jl 0x4e987
incq %rbx
cmpq %r15, %rbp
jg 0x4e9e0
callq 0x4510
decq %rbp
movzbl 0x1(%r14,%rbp), %ecx
movq (%rax), %rdx
testb $0x20, 0x1(%rdx,%rcx,2)
jne 0x4e9d8
andb $-0x21, %cl
movb %cl, (%r14,%r13)
incq %r13
incq %rbp
cmpq %r15, %rbp
jl 0x4e9be
cmpq %rbx, %r13
je 0x4ea18
leaq 0x4cc44(%rip), %rsi     # 0x9b630
leaq 0x4cc4e(%rip), %rdx     # 0x9b641
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
movq %r14, %rdi
callq 0x45c0
jmp 0x4ea15
leaq 0x4cbe3(%rip), %rdi     # 0x9b5f3
callq 0x43d0
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4cbe1(%rip), %rdi     # 0x9b612
callq 0x43d0
jmp 0x4e9ff