// File: /workspace/github_cpp_makefile_star2_O3/zeeev[P]DASVC/bamtools/src/toolkit/bamtools_sort.cpp
// Function: BamTools::SortTool::SortToolPrivate::GenerateSortedRuns()
bool SortTool::SortToolPrivate::GenerateSortedRuns(void) {
    
    // open input BAM file
    BamReader reader;
    if ( !reader.Open(m_settings->InputBamFilename) ) {
        cerr << "bamtools sort ERROR: could not open " << m_settings->InputBamFilename
             << " for reading... Aborting." << endl;
        return false;
    }
    
    // get basic data that will be shared by all temp/output files 
    SamHeader header = reader.GetHeader();
    if ( !header.HasVersion() )
        header.Version = Constants::SAM_CURRENT_VERSION;
    header.SortOrder = ( m_settings->IsSortingByName
                       ? Constants::SAM_HD_SORTORDER_QUERYNAME
                       : Constants::SAM_HD_SORTORDER_COORDINATE );
    m_headerText = header.ToString();
    m_references = reader.GetReferenceData();
    
    // set up alignments buffer
    BamAlignment al;
    vector<BamAlignment> buffer;
    buffer.reserve( (size_t)(m_settings->MaxBufferCount*1.1) );
    bool bufferFull = false;

    // if sorting by name, we need to generate full char data
    // so can't use GetNextAlignmentCore()
    if ( m_settings->IsSortingByName ) {

        // iterate through file
        while ( reader.GetNextAlignment(al)) {

            // check buffer's usage
            bufferFull = ( buffer.size() >= m_settings->MaxBufferCount );

            // store alignments until buffer is "full"
            if ( !bufferFull )
                buffer.push_back(al);

            // if buffer is "full"
            else {
                // so create a sorted temp file with current buffer contents
                // then push "al" into fresh buffer
                CreateSortedTempFile(buffer);
                buffer.push_back(al);
            }
        }
    }

    // sorting by position, can take advantage of GNACore() speedup
    else {

        // iterate through file
        while ( reader.GetNextAlignmentCore(al) ) {

            // check buffer's usage
            bufferFull = ( buffer.size() >= m_settings->MaxBufferCount );

            // store alignments until buffer is "full"
            if ( !bufferFull )
                buffer.push_back(al);

            // if buffer is "full"
            else {
                // create a sorted temp file with current buffer contents
                // then push "al" into fresh buffer
                CreateSortedTempFile(buffer);
                buffer.push_back(al);
            }
        }
    }

    // handle any leftover buffer contents
    if ( !buffer.empty() )
        CreateSortedTempFile(buffer);
    
    // close reader & return success
    reader.Close();
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2d8, %rsp            # imm = 0x2D8
movq %rdi, %r14
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x273f0
movq (%r14), %rsi
addq $0x8, %rsi
movq %rbx, %rdi
callq 0x27790
movl %eax, %ebx
testb %al, %al
je 0x4dfa7
leaq 0x180(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x277c0
leaq 0x180(%rsp), %rdi
callq 0x27720
testb %al, %al
jne 0x4de73
leaq 0x41e72(%rip), %rsi     # 0x8fcd8
leaq 0x180(%rsp), %rdi
callq 0x27350
movq (%r14), %rax
leaq 0x4227b(%rip), %rcx     # 0x900f8
cmpb $0x0, 0x4(%rax)
leaq 0x42250(%rip), %rsi     # 0x900d8
cmovneq %rcx, %rsi
leaq 0x1a0(%rsp), %rdi
callq 0x27350
leaq 0x28(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x27910
leaq 0x30(%r14), %rdi
leaq 0x28(%rsp), %rsi
callq 0x27350
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4decc
callq 0x27560
leaq 0x8(%rsp), %rdi
callq 0x27090
leaq 0x50(%r14), %rdi
movq %rax, %rsi
callq 0x2e1d8
leaq 0x28(%rsp), %rdi
callq 0x27220
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq (%r14), %rax
movl 0x48(%rax), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x26b73(%rip), %xmm0    # 0x74a88
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x26b67(%rip), %xmm0    # 0x74a90
cvttsd2si %xmm0, %rsi
andq %rcx, %rsi
orq %rax, %rsi
callq 0x4efb6
movq (%r14), %rax
cmpb $0x0, 0x4(%rax)
je 0x4e019
leaq 0x8(%rsp), %r15
leaq 0x28(%rsp), %r12
movabsq $-0x7d05f417d05f417d, %rbp # imm = 0x82FA0BE82FA0BE83
leaq 0x10(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x27980
testb %al, %al
je 0x4e076
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x3, %rax
imulq %rbp, %rax
movq (%r14), %rcx
movl 0x48(%rcx), %ecx
cmpq %rcx, %rax
jb 0x4df9a
movq %r14, %rdi
movq %r13, %rsi
callq 0x4e14a
movq %r13, %rdi
movq %r12, %rsi
callq 0x4f07e
jmp 0x4df5f
movq 0x41032(%rip), %rdi     # 0x8efe0
leaq 0x26b6a(%rip), %rsi     # 0x74b1f
movl $0x24, %edx
callq 0x276b0
movq (%r14), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq 0x4100f(%rip), %rdi     # 0x8efe0
callq 0x276b0
movq %rax, %r14
leaq 0x248ac(%rip), %rsi     # 0x7288c
movl $0x19, %edx
movq %rax, %rdi
callq 0x276b0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq %r14, %rdi
callq 0x27040
movq %rax, %rdi
callq 0x27410
jmp 0x4e0ba
leaq 0x8(%rsp), %r15
leaq 0x28(%rsp), %r12
movabsq $-0x7d05f417d05f417d, %rbp # imm = 0x82FA0BE82FA0BE83
leaq 0x10(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x27a40
testb %al, %al
je 0x4e076
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x3, %rax
imulq %rbp, %rax
movq (%r14), %rcx
movl 0x48(%rcx), %ecx
cmpq %rcx, %rax
jb 0x4e069
movq %r14, %rdi
movq %r13, %rsi
callq 0x4e14a
movq %r13, %rdi
movq %r12, %rsi
callq 0x4f07e
jmp 0x4e032
movq 0x10(%rsp), %rax
cmpq 0x18(%rsp), %rax
je 0x4e08f
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x4e14a
leaq 0x8(%rsp), %rdi
callq 0x270b0
leaq 0x10(%rsp), %rdi
callq 0x4f0ae
leaq 0x28(%rsp), %rdi
callq 0x27920
leaq 0x180(%rsp), %rdi
callq 0x275a0
leaq 0x8(%rsp), %rdi
callq 0x27620
movl %ebx, %eax
addq $0x2d8, %rsp            # imm = 0x2D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x4e118
jmp 0x4e10b
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4e122
callq 0x27560
jmp 0x4e122
jmp 0x4e0ff
jmp 0x4e10b
jmp 0x4e0ff
jmp 0x4e104
movq %rax, %rbx
jmp 0x4e122
movq %rax, %rbx
jmp 0x4e12f
jmp 0x4e10b
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x4f0ae
leaq 0x28(%rsp), %rdi
callq 0x27920
leaq 0x180(%rsp), %rdi
callq 0x275a0
leaq 0x8(%rsp), %rdi
callq 0x27620
movq %rbx, %rdi
callq 0x27b10
movq %rax, %rdi
callq 0x2e1cd
nop