// File: /workspace/github_cpp_makefile_star2_O3/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 0x61930
movq %rsi, %r14
movq %rdi, %rbx
movq (%rdi), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x27a30
testl %eax, %eax
je 0x61988
movq 0x2d6be(%rip), %rbx     # 0x8efe0
leaq 0x142a2(%rip), %rsi     # 0x75bcb
movl $0x29, %edx
jmp 0x61943
movq 0x2d6a9(%rip), %rbx     # 0x8efe0
leaq 0x14256(%rip), %rsi     # 0x75b94
movl $0x36, %edx
movq %rbx, %rdi
callq 0x276b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq %rbx, %rdi
callq 0x27040
movq %rax, %rdi
callq 0x27410
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rbx), %r15
movq %r15, %rdi
callq 0x62f50
movq (%rbx), %rdx
leaq 0x80(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x27970
testq %rax, %rax
je 0x61a1d
movq (%rbx), %rdi
callq 0x27900
testl %eax, %eax
jne 0x61972
cmpb $0x3e, 0x80(%rsp)
jne 0x61a35
movq (%rbx), %rdi
callq 0x27950
movsbl %al, %edi
shll $0x18, %eax
testl %edi, %edi
sets %cl
cmpl $0xa000000, %eax        # imm = 0xA000000
sete %al
orb %cl, %al
jne 0x61a74
xorl %r12d, %r12d
movl $0x1, %r13d
callq 0x27540
cmpl $0x1, %eax
sbbl $-0x1, %r12d
movq (%rbx), %rdi
callq 0x27950
movl %eax, %ecx
shll $0x18, %ecx
incl %r13d
cmpl $0xa000000, %ecx        # imm = 0xA000000
je 0x61a7d
movsbl %al, %edi
testl %edi, %edi
jns 0x619f0
jmp 0x61a7d
movq 0x2d5bc(%rip), %rbx     # 0x8efe0
leaq 0x141ca(%rip), %rsi     # 0x75bf5
movl $0x26, %edx
jmp 0x61943
movq 0x2d5a4(%rip), %rbx     # 0x8efe0
leaq 0x141d9(%rip), %rsi     # 0x75c1c
movl $0x2f, %edx
movq %rbx, %rdi
callq 0x276b0
movb 0x80(%rsp), %al
movq %rsp, %rsi
movb %al, (%rsi)
movl $0x1, %edx
movq %rbx, %rdi
callq 0x276b0
movq %rax, %rbx
movq (%rax), %rax
jmp 0x6194e
movl $0x1, %r13d
xorl %r12d, %r12d
cmpb $0x1, 0x8(%rbx)
jne 0x61a93
movq (%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x27a30
testl %eax, %eax
je 0x61abb
movq 0x2d546(%rip), %rbx     # 0x8efe0
leaq 0x1412a(%rip), %rsi     # 0x75bcb
movl $0x29, %edx
movq %rbx, %rdi
callq 0x276b0
movq %rbx, %rdi
callq 0x27310
jmp 0x61972
movq %r15, 0x38(%rsp)
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x14d47(%rip), %rbp     # 0x76817
leaq 0x60(%rsp), %rdi
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x2f1a0
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x2f1a0
leaq 0x60(%rsp), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x61d2c
testb %al, %al
je 0x61bf8
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
movb $0x0, 0x10(%rsp)
movq (%rbx), %rdi
callq 0x278c0
movq %rax, 0x28(%rsp)
movq %rbp, %r15
movq %rbp, %rsi
movq %rsp, %rdx
callq 0x61f04
movl $0x39, %edx
leaq 0x140fd(%rip), %rsi     # 0x75c4c
testb %al, %al
je 0x61b94
movq %rbx, %rdi
leaq 0x40(%rsp), %rsi
callq 0x62068
movl $0x3d, %edx
leaq 0x1411a(%rip), %rsi     # 0x75c86
testb %al, %al
je 0x61b94
movl 0x48(%rsp), %eax
movl %eax, 0x20(%rsp)
movl %r12d, 0x30(%rsp)
movl %r13d, 0x34(%rsp)
movb $0x1, %bpl
movq 0x38(%rsp), %rdi
movq %rsp, %rsi
callq 0x62f96
jmp 0x61bd4
movq 0x2d445(%rip), %rdi     # 0x8efe0
callq 0x276b0
movq 0x2d439(%rip), %rcx     # 0x8efe0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x2d41b(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
xorl %ebp, %ebp
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x61be7
callq 0x27560
testb %bpl, %bpl
movq %r15, %rbp
jne 0x61afe
jmp 0x61c86
cmpq $0x0, 0x8(%r14)
je 0x61c1b
movq (%r14), %rdi
leaq 0x140bb(%rip), %rsi     # 0x75cc4
callq 0x27660
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x61c3c
movb $0x1, 0x19(%rbx)
movq %rbx, %rdi
callq 0x6222a
testb %al, %al
je 0x61c86
movb $0x1, 0x18(%rbx)
movq 0x10(%rbx), %rdi
callq 0x275b0
movb $0x0, 0x19(%rbx)
movb $0x1, %bl
jmp 0x61c88
movq 0x2d39d(%rip), %rdi     # 0x8efe0
leaq 0x1407d(%rip), %rsi     # 0x75cc7
movl $0x1d, %edx
callq 0x276b0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x2d37e(%rip), %rdi     # 0x8efe0
callq 0x276b0
movq %rax, %rbx
leaq 0x101ff(%rip), %rsi     # 0x71e70
movl $0xd, %edx
movq %rax, %rdi
callq 0x276b0
movq %rbx, %rdi
callq 0x27310
xorl %ebx, %ebx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x61c9c
callq 0x27560
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x61974
callq 0x27560
jmp 0x61974
jmp 0x61cc0
movq %rax, %rbx
jmp 0x61cef
movq %rax, %rbx
jmp 0x61cdb
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x61cdb
callq 0x27560
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x61cef
callq 0x27560
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x61d03
callq 0x27560
movq %rbx, %rdi
callq 0x27b10
nop