// File: 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:
subq $0x228, %rsp            # imm = 0x228
movq %rdi, 0x218(%rsp)
movq 0x218(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x210(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x86db0
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdi
addq $0x68, %rsi
callq 0x864a0
movb %al, 0x77(%rsp)
jmp 0xdb957
movb 0x77(%rsp), %al
testb $0x1, %al
jne 0xdb9bf
jmp 0xdb961
movq 0x89678(%rip), %rdi     # 0x164fe0
leaq 0x569e6(%rip), %rsi     # 0x132355
callq 0x86700
movq %rax, 0x58(%rsp)
jmp 0xdb97b
movq 0x58(%rsp), %rdi
movq 0x895e9(%rip), %rsi     # 0x164f70
callq 0x86770
jmp 0xdb98e
movb $0x0, 0x227(%rsp)
movl $0x1, 0x200(%rsp)
jmp 0xdbc66
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0xdbc84
leaq 0x1f8(%rsp), %rdi
callq 0x863f0
jmp 0xdb9ce
movq 0x60(%rsp), %rcx
movq (%rcx), %rsi
addq $0x28, %rsi
movq %rcx, %rdx
addq $0x30, %rdx
addq $0x50, %rcx
leaq 0x1f8(%rsp), %rdi
callq 0x86bd0
movb %al, 0x57(%rsp)
jmp 0xdb9f8
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0xdbaa8
jmp 0xdba06
movq 0x895d3(%rip), %rdi     # 0x164fe0
leaq 0x5691c(%rip), %rsi     # 0x132330
callq 0x86700
movq %rax, 0x48(%rsp)
jmp 0xdba20
movq 0x48(%rsp), %rdi
movq 0x60(%rsp), %rax
movq (%rax), %rsi
addq $0x28, %rsi
callq 0x86640
movq %rax, 0x40(%rsp)
jmp 0xdba3d
movq 0x40(%rsp), %rdi
leaq 0x54aea(%rip), %rsi     # 0x130533
callq 0x86700
movq %rax, 0x38(%rsp)
jmp 0xdba55
movq 0x38(%rsp), %rdi
movq 0x8950f(%rip), %rsi     # 0x164f70
callq 0x86770
jmp 0xdba68
leaq 0x210(%rsp), %rdi
callq 0x861e0
jmp 0xdba77
movb $0x0, 0x227(%rsp)
movl $0x1, 0x200(%rsp)
jmp 0xdbc44
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0xdbc55
leaq 0xa0(%rsp), %rdi
callq 0x86260
jmp 0xdbab7
jmp 0xdbab9
leaq 0x210(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x86190
movb %al, 0x37(%rsp)
jmp 0xdbad4
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0xdbade
jmp 0xdbb1d
leaq 0x1f8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x86e80
jmp 0xdbaf5
jmp 0xdbab9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x86b30
jmp 0xdbc42
leaq 0x210(%rsp), %rdi
callq 0x861e0
jmp 0xdbb2c
leaq 0x1f8(%rsp), %rdi
callq 0x86410
jmp 0xdbb3b
movq 0x60(%rsp), %rdi
addq $0x68, %rdi
callq 0x935d0
movq %rax, 0x28(%rsp)
jmp 0xdbb50
movq 0x28(%rsp), %rax
movq %rax, 0x90(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xa7230
jmp 0xdbb74
movq 0x60(%rsp), %rdi
addq $0x68, %rdi
callq 0x93440
movq %rax, 0x20(%rsp)
jmp 0xdbb89
movq 0x20(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xa7230
jmp 0xdbbad
jmp 0xdbbaf
leaq 0x98(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x96260
movb %al, 0x1f(%rsp)
jmp 0xdbbca
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0xdbbd4
jmp 0xdbc1e
leaq 0x98(%rsp), %rdi
callq 0x962a0
movq %rax, 0x10(%rsp)
jmp 0xdbbe8
movq 0x10(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rdi
callq 0x86320
movq %rax, 0x8(%rsp)
jmp 0xdbc03
movq 0x8(%rsp), %rdi
callq 0x86de0
leaq 0x98(%rsp), %rdi
callq 0x962b0
jmp 0xdbc1c
jmp 0xdbbaf
movb $0x1, 0x227(%rsp)
movl $0x1, 0x200(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x86b30
jmp 0xdbc40
jmp 0xdbc44
jmp 0xdbc55
leaq 0x1f8(%rsp), %rdi
callq 0x86510
jmp 0xdbc53
jmp 0xdbc66
leaq 0x1f8(%rsp), %rdi
callq 0x86510
jmp 0xdbc64
jmp 0xdbc84
leaq 0x210(%rsp), %rdi
callq 0x86150
movb 0x227(%rsp), %al
andb $0x1, %al
addq $0x228, %rsp            # imm = 0x228
retq
leaq 0x210(%rsp), %rdi
callq 0x86150
jmp 0xdbc93
jmp 0xdbc95
movq 0x208(%rsp), %rdi
callq 0x86d80
movq %rax, %rdi
callq 0x91290
nopw (%rax,%rax)