// File: /workspace/github_cpp_makefile_star2_O1/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, 0x48(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x10(%rsp), %rdi
callq 0x36fa
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x4fe3(%rip), %rcx      # 0x9006
xorl %r14d, %r14d
xorl %esi, %esi
xorl %r8d, %r8d
callq 0x33f0
movl %r14d, 0x30(%rsp)
movq $-0x1, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movl %r14d, 0x8(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x3310
movq (%rbx), %rsi
leaq 0x90(%rsp), %rdi
movl $0x8, %edx
callq 0x31b0
leaq 0x108(%rsp), %rdi
callq 0x30a0
testb %al, %al
je 0x4346
movl $0x1, %r14d
xorl %ebp, %ebp
leaq 0x50(%rsp), %r12
leaq 0x10(%rsp), %rbx
movl $0x0, 0xc(%rsp)
xorl %r15d, %r15d
leaq 0x90(%rsp), %rdi
movq %r12, %rsi
leaq 0x8(%rsp), %rdx
callq 0x3f7d
testb %al, %al
je 0x4238
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %r13
movzbl (%rax), %eax
cmpl $0x3d, %eax
jg 0x40e6
cmpl $0x2b, %eax
je 0x41d6
cmpl $0x3b, %eax
je 0x4228
jmp 0x41a3
cmpl $0x3e, %eax
je 0x40f4
cmpl $0x40, %eax
jne 0x41a3
movq %rbx, %rdi
leaq 0x4f08(%rip), %rsi      # 0x9006
callq 0x30d0
testl %eax, %eax
je 0x4152
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x43fa
movq 0x18(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
leaq 0x4ee1(%rip), %rcx      # 0x9006
xorl %r8d, %r8d
callq 0x33f0
movl $0x0, 0x30(%rsp)
movq $-0x1, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movl $0x0, 0xc(%rsp)
xorl %r15d, %r15d
decl %r13d
movslq %r13d, %rcx
movl $0x1, %edx
leaq 0x70(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x32e0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3370
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x4228
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x32d0
jmp 0x4228
cmpq $-0x1, 0x38(%rsp)
jne 0x41b0
movq %rbp, 0x38(%rsp)
addl %r13d, 0x30(%rsp)
movl 0x44(%rsp), %eax
testl %eax, %eax
je 0x41f5
testb $0x1, 0xc(%rsp)
jne 0x41ca
testb $0x1, %r15b
je 0x4204
movb $0x1, %cl
testl %r13d, %r13d
je 0x4207
jmp 0x429d
movslq 0x8(%rsp), %r13
leaq 0x90(%rsp), %rdi
movq %r12, %rsi
leaq 0x8(%rsp), %rdx
callq 0x3f7d
addq %r13, %rbp
jmp 0x4228
movl 0x8(%rsp), %eax
movl %eax, 0x44(%rsp)
movl %r13d, 0x40(%rsp)
jmp 0x4228
movl %r15d, %ecx
cmpl 0x8(%rsp), %eax
jne 0x4212
movl %ecx, %r15d
jmp 0x4228
testl %r13d, %r13d
movzbl %cl, %r15d
movl $0x1, %eax
cmovel %eax, %r15d
movb $0x1, %al
movl %eax, 0xc(%rsp)
movslq 0x8(%rsp), %rax
addq %rax, %rbp
incq %r14
jmp 0x40a0
leaq 0x10(%rsp), %rsi
movq 0x48(%rsp), %rdi
callq 0x43fa
leaq 0x90(%rsp), %rdi
callq 0x30b0
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x426f
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x428b
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x32d0
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4dc7(%rip), %rax      # 0x906b
leaq 0x4dd8(%rip), %rsi      # 0x9083
testb $0x1, %r15b
cmovneq %rax, %rsi
movl $0x17, %eax
movl $0x1e, %edx
cmovneq %rax, %rdx
movq 0x8d28(%rip), %rdi      # 0xcff0
callq 0x3320
movq 0x8d1c(%rip), %rdi      # 0xcff0
leaq 0x4dc7(%rip), %rsi      # 0x90a2
movl $0x9, %edx
callq 0x3320
movq 0x8d04(%rip), %rdi      # 0xcff0
movq %r14, %rsi
callq 0x3120
movq %rax, %rbx
leaq 0x4dae(%rip), %rsi      # 0x90ac
movl $0x11, %edx
movq %rax, %rdi
callq 0x3320
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rbx, %rdi
callq 0x3320
movq %rax, %rdi
callq 0x3150
movq %rax, %rbx
leaq 0x4d8f(%rip), %rsi      # 0x90be
movl $0x2d, %edx
movq %rax, %rdi
callq 0x3320
movq %rbx, %rdi
callq 0x3150
jmp 0x4390
movq 0x8ca3(%rip), %rdi      # 0xcff0
leaq 0x4d98(%rip), %rsi      # 0x90ec
movl $0x1e, %edx
callq 0x3320
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq 0x8c84(%rip), %rdi      # 0xcff0
callq 0x3320
movq %rax, %rbx
leaq 0x4d90(%rip), %rsi      # 0x910b
movl $0xe, %edx
movq %rax, %rdi
callq 0x3320
movq %rbx, %rdi
callq 0x3150
movl $0x1, %edi
callq 0x3360
jmp 0x439c
movq %rax, %rbx
jmp 0x43ba
movq %rax, %rbx
jmp 0x43d5
jmp 0x43aa
jmp 0x43aa
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x30b0
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x43d5
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x43f1
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x32d0
movq %rbx, %rdi
callq 0x3490
nop