// File: /workspace/github_cpp_makefile_star2_O2/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 0x353f0
movq (%r14), %rsi
addq $0x8, %rsi
movq %rbx, %rdi
callq 0x357b0
movl %eax, %ebx
testb %al, %al
je 0x54b6a
leaq 0x180(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x357e0
leaq 0x180(%rsp), %rdi
callq 0x35740
testb %al, %al
jne 0x54a4d
leaq 0x412d8(%rip), %rsi     # 0x95d18
leaq 0x180(%rsp), %rdi
callq 0x35340
movq (%r14), %rax
leaq 0x416e1(%rip), %rcx     # 0x96138
cmpb $0x0, 0x4(%rax)
leaq 0x416b6(%rip), %rsi     # 0x96118
cmovneq %rcx, %rsi
leaq 0x1a0(%rsp), %rdi
callq 0x35340
leaq 0x28(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x35930
leaq 0x30(%r14), %rdi
leaq 0x28(%rsp), %rsi
callq 0x35340
leaq 0x28(%rsp), %rdi
callq 0x35730
leaq 0x8(%rsp), %rdi
callq 0x35080
leaq 0x50(%r14), %rdi
movq %rax, %rsi
callq 0x3a1a4
leaq 0x28(%rsp), %rdi
callq 0x35200
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movq (%r14), %rax
movl 0x48(%rax), %eax
cvtsi2sd %rax, %xmm0
mulsd 0x1ff20(%rip), %xmm0    # 0x74a00
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x1ff14(%rip), %xmm0    # 0x74a08
cvttsd2si %xmm0, %rsi
andq %rcx, %rsi
orq %rax, %rsi
callq 0x55766
movq (%r14), %rax
cmpb $0x0, 0x4(%rax)
je 0x54ba8
leaq 0x8(%rsp), %r15
leaq 0x28(%rsp), %r12
movl $0x158, %ebp            # imm = 0x158
leaq 0x10(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x359b0
testb %al, %al
je 0x54bfd
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
cqto
idivq %rbp
movq (%r14), %rcx
movl 0x48(%rcx), %ecx
cmpq %rcx, %rax
jb 0x54b5d
movq %r14, %rdi
movq %r13, %rsi
callq 0x54cc8
movq %r13, %rdi
movq %r12, %rsi
callq 0x55800
jmp 0x54b25
movq 0x4046f(%rip), %rdi     # 0x94fe0
leaq 0x1ff78(%rip), %rsi     # 0x74af0
callq 0x355b0
movq (%r14), %rsi
addq $0x8, %rsi
movq %rax, %rdi
callq 0x35510
leaq 0x1dc72(%rip), %rsi     # 0x72805
movq %rax, %rdi
callq 0x355b0
movq %rax, %rdi
callq 0x35300
jmp 0x54c41
leaq 0x8(%rsp), %r15
leaq 0x28(%rsp), %r12
movl $0x158, %ebp            # imm = 0x158
leaq 0x10(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x35a70
testb %al, %al
je 0x54bfd
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
cqto
idivq %rbp
movq (%r14), %rcx
movl 0x48(%rcx), %ecx
cmpq %rcx, %rax
jb 0x54bf0
movq %r14, %rdi
movq %r13, %rsi
callq 0x54cc8
movq %r13, %rdi
movq %r12, %rsi
callq 0x55800
jmp 0x54bbc
movq 0x10(%rsp), %rax
cmpq 0x18(%rsp), %rax
je 0x54c16
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x54cc8
leaq 0x8(%rsp), %rdi
callq 0x350a0
leaq 0x10(%rsp), %rdi
callq 0x55830
leaq 0x28(%rsp), %rdi
callq 0x35940
leaq 0x180(%rsp), %rdi
callq 0x355a0
leaq 0x8(%rsp), %rdi
callq 0x35620
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 0x54c96
jmp 0x54c89
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x35730
jmp 0x54ca0
jmp 0x54c89
jmp 0x54c7d
jmp 0x54c82
jmp 0x54c7d
movq %rax, %rbx
jmp 0x54ca0
movq %rax, %rbx
jmp 0x54cad
jmp 0x54c89
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x55830
leaq 0x28(%rsp), %rdi
callq 0x35940
leaq 0x180(%rsp), %rdi
callq 0x355a0
leaq 0x8(%rsp), %rdi
callq 0x35620
movq %rbx, %rdi
callq 0x35b50
movq %rax, %rdi
callq 0x3a198
nop