// File: /workspace/github_cpp_makefile_star2_O2/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, 0x10(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x18(%rsp), %rdi
callq 0x3606
leaq 0x18(%rsp), %rdi
callq 0x363e
leaq 0x90(%rsp), %rdi
callq 0x32a0
movq (%rbx), %rsi
leaq 0x90(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x3180
leaq 0x108(%rsp), %rdi
callq 0x30a0
testb %al, %al
je 0x3e67
pushq $0x1
popq %r14
xorl %r15d, %r15d
leaq 0x50(%rsp), %r13
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
callq 0x3080
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x3da8
movq 0x50(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x2b, %eax
je 0x3cdf
movq 0x58(%rsp), %r12
cmpl $0x3b, %eax
je 0x3d88
cmpl $0x3e, %eax
je 0x3c72
cmpl $0x40, %eax
jne 0x3d23
leaq 0x18(%rsp), %rdi
leaq 0x4388(%rip), %rsi      # 0x8006
callq 0x49cb
testb %al, %al
je 0x3caa
movq 0x10(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x3ee0
movl $0x0, 0xc(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x363e
xorl %ebp, %ebp
leal -0x1(%r12), %eax
movslq %eax, %rcx
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
callq 0x3280
leaq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x32e0
movq %rbx, %rdi
callq 0x3120
jmp 0x3d88
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
callq 0x3080
movq 0x58(%rsp), %r12
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
callq 0x3080
shlq $0x20, %r12
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r12
sarq $0x20, %r12
addq %r12, %r15
movq 0x58(%rsp), %r12
jmp 0x3d88
cmpq $-0x1, 0x40(%rsp)
jne 0x3d30
movq %r15, 0x40(%rsp)
addl %r12d, 0x38(%rsp)
movl 0x4c(%rsp), %eax
testl %eax, %eax
je 0x3d56
testb $0x1, 0xc(%rsp)
jne 0x3d4a
testb $0x1, %bpl
je 0x3d61
movb $0x1, %cl
testl %r12d, %r12d
je 0x3d63
jmp 0x3dea
leal 0x1(%r12), %eax
movl %eax, 0x4c(%rsp)
jmp 0x3d82
movl %ebp, %ecx
leal 0x1(%r12), %edx
cmpl %edx, %eax
jne 0x3d70
movl %ecx, %ebp
jmp 0x3d82
testl %r12d, %r12d
movzbl %cl, %ebp
pushq $0x1
popq %rcx
cmovel %ecx, %ebp
movb $0x1, %cl
movl %ecx, 0xc(%rsp)
decl %eax
movl %eax, 0x48(%rsp)
shlq $0x20, %r12
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r12
sarq $0x20, %r12
addq %r12, %r15
incq %r14
jmp 0x3c23
leaq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x3ee0
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 0x427a(%rip), %rax      # 0x806b
leaq 0x428b(%rip), %rsi      # 0x8083
testb $0x1, %bpl
cmovneq %rax, %rsi
movq 0x81e9(%rip), %rdi      # 0xbff0
callq 0x3240
movq 0x81dd(%rip), %rdi      # 0xbff0
leaq 0x4288(%rip), %rsi      # 0x80a2
callq 0x3240
movq %rax, %rdi
movq %r14, %rsi
callq 0x30f0
leaq 0x427b(%rip), %rsi      # 0x80ac
movq %rax, %rdi
callq 0x3240
leaq 0x18(%rsp), %rsi
movq %rax, %rdi
callq 0x3210
movq %rax, %rdi
callq 0x3130
leaq 0x4269(%rip), %rsi      # 0x80be
movq %rax, %rdi
callq 0x3240
movq %rax, %rdi
callq 0x3130
jmp 0x3e9c
movq 0x8182(%rip), %rdi      # 0xbff0
leaq 0x4277(%rip), %rsi      # 0x80ec
callq 0x3240
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3210
leaq 0x427f(%rip), %rsi      # 0x810b
movq %rax, %rdi
callq 0x3240
movq %rax, %rdi
callq 0x3130
pushq $0x1
popq %rdi
callq 0x32d0
jmp 0x3ea6
movq %rax, %rbx
jmp 0x3ec4
movq %rax, %rbx
jmp 0x3ece
jmp 0x3eb4
jmp 0x3eb4
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 0x3410