// File: /workspace/github_cpp_makefile_star2_O2/ekg[P]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;
    int line_bytes = 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 (getlineCounting(refFile, line, line_bytes)) {
            ++line_number;
            line_length = line.length();
            if (line[0] == ';') {
                // fasta comment, skip
            } else if (line[0] == '+') {
                // fastq quality header
                
                // account for header offset
                offset += line_bytes;
                
                // read in quality line so its offset will be accounted for too
                // TODO: we don't support the quality offset field of the FAI format
                getlineCounting(refFile, line, line_bytes);
                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) {
                    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_bytes) {
                        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_bytes; // first line
                    entry.line_blen = line_length;
                }
            }
            offset += line_bytes;
        }
        // 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, 0x10(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x18(%rsp), %rdi
callq 0x3626
leaq 0x18(%rsp), %rdi
callq 0x365e
andl $0x0, 0xc(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x32b0
movq (%rbx), %rsi
leaq 0x90(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x3180
leaq 0x108(%rsp), %rdi
callq 0x30a0
testb %al, %al
je 0x3ea5
pushq $0x1
popq %r14
xorl %ebx, %ebx
leaq 0x50(%rsp), %r13
xorl %r15d, %r15d
xorl %r12d, %r12d
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
leaq 0xc(%rsp), %rdx
callq 0x3bd7
testb %al, %al
je 0x3de6
movq 0x50(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x2b, %eax
je 0x3d5e
cmpl $0x3b, %eax
je 0x3dd6
movq 0x58(%rsp), %rbp
cmpl $0x3e, %eax
je 0x3cfb
cmpl $0x40, %eax
jne 0x3d7d
leaq 0x18(%rsp), %rdi
leaq 0x42ff(%rip), %rsi      # 0x8006
callq 0x4a5f
testb %al, %al
je 0x3d2f
movq 0x10(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x3f1e
xorl %r15d, %r15d
leaq 0x18(%rsp), %rdi
callq 0x365e
xorl %r12d, %r12d
decl %ebp
movslq %ebp, %rcx
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
callq 0x3290
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x3300
movq %rbp, %rdi
callq 0x3120
jmp 0x3dd6
movslq 0xc(%rsp), %rbp
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
leaq 0xc(%rsp), %rdx
callq 0x3bd7
addq %rbp, %rbx
jmp 0x3dd6
cmpq $-0x1, 0x40(%rsp)
jne 0x3d8a
movq %rbx, 0x40(%rsp)
addl %ebp, 0x38(%rsp)
movl 0x4c(%rsp), %eax
testl %eax, %eax
je 0x3daa
testb $0x1, %r15b
jne 0x3da2
testb $0x1, %r12b
je 0x3db8
movb $0x1, %cl
testl %ebp, %ebp
je 0x3dbb
jmp 0x3e28
movl 0xc(%rsp), %eax
movl %eax, 0x4c(%rsp)
movl %ebp, 0x48(%rsp)
jmp 0x3dd6
movl %r12d, %ecx
cmpl 0xc(%rsp), %eax
jne 0x3dc6
movl %ecx, %r12d
jmp 0x3dd6
testl %ebp, %ebp
movzbl %cl, %r12d
pushq $0x1
popq %rax
cmovel %eax, %r12d
movb $0x1, %r15b
movslq 0xc(%rsp), %rax
addq %rax, %rbx
incq %r14
jmp 0x3cb5
leaq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x3f1e
leaq 0x90(%rsp), %rdi
callq 0x30b0
leaq 0x18(%rsp), %rdi
callq 0x3120
leaq 0x50(%rsp), %rdi
callq 0x3120
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x423c(%rip), %rax      # 0x806b
leaq 0x424d(%rip), %rsi      # 0x8083
testb $0x1, %r12b
cmovneq %rax, %rsi
movq 0x81ab(%rip), %rdi      # 0xbff0
callq 0x3250
movq 0x819f(%rip), %rdi      # 0xbff0
leaq 0x424a(%rip), %rsi      # 0x80a2
callq 0x3250
movq %rax, %rdi
movq %r14, %rsi
callq 0x30f0
leaq 0x423d(%rip), %rsi      # 0x80ac
movq %rax, %rdi
callq 0x3250
leaq 0x18(%rsp), %rsi
movq %rax, %rdi
callq 0x3220
movq %rax, %rdi
callq 0x3130
leaq 0x422b(%rip), %rsi      # 0x80be
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
callq 0x3130
jmp 0x3eda
movq 0x8144(%rip), %rdi      # 0xbff0
leaq 0x4239(%rip), %rsi      # 0x80ec
callq 0x3250
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3220
leaq 0x4241(%rip), %rsi      # 0x810b
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
callq 0x3130
pushq $0x1
popq %rdi
callq 0x32f0
jmp 0x3ee4
movq %rax, %rbx
jmp 0x3f02
movq %rax, %rbx
jmp 0x3f0c
jmp 0x3ef2
jmp 0x3ef2
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x30b0
leaq 0x18(%rsp), %rdi
callq 0x3120
leaq 0x50(%rsp), %rdi
callq 0x3120
movq %rbx, %rdi
callq 0x3430