// File: /workspace/github_cpp_makefile_star2_O1/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 0x61e50
movq %rsi, %r14
movq %rdi, %rbx
movq (%rdi), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x27a40
testl %eax, %eax
je 0x61ea8
movq 0x2e19e(%rip), %rbx     # 0x8ffe0
leaq 0x14d82(%rip), %rsi     # 0x76bcb
movl $0x29, %edx
jmp 0x61e63
movq 0x2e189(%rip), %rbx     # 0x8ffe0
leaq 0x14d36(%rip), %rsi     # 0x76b94
movl $0x36, %edx
movq %rbx, %rdi
callq 0x276c0
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 0x6348c
movq (%rbx), %rdx
leaq 0x80(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x27980
testq %rax, %rax
je 0x61f3d
movq (%rbx), %rdi
callq 0x27910
testl %eax, %eax
jne 0x61e92
cmpb $0x3e, 0x80(%rsp)
jne 0x61f55
movq (%rbx), %rdi
callq 0x27960
movsbl %al, %edi
shll $0x18, %eax
testl %edi, %edi
sets %cl
cmpl $0xa000000, %eax        # imm = 0xA000000
sete %al
orb %cl, %al
jne 0x61f94
xorl %r12d, %r12d
movl $0x1, %r13d
callq 0x27540
cmpl $0x1, %eax
sbbl $-0x1, %r12d
movq (%rbx), %rdi
callq 0x27960
movl %eax, %ecx
shll $0x18, %ecx
incl %r13d
cmpl $0xa000000, %ecx        # imm = 0xA000000
je 0x61f9d
movsbl %al, %edi
testl %edi, %edi
jns 0x61f10
jmp 0x61f9d
movq 0x2e09c(%rip), %rbx     # 0x8ffe0
leaq 0x14caa(%rip), %rsi     # 0x76bf5
movl $0x26, %edx
jmp 0x61e63
movq 0x2e084(%rip), %rbx     # 0x8ffe0
leaq 0x14cb9(%rip), %rsi     # 0x76c1c
movl $0x2f, %edx
movq %rbx, %rdi
callq 0x276c0
movb 0x80(%rsp), %al
movq %rsp, %rsi
movb %al, (%rsi)
movl $0x1, %edx
movq %rbx, %rdi
callq 0x276c0
movq %rax, %rbx
movq (%rax), %rax
jmp 0x61e6e
movl $0x1, %r13d
xorl %r12d, %r12d
cmpb $0x1, 0x8(%rbx)
jne 0x61fb3
movq (%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x27a40
testl %eax, %eax
je 0x61fdb
movq 0x2e026(%rip), %rbx     # 0x8ffe0
leaq 0x14c0a(%rip), %rsi     # 0x76bcb
movl $0x29, %edx
movq %rbx, %rdi
callq 0x276c0
movq %rbx, %rdi
callq 0x27310
jmp 0x61e92
movq %r15, 0x38(%rsp)
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1582b(%rip), %rbp     # 0x7781b
leaq 0x60(%rsp), %rdi
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x2f1b2
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x2f1b2
leaq 0x60(%rsp), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x6224c
testb %al, %al
je 0x62118
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
movb $0x0, 0x10(%rsp)
movq (%rbx), %rdi
callq 0x278d0
movq %rax, 0x28(%rsp)
movq %rbp, %r15
movq %rbp, %rsi
movq %rsp, %rdx
callq 0x62424
movl $0x39, %edx
leaq 0x14bdd(%rip), %rsi     # 0x76c4c
testb %al, %al
je 0x620b4
movq %rbx, %rdi
leaq 0x40(%rsp), %rsi
callq 0x62594
movl $0x3d, %edx
leaq 0x14bfa(%rip), %rsi     # 0x76c86
testb %al, %al
je 0x620b4
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 0x634d2
jmp 0x620f4
movq 0x2df25(%rip), %rdi     # 0x8ffe0
callq 0x276c0
movq 0x2df19(%rip), %rcx     # 0x8ffe0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x2defb(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
xorl %ebp, %ebp
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x62107
callq 0x27560
testb %bpl, %bpl
movq %r15, %rbp
jne 0x6201e
jmp 0x621a6
cmpq $0x0, 0x8(%r14)
je 0x6213b
movq (%r14), %rdi
leaq 0x14b9b(%rip), %rsi     # 0x76cc4
callq 0x27670
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x6215c
movb $0x1, 0x19(%rbx)
movq %rbx, %rdi
callq 0x62776
testb %al, %al
je 0x621a6
movb $0x1, 0x18(%rbx)
movq 0x10(%rbx), %rdi
callq 0x275b0
movb $0x0, 0x19(%rbx)
movb $0x1, %bl
jmp 0x621a8
movq 0x2de7d(%rip), %rdi     # 0x8ffe0
leaq 0x14b5d(%rip), %rsi     # 0x76cc7
movl $0x1d, %edx
callq 0x276c0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x2de5e(%rip), %rdi     # 0x8ffe0
callq 0x276c0
movq %rax, %rbx
leaq 0x10cdf(%rip), %rsi     # 0x72e70
movl $0xd, %edx
movq %rax, %rdi
callq 0x276c0
movq %rbx, %rdi
callq 0x27310
xorl %ebx, %ebx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x621bc
callq 0x27560
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x61e94
callq 0x27560
jmp 0x61e94
jmp 0x621e0
movq %rax, %rbx
jmp 0x6220f
movq %rax, %rbx
jmp 0x621fb
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x621fb
callq 0x27560
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x6220f
callq 0x27560
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x62223
callq 0x27560
movq %rbx, %rdi
callq 0x27b20
nop