// File: /workspace/github_cpp_makefile_star2_O1/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 0x277a0
movl %eax, %ebx
testb %al, %al
je 0x4e11b
leaq 0x180(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x277d0
leaq 0x180(%rsp), %rdi
callq 0x27730
testb %al, %al
jne 0x4dfe7
leaq 0x42d06(%rip), %rsi     # 0x90ce0
leaq 0x180(%rsp), %rdi
callq 0x27350
movq (%r14), %rax
leaq 0x4310f(%rip), %rcx     # 0x91100
cmpb $0x0, 0x4(%rax)
leaq 0x430e4(%rip), %rsi     # 0x910e0
cmovneq %rcx, %rsi
leaq 0x1a0(%rsp), %rdi
callq 0x27350
leaq 0x28(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x27920
leaq 0x30(%r14), %rdi
leaq 0x28(%rsp), %rsi
callq 0x27350
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4e040
callq 0x27560
leaq 0x8(%rsp), %rdi
callq 0x27090
leaq 0x50(%r14), %rdi
movq %rax, %rsi
callq 0x2e1f8
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 0x279e7(%rip), %xmm0    # 0x75a70
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x279db(%rip), %xmm0    # 0x75a78
cvttsd2si %xmm0, %rsi
andq %rcx, %rsi
orq %rax, %rsi
callq 0x4f12a
movq (%r14), %rax
cmpb $0x0, 0x4(%rax)
je 0x4e18d
leaq 0x8(%rsp), %r15
leaq 0x28(%rsp), %r12
movabsq $-0x7d05f417d05f417d, %rbp # imm = 0x82FA0BE82FA0BE83
leaq 0x10(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x27990
testb %al, %al
je 0x4e1ea
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 0x4e10e
movq %r14, %rdi
movq %r13, %rsi
callq 0x4e2be
movq %r13, %rdi
movq %r12, %rsi
callq 0x4f1ec
jmp 0x4e0d3
movq 0x41ebe(%rip), %rdi     # 0x8ffe0
leaq 0x279de(%rip), %rsi     # 0x75b07
movl $0x24, %edx
callq 0x276c0
movq (%r14), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq 0x41e9b(%rip), %rdi     # 0x8ffe0
callq 0x276c0
movq %rax, %r14
leaq 0x25721(%rip), %rsi     # 0x73875
movl $0x19, %edx
movq %rax, %rdi
callq 0x276c0
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 0x4e22e
leaq 0x8(%rsp), %r15
leaq 0x28(%rsp), %r12
movabsq $-0x7d05f417d05f417d, %rbp # imm = 0x82FA0BE82FA0BE83
leaq 0x10(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x27a50
testb %al, %al
je 0x4e1ea
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 0x4e1dd
movq %r14, %rdi
movq %r13, %rsi
callq 0x4e2be
movq %r13, %rdi
movq %r12, %rsi
callq 0x4f1ec
jmp 0x4e1a6
movq 0x10(%rsp), %rax
cmpq 0x18(%rsp), %rax
je 0x4e203
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x4e2be
leaq 0x8(%rsp), %rdi
callq 0x270b0
leaq 0x10(%rsp), %rdi
callq 0x4f21c
leaq 0x28(%rsp), %rdi
callq 0x27930
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 0x4e28c
jmp 0x4e27f
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4e296
callq 0x27560
jmp 0x4e296
jmp 0x4e273
jmp 0x4e27f
jmp 0x4e273
jmp 0x4e278
movq %rax, %rbx
jmp 0x4e296
movq %rax, %rbx
jmp 0x4e2a3
jmp 0x4e27f
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x4f21c
leaq 0x28(%rsp), %rdi
callq 0x27930
leaq 0x180(%rsp), %rdi
callq 0x275a0
leaq 0x8(%rsp), %rdi
callq 0x27620
movq %rbx, %rdi
callq 0x27b20
movq %rax, %rdi
callq 0x2e1ed
nop