// File: /workspace/github_cpp_makefile_star2_O3/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, %r15d
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
movq 0x40(%rsi), %r12
leaq 0x58(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rdx), %rsi
movq 0x8(%rdx), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %r13
movq %r13, %rdi
callq 0x558a
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x4794
movq 0x48(%rsp), %rdi
cmpq %r14, %rdi
je 0x4f8f
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x32d0
movl 0x30(%rsp), %esi
subl %r15d, %esi
cmpl %ebp, %esi
cmovgel %ebp, %esi
testl %r15d, %r15d
js 0x4fba
testl %esi, %esi
jle 0x4fba
testl %r15d, %r15d
je 0x4fd8
leal -0x1(%r15), %ecx
movl 0x40(%rsp), %edi
movl %ecx, %eax
cltd
idivl %edi
movl %eax, %ebp
jmp 0x4fe3
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x403e(%rip), %rdx      # 0x9006
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x5620
jmp 0x50d6
xorl %ebp, %ebp
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
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, %r14d
addl %esi, %r14d
movslq %eax, %rdi
movl $0x1, %esi
callq 0x31c0
movq %rax, %r12
movq 0x8(%rsp), %r13
movq 0x28(%r13), %rdi
movslq %ebp, %rax
movl %r15d, %esi
addq %rax, %rsi
addq 0x38(%rsp), %rsi
xorl %edx, %edx
callq 0x31a0
leaq 0x10(%rbx), %rbp
movq %rbp, (%rbx)
movq $0x0, 0x8(%rbx)
movb $0x0, 0x10(%rbx)
movslq %r14d, %r15
movq 0x28(%r13), %rcx
movl $0x1, %esi
movq %r12, %rdi
movq %r15, %rdx
callq 0x33c0
testq %rax, %rax
je 0x50d6
leaq (%r12,%r15), %rsi
movb $0x0, (%r12,%r15)
leaq 0x7(%rsp), %rdx
movb $0xd, (%rdx)
movq %r12, %rdi
callq 0x71c6
leaq 0x7(%rsp), %rdx
movb $0xa, (%rdx)
movq %r12, %rdi
movq %rax, %rsi
callq 0x71c6
leaq 0x7(%rsp), %rdx
movb $0x0, (%rdx)
movq %r12, %rdi
movq %rax, %rsi
callq 0x71c6
movq %rax, %r14
movq 0x8(%rbx), %r15
movq %r12, %rdi
callq 0x3140
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x33f0
movq %r12, %rdi
callq 0x3340
subq %r12, %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x30e0
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x50f1
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32d0
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x510e
jmp 0x510e
jmp 0x510e
movq %rax, %r15
jmp 0x5125
movq %rax, %r15
movq (%rbx), %rdi
cmpq %rbp, %rdi
je 0x5125
movq (%rbp), %rsi
incq %rsi
callq 0x32d0
leaq 0x20(%rsp), %r14
movq -0x10(%r14), %rdi
jmp 0x5138
movq %rax, %r15
movq 0x48(%rsp), %rdi
cmpq %r14, %rdi
je 0x5148
movq (%r14), %rsi
incq %rsi
callq 0x32d0
movq %r15, %rdi
callq 0x3490