// File: /workspace/github_cpp_makefile_star2_O3/zeeev[P]DASVC/fastahack/Fasta.cpp
// Function: FastaIndex::indexReference(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void FastaIndex::indexReference(string refname) {
    // overview:
    //  for line in the reference fasta file
    //  track byte offset from the start of the file
    //  if line is a fasta header, take the name and dump the last sequnece to the index
    //  if line is a sequence, add it to the current sequence
    //cerr << "indexing fasta reference " << refname << endl;
    string line;
    FastaIndexEntry entry;  // an entry buffer used in processing
    entry.clear();
    int line_length = 0;
    long long offset = 0;  // byte offset from start of file
    long long line_number = 0; // current line number
    bool mismatchedLineLengths = false; // flag to indicate if our line length changes mid-file
                                        // this will be used to raise an error
                                        // if we have a line length change at
                                        // any line other than the last line in
                                        // the sequence
    bool emptyLine = false;  // flag to catch empty lines, which we allow for
                             // index generation only on the last line of the sequence
    ifstream refFile;
    refFile.open(refname.c_str());
    if (refFile.is_open()) {
        while (getline(refFile, line)) {
            ++line_number;
            line_length = line.length();
            if (line[0] == ';') {
                // fasta comment, skip
            } else if (line[0] == '+') {
                // fastq quality header
                getline(refFile, line);
                line_length = line.length();
                offset += line_length + 1;
                // get and don't handle the quality line
                getline(refFile, line);
                line_length = line.length();
            } else if (line[0] == '>' || line[0] == '@') { // fasta /fastq header
                // if we aren't on the first entry, push the last sequence into the index
                if (entry.name != "") {
                    mismatchedLineLengths = false; // reset line length error tracker for every new sequence
                    emptyLine = false;
                    flushEntryToIndex(entry);
                    entry.clear();
                }
                entry.name = line.substr(1, line_length - 1);
            } else { // we assume we have found a sequence line
                if (entry.offset == -1) // NB initially the offset is -1
                    entry.offset = offset;
                entry.length += line_length;
                if (entry.line_len) {
                    //entry.line_len = entry.line_len ? entry.line_len : line_length + 1;
                    if (mismatchedLineLengths || emptyLine) {
                        if (line_length == 0) {
                            emptyLine = true; // flag empty lines, raise error only if this is embedded in the sequence
                        } else {
                            if (emptyLine) {
                                cerr << "ERROR: embedded newline";
                            } else {
                                cerr << "ERROR: mismatched line lengths";
                            }
                            cerr << " at line " << line_number << " within sequence " << entry.name <<
                                endl << "File not suitable for fasta index generation." << endl;
                            exit(1);
                        }
                    }
                    // this flag is set here and checked on the next line
                    // because we may have reached the end of the sequence, in
                    // which case a mismatched line length is OK
                    if (entry.line_len != line_length + 1) {
                        mismatchedLineLengths = true;
                        if (line_length == 0) {
                            emptyLine = true; // flag empty lines, raise error only if this is embedded in the sequence
                        }
                    }
                } else {
                    entry.line_len = line_length + 1; // first line
                }
                entry.line_blen = entry.line_len - 1;
            }
            offset += line_length + 1;
        }
        // we've hit the end of the fasta file!
        // flush the last entry
        flushEntryToIndex(entry);
    } else {
        cerr << "could not open reference file " << refname << " for indexing!" << endl;
        exit(1);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rsi, %rbx
movq %rdi, 0x48(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x10(%rsp), %rdi
callq 0x36ba
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x5097(%rip), %rcx      # 0x9006
xorl %esi, %esi
xorl %r8d, %r8d
callq 0x33c0
movl $0x0, 0x30(%rsp)
movq $-0x1, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x32f0
movq (%rbx), %rsi
leaq 0x90(%rsp), %r15
movq %r15, %rdi
movl $0x8, %edx
callq 0x31b0
leaq 0x108(%rsp), %rdi
callq 0x30a0
testb %al, %al
je 0x432c
movl $0x1, %r14d
xorl %ebx, %ebx
leaq 0x50(%rsp), %r12
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
movq 0x90(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x3490
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x421d
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rbp
movzbl (%rax), %eax
cmpl $0x3d, %eax
jg 0x404c
cmpl $0x2b, %eax
je 0x4154
cmpl $0x3b, %eax
je 0x41fd
jmp 0x4116
cmpl $0x3e, %eax
je 0x405a
cmpl $0x40, %eax
jne 0x4116
leaq 0x10(%rsp), %rdi
leaq 0x4fa0(%rip), %rsi      # 0x9006
callq 0x30d0
testl %eax, %eax
je 0x40c3
movq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x43e0
movq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
leaq 0x4f75(%rip), %rcx      # 0x9006
xorl %r8d, %r8d
callq 0x33c0
movl $0x0, 0x30(%rsp)
movq $-0x1, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
leal -0x1(%rbp), %eax
movslq %eax, %rcx
movl $0x1, %edx
leaq 0x70(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x32c0
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x3340
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x41fd
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x41fd
cmpq $-0x1, 0x38(%rsp)
jne 0x4123
movq %rbx, 0x38(%rsp)
addl %ebp, 0x30(%rsp)
movl 0x44(%rsp), %eax
testl %eax, %eax
je 0x41c6
testb $0x1, 0xc(%rsp)
jne 0x4145
testb $0x1, 0x8(%rsp)
je 0x41cf
movb $0x1, %cl
testl %ebp, %ebp
je 0x41d3
jmp 0x4282
movq 0x90(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x3490
movq 0x58(%rsp), %rbp
movq 0x90(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x3490
shlq $0x20, %rbp
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %rbp
sarq $0x20, %rbp
addq %rbp, %rbx
movq 0x58(%rsp), %rbp
jmp 0x41fd
leal 0x1(%rbp), %eax
movl %eax, 0x44(%rsp)
jmp 0x41f7
movl 0x8(%rsp), %ecx
leal 0x1(%rbp), %edx
cmpl %edx, %eax
jne 0x41e0
movl %ecx, 0x8(%rsp)
jmp 0x41f7
testl %ebp, %ebp
movzbl %cl, %edx
movl $0x1, %ecx
cmovel %ecx, %edx
movl %edx, 0x8(%rsp)
movb $0x1, %cl
movl %ecx, 0xc(%rsp)
decl %eax
movl %eax, 0x40(%rsp)
shlq $0x20, %rbp
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %rbp
sarq $0x20, %rbp
addq %rbp, %rbx
incq %r14
jmp 0x3fea
leaq 0x10(%rsp), %rsi
movq 0x48(%rsp), %rdi
callq 0x43e0
leaq 0x90(%rsp), %rdi
callq 0x30b0
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4254
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x4270
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x32b0
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4de2(%rip), %rax      # 0x906b
leaq 0x4df3(%rip), %rsi      # 0x9083
testb $0x1, 0x8(%rsp)
cmovneq %rax, %rsi
movl $0x17, %eax
movl $0x1e, %edx
cmovneq %rax, %rdx
movq 0x8d42(%rip), %rdi      # 0xcff0
callq 0x3300
movq 0x8d36(%rip), %rdi      # 0xcff0
leaq 0x4de1(%rip), %rsi      # 0x90a2
movl $0x9, %edx
callq 0x3300
movq 0x8d1e(%rip), %rdi      # 0xcff0
movq %r14, %rsi
callq 0x3120
movq %rax, %rbx
leaq 0x4dc8(%rip), %rsi      # 0x90ac
movl $0x11, %edx
movq %rax, %rdi
callq 0x3300
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rbx, %rdi
callq 0x3300
movq %rax, %rdi
callq 0x3150
movq %rax, %rbx
leaq 0x4da9(%rip), %rsi      # 0x90be
movl $0x2d, %edx
movq %rax, %rdi
callq 0x3300
movq %rbx, %rdi
callq 0x3150
jmp 0x4376
movq 0x8cbd(%rip), %rdi      # 0xcff0
leaq 0x4db2(%rip), %rsi      # 0x90ec
movl $0x1e, %edx
callq 0x3300
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq 0x8c9e(%rip), %rdi      # 0xcff0
callq 0x3300
movq %rax, %rbx
leaq 0x4daa(%rip), %rsi      # 0x910b
movl $0xe, %edx
movq %rax, %rdi
callq 0x3300
movq %rbx, %rdi
callq 0x3150
movl $0x1, %edi
callq 0x3330
jmp 0x4382
movq %rax, %rbx
jmp 0x43a0
movq %rax, %rbx
jmp 0x43bb
jmp 0x4390
jmp 0x4390
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x30b0
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x43bb
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x43d7
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x32b0
movq %rbx, %rdi
callq 0x3460
nop