// File: /workspace/github_cpp_makefile_star2_O2/zeeev[P]DASVC/bamtools/src/api/internal/index/BamToolsIndex_p.cpp
// Function: BamTools::Internal::BamToolsIndex::Create()
bool BamToolsIndex::Create(void) {

    // skip if BamReader is invalid or not open
    if ( m_reader == 0 || !m_reader->IsOpen() ) {
        SetErrorString("BamToolsIndex::Create", "could not create index: reader is not open");
        return false;
    }

    // rewind BamReader
    if ( !m_reader->Rewind() ) {
        const string readerError = m_reader->GetErrorString();
        const string message = "could not create index: \n\t" + readerError;
        SetErrorString("BamToolsIndex::Create", message);
        return false;
    }

    try {
        // open new index file (read & write)
        const string indexFilename = m_reader->Filename() + Extension();
        OpenFile(indexFilename, IBamIODevice::ReadWrite);

        // initialize BtiFileSummary with number of references
        const int& numReferences = m_reader->GetReferenceCount();
        InitializeFileSummary(numReferences);

        // intialize output file header
        WriteHeader();

        // index building markers
        uint32_t currentBlockCount      = 0;
        int64_t currentAlignmentOffset  = m_reader->Tell();
        int32_t blockRefId              = -1;
        int32_t blockMaxEndPosition     = -1;
        int64_t blockStartOffset        = currentAlignmentOffset;
        int32_t blockStartPosition      = -1;

        // plow through alignments, storing index entries
        BamAlignment al;
        BtiReferenceEntry refEntry;
        while ( m_reader->LoadNextAlignment(al) ) {

            // if moved to new reference
            if ( al.RefID != blockRefId ) {

                // if first pass, check:
                if ( currentBlockCount == 0 ) {

                    // write any empty references up to (but not including) al.RefID
                    for ( int i = 0; i < al.RefID; ++i )
                        WriteReferenceEntry( BtiReferenceEntry(i) );
                }

                // not first pass:
                else {

                    // store previous BTI block data in reference entry
                    const BtiBlock block(blockMaxEndPosition, blockStartOffset, blockStartPosition);
                    refEntry.Blocks.push_back(block);

                    // write reference entry, then clear
                    WriteReferenceEntry(refEntry);
                    ClearReferenceEntry(refEntry);

                    // write any empty references between (but not including)
                    // the last blockRefID and current al.RefID
                    for ( int i = blockRefId+1; i < al.RefID; ++i )
                        WriteReferenceEntry( BtiReferenceEntry(i) );

                    // reset block count
                    currentBlockCount = 0;
                }

                // set ID for new reference entry
                refEntry.ID = al.RefID;
            }

            // if beginning of block, update counters
            if ( currentBlockCount == 0 ) {
                blockRefId          = al.RefID;
                blockStartOffset    = currentAlignmentOffset;
                blockStartPosition  = al.Position;
                blockMaxEndPosition = al.GetEndPosition();
            }

            // increment block counter
            ++currentBlockCount;

            // check end position
            const int32_t alignmentEndPosition = al.GetEndPosition();
            if ( alignmentEndPosition > blockMaxEndPosition )
                blockMaxEndPosition = alignmentEndPosition;

            // if block is full, get offset for next block, reset currentBlockCount
            if ( currentBlockCount == m_blockSize ) {

                // store previous block data in reference entry
                const BtiBlock block(blockMaxEndPosition, blockStartOffset, blockStartPosition);
                refEntry.Blocks.push_back(block);

                // update markers
                blockStartOffset  = m_reader->Tell();
                currentBlockCount = 0;
            }

            // not the best name, but for the next iteration, this value will be the offset of the
            // *current* alignment. this is necessary because we won't know if this next alignment
            // is on a new reference until we actually read it
            currentAlignmentOffset = m_reader->Tell();
        }

        // after finishing alignments, if any data was read, check:
        if ( blockRefId >= 0 ) {

            // store last BTI block data in reference entry
            const BtiBlock block(blockMaxEndPosition, blockStartOffset, blockStartPosition);
            refEntry.Blocks.push_back(block);

            // write last reference entry, then clear
            WriteReferenceEntry(refEntry);
            ClearReferenceEntry(refEntry);

            // then write any empty references remaining at end of file
            for ( int i = blockRefId+1; i < numReferences; ++i )
                WriteReferenceEntry( BtiReferenceEntry(i) );
        }

    } catch ( BamException& e ) {
        m_errorString = e.what();
        return false;
    }

    // rewind BamReader
    if ( !m_reader->Rewind() ) {
        const string readerError = m_reader->GetErrorString();
        const string message = "could not create index: \n\t" + readerError;
        SetErrorString("BamToolsIndex::Create", message);
        return false;
    }

    // return success
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rdi, %rbx
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
je 0x4b73c
callq 0x33780
testb %al, %al
je 0x4b73c
movq 0x8(%rbx), %rdi
callq 0x34cd0
testb %al, %al
je 0x4b79c
movq 0x8(%rbx), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x33b80
leaq 0x8(%rsp), %rdi
callq 0x346c0
leaq 0x30(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x355e0
leaq 0x8(%rsp), %rdi
callq 0x33a00
leaq 0xa0(%rsp), %rdi
callq 0x33a00
leaq 0x30(%rsp), %rsi
pushq $0x3
popq %rdx
movq %rbx, %rdi
callq 0x323c0
movq 0x8(%rbx), %rdi
callq 0x33470
movl %eax, %ebp
leaq 0x84(%rsp), %rsi
movl %eax, (%rsi)
movq %rbx, %rdi
callq 0x34fa0
movq %rbx, %rdi
callq 0x33f60
movq 0x8(%rbx), %rdi
callq 0x31f40
movq %rax, 0x28(%rsp)
movl %ebp, 0x54(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x318c0
leaq 0x10(%rsp), %rax
orl $-0x1, -0x8(%rax)
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
leaq 0x68(%rsp), %r12
xorl %r14d, %r14d
pushq $-0x1
popq %rbp
leaq 0x60(%rsp), %r13
movl %ebp, %r15d
movq 0x28(%rsp), %rax
movq %rax, 0x58(%rsp)
movl %ebp, 0x4(%rsp)
movq 0x8(%rbx), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x32570
testb %al, %al
je 0x4b805
movl 0x148(%rsp), %eax
cmpl %ebp, %eax
jne 0x4b5d9
testl %r14d, %r14d
jne 0x4b6d3
jmp 0x4b6a7
testl %r14d, %r14d
je 0x4b66b
movl %r15d, 0x88(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x90(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0x98(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x330e0
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
callq 0x315b0
orl $-0x1, 0x8(%rsp)
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x4b637
movq %rax, 0x18(%rsp)
incl %ebp
movl 0x148(%rsp), %eax
cmpl %eax, %ebp
jge 0x4b6a1
movl %ebp, 0x60(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
andq $0x0, 0x10(%r12)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x315b0
movq %r12, %rdi
callq 0x33fb0
jmp 0x4b637
xorl %ebp, %ebp
cmpl %eax, %ebp
jge 0x4b6a1
movl %ebp, 0x60(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
andq $0x0, 0x10(%r12)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x315b0
movq %r12, %rdi
callq 0x33fb0
incl %ebp
movl 0x148(%rsp), %eax
jmp 0x4b66d
movl %eax, 0x8(%rsp)
movl %eax, %ebp
xorl %r14d, %r14d
movl 0x14c(%rsp), %eax
movl %eax, 0x4(%rsp)
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x32eb0
movl %eax, %r15d
movq 0x58(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x32eb0
incl %r14d
cmpl %r15d, %eax
cmovgl %eax, %r15d
cmpl 0x50(%rbx), %r14d
jne 0x4b729
movl %r15d, 0x60(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x68(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0x70(%rsp)
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x330e0
movq 0x8(%rbx), %rdi
callq 0x31f40
movq %rax, 0x28(%rsp)
xorl %r14d, %r14d
movq 0x8(%rbx), %rdi
callq 0x31f40
movq %rax, 0x58(%rsp)
jmp 0x4b5a7
leaq 0x10f2e(%rip), %rsi     # 0x5c671
leaq 0xa0(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x31b30
leaq 0x107de(%rip), %rsi     # 0x5bf3a
leaq 0x30(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x31b30
leaq 0xa0(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
callq 0x31500
leaq 0x30(%rsp), %rdi
callq 0x33a00
leaq 0xa0(%rsp), %rdi
callq 0x33a00
jmp 0x4b932
movq 0x8(%rbx), %rsi
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
callq 0x35130
leaq 0x101c2(%rip), %rsi     # 0x5b979
leaq 0x30(%rsp), %rdi
movq %r14, %rdx
callq 0x32d90
leaq 0x10ea6(%rip), %rsi     # 0x5c671
leaq 0x8(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x31b30
leaq 0x8(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
callq 0x31500
leaq 0x8(%rsp), %rdi
callq 0x33a00
leaq 0x30(%rsp), %rdi
callq 0x33a00
jmp 0x4b925
testl %ebp, %ebp
js 0x4b88f
leaq 0x88(%rsp), %rsi
movl %r15d, (%rsi)
movq 0x28(%rsp), %rax
movq %rax, 0x8(%rsi)
movl 0x4(%rsp), %eax
movl %eax, 0x10(%rsi)
leaq 0x10(%rsp), %rdi
callq 0x330e0
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x315b0
orl $-0x1, 0x8(%rsp)
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
movl 0x54(%rsp), %r15d
je 0x4b85a
movq %rax, 0x18(%rsp)
incl %ebp
leaq 0x60(%rsp), %r14
cmpl %r15d, %ebp
jge 0x4b88f
movl %ebp, 0x60(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
andq $0x0, 0x10(%r12)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x315b0
movq %r12, %rdi
callq 0x33fb0
incl %ebp
jmp 0x4b861
leaq 0x10(%rsp), %rdi
callq 0x33fb0
leaq 0xa0(%rsp), %rdi
callq 0x346b0
leaq 0x30(%rsp), %rdi
callq 0x33a00
movq 0x8(%rbx), %rdi
callq 0x34cd0
movl %eax, %ecx
movb $0x1, %al
testb %cl, %cl
jne 0x4b934
movq 0x8(%rbx), %rsi
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
callq 0x35130
leaq 0x1009d(%rip), %rsi     # 0x5b979
leaq 0x30(%rsp), %rdi
movq %r14, %rdx
callq 0x32d90
leaq 0x10d81(%rip), %rsi     # 0x5c671
leaq 0x8(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x31b30
leaq 0x8(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
callq 0x31500
leaq 0x8(%rsp), %rdi
callq 0x33a00
leaq 0x30(%rsp), %rdi
callq 0x33a00
leaq 0xa0(%rsp), %rdi
callq 0x33a00
xorl %eax, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
leaq 0x8(%rsp), %rdi
callq 0x33a00
jmp 0x4b95d
movq %rax, %r15
jmp 0x4b967
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x33a00
leaq 0xa0(%rsp), %rdi
callq 0x33a00
jmp 0x4ba27
jmp 0x4b9e0
movq %rdx, %r13
movq %rax, %r15
jmp 0x4ba7b
jmp 0x4b9b4
jmp 0x4b9b4
jmp 0x4b9b4
movq %rax, %r15
leaq 0x8(%rsp), %rdi
callq 0x33a00
jmp 0x4b9c7
movq %rdx, %r13
movq %rax, %r15
leaq 0x8(%rsp), %rdi
callq 0x33a00
jmp 0x4b9f1
jmp 0x4ba5b
jmp 0x4b9b4
movq %rdx, %r13
movq %rax, %r15
jmp 0x4ba88
movq %rax, %r15
jmp 0x4b9d1
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x33a00
leaq 0xa0(%rsp), %rdi
callq 0x33a00
jmp 0x4ba27
movq %rdx, %r13
movq %rax, %r15
jmp 0x4ba92
movq %rdx, %r13
movq %rax, %r15
leaq 0xa0(%rsp), %rdi
callq 0x33a00
jmp 0x4ba92
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x33a00
jmp 0x4ba1a
movq %rax, %r15
jmp 0x4ba27
movq %rax, %r15
leaq 0xa0(%rsp), %rdi
callq 0x33a00
movq %r15, %rdi
callq 0x34ed0
jmp 0x4ba5b
movq %rdx, %r13
movq %rax, %r15
movq %r12, %rdi
callq 0x33fb0
jmp 0x4ba71
jmp 0x4ba5b
jmp 0x4ba5b
jmp 0x4ba5b
jmp 0x4ba5b
jmp 0x4ba5b
movq %rdx, %r13
movq %rax, %r15
movq %r12, %rdi
callq 0x33fb0
jmp 0x4ba71
movq %rdx, %r13
movq %rax, %r15
jmp 0x4ba71
movq %rdx, %r13
movq %rax, %r15
movq %r12, %rdi
callq 0x33fb0
leaq 0x10(%rsp), %rdi
callq 0x33fb0
leaq 0xa0(%rsp), %rdi
callq 0x346b0
leaq 0x30(%rsp), %rdi
callq 0x33a00
cmpl $0x1, %r13d
jne 0x4ba27
movq %r15, %rdi
callq 0x31bb0
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
addq $0x10, %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x35550
callq 0x34970
jmp 0x4b932
movq %rax, %r15
callq 0x34970
jmp 0x4ba27
movq %rax, %rdi
callq 0x39b2f
nop