// File: /workspace/github_cpp_makefile_star2_O1/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 0x27b40
leaq 0x68(%r14), %rsi
movq %rbx, %rdi
callq 0x273c0
testb %al, %al
je 0x4e6be
leaq 0x8(%rsp), %rdi
callq 0x27360
movq (%r14), %rsi
addq $0x28, %rsi
leaq 0x30(%r14), %rdx
leaq 0x50(%r14), %rcx
leaq 0x8(%rsp), %rdi
callq 0x279b0
movl %eax, %ebx
testb %al, %al
je 0x4e70b
leaq 0x18(%rsp), %rdi
callq 0x27220
leaq 0x10(%rsp), %r15
leaq 0x18(%rsp), %r12
leaq 0x8(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x27140
testb %al, %al
je 0x4e784
movq %r13, %rdi
movq %r12, %rsi
callq 0x27be0
jmp 0x4e69e
movq 0x4191b(%rip), %rbx     # 0x8ffe0
leaq 0x27460(%rip), %rsi     # 0x75b2c
movl $0x56, %edx
movq %rbx, %rdi
callq 0x276c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x418e9(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
xorl %ebx, %ebx
jmp 0x4e7c7
movq 0x418ce(%rip), %rdi     # 0x8ffe0
leaq 0x273ee(%rip), %rsi     # 0x75b07
movl $0x24, %edx
callq 0x276c0
movq (%r14), %rax
movq 0x28(%rax), %rsi
movq 0x30(%rax), %rdx
movq 0x418ab(%rip), %rdi     # 0x8ffe0
callq 0x276c0
movq %rax, %r14
leaq 0x2562f(%rip), %rsi     # 0x73d73
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
leaq 0x10(%rsp), %rdi
callq 0x27190
jmp 0x4e7bd
leaq 0x10(%rsp), %rdi
callq 0x27190
leaq 0x8(%rsp), %rdi
callq 0x27370
movq 0x68(%r14), %r15
movq 0x70(%r14), %r14
cmpq %r14, %r15
je 0x4e7b3
movq (%r15), %rdi
callq 0x27b60
addq $0x20, %r15
jmp 0x4e7a0
leaq 0x18(%rsp), %rdi
callq 0x27930
leaq 0x8(%rsp), %rdi
callq 0x27430
leaq 0x10(%rsp), %rdi
callq 0x27110
movl %ebx, %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x4e7f4
jmp 0x4e7ea
jmp 0x4e7ef
movq %rax, %rbx
jmp 0x4e801
movq %rax, %rbx
jmp 0x4e80b
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x27930
leaq 0x8(%rsp), %rdi
callq 0x27430
leaq 0x10(%rsp), %rdi
callq 0x27110
movq %rbx, %rdi
callq 0x27b20
movq %rax, %rdi
callq 0x2e1ed
nop