// File: /workspace/github_cpp_makefile_star2_O1/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 0x55ac
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x47c2
movq 0x48(%rsp), %rdi
cmpq %r14, %rdi
je 0x4fbd
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x32d0
movl 0x30(%rsp), %ecx
subl %r15d, %ecx
cmpl %ebp, %ecx
cmovgel %ebp, %ecx
testl %r15d, %r15d
js 0x4fe4
testl %ecx, %ecx
jle 0x4fe4
testl %r15d, %r15d
jle 0x5002
leal -0x1(%r15), %eax
cltd
idivl 0x40(%rsp)
movl %eax, %ebp
jmp 0x5004
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x4014(%rip), %rdx      # 0x9006
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x563c
jmp 0x50fd
xorl %ebp, %ebp
leal (%r15,%rcx), %eax
decl %eax
movl 0x40(%rsp), %esi
movl 0x44(%rsp), %edi
cltd
idivl %esi
subl %ebp, %eax
subl %esi, %edi
imull %eax, %edi
movl %ecx, %r14d
addl %edi, %r14d
leal (%rdi,%rcx), %eax
incl %eax
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 0x50fd
leaq (%r12,%r15), %rsi
movb $0x0, (%r12,%r15)
leaq 0x7(%rsp), %rdx
movb $0xd, (%rdx)
movq %r12, %rdi
callq 0x71fc
leaq 0x7(%rsp), %rdx
movb $0xa, (%rdx)
movq %r12, %rdi
movq %rax, %rsi
callq 0x71fc
leaq 0x7(%rsp), %rdx
movb $0x0, (%rdx)
movq %r12, %rdi
movq %rax, %rsi
callq 0x71fc
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 0x5118
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 0x5135
jmp 0x5135
jmp 0x5135
movq %rax, %r15
jmp 0x514c
movq %rax, %r15
movq (%rbx), %rdi
cmpq %rbp, %rdi
je 0x514c
movq (%rbp), %rsi
incq %rsi
callq 0x32d0
leaq 0x20(%rsp), %r14
movq -0x10(%r14), %rdi
jmp 0x515f
movq %rax, %r15
movq 0x48(%rsp), %rdi
cmpq %r14, %rdi
je 0x516f
movq (%r14), %rsi
incq %rsi
callq 0x32d0
movq %r15, %rdi
callq 0x3490
nop