// File: /workspace/github_cpp_makefile_star2_O3/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 0x27b30
leaq 0x68(%r14), %rsi
movq %rbx, %rdi
callq 0x273c0
testb %al, %al
je 0x4e54a
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 0x279a0
movl %eax, %ebx
testb %al, %al
je 0x4e597
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 0x4e610
movq %r13, %rdi
movq %r12, %rsi
callq 0x27bd0
jmp 0x4e52a
movq 0x40a8f(%rip), %rbx     # 0x8efe0
leaq 0x265ec(%rip), %rsi     # 0x74b44
movl $0x56, %edx
movq %rbx, %rdi
callq 0x276b0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x40a5d(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
xorl %ebx, %ebx
jmp 0x4e653
movq 0x40a42(%rip), %rdi     # 0x8efe0
leaq 0x2657a(%rip), %rsi     # 0x74b1f
movl $0x24, %edx
callq 0x276b0
movq (%r14), %rax
movq 0x28(%rax), %rsi
movq 0x30(%rax), %rdx
movq 0x40a1f(%rip), %rdi     # 0x8efe0
callq 0x276b0
movq %rax, %r14
leaq 0x247bb(%rip), %rsi     # 0x72d8b
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
leaq 0x10(%rsp), %rdi
callq 0x27190
jmp 0x4e649
leaq 0x10(%rsp), %rdi
callq 0x27190
leaq 0x8(%rsp), %rdi
callq 0x27370
movq 0x68(%r14), %r15
movq 0x70(%r14), %r14
cmpq %r14, %r15
je 0x4e63f
movq (%r15), %rdi
callq 0x27b50
addq $0x20, %r15
jmp 0x4e62c
leaq 0x18(%rsp), %rdi
callq 0x27920
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 0x4e680
jmp 0x4e676
jmp 0x4e67b
movq %rax, %rbx
jmp 0x4e68d
movq %rax, %rbx
jmp 0x4e697
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x27920
leaq 0x8(%rsp), %rdi
callq 0x27430
leaq 0x10(%rsp), %rdi
callq 0x27110
movq %rbx, %rdi
callq 0x27b10
movq %rax, %rdi
callq 0x2e1cd
nop