// File: /workspace/github_cpp_makefile_star2_O1/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 0x36ca
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x508b(%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 0x433e
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 0x422f
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rbp
movzbl (%rax), %eax
cmpl $0x3d, %eax
jg 0x4058
cmpl $0x2b, %eax
je 0x4160
cmpl $0x3b, %eax
je 0x420f
jmp 0x4122
cmpl $0x3e, %eax
je 0x4066
cmpl $0x40, %eax
jne 0x4122
leaq 0x10(%rsp), %rdi
leaq 0x4f94(%rip), %rsi      # 0x9006
callq 0x30d0
testl %eax, %eax
je 0x40cf
movq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x43f2
movq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
leaq 0x4f69(%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 0x420f
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x420f
cmpq $-0x1, 0x38(%rsp)
jne 0x412f
movq %rbx, 0x38(%rsp)
addl %ebp, 0x30(%rsp)
movl 0x44(%rsp), %eax
testl %eax, %eax
je 0x41d2
testb $0x1, 0xc(%rsp)
jne 0x4151
testb $0x1, 0x8(%rsp)
je 0x41db
movb $0x1, %cl
testl %ebp, %ebp
je 0x41df
jmp 0x4294
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 0x420f
leal 0x1(%rbp), %eax
movl %eax, 0x44(%rsp)
jmp 0x4205
movl 0x8(%rsp), %ecx
leal 0x1(%rbp), %edx
cmpl %edx, %eax
jne 0x41ee
movl %ecx, %eax
movl %eax, 0x8(%rsp)
jmp 0x4205
testl %ebp, %ebp
movzbl %cl, %ecx
movl $0x1, %eax
cmovel %eax, %ecx
movl %ecx, 0x8(%rsp)
movb $0x1, %al
movl %eax, 0xc(%rsp)
movl 0x44(%rsp), %eax
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 0x3ff6
leaq 0x10(%rsp), %rsi
movq 0x48(%rsp), %rdi
callq 0x43f2
leaq 0x90(%rsp), %rdi
callq 0x30b0
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4266
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x4282
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 0x4dd0(%rip), %rax      # 0x906b
leaq 0x4de1(%rip), %rsi      # 0x9083
testb $0x1, 0x8(%rsp)
cmovneq %rax, %rsi
movl $0x17, %eax
movl $0x1e, %edx
cmovneq %rax, %rdx
movq 0x8d30(%rip), %rdi      # 0xcff0
callq 0x3300
movq 0x8d24(%rip), %rdi      # 0xcff0
leaq 0x4dcf(%rip), %rsi      # 0x90a2
movl $0x9, %edx
callq 0x3300
movq 0x8d0c(%rip), %rdi      # 0xcff0
movq %r14, %rsi
callq 0x3120
movq %rax, %rbx
leaq 0x4db6(%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 0x4d97(%rip), %rsi      # 0x90be
movl $0x2d, %edx
movq %rax, %rdi
callq 0x3300
movq %rbx, %rdi
callq 0x3150
jmp 0x4388
movq 0x8cab(%rip), %rdi      # 0xcff0
leaq 0x4da0(%rip), %rsi      # 0x90ec
movl $0x1e, %edx
callq 0x3300
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq 0x8c8c(%rip), %rdi      # 0xcff0
callq 0x3300
movq %rax, %rbx
leaq 0x4d98(%rip), %rsi      # 0x910b
movl $0xe, %edx
movq %rax, %rdi
callq 0x3300
movq %rbx, %rdi
callq 0x3150
movl $0x1, %edi
callq 0x3330
jmp 0x4394
movq %rax, %rbx
jmp 0x43b2
movq %rax, %rbx
jmp 0x43cd
jmp 0x43a2
jmp 0x43a2
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x30b0
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x43cd
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x43e9
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x32b0
movq %rbx, %rdi
callq 0x3460
nop