// File: /workspace/github_cpp_makefile_star2_O2/ekg[P]fastahack/Fasta.cpp
// Function: FastaReference::getSubSequence(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, int)
string FastaReference::getSubSequence(string seqname, int start, int length) {
    FastaIndexEntry entry = index->entry(seqname);
    length = min(length, entry.length - start);
    if (start < 0 || length < 1) {
        //cerr << "Empty sequence" << endl;
        return "";
    }
    // we have to handle newlines
    // approach: count newlines before start
    //           count newlines by end of read
    //             subtracting newlines before start find count of embedded newlines
    int newlines_before = start > 0 ? (start - 1) / entry.line_blen : 0;
    int newlines_by_end = (start + length - 1) / entry.line_blen;
    int newlines_inside = newlines_by_end - newlines_before;
    int bytes_per_newline = entry.line_len - entry.line_blen;
    int seqlen = length + newlines_inside * bytes_per_newline;
    char* seq = (char*) calloc (seqlen + 1, sizeof(char));
    fseek64(file, (off_t) (entry.offset + newlines_before + start), SEEK_SET);
    string s;
    if (fread(seq, sizeof(char), (off_t) seqlen, file)) {
        seq[seqlen] = '\0';
        char* pbegin = seq;
        char* pend = seq + (seqlen/sizeof(char));
        pend = remove(pbegin, pend, '\r');
        pend = remove(pbegin, pend, '\n');
        pend = remove(pbegin, pend, '\0');
        s = seq;
        free(seq);
        s.resize((pend - pbegin)/sizeof(char));
    }
    return s;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r8d, %ebp
movl %ecx, %r12d
movq %rsi, %r15
movq %rdi, %r14
movq 0x40(%rsi), %rbx
leaq 0x48(%rsp), %r13
movq %r13, %rdi
movq %rdx, %rsi
callq 0x3330
leaq 0x10(%rsp), %rdi
movq %rdi, 0x8(%rsp)
movq %rbx, %rsi
movq %r13, %rdx
callq 0x4192
leaq 0x48(%rsp), %rdi
callq 0x3120
movl 0x30(%rsp), %esi
subl %r12d, %esi
cmpl %ebp, %esi
cmovgel %ebp, %esi
testl %r12d, %r12d
js 0x474f
testl %esi, %esi
jle 0x474f
testl %r12d, %r12d
je 0x4768
leal -0x1(%r12), %ecx
movl 0x40(%rsp), %edi
movl %ecx, %eax
cltd
idivl %edi
movl %eax, %ebp
jmp 0x4771
leaq 0x38b0(%rip), %rsi      # 0x8006
leaq 0x7(%rsp), %rdx
movq %r14, %rdi
callq 0x492e
jmp 0x4842
xorl %ebp, %ebp
pushq $-0x1
popq %rcx
movl 0x40(%rsp), %edi
addl %esi, %ecx
movl %ecx, %eax
cltd
idivl %edi
subl %ebp, %eax
movl 0x44(%rsp), %ecx
subl %edi, %ecx
imull %eax, %ecx
leal (%rcx,%rsi), %eax
incl %eax
movl %ecx, %ebx
addl %esi, %ebx
movslq %eax, %rdi
pushq $0x1
popq %rsi
callq 0x3190
movq %rax, %r13
movq 0x28(%r15), %rdi
movslq %ebp, %rax
movl %r12d, %esi
addq %rax, %rsi
addq 0x38(%rsp), %rsi
xorl %edx, %edx
callq 0x3170
leaq 0x10(%r14), %rax
movq %rax, (%r14)
andq $0x0, 0x8(%r14)
movb $0x0, 0x10(%r14)
movslq %ebx, %r12
movq 0x28(%r15), %rcx
movq %r13, %rdi
pushq $0x1
popq %rsi
movq %r12, %rdx
callq 0x3380
testq %rax, %rax
je 0x4842
leaq (%r12,%r13), %rsi
movb $0x0, (%r13,%r12)
leaq 0x7(%rsp), %rdx
movb $0xd, (%rdx)
movq %r13, %rdi
callq 0x6056
leaq 0x7(%rsp), %rdx
movb $0xa, (%rdx)
movq %r13, %rdi
movq %rax, %rsi
callq 0x6056
leaq 0x7(%rsp), %rdx
movb $0x0, (%rdx)
movq %r13, %rdi
movq %rax, %rsi
callq 0x6056
movq %rax, %r15
movq %r14, %rdi
movq %r13, %rsi
callq 0x3470
movq %r13, %rdi
callq 0x32d0
subq %r13, %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x3340
leaq 0x10(%rsp), %rdi
callq 0x3120
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4869
jmp 0x4869
jmp 0x4869
movq %rax, %r15
jmp 0x487e
movq %rax, %r15
movq %r14, %rdi
callq 0x3120
jmp 0x487e
movq %rax, %r15
movq %r13, 0x8(%rsp)
movq 0x8(%rsp), %rdi
callq 0x3120
movq %r15, %rdi
callq 0x3430