// File: zeeev[P]DASVC/bamtools/src/toolkit/bamtools_split.cpp
// Function: BamTools::SplitTool::SplitToolPrivate::SplitReference()
bool SplitTool::SplitToolPrivate::SplitReference(void) {
  
    // set up splitting data structure
    map<int32_t, BamWriter*> outputFiles;
    map<int32_t, BamWriter*>::iterator writerIter;
    
    // determine reference prefix
    string refPrefix = SPLIT_REFERENCE_TOKEN;
    if ( m_settings->HasCustomRefPrefix )
        refPrefix = m_settings->CustomRefPrefix;

    // make sure prefix starts with '.'
    const size_t dotFound = refPrefix.find('.');
    if ( dotFound != 0 )
        refPrefix = string(".") + refPrefix;

    // iterate through alignments
    BamAlignment al;
    BamWriter* writer;
    int32_t currentRefId;
    while ( m_reader.GetNextAlignment(al) ) {
      
        // see if bool value exists
        currentRefId = al.RefID;
        writerIter = outputFiles.find(currentRefId);
          
        // if no writer associated with this value
        if ( writerIter == outputFiles.end() ) {
        
            // fetch reference name for ID
            string refName;
            if ( currentRefId == -1 )
                refName = "unmapped";
            else
                refName = m_references.at(currentRefId).RefName;

            // construct new output filename
            const string outputFilename = m_outputFilenameStub + refPrefix + refName + ".bam";

            // open new BamWriter
            writer = new BamWriter;
            if ( !writer->Open(outputFilename, m_header, m_references) ) {
                cerr << "bamtools split ERROR: could not open " << outputFilename
                     << " for writing." << endl;
                return false;
            }

            // store in map
            outputFiles.insert( make_pair(currentRefId, writer) );
        } 
        
        // else grab corresponding writer
        else writer = (*writerIter).second;
        
        // store alignment in proper BAM output file 
        if ( writer ) 
            writer->SaveAlignment(al);
    }
    
    // clean up BamWriters 
    CloseWriters(outputFiles);
    
    // return success
    return true;
}

// Assembly:
subq $0x388, %rsp            # imm = 0x388
movq %rdi, 0x378(%rsp)
movq 0x378(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x348(%rsp), %rdi
callq 0xe83c0
leaq 0x340(%rsp), %rdi
callq 0xe83d0
jmp 0xe5c4b
leaq 0x80866(%rip), %rsi     # 0x1664b8
leaq 0x310(%rsp), %rdi
callq 0x86350
jmp 0xe5c61
movq 0x88(%rsp), %rax
movq (%rax), %rax
testb $0x1, 0x2(%rax)
je 0xe5cc4
movq 0x88(%rsp), %rax
movq (%rax), %rsi
addq $0x30, %rsi
leaq 0x310(%rsp), %rdi
callq 0x86070
jmp 0xe5c90
jmp 0xe5cc4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x338(%rsp)
movl %eax, 0x334(%rsp)
jmp 0xe630d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x338(%rsp)
movl %eax, 0x334(%rsp)
jmp 0xe62fc
xorl %eax, %eax
movl %eax, %edx
leaq 0x310(%rsp), %rdi
movl $0x2e, %esi
callq 0x86960
movq %rax, 0x80(%rsp)
jmp 0xe5ce4
movq 0x80(%rsp), %rax
movq %rax, 0x308(%rsp)
cmpq $0x0, 0x308(%rsp)
je 0xe5e07
leaq 0x2c7(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0x86da0
movq 0x78(%rsp), %rdx
leaq 0x4e5c0(%rip), %rsi     # 0x1342e1
leaq 0x2c8(%rsp), %rdi
callq 0x86a00
jmp 0xe5d30
leaq 0x2e8(%rsp), %rdi
leaq 0x2c8(%rsp), %rsi
leaq 0x310(%rsp), %rdx
callq 0xd0140
jmp 0xe5d4f
leaq 0x310(%rsp), %rdi
leaq 0x2e8(%rsp), %rsi
callq 0x86070
jmp 0xe5d66
leaq 0x2e8(%rsp), %rdi
callq 0x86f68
jmp 0xe5d75
leaq 0x2c8(%rsp), %rdi
callq 0x86f68
jmp 0xe5d84
leaq 0x2c7(%rsp), %rdi
callq 0x867b0
jmp 0xe5e07
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x338(%rsp)
movl %eax, 0x334(%rsp)
jmp 0xe5df5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x338(%rsp)
movl %eax, 0x334(%rsp)
jmp 0xe5de4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x338(%rsp)
movl %eax, 0x334(%rsp)
leaq 0x2e8(%rsp), %rdi
callq 0x86f68
jmp 0xe5de2
jmp 0xe5de4
leaq 0x2c8(%rsp), %rdi
callq 0x86f68
jmp 0xe5df3
jmp 0xe5df5
leaq 0x2c7(%rsp), %rdi
callq 0x867b0
jmp 0xe62fc
leaq 0x168(%rsp), %rdi
callq 0x86260
jmp 0xe5e16
jmp 0xe5e18
movq 0x88(%rsp), %rdi
addq $0x28, %rdi
leaq 0x168(%rsp), %rsi
callq 0x86bb0
movb %al, 0x77(%rsp)
jmp 0xe5e37
movb 0x77(%rsp), %al
testb $0x1, %al
jne 0xe5e44
jmp 0xe6285
movl 0x210(%rsp), %eax
movl %eax, 0x15c(%rsp)
leaq 0x348(%rsp), %rdi
leaq 0x15c(%rsp), %rsi
callq 0xe83f0
movq %rax, 0x68(%rsp)
jmp 0xe5e6e
movq 0x68(%rsp), %rax
movq %rax, 0x150(%rsp)
movq 0x150(%rsp), %rax
movq %rax, 0x340(%rsp)
leaq 0x348(%rsp), %rdi
callq 0xe8440
movq %rax, 0x60(%rsp)
jmp 0xe5e9f
movq 0x60(%rsp), %rax
movq %rax, 0x148(%rsp)
leaq 0x340(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0xe8420
movb %al, 0x5f(%rsp)
jmp 0xe5ec7
movb 0x5f(%rsp), %al
testb $0x1, %al
jne 0xe5ed4
jmp 0xe6239
leaq 0x128(%rsp), %rdi
callq 0x86a30
jmp 0xe5ee3
cmpl $-0x1, 0x15c(%rsp)
jne 0xe5f37
leaq 0x4d089(%rip), %rsi     # 0x132f7d
leaq 0x128(%rsp), %rdi
callq 0x869f0
jmp 0xe5f03
jmp 0xe5f6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x338(%rsp)
movl %eax, 0x334(%rsp)
jmp 0xe62cd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x338(%rsp)
movl %eax, 0x334(%rsp)
jmp 0xe6225
movq 0x88(%rsp), %rdi
addq $0x50, %rdi
movslq 0x15c(%rsp), %rsi
callq 0x916c0
movq %rax, 0x50(%rsp)
jmp 0xe5f57
movq 0x50(%rsp), %rsi
leaq 0x128(%rsp), %rdi
callq 0x86070
jmp 0xe5f6b
jmp 0xe5f6d
movq 0x88(%rsp), %rsi
addq $0x8, %rsi
leaq 0xc8(%rsp), %rdi
leaq 0x310(%rsp), %rdx
callq 0xd0140
jmp 0xe5f90
leaq 0xe8(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
leaq 0x128(%rsp), %rdx
callq 0xd0140
jmp 0xe5faf
leaq 0x4c369(%rip), %rdx     # 0x13231f
leaq 0x108(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x927c0
jmp 0xe5fcd
leaq 0xe8(%rsp), %rdi
callq 0x86f68
jmp 0xe5fdc
leaq 0xc8(%rsp), %rdi
callq 0x86f68
jmp 0xe5feb
movl $0x8, %edi
callq 0x86720
movq %rax, 0x48(%rsp)
jmp 0xe5ffc
movq 0x48(%rsp), %rdi
callq 0x863f0
jmp 0xe6008
movq 0x88(%rsp), %rcx
movq 0x48(%rsp), %rax
movq %rax, 0x160(%rsp)
movq 0x160(%rsp), %rdi
movq %rcx, %rdx
addq $0x30, %rdx
addq $0x50, %rcx
leaq 0x108(%rsp), %rsi
callq 0x86bd0
movb %al, 0x47(%rsp)
jmp 0xe6043
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0xe6152
jmp 0xe6051
movq 0x7ef88(%rip), %rdi     # 0x164fe0
leaq 0x4ced3(%rip), %rsi     # 0x132f32
callq 0x86700
movq %rax, 0x38(%rsp)
jmp 0xe606b
movq 0x38(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x86640
movq %rax, 0x30(%rsp)
jmp 0xe6084
movq 0x30(%rsp), %rdi
leaq 0x495b5(%rip), %rsi     # 0x12f645
callq 0x86700
movq %rax, 0x28(%rsp)
jmp 0xe609c
movq 0x28(%rsp), %rdi
movq 0x7eec8(%rip), %rsi     # 0x164f70
callq 0x86770
jmp 0xe60af
movb $0x0, 0x387(%rsp)
movl $0x1, 0xc4(%rsp)
jmp 0xe61e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x338(%rsp)
movl %eax, 0x334(%rsp)
jmp 0xe6102
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x338(%rsp)
movl %eax, 0x334(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x86f68
jmp 0xe6100
jmp 0xe6102
leaq 0xc8(%rsp), %rdi
callq 0x86f68
jmp 0xe6111
jmp 0xe6225
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x338(%rsp)
movl %eax, 0x334(%rsp)
jmp 0xe6214
movq 0x48(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x338(%rsp)
movl %eax, 0x334(%rsp)
callq 0x866a0
jmp 0xe6214
movl 0x15c(%rsp), %edi
movq 0x160(%rsp), %rsi
callq 0xe84c0
movq %rdx, 0x18(%rsp)
movl %eax, 0x24(%rsp)
jmp 0xe6171
movq 0x18(%rsp), %rax
movl 0x24(%rsp), %ecx
movl %ecx, 0xa0(%rsp)
movq %rax, 0xa8(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0xe84f0
jmp 0xe61a0
leaq 0x348(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xe8470
movb %dl, 0xf(%rsp)
movq %rax, 0x10(%rsp)
jmp 0xe61c0
movb 0xf(%rsp), %al
movq 0x10(%rsp), %rcx
movq %rcx, 0x90(%rsp)
movb %al, 0x98(%rsp)
movl $0x0, 0xc4(%rsp)
leaq 0x108(%rsp), %rdi
callq 0x86f68
jmp 0xe61f2
leaq 0x128(%rsp), %rdi
callq 0x86f68
jmp 0xe6201
movl 0xc4(%rsp), %eax
testl %eax, %eax
jne 0xe62af
jmp 0xe6212
jmp 0xe625c
leaq 0x108(%rsp), %rdi
callq 0x86f68
jmp 0xe6223
jmp 0xe6225
leaq 0x128(%rsp), %rdi
callq 0x86f68
jmp 0xe6234
jmp 0xe62cd
leaq 0x340(%rsp), %rdi
callq 0xe8520
movq %rax, (%rsp)
jmp 0xe624c
movq (%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x160(%rsp)
cmpq $0x0, 0x160(%rsp)
je 0xe6280
movq 0x160(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x86e80
jmp 0xe627e
jmp 0xe6280
jmp 0xe5e18
movq 0x88(%rsp), %rdi
leaq 0x348(%rsp), %rsi
callq 0xe8540
jmp 0xe629c
movb $0x1, 0x387(%rsp)
movl $0x1, 0xc4(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x86b30
jmp 0xe62be
leaq 0x310(%rsp), %rdi
callq 0x86f68
jmp 0xe62de
leaq 0x168(%rsp), %rdi
callq 0x86b30
jmp 0xe62dc
jmp 0xe62fc
leaq 0x348(%rsp), %rdi
callq 0xe8620
movb 0x387(%rsp), %al
andb $0x1, %al
addq $0x388, %rsp            # imm = 0x388
retq
leaq 0x310(%rsp), %rdi
callq 0x86f68
jmp 0xe630b
jmp 0xe630d
leaq 0x348(%rsp), %rdi
callq 0xe8620
jmp 0xe631c
jmp 0xe631e
movq 0x338(%rsp), %rdi
callq 0x86d80
movq %rax, %rdi
callq 0x91290
nopw %cs:(%rax,%rax)