// File: /workspace/github_cpp_makefile_star2_O2/zeeev[P]DASVC/bamtools/src/toolkit/bamtools_sort.cpp
// Function: BamTools::SortTool::SortToolPrivate::MergeSortedRuns()
bool SortTool::SortToolPrivate::MergeSortedRuns(void) {
  
    // open up multi reader for all of our temp files
    // this might get broken up if we do a multi-pass system later ??
    BamMultiReader multiReader;
    if ( !multiReader.Open(m_tempFilenames) ) {
        cerr << "bamtools sort ERROR: could not open BamMultiReader for merging temp files... Aborting."
             << endl;
        return false;
    }

    // open writer for our completely sorted output BAM file
    BamWriter mergedWriter;
    if ( !mergedWriter.Open(m_settings->OutputBamFilename, m_headerText, m_references) ) {
        cerr << "bamtools sort ERROR: could not open " << m_settings->OutputBamFilename
             << " for writing... Aborting." << endl;
        multiReader.Close();
        return false;
    }
    
    // while data available in temp files
    BamAlignment al;
    while ( multiReader.GetNextAlignmentCore(al) )
        mergedWriter.SaveAlignment(al);
  
    // close files
    multiReader.Close();
    mergedWriter.Close();
    
    // delete all temp files
    vector<string>::const_iterator tempIter = m_tempFilenames.begin();
    vector<string>::const_iterator tempEnd  = m_tempFilenames.end();
    for ( ; tempIter != tempEnd; ++tempIter ) {
        const string& tempFilename = (*tempIter);
        remove(tempFilename.c_str());
    }
  
    // return success
    return true;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x170, %rsp            # imm = 0x170
movq %rdi, %r14
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x35b70
leaq 0x68(%r14), %rsi
movq %rbx, %rdi
callq 0x353c0
testb %al, %al
je 0x54f28
leaq 0x8(%rsp), %rdi
callq 0x35350
movq (%r14), %rsi
addq $0x28, %rsi
leaq 0x30(%r14), %rdx
leaq 0x50(%r14), %rcx
leaq 0x8(%rsp), %rdi
callq 0x359d0
movl %eax, %ebx
testb %al, %al
je 0x54f4a
leaq 0x18(%rsp), %rdi
callq 0x35200
leaq 0x10(%rsp), %r15
leaq 0x18(%rsp), %r12
leaq 0x8(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x35140
testb %al, %al
je 0x54f8f
movq %r13, %rdi
movq %r12, %rsi
callq 0x35c00
jmp 0x54f0c
movq 0x400b1(%rip), %rdi     # 0x94fe0
leaq 0x1fbdf(%rip), %rsi     # 0x74b15
callq 0x355b0
movq %rax, %rdi
callq 0x35300
xorl %ebx, %ebx
jmp 0x54fd2
movq 0x4008f(%rip), %rdi     # 0x94fe0
leaq 0x1fb98(%rip), %rsi     # 0x74af0
callq 0x355b0
movq (%r14), %rsi
addq $0x28, %rsi
movq %rax, %rdi
callq 0x35510
leaq 0x1dd90(%rip), %rsi     # 0x72d03
movq %rax, %rdi
callq 0x355b0
movq %rax, %rdi
callq 0x35300
leaq 0x10(%rsp), %rdi
callq 0x35190
jmp 0x54fc8
leaq 0x10(%rsp), %rdi
callq 0x35190
leaq 0x8(%rsp), %rdi
callq 0x35360
movq 0x68(%r14), %r15
movq 0x70(%r14), %r14
cmpq %r14, %r15
je 0x54fbe
movq (%r15), %rdi
callq 0x35b90
addq $0x20, %r15
jmp 0x54fab
leaq 0x18(%rsp), %rdi
callq 0x35940
leaq 0x8(%rsp), %rdi
callq 0x35420
leaq 0x10(%rsp), %rdi
callq 0x35110
movl %ebx, %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x54fff
jmp 0x54ff5
jmp 0x54ffa
movq %rax, %rbx
jmp 0x5500c
movq %rax, %rbx
jmp 0x55016
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x35940
leaq 0x8(%rsp), %rdi
callq 0x35420
leaq 0x10(%rsp), %rdi
callq 0x35110
movq %rbx, %rdi
callq 0x35b50
movq %rax, %rdi
callq 0x3a198