// File: /workspace/github_cpp_makefile_star2_O1/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 0x434a6
callq 0x27b00
testb %al, %al
je 0x434a6
movq 0x8(%rbx), %rdi
callq 0x28a70
testb %al, %al
je 0x43529
movq 0x8(%rbx), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x27dc0
leaq 0x10(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x27d67(%rip), %rax     # 0x6af20
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0x267c0
leaq 0x60(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movq %rsp, %rdx
callq 0x29130
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x431ee
callq 0x27560
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x43204
callq 0x27560
leaq 0x60(%rsp), %rsi
movq %rbx, %rdi
movl $0x3, %edx
callq 0x26d80
movq 0x8(%rbx), %rdi
callq 0x27930
movl %eax, %ebp
leaq 0x9c(%rsp), %rsi
movl %eax, (%rsi)
movq %rbx, %rdi
callq 0x28cd0
movq %rbx, %rdi
callq 0x28080
movq 0x8(%rbx), %rdi
callq 0x26a70
movq %rax, 0x28(%rsp)
movl %ebp, 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 0x48(%rsp), %r15
xorl %ebp, %ebp
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
leaq 0x40(%rsp), %r12
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x28(%rsp), %r14
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x38(%rsp)
movq 0x8(%rbx), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x26ea0
testb %al, %al
je 0x435b2
movq 0x38(%rsp), %rax
cmpl %eax, 0x148(%rsp)
je 0x4336e
testl %ebp, %ebp
je 0x4331e
movl %r13d, 0x80(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x90(%rsp)
movq 0x10(%rsp), %rsi
cmpq 0x18(%rsp), %rsi
je 0x4342b
movq 0x90(%rsp), %rax
movq %rax, 0x10(%rsi)
movups 0x80(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x10(%rsp)
jmp 0x4343d
cmpl $0x0, 0x148(%rsp)
jle 0x43362
xorl %ebp, %ebp
movl %ebp, 0x40(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x26400
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x43357
callq 0x27560
incl %ebp
cmpl 0x148(%rsp), %ebp
jl 0x4332a
movl 0x148(%rsp), %eax
movl %eax, (%rsp)
xorl %ebp, %ebp
testl %ebp, %ebp
jne 0x433a2
movl 0x148(%rsp), %eax
movq %rax, 0x38(%rsp)
movl 0x14c(%rsp), %eax
movl %eax, 0x24(%rsp)
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x274a0
movl %eax, %r13d
movq %r14, 0x28(%rsp)
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x274a0
incl %ebp
cmpl %r13d, %eax
cmovgl %eax, %r13d
cmpl 0x50(%rbx), %ebp
jne 0x4341a
movl %r13d, 0x40(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x48(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0x10(%rsp), %rsi
cmpq 0x18(%rsp), %rsi
je 0x433fd
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsi)
movups 0x40(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x10(%rsp)
jmp 0x4340a
leaq 0x8(%rsp), %rdi
movq %r12, %rdx
callq 0x26100
movq 0x8(%rbx), %rdi
callq 0x26a70
movq %rax, 0x28(%rsp)
xorl %ebp, %ebp
movq 0x8(%rbx), %rdi
callq 0x26a70
movq %rax, %r14
jmp 0x4329d
leaq 0x8(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0x26100
movq %rbx, %rdi
movq %rsp, %rsi
callq 0x26400
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rax
cmpq %rax, 0x10(%rsp)
je 0x43460
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rax
leal 0x1(%rax), %ebp
cmpl 0x148(%rsp), %ebp
jge 0x43362
movl %ebp, 0x40(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x26400
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x434a2
callq 0x27560
incl %ebp
jmp 0x43468
leaq 0xb0(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x131c7(%rip), %rsi     # 0x56680
leaq 0x131d5(%rip), %rdx     # 0x56695
leaq 0xa0(%rsp), %rdi
callq 0x26750
leaq 0x70(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x12a87(%rip), %rsi     # 0x55f65
leaq 0x12aaa(%rip), %rdx     # 0x55f8f
leaq 0x60(%rsp), %rdi
callq 0x26750
leaq 0xa0(%rsp), %rsi
leaq 0x60(%rsp), %rdx
movq %rbx, %rdi
callq 0x26370
movq 0x60(%rsp), %rdi
cmpq %r12, %rdi
je 0x43513
callq 0x27560
movq 0xa0(%rsp), %rdi
cmpq %r15, %rdi
jne 0x43757
jmp 0x4375c
movq 0x8(%rbx), %rsi
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
callq 0x28e00
leaq 0x123f0(%rip), %rsi     # 0x55934
leaq 0x60(%rsp), %rdi
movq %r14, %rdx
callq 0x273f0
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1311f(%rip), %rsi     # 0x56680
leaq 0x1312d(%rip), %rdx     # 0x56695
movq %rsp, %rdi
callq 0x26750
movq %rsp, %rsi
leaq 0x60(%rsp), %rdx
movq %rbx, %rdi
callq 0x26370
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x4358e
callq 0x27560
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x435a1
callq 0x27560
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
jmp 0x43752
movq 0x38(%rsp), %r12
testl %r12d, %r12d
js 0x4368b
movl %r13d, 0x80(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x90(%rsp)
movq 0x10(%rsp), %rsi
cmpq 0x18(%rsp), %rsi
je 0x4360f
movq 0x90(%rsp), %rax
movq %rax, 0x10(%rsi)
movups 0x80(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x10(%rsp)
movl 0x34(%rsp), %ebp
jmp 0x43625
leaq 0x80(%rsp), %rdx
leaq 0x8(%rsp), %rdi
callq 0x26100
movl 0x34(%rsp), %ebp
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x26400
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rax
cmpq %rax, 0x10(%rsp)
je 0x43648
movq %rax, 0x10(%rsp)
incl %r12d
cmpl %ebp, %r12d
jge 0x4368b
leaq 0x40(%rsp), %r14
movl %r12d, 0x40(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x26400
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x43683
callq 0x27560
incl %r12d
cmpl %r12d, %ebp
jne 0x43655
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x4369a
callq 0x27560
leaq 0xa0(%rsp), %rdi
callq 0x28600
leaq 0x10(%rsp), %r12
leaq 0x70(%rsp), %r15
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0x436bf
callq 0x27560
movq 0x8(%rbx), %rdi
callq 0x28a70
movl %eax, %ecx
movb $0x1, %al
testb %cl, %cl
jne 0x4375e
movq 0x8(%rbx), %rsi
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
callq 0x28e00
leaq 0x12245(%rip), %rsi     # 0x55934
leaq 0x60(%rsp), %rdi
movq %r14, %rdx
callq 0x273f0
movq %rsp, %rdi
movq %r12, (%rdi)
leaq 0x12f77(%rip), %rsi     # 0x56680
leaq 0x12f85(%rip), %rdx     # 0x56695
callq 0x26750
movq %rsp, %rsi
leaq 0x60(%rsp), %rdx
movq %rbx, %rdi
callq 0x26370
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x43733
callq 0x27560
movq 0x60(%rsp), %rdi
cmpq %r15, %rdi
je 0x43742
callq 0x27560
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x4375c
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 %r12, %rdi
je 0x43786
callq 0x27560
jmp 0x43786
movq %rax, %r14
movq 0x60(%rsp), %rdi
cmpq %r15, %rdi
je 0x4379a
callq 0x27560
jmp 0x4379a
movq %rax, %r14
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
jmp 0x4381a
jmp 0x43874
jmp 0x4382e
jmp 0x4382e
jmp 0x4382e
movq %rax, %r14
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x437e3
callq 0x27560
jmp 0x437e3
movq %rdx, %r15
movq %rax, %r14
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x437fe
callq 0x27560
jmp 0x437fe
movq %rax, %r14
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4380e
callq 0x27560
jmp 0x4380e
movq %rdx, %r15
movq %rax, %r14
leaq 0xb0(%rsp), %rax
jmp 0x438b4
movq %rax, %r14
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x4385a
jmp 0x43864
movq %rdx, %r15
movq %rax, %r14
jmp 0x438c2
jmp 0x4382e
movq %rdx, %r15
movq %rax, %r14
jmp 0x438af
movq %rax, %r14
movq 0x60(%rsp), %rdi
cmpq %r12, %rdi
je 0x4384d
callq 0x27560
jmp 0x4384d
movq %rax, %r14
movq 0xa0(%rsp), %rdi
cmpq %r15, %rdi
je 0x43864
callq 0x27560
jmp 0x43864
movq %rax, %r14
movq %r14, %rdi
callq 0x28c20
jmp 0x4387e
jmp 0x43874
jmp 0x43874
jmp 0x43874
movq %rdx, %r15
movq %rax, %r14
jmp 0x43893
jmp 0x4387e
movq %rdx, %r15
movq %rax, %r14
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x43893
callq 0x27560
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x438a2
callq 0x27560
leaq 0xa0(%rsp), %rdi
callq 0x28600
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x438c2
callq 0x27560
cmpl $0x1, %r15d
jne 0x43864
movq %r14, %rdi
callq 0x267d0
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
addq $0x10, %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x28000
callq 0x287e0
jmp 0x4375c
movq %rax, %r14
callq 0x287e0
jmp 0x43864
movq %rax, %rdi
callq 0x2f0bd
nop