// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
movq %rcx, %r13
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %rbx
cmpq %rdx, %rcx
cmovleq %rdx, %r13
movslq 0x2c(%rsi), %rcx
testq %rcx, %rcx
je 0x511f9
leaq -0x1(%rbp), %rax
movslq 0x28(%r12), %rsi
cqto
idivq %rsi
movq %rax, %r14
imulq %rcx, %r14
addq 0x20(%r12), %r14
addq %rdx, %r14
jmp 0x511fd
leaq -0x1(%rbp), %r14
testl %ecx, %ecx
je 0x51220
leaq -0x1(%r13), %rax
movslq 0x28(%r12), %rsi
cqto
idivq %rsi
movq %rax, %r15
imulq %rcx, %r15
addq 0x20(%r12), %r15
addq %rdx, %r15
jmp 0x51224
leaq -0x1(%r13), %r15
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xe607
testl %eax, %eax
js 0x513b4
movq %r12, 0x8(%rsp)
movq %r13, %r12
subq %r14, %r15
movq %r15, %r13
incq %r13
je 0x513c0
movq %r13, %rdi
callq 0x4680
testq %rax, %rax
je 0x513c0
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0xbf5d
cmpq %rax, %r13
jne 0x513d5
subq %rbp, %r12
movq %r12, 0x10(%rsp)
cmpq %r12, %r15
jne 0x512b4
testq %r15, %r15
js 0x513c3
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, %r13
jne 0x51298
jmp 0x513c3
xorl %ebx, %ebx
testq %r15, %r15
js 0x512e5
callq 0x4510
movq 0x8(%rsp), %r12
movzbl (%r14,%rbx), %ecx
movq (%rax), %rdx
testb $0x20, 0x1(%rdx,%rcx,2)
jne 0x512ea
andb $-0x21, %cl
movb %cl, (%r14,%rbx)
incq %rbx
cmpq %rbx, %r13
jne 0x512c5
jmp 0x512ea
movq 0x8(%rsp), %r12
movq %rbx, %rbp
cmpq %r15, %rbx
jg 0x51318
callq 0x4510
movq (%rax), %rax
movq %rbx, %rbp
movzbl (%r14,%rbp), %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x51318
leaq 0x1(%rbp), %rcx
cmpq %r15, %rbp
movq %rcx, %rbp
jl 0x512fd
movq %r13, %rbp
movslq 0x2c(%r12), %rax
subq %rax, %r13
cmpq %r13, %rbp
jge 0x51354
movslq 0x28(%r12), %rcx
subq %rcx, %rax
testl %ecx, %ecx
jle 0x5134c
leaq (%rbx,%rcx), %rdx
movb (%r14,%rbp), %sil
incq %rbp
andb $-0x21, %sil
movb %sil, (%r14,%rbx)
incq %rbx
cmpq %rdx, %rbx
jl 0x51335
addq %rax, %rbp
cmpq %r13, %rbp
jl 0x5132d
movq 0x10(%rsp), %r12
incq %r12
cmpq %r15, %rbp
jg 0x5138b
callq 0x4510
decq %rbp
movzbl 0x1(%r14,%rbp), %ecx
movq (%rax), %rdx
testb $0x20, 0x1(%rdx,%rcx,2)
jne 0x51383
andb $-0x21, %cl
movb %cl, (%r14,%rbx)
incq %rbx
incq %rbp
cmpq %r15, %rbp
jl 0x51369
cmpq %r12, %rbx
je 0x513c3
leaq 0x4d0a1(%rip), %rsi     # 0x9e438
leaq 0x4d0ab(%rip), %rdx     # 0x9e449
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
movq %r14, %rdi
callq 0x45c0
jmp 0x513c0
leaq 0x4d040(%rip), %rdi     # 0x9e3fb
callq 0x43d0
xorl %r14d, %r14d
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4d03e(%rip), %rdi     # 0x9e41a
callq 0x43d0
jmp 0x513aa