// File: 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
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdx, -0x100(%rbp)
movq %rsi, %rax
movq -0x100(%rbp), %rsi
movq %rdi, -0xf8(%rbp)
movq %rdi, %rdx
movq %rdx, -0xf0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rax, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq 0x40(%rax), %rax
movq %rax, -0xe0(%rbp)
leaq -0x78(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x3130
movq -0xe0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
leaq -0x58(%rbp), %rdi
callq 0x4b30
jmp 0x55cf
leaq -0x78(%rbp), %rdi
callq 0x31a0
movl -0x38(%rbp), %eax
movl -0x1c(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x88(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x6390
movq %rax, -0x108(%rbp)
jmp 0x55ff
movq -0x108(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0x561b
cmpl $0x1, -0x20(%rbp)
jge 0x56b6
leaq -0x89(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x3550
movq -0xf8(%rbp), %rdi
movq -0x110(%rbp), %rdx
leaq 0x89d3(%rip), %rsi      # 0xe016
callq 0x5b80
jmp 0x564a
leaq -0x89(%rbp), %rdi
callq 0x3340
movl $0x1, -0x90(%rbp)
jmp 0x5939
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x31a0
jmp 0x595b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x5952
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x89(%rbp), %rdi
callq 0x3340
jmp 0x5952
cmpl $0x0, -0x1c(%rbp)
jle 0x56ce
movl -0x1c(%rbp), %eax
subl $0x1, %eax
cltd
idivl -0x28(%rbp)
movl %eax, -0x114(%rbp)
jmp 0x56d8
xorl %eax, %eax
movl %eax, -0x114(%rbp)
jmp 0x56d8
movl -0x114(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal -0x1(%rax,%rcx), %eax
movl -0x28(%rbp), %ecx
cltd
idivl %ecx
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
movl -0x94(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x9c(%rbp)
movl -0x28(%rbp), %ecx
movl -0x24(%rbp), %eax
subl %ecx, %eax
movl %eax, -0xa0(%rbp)
movl -0x20(%rbp), %eax
movl -0x9c(%rbp), %ecx
movl -0xa0(%rbp), %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
incl %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x31c0
movq %rax, %rcx
movq -0xe8(%rbp), %rax
movq %rcx, -0xb0(%rbp)
movq 0x28(%rax), %rdi
movq -0x30(%rbp), %rsi
movslq -0x94(%rbp), %rax
addq %rax, %rsi
movslq -0x1c(%rbp), %rax
addq %rax, %rsi
xorl %edx, %edx
callq 0x3190
jmp 0x5782
movq -0xf8(%rbp), %rdi
movb $0x0, -0xb1(%rbp)
callq 0x3430
movq -0xe8(%rbp), %rax
movq -0xb0(%rbp), %rdi
movslq -0xa4(%rbp), %rdx
movq 0x28(%rax), %rcx
movl $0x1, %esi
callq 0x3460
movq %rax, -0x120(%rbp)
jmp 0x57c1
movq -0x120(%rbp), %rax
cmpq $0x0, %rax
je 0x5911
movq -0xb0(%rbp), %rax
movslq -0xa4(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0xb0(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xb0(%rbp), %rax
movslq -0xa4(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movb $0xd, -0xc9(%rbp)
leaq -0xc9(%rbp), %rdx
callq 0x6300
movq %rax, -0x128(%rbp)
jmp 0x5834
movq -0x128(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movb $0xa, -0xca(%rbp)
leaq -0xca(%rbp), %rdx
callq 0x6300
movq %rax, -0x130(%rbp)
jmp 0x586c
movq -0x130(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movb $0x0, -0xcb(%rbp)
leaq -0xcb(%rbp), %rdx
callq 0x6300
movq %rax, -0x138(%rbp)
jmp 0x58a4
movq -0xf8(%rbp), %rdi
movq -0x138(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xb0(%rbp), %rsi
callq 0x3410
jmp 0x58c7
movq -0xb0(%rbp), %rdi
callq 0x3390
movq -0xf8(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movq -0xc0(%rbp), %rax
subq %rax, %rsi
callq 0x3420
jmp 0x58f2
jmp 0x5911
movq -0xf8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
callq 0x31a0
jmp 0x5952
movb $0x1, -0xb1(%rbp)
movl $0x1, -0x90(%rbp)
testb $0x1, -0xb1(%rbp)
jne 0x5937
movq -0xf8(%rbp), %rdi
callq 0x31a0
jmp 0x5939
leaq -0x58(%rbp), %rdi
callq 0x3840
movq -0xf0(%rbp), %rax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
leaq -0x58(%rbp), %rdi
callq 0x3840
movq -0x80(%rbp), %rdi
callq 0x3540
nopw %cs:(%rax,%rax)