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

    // skip if BamReader is invalid or not open
    if ( m_reader == 0 || !m_reader->IsOpen() ) {
        SetErrorString("BamStandardIndex::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("BamStandardIndex::Create", message);
        return false;
    }

    try {

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

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

        // initialize output file
        WriteHeader();

        // set up bin, ID, offset, & coordinate markers
        const uint32_t defaultValue = 0xffffffffu;
        uint32_t currentBin    = defaultValue;
        uint32_t lastBin       = defaultValue;
        int32_t  currentRefID  = defaultValue;
        int32_t  lastRefID     = defaultValue;
        uint64_t currentOffset = (uint64_t)m_reader->Tell();
        uint64_t lastOffset    = currentOffset;
        int32_t  lastPosition  = defaultValue;

        // iterate through alignments in BAM file
        BamAlignment al;
        BaiReferenceEntry refEntry;
        while ( m_reader->LoadNextAlignment(al) ) {

            // changed to new reference
            if ( lastRefID != al.RefID ) {

                // if not first reference, save previous reference data
                if ( lastRefID != (int32_t)defaultValue ) {

                    SaveAlignmentChunkToBin(refEntry.Bins, currentBin, currentOffset, lastOffset);
                    WriteReferenceEntry(refEntry);
                    ClearReferenceEntry(refEntry);

                    // write any empty references between (but *NOT* including) lastRefID & al.RefID
                    for ( int i = lastRefID+1; i < al.RefID; ++i ) {
                        BaiReferenceEntry emptyEntry(i);
                        WriteReferenceEntry(emptyEntry);
                    }

                    // update bin markers
                    currentOffset = lastOffset;
                    currentBin    = al.Bin;
                    lastBin       = al.Bin;
                    currentRefID  = al.RefID;
                }

                // otherwise, this is first pass
                // be sure to write any empty references up to (but *NOT* including) current RefID
                else {
                    for ( int i = 0; i < al.RefID; ++i ) {
                        BaiReferenceEntry emptyEntry(i);
                        WriteReferenceEntry(emptyEntry);
                    }
                }

                // update reference markers
                refEntry.ID = al.RefID;
                lastRefID   = al.RefID;
                lastBin     = defaultValue;
            }

            // if lastPosition greater than current alignment position - file not sorted properly
            else if ( lastPosition > al.Position ) {
                stringstream s("");
                s << "BAM file is not properly sorted by coordinate" << endl
                  << "Current alignment position: " << al.Position
                  << " < previous alignment position: " << lastPosition
                  << " on reference ID: " << al.RefID << endl;
                SetErrorString("BamStandardIndex::Create", s.str());
                return false;
            }

            // if alignment's ref ID is valid & its bin is not a 'leaf'
            if ( (al.RefID >= 0) && (al.Bin < 4681) )
                SaveLinearOffsetEntry(refEntry.LinearOffsets, al.Position, al.GetEndPosition(), lastOffset);

            // changed to new BAI bin
            if ( al.Bin != lastBin ) {

                // if not first bin on reference, save previous bin data
                if ( currentBin != defaultValue )
                    SaveAlignmentChunkToBin(refEntry.Bins, currentBin, currentOffset, lastOffset);

                // update markers
                currentOffset = lastOffset;
                currentBin    = al.Bin;
                lastBin       = al.Bin;
                currentRefID  = al.RefID;

                // if invalid RefID, break out
                if ( currentRefID < 0 )
                    break;
            }

            // make sure that current file pointer is beyond lastOffset
            if ( m_reader->Tell() <= (int64_t)lastOffset ) {
                SetErrorString("BamStandardIndex::Create", "calculating offsets failed");
                return false;
            }

            // update lastOffset & lastPosition
            lastOffset   = m_reader->Tell();
            lastPosition = al.Position;
        }

        // after finishing alignments, if any data was read, check:
        if ( lastOffset != currentOffset ) {

            // store last alignment chunk to its bin, then write last reference entry with data
            SaveAlignmentChunkToBin(refEntry.Bins, currentBin, currentOffset, lastOffset);
            WriteReferenceEntry(refEntry);
        }

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

    } 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("BamStandardIndex::Create", message);
        return false;
    }

    // return success
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3d8, %rsp            # imm = 0x3D8
movq %rdi, %rbx
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
je 0x477dd
callq 0x33780
testb %al, %al
je 0x477dd
movq 0x8(%rbx), %rdi
callq 0x34cd0
testb %al, %al
je 0x47849
movq 0x8(%rbx), %rsi
leaq 0xf8(%rsp), %rdi
movq %rbx, 0x10(%rsp)
callq 0x33b80
leaq 0x280(%rsp), %rdi
callq 0x33ea0
leaq 0xb8(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
leaq 0x280(%rsp), %rdx
callq 0x355e0
leaq 0x280(%rsp), %rdi
callq 0x33a00
leaq 0xf8(%rsp), %rdi
callq 0x33a00
leaq 0xb8(%rsp), %rsi
pushq $0x3
popq %rdx
movq %rbx, %rdi
callq 0x349c0
movq 0x8(%rbx), %rdi
callq 0x33470
movl %eax, %ebp
leaq 0xb4(%rsp), %rsi
movl %eax, (%rsi)
movq %rbx, %rdi
callq 0x34900
movq %rbx, %rdi
callq 0x322e0
orl $-0x1, 0xc(%rsp)
movq 0x8(%rbx), %rdi
callq 0x31f40
movq %rax, %r14
movl %ebp, 0x3c(%rsp)
movq %rax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
leaq 0x280(%rsp), %rdi
callq 0x318c0
leaq 0x70(%rsp), %rax
orl $-0x1, -0x10(%rax)
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
leaq 0x108(%rsp), %rbp
pushq $-0x1
popq %rbx
leaq 0xf8(%rsp), %r15
movq %r14, 0x30(%rsp)
movl %ebx, 0x1c(%rsp)
movl %ebx, %r12d
movl %ebx, %r13d
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x280(%rsp), %rsi
callq 0x32570
testb %al, %al
je 0x478cb
movl 0x328(%rsp), %eax
cmpl %eax, %r13d
jne 0x475d7
cmpl 0x32c(%rsp), %ebx
jle 0x4769a
jmp 0x4793d
cmpl $-0x1, %r13d
je 0x47780
leaq 0x68(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x20(%rsp), %r8
callq 0x35050
movq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x31810
leaq 0x60(%rsp), %rsi
callq 0x32200
incl %r13d
movq 0x10(%rsp), %rbx
movl 0x328(%rsp), %eax
cmpl %eax, %r13d
jge 0x47673
movl %r13d, 0xf8(%rsp)
andl $0x0, 0x108(%rsp)
andq $0x0, 0x110(%rsp)
movq %rbp, 0x118(%rsp)
movq %rbp, 0x120(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rbp)
movups %xmm0, 0x20(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x31810
movq %r15, %rdi
callq 0x32270
incl %r13d
jmp 0x4761b
movq %r14, 0x28(%rsp)
movzwl 0x330(%rsp), %ecx
movl %ecx, 0xc(%rsp)
movq %r14, 0x30(%rsp)
movl %eax, %r12d
movl %eax, 0x60(%rsp)
movl %eax, %r13d
pushq $-0x1
popq %rbx
movl %ebx, 0x1c(%rsp)
testl %r13d, %r13d
setns %cl
movzwl 0x330(%rsp), %eax
cmpw $0x1249, %ax            # imm = 0x1249
setb %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x476f3
leaq 0x280(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x32eb0
movl %eax, 0xf8(%rsp)
leaq 0x98(%rsp), %rsi
leaq 0x32c(%rsp), %rdx
movq %r15, %rcx
leaq 0x20(%rsp), %r8
callq 0x34dc0
movzwl 0x330(%rsp), %eax
movzwl %ax, %eax
cmpl %eax, 0x1c(%rsp)
je 0x47747
cmpl $-0x1, 0xc(%rsp)
je 0x47724
leaq 0x68(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x20(%rsp), %r8
callq 0x35050
movzwl 0x330(%rsp), %eax
movq %r14, 0x28(%rsp)
movl %eax, 0xc(%rsp)
movl 0x328(%rsp), %r12d
movq %r14, 0x30(%rsp)
movl %eax, 0x1c(%rsp)
testl %r12d, %r12d
js 0x47a5e
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x31f40
cmpq %r14, %rax
jle 0x478d5
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x31f40
movq %rax, %r14
movq %rax, 0x20(%rsp)
movl 0x32c(%rsp), %ebx
jmp 0x4759b
xorl %ebx, %ebx
cmpl %eax, %ebx
jge 0x4768c
movl %ebx, 0xf8(%rsp)
andl $0x0, 0x108(%rsp)
andq $0x0, 0x110(%rsp)
movq %rbp, 0x118(%rsp)
movq %rbp, 0x120(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rbp)
movups %xmm0, 0x20(%rbp)
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x31810
movq %r15, %rdi
callq 0x32270
incl %ebx
movl 0x328(%rsp), %eax
jmp 0x47782
leaq 0x1473d(%rip), %rsi     # 0x5bf21
leaq 0xf8(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x31b30
leaq 0x1473d(%rip), %rsi     # 0x5bf3a
leaq 0x280(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0x31b30
leaq 0xf8(%rsp), %rsi
leaq 0x280(%rsp), %rdx
movq %rbx, %rdi
callq 0x31500
leaq 0x280(%rsp), %rdi
callq 0x33a00
leaq 0xf8(%rsp), %rdi
callq 0x33a00
jmp 0x47dd5
movq 0x8(%rbx), %rsi
leaq 0xf8(%rsp), %r14
movq %r14, %rdi
callq 0x35130
leaq 0x14115(%rip), %rsi     # 0x5b979
leaq 0x280(%rsp), %rdi
movq %r14, %rdx
callq 0x32d90
leaq 0x146a6(%rip), %rsi     # 0x5bf21
leaq 0x60(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0x31b30
leaq 0x60(%rsp), %rsi
leaq 0x280(%rsp), %rdx
movq %rbx, %rdi
callq 0x31500
leaq 0x60(%rsp), %rdi
callq 0x33a00
leaq 0x280(%rsp), %rdi
callq 0x33a00
leaq 0xf8(%rsp), %rdi
callq 0x33a00
jmp 0x47dd5
movq 0x30(%rsp), %rax
jmp 0x47a61
leaq 0x14645(%rip), %rsi     # 0x5bf21
leaq 0xf8(%rsp), %rdi
leaq 0xd8(%rsp), %rdx
movq 0x10(%rsp), %r15
callq 0x31b30
leaq 0x146e7(%rip), %rsi     # 0x5bfe4
leaq 0x40(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x31b30
leaq 0xf8(%rsp), %rsi
leaq 0x40(%rsp), %rdx
movq %r15, %rdi
callq 0x31500
leaq 0x40(%rsp), %rdi
callq 0x33a00
leaq 0xf8(%rsp), %rdi
callq 0x33a00
jmp 0x47a57
leaq 0x14520(%rip), %rsi     # 0x5be64
leaq 0x40(%rsp), %rdi
leaq 0xd8(%rsp), %rdx
movq 0x10(%rsp), %r15
callq 0x31b30
leaq 0xf8(%rsp), %rdi
leaq 0x40(%rsp), %rsi
pushq $0x18
popq %rdx
callq 0x34880
leaq 0x40(%rsp), %rdi
callq 0x33a00
leaq 0x108(%rsp), %rdi
leaq 0x145dc(%rip), %rsi     # 0x5bf65
callq 0x330f0
movq %rax, %rdi
callq 0x31e60
leaq 0x145f6(%rip), %rsi     # 0x5bf93
movq %rax, %rdi
callq 0x330f0
movl 0x32c(%rsp), %esi
movq %rax, %rdi
callq 0x34d10
leaq 0x145f5(%rip), %rsi     # 0x5bfb0
movq %rax, %rdi
callq 0x330f0
movq %rax, %rdi
movl %ebx, %esi
callq 0x34d10
leaq 0x145fd(%rip), %rsi     # 0x5bfd1
movq %rax, %rdi
callq 0x330f0
movl 0x328(%rsp), %esi
movq %rax, %rdi
callq 0x34d10
movq %rax, %rdi
callq 0x31e60
leaq 0x14527(%rip), %rsi     # 0x5bf21
leaq 0x40(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x31b30
leaq 0x110(%rsp), %rsi
leaq 0xd8(%rsp), %rdi
callq 0x34870
leaq 0x40(%rsp), %rsi
leaq 0xd8(%rsp), %rdx
movq %r15, %rdi
callq 0x31500
leaq 0xd8(%rsp), %rdi
callq 0x33a00
leaq 0x40(%rsp), %rdi
callq 0x33a00
leaq 0xf8(%rsp), %rdi
callq 0x32870
xorl %ebx, %ebx
jmp 0x47af7
movq %r14, %rax
cmpq %rax, %r14
movl 0x3c(%rsp), %ebx
je 0x47a92
leaq 0xc(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x68(%rsp), %rsi
callq 0x35050
leaq 0x60(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x31810
incl %r12d
leaq 0xf8(%rsp), %r14
cmpl %ebx, %r12d
jge 0x47af0
movl %r12d, 0xf8(%rsp)
andl $0x0, 0x108(%rsp)
andq $0x0, 0x110(%rsp)
movq %rbp, 0x118(%rsp)
movq %rbp, 0x120(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rbp)
movups %xmm0, 0x20(%rbp)
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x31810
movq %r14, %rdi
callq 0x32270
incl %r12d
jmp 0x47a9d
movb $0x1, %bl
movq 0x10(%rsp), %r15
leaq 0x60(%rsp), %rdi
callq 0x32270
leaq 0x280(%rsp), %rdi
callq 0x346b0
leaq 0xb8(%rsp), %rdi
callq 0x33a00
testb %bl, %bl
je 0x47dd5
movq 0x8(%r15), %rdi
callq 0x34cd0
movl %eax, %ecx
movb $0x1, %al
testb %cl, %cl
jne 0x47dd7
movq 0x8(%r15), %rsi
leaq 0xf8(%rsp), %r14
movq %r14, %rdi
callq 0x35130
leaq 0x13e26(%rip), %rsi     # 0x5b979
leaq 0x280(%rsp), %rdi
movq %r14, %rdx
callq 0x32d90
leaq 0x143b7(%rip), %rsi     # 0x5bf21
leaq 0x60(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0x31b30
leaq 0x60(%rsp), %rsi
leaq 0x280(%rsp), %rdx
movq %r15, %rdi
callq 0x31500
leaq 0x60(%rsp), %rdi
callq 0x33a00
leaq 0x280(%rsp), %rdi
callq 0x33a00
jmp 0x478b9
jmp 0x47d7e
movq %rdx, %r15
movq %rax, %r14
leaq 0xd8(%rsp), %rdi
callq 0x33a00
jmp 0x47c08
movq %rdx, %r15
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x33a00
jmp 0x47d84
movq %rax, %r14
leaq 0x60(%rsp), %rdi
callq 0x33a00
jmp 0x47c24
movq %rdx, %r15
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x33a00
jmp 0x47c53
jmp 0x47c9c
movq %rdx, %r15
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x33a00
jmp 0x47ca2
jmp 0x47d7e
movq %rax, %r14
jmp 0x47c31
movq %rax, %r14
leaq 0x280(%rsp), %rdi
callq 0x33a00
leaq 0xf8(%rsp), %rdi
callq 0x33a00
jmp 0x47d2f
jmp 0x47d7e
jmp 0x47d7e
movq %rdx, %r15
movq %rax, %r14
leaq 0xf8(%rsp), %rdi
callq 0x33a00
jmp 0x47d84
jmp 0x47ce5
movq %rdx, %r15
movq %rax, %r14
jmp 0x47d8e
jmp 0x47cb6
jmp 0x47cb6
jmp 0x47cb6
movq %rax, %r14
leaq 0x60(%rsp), %rdi
callq 0x33a00
jmp 0x47cc9
movq %rdx, %r15
movq %rax, %r14
leaq 0x280(%rsp), %rdi
callq 0x33a00
jmp 0x47cf6
movq %rdx, %r15
movq %rax, %r14
leaq 0xf8(%rsp), %rdi
callq 0x32870
jmp 0x47d84
jmp 0x47cb6
movq %rdx, %r15
movq %rax, %r14
jmp 0x47d9b
movq %rax, %r14
jmp 0x47cd6
movq %rax, %r14
leaq 0x280(%rsp), %rdi
callq 0x33a00
leaq 0xf8(%rsp), %rdi
callq 0x33a00
jmp 0x47d2f
movq %rdx, %r15
movq %rax, %r14
jmp 0x47da8
movq %rdx, %r15
movq %rax, %r14
leaq 0xf8(%rsp), %rdi
callq 0x33a00
jmp 0x47da8
movq %rax, %r14
leaq 0x280(%rsp), %rdi
callq 0x33a00
jmp 0x47d22
movq %rax, %r14
jmp 0x47d2f
movq %rax, %r14
leaq 0xf8(%rsp), %rdi
callq 0x33a00
movq %r14, %rdi
callq 0x34ed0
jmp 0x47d7e
jmp 0x47d7e
movq %rdx, %r15
movq %rax, %r14
leaq 0xf8(%rsp), %rdi
callq 0x32270
jmp 0x47d84
jmp 0x47d7e
movq %rdx, %r15
movq %rax, %r14
leaq 0xf8(%rsp), %rdi
callq 0x32270
jmp 0x47d84
jmp 0x47d7e
movq %rdx, %r15
movq %rax, %r14
leaq 0xf8(%rsp), %rdi
callq 0x32270
jmp 0x47d84
movq %rdx, %r15
movq %rax, %r14
leaq 0x60(%rsp), %rdi
callq 0x32270
leaq 0x280(%rsp), %rdi
callq 0x346b0
leaq 0xb8(%rsp), %rdi
callq 0x33a00
cmpl $0x1, %r15d
jne 0x47d2f
movq %r14, %rdi
callq 0x31bb0
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x10(%rsp), %rdi
addq $0x10, %rdi
movq %rax, %rsi
callq 0x35550
callq 0x34970
xorl %eax, %eax
addq $0x3d8, %rsp            # imm = 0x3D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
callq 0x34970
jmp 0x47d2f
movq %rax, %rdi
callq 0x39b2f