// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
je 0x43464
callq 0x27b00
testb %al, %al
je 0x43464
movq 0x8(%r13), %rdi
callq 0x28a60
testb %al, %al
je 0x434e6
movq 0x8(%r13), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x27db0
leaq 0x10(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x27d86(%rip), %rax     # 0x6af20
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0x267c0
leaq 0x38(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movq %rsp, %rdx
callq 0x29120
movq (%rsp), %rdi
cmpq %rbx, %rdi
je 0x431cf
callq 0x27560
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x431e5
callq 0x27560
leaq 0x38(%rsp), %rsi
movq %r13, %rdi
movl $0x3, %edx
callq 0x26d80
movq 0x8(%r13), %rdi
callq 0x27930
movl %eax, %ebx
leaq 0x9c(%rsp), %rsi
movl %eax, (%rsi)
movq %r13, %rdi
callq 0x28cc0
movq %r13, %rdi
callq 0x28070
movq 0x8(%r13), %rdi
callq 0x26a70
movq %rax, 0x28(%rsp)
movl %ebx, 0x34(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x265e0
leaq 0x8(%rsp), %rax
movl $0xffffffff, -0x8(%rax) # imm = 0xFFFFFFFF
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
leaq 0x78(%rsp), %r15
xorl %r12d, %r12d
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq 0x28(%rsp), %rax
movq %rax, 0x90(%rsp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq %r13, %rbp
movq 0x8(%r13), %rdi
leaq 0xa0(%rsp), %r13
movq %r13, %rsi
callq 0x26ea0
testb %al, %al
je 0x4356f
movl 0x148(%rsp), %eax
cmpl %ebx, %eax
jne 0x432b5
testl %r12d, %r12d
jne 0x4335f
jmp 0x43335
testl %r12d, %r12d
je 0x432fd
movl %r14d, 0x58(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x60(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x68(%rsp)
movq 0x10(%rsp), %rsi
cmpq 0x18(%rsp), %rsi
je 0x433f6
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsi)
movups 0x58(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x10(%rsp)
jmp 0x43405
testl %eax, %eax
jle 0x43330
xorl %ebx, %ebx
movl %ebx, 0x70(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movq %rbp, %rdi
leaq 0x70(%rsp), %rsi
callq 0x26400
movl 0x148(%rsp), %eax
incl %ebx
cmpl %eax, %ebx
jl 0x43303
movl %eax, (%rsp)
movl %eax, %ebx
xorl %r12d, %r12d
movl 0x14c(%rsp), %eax
movl %eax, 0x24(%rsp)
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x274a0
movl %eax, %r14d
movq 0x90(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbp, %r13
callq 0x274a0
incl %r12d
cmpl %r14d, %eax
cmovgl %eax, %r14d
cmpl 0x50(%r13), %r12d
jne 0x433e0
movl %r14d, 0x70(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x78(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x80(%rsp)
movq 0x10(%rsp), %rsi
cmpq 0x18(%rsp), %rsi
je 0x433c0
movq 0x80(%rsp), %rax
movq %rax, 0x10(%rsi)
movups 0x70(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x10(%rsp)
jmp 0x433cf
leaq 0x8(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x26100
movq 0x8(%r13), %rdi
callq 0x26a70
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
movq 0x8(%r13), %rdi
callq 0x26a70
movq %rax, 0x90(%rsp)
jmp 0x4327d
leaq 0x8(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x26100
movq %rbp, %rdi
movq %rsp, %rsi
callq 0x26400
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rax
cmpq %rax, 0x10(%rsp)
je 0x43428
movq %rax, 0x10(%rsp)
incl %ebx
movl 0x148(%rsp), %eax
cmpl %eax, %ebx
jge 0x43330
movl %ebx, 0x70(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movq %rbp, %rdi
leaq 0x70(%rsp), %rsi
callq 0x26400
movl 0x148(%rsp), %eax
incl %ebx
jmp 0x43431
leaq 0xb0(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x13209(%rip), %rsi     # 0x56680
leaq 0x13217(%rip), %rdx     # 0x56695
leaq 0xa0(%rsp), %rdi
callq 0x26750
leaq 0x48(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x12aca(%rip), %rsi     # 0x55f65
leaq 0x12aed(%rip), %rdx     # 0x55f8f
leaq 0x38(%rsp), %rdi
callq 0x26750
leaq 0xa0(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq %r13, %rdi
callq 0x26370
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x434d0
callq 0x27560
movq 0xa0(%rsp), %rdi
cmpq %rbx, %rdi
jne 0x436ef
jmp 0x436f4
movq 0x8(%r13), %rsi
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
callq 0x28df0
leaq 0x12433(%rip), %rsi     # 0x55934
leaq 0x38(%rsp), %rdi
movq %r14, %rdx
callq 0x273f0
leaq 0x10(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x13162(%rip), %rsi     # 0x56680
leaq 0x13170(%rip), %rdx     # 0x56695
movq %rsp, %rdi
callq 0x26750
movq %rsp, %rsi
leaq 0x38(%rsp), %rdx
movq %r13, %rdi
callq 0x26370
movq (%rsp), %rdi
cmpq %rbx, %rdi
je 0x4354b
callq 0x27560
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4355e
callq 0x27560
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
jmp 0x436ea
testl %ebx, %ebx
movq %rbp, %r13
js 0x43623
movl %r14d, 0x58(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x60(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x68(%rsp)
movq 0x10(%rsp), %rsi
cmpq 0x18(%rsp), %rsi
je 0x435bb
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsi)
movups 0x58(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x10(%rsp)
movl 0x34(%rsp), %r12d
jmp 0x435cf
leaq 0x58(%rsp), %rdx
leaq 0x8(%rsp), %rdi
callq 0x26100
movl 0x34(%rsp), %r12d
movq %rsp, %rsi
movq %r13, %rdi
callq 0x26400
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rax
cmpq %rax, 0x10(%rsp)
je 0x435f2
movq %rax, 0x10(%rsp)
incl %ebx
cmpl %r12d, %ebx
jge 0x43623
leaq 0x70(%rsp), %r14
movl %ebx, 0x70(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movq %r13, %rdi
movq %r14, %rsi
callq 0x26400
incl %ebx
cmpl %ebx, %r12d
jne 0x435fe
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x43632
callq 0x27560
leaq 0xa0(%rsp), %rdi
callq 0x285f0
leaq 0x10(%rsp), %r15
leaq 0x48(%rsp), %rbx
movq -0x10(%rbx), %rdi
cmpq %rbx, %rdi
je 0x43657
callq 0x27560
movq 0x8(%r13), %rdi
callq 0x28a60
movl %eax, %ecx
movb $0x1, %al
testb %cl, %cl
jne 0x436f6
movq 0x8(%r13), %rsi
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
callq 0x28df0
leaq 0x122ad(%rip), %rsi     # 0x55934
leaq 0x38(%rsp), %rdi
movq %r14, %rdx
callq 0x273f0
movq %rsp, %rdi
movq %r15, (%rdi)
leaq 0x12fdf(%rip), %rsi     # 0x56680
leaq 0x12fed(%rip), %rdx     # 0x56695
callq 0x26750
movq %rsp, %rsi
leaq 0x38(%rsp), %rdx
movq %r13, %rdi
callq 0x26370
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x436cb
callq 0x27560
movq 0x38(%rsp), %rdi
cmpq %rbx, %rdi
je 0x436da
callq 0x27560
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x436f4
callq 0x27560
xorl %eax, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x4371e
callq 0x27560
jmp 0x4371e
movq %rax, %r14
movq 0x38(%rsp), %rdi
cmpq %rbx, %rdi
je 0x43732
callq 0x27560
jmp 0x43732
movq %rax, %r14
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
jmp 0x437c1
jmp 0x43825
jmp 0x437d8
jmp 0x437d8
jmp 0x437d8
movq %rax, %r14
movq (%rsp), %rdi
cmpq %rbx, %rdi
je 0x43787
callq 0x27560
jmp 0x43787
movq %r13, %rbp
movq %rdx, %r15
movq %rax, %r14
movq (%rsp), %rdi
cmpq %rbx, %rdi
je 0x437a5
callq 0x27560
jmp 0x437a5
movq %rax, %r14
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x437b5
callq 0x27560
jmp 0x437b5
movq %r13, %rbp
movq %rdx, %r15
movq %rax, %r14
leaq 0xb0(%rsp), %rax
jmp 0x4384c
movq %rax, %r14
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x43807
jmp 0x43811
movq %r13, %rbp
movq %rdx, %r15
movq %rax, %r14
jmp 0x4385a
jmp 0x437d8
movq %r13, %rbp
movq %rdx, %r15
movq %rax, %r14
jmp 0x43847
movq %rax, %r14
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x437fa
callq 0x27560
jmp 0x437fa
movq %rax, %r14
movq 0xa0(%rsp), %rdi
cmpq %rbx, %rdi
je 0x43811
callq 0x27560
jmp 0x43811
movq %rax, %r14
movq %r14, %rdi
callq 0x28c10
jmp 0x43825
jmp 0x43825
jmp 0x43825
jmp 0x43825
jmp 0x43825
jmp 0x43825
movq %rdx, %r15
movq %rax, %r14
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x4383a
callq 0x27560
leaq 0xa0(%rsp), %rdi
callq 0x285f0
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4385a
callq 0x27560
cmpl $0x1, %r15d
jne 0x43811
movq %r14, %rdi
callq 0x267d0
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
addq $0x10, %rbp
movq %rbp, %rdi
movq %rax, %rsi
callq 0x27ff0
callq 0x287d0
jmp 0x436f4
movq %rax, %r14
callq 0x287d0
jmp 0x43811
movq %rax, %rdi
callq 0x2f047
nop