// File: /workspace/github_cpp_makefile_star2_O2/zeeev[P]DASVC/bamtools/src/utils/bamtools_fasta.cpp
// Function: BamTools::Fasta::FastaPrivate::CreateIndex(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
bool Fasta::FastaPrivate::CreateIndex(const string& indexFilename) {
  
    // check that file is open
    if ( !IsOpen ) {
        cerr << "FASTA error : cannot create index, FASTA file not open" << endl;
        return false;
    }
  
    // rewind FASTA file
    if ( !Rewind() ) {
        cerr << "FASTA error : could not rewind FASTA file" << endl;
        return false;
    }
    
    // clear out prior index data
    Index.clear();
    
    // -------------------------------------------
    // calculate lineLength & byteLength
    
    int lineLength = 0;
    int byteLength = 0;
    
    // skip over header
    char buffer[1024];
    if ( fgets(buffer, 1024, Stream) == 0 ) {
        cerr << "FASTA error : could not read from file" << endl;
        return false;
    }
    if ( feof(Stream) ) return false;
    if ( buffer[0] != '>' ) { 
        cerr << "FASTA error : expected header ('>'), instead : " << buffer[0] << endl;
        return false;
    }
  
    // read in first line of sequence  
    char c = fgetc(Stream);
    while ( (c >= 0) && (c != '\n') ) {
        ++byteLength;
        if (isgraph(c)) ++lineLength;
        c = fgetc(Stream);
    }
    ++byteLength; // store newline
    
    // rewind FASTA file
    if ( !Rewind() ) {
        cerr << "FASTA error : could not rewind FASTA file" << endl;
        return false;
    }
    
    // iterate through fasta entries
    int currentId   = 0;
    string header   = "";
    string sequence = "";
    while ( GetNextHeader(header) ) {
        
        // ---------------------------
        // build index entry data
        FastaIndexData data;
        
        // store file offset of beginning of DNA sequence (after header)
        data.Offset = ftello(Stream);
        
        // parse header, store sequence name in data.Name
        if ( !GetNameFromHeader(header, data.Name) ) {
            cerr << "FASTA error : could not parse read name from FASTA header" << endl;
            return false;
        }
        
        // retrieve FASTA sequence
        if ( !GetNextSequence(sequence) ) {
            cerr << "FASTA error : could not read in next sequence from FASTA file" << endl;
            return false;
        }
        
        // store sequence length & line/byte lengths
        data.Length = sequence.length();
        data.LineLength = lineLength;
        data.ByteLength = byteLength;
        
        // store index entry
        Index.push_back(data);
        
        // update ref Id
        ++currentId;
    }
    
    // open index file
    if ( !indexFilename.empty() ) {
        IndexStream = fopen(indexFilename.c_str(), "wb");
        if ( !IndexStream ) {
            cerr << "FASTA error : Could not open " << indexFilename << " for writing." << endl;
            return false;
        }
        IsIndexOpen = true;
    }
    
    // write index data
    if ( !WriteIndexData() ) return false;
    HasIndex = true;
    
    // close index file
    fclose(IndexStream);
    IsIndexOpen = false;
    
    // return succes status
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
cmpb $0x0, 0x8(%rdi)
je 0x63f68
movq %rsi, %r14
movq %rdi, %rbx
callq 0x641b2
testb %al, %al
je 0x640ac
leaq 0x20(%rbx), %r15
movq %r15, %rdi
callq 0x64fd0
movq (%rbx), %rdx
leaq 0x80(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x359a0
testq %rax, %rax
je 0x63f7b
movq (%rbx), %rdi
callq 0x35920
testl %eax, %eax
jne 0x640c7
cmpb $0x3e, 0x80(%rsp)
jne 0x63f8e
xorl %ebp, %ebp
pushq $0x1
popq %r12
movq (%rbx), %rdi
callq 0x35970
movl %eax, %ecx
shll $0x18, %ecx
cmpl $0xa000000, %ecx        # imm = 0xA000000
je 0x63fb6
movsbl %al, %edi
testl %edi, %edi
js 0x63fb6
callq 0x35550
cmpl $0x1, %eax
sbbl $-0x1, %ebp
incl %r12d
jmp 0x63f3c
movq 0x31071(%rip), %rdi     # 0x94fe0
leaq 0x11be4(%rip), %rsi     # 0x75b5a
jmp 0x640ba
movq 0x3105e(%rip), %rdi     # 0x94fe0
leaq 0x11c32(%rip), %rsi     # 0x75bbb
jmp 0x640ba
movq 0x3104b(%rip), %rdi     # 0x94fe0
leaq 0x11c46(%rip), %rsi     # 0x75be2
callq 0x355b0
movsbl 0x80(%rsp), %esi
movq %rax, %rdi
callq 0x356e0
jmp 0x640bf
movq %rbx, %rdi
callq 0x641b2
testb %al, %al
je 0x640ac
leaq 0x1282a(%rip), %r13     # 0x767f7
leaq 0x60(%rsp), %rdi
leaq 0x8(%rsp), %rdx
movq %r13, %rsi
callq 0x35280
leaq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rdx
movq %r13, %rsi
callq 0x35280
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
callq 0x641d2
testb %al, %al
je 0x640dd
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
andq $0x0, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
movq (%rbx), %rdi
callq 0x358f0
movq %rax, 0x30(%rsp)
leaq 0x60(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x64330
leaq 0x11bd4(%rip), %rsi     # 0x75c12
testb %al, %al
je 0x6407d
movq %rbx, %rdi
leaq 0x40(%rsp), %rsi
callq 0x64428
leaq 0x11bf6(%rip), %rsi     # 0x75c4c
testb %al, %al
je 0x6407d
movl 0x48(%rsp), %eax
movl %eax, 0x28(%rsp)
movl %ebp, 0x38(%rsp)
movl %r12d, 0x3c(%rsp)
movb $0x1, %r13b
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x64fd8
jmp 0x64094
movq 0x30f5c(%rip), %rdi     # 0x94fe0
callq 0x355b0
movq %rax, %rdi
callq 0x35300
xorl %r13d, %r13d
leaq 0x8(%rsp), %rdi
callq 0x35730
testb %r13b, %r13b
jne 0x63ff1
jmp 0x64156
movq 0x30f2d(%rip), %rdi     # 0x94fe0
leaq 0x11ad7(%rip), %rsi     # 0x75b91
callq 0x355b0
movq %rax, %rdi
callq 0x35300
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x8(%r14)
je 0x64100
movq (%r14), %rdi
leaq 0x11b9c(%rip), %rsi     # 0x75c8a
callq 0x35670
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x64121
movb $0x1, 0x19(%rbx)
movq %rbx, %rdi
callq 0x64592
testb %al, %al
je 0x64156
movb $0x1, 0x18(%rbx)
movq 0x10(%rbx), %rdi
callq 0x355c0
movb $0x0, 0x19(%rbx)
movb $0x1, %bl
jmp 0x64158
movq 0x30eb8(%rip), %rdi     # 0x94fe0
leaq 0x11b5e(%rip), %rsi     # 0x75c8d
callq 0x355b0
movq %rax, %rdi
movq %r14, %rsi
callq 0x35510
leaq 0xdcb7(%rip), %rsi      # 0x71dfd
movq %rax, %rdi
callq 0x355b0
movq %rax, %rdi
callq 0x35300
xorl %ebx, %ebx
leaq 0x40(%rsp), %rdi
callq 0x35730
leaq 0x60(%rsp), %rdi
callq 0x35730
jmp 0x640c9
jmp 0x6417c
jmp 0x64175
movq %rax, %rbx
jmp 0x64198
jmp 0x6417c
movq %rax, %rbx
jmp 0x6418e
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x35730
leaq 0x40(%rsp), %rdi
callq 0x35730
leaq 0x60(%rsp), %rdi
callq 0x35730
movq %rbx, %rdi
callq 0x35b50
movq %rax, %rdi
callq 0x3a198