// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rdi, %rbx
leaq 0xa8(%rsp), %rcx
movl $0x0, (%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x3c760(%rip), %rsi     # 0x90328
movq 0x3c761(%rip), %rdx     # 0x90330
addq %rsi, %rdx
leaq 0x48(%rsp), %rdi
callq 0x2f10a
movq (%rbx), %rsi
cmpb $0x1, 0x2(%rsi)
jne 0x53bf3
addq $0x30, %rsi
leaq 0x48(%rsp), %rdi
callq 0x27350
leaq 0x48(%rsp), %rdi
movl $0x2e, %esi
xorl %edx, %edx
callq 0x27770
testq %rax, %rax
je 0x53c73
leaq 0x10(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x22548(%rip), %rsi     # 0x76161
leaq 0x22542(%rip), %rdx     # 0x76162
movq %rsp, %rdi
callq 0x2f1a0
leaq 0xf0(%rsp), %rdi
movq %rsp, %rsi
leaq 0x48(%rsp), %rdx
callq 0x4aed0
leaq 0x48(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x27350
leaq 0x100(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x53c65
callq 0x27560
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x53c73
callq 0x27560
leaq 0xf0(%rsp), %rdi
callq 0x27220
leaq 0x28(%rbx), %r14
leaq 0x50(%rbx), %rax
movq %rax, 0x90(%rsp)
leaq 0x8(%rbx), %rax
movq %rax, 0x98(%rsp)
movq %rbx, 0x20(%rsp)
leaq 0x30(%rbx), %rax
movq %rax, 0x88(%rsp)
leaq 0x28(%rsp), %r12
movq %r14, %rdi
leaq 0xf0(%rsp), %rsi
callq 0x27980
movl %eax, %r15d
testb %al, %al
je 0x53f33
movl 0x198(%rsp), %ebp
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x53d27
leaq 0xa8(%rsp), %rax
movq %rax, %rcx
movq %rdi, %rdx
movl 0x20(%rdi), %esi
xorl %edi, %edi
cmpl %ebp, %esi
setl %dil
movq %rdx, %rax
cmovlq %rcx, %rax
movq 0x10(%rdx,%rdi,8), %rdi
testq %rdi, %rdi
jne 0x53ce9
leaq 0xa8(%rsp), %rdi
cmpq %rdi, %rax
je 0x53d27
cmpl %ebp, %esi
cmovlq %rcx, %rdx
cmpl 0x20(%rdx), %ebp
jge 0x53f25
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
movb $0x0, 0x10(%rsp)
cmpl $-0x1, %ebp
je 0x53d83
movslq %ebp, %rsi
movq 0x20(%rsp), %rcx
movq 0x50(%rcx), %rax
movq 0x58(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x53f90
leaq (%rsi,%rsi,4), %rcx
leaq (%rax,%rcx,8), %rsi
movq %rsp, %rdi
callq 0x27350
jmp 0x53d9c
movl $0x8, %r8d
movq %rsp, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x21070(%rip), %rcx     # 0x74e07
callq 0x278f0
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
movq 0x98(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x4aed0
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
movq %rsp, %rdx
callq 0x4aed0
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x2f10a
movq %r12, %rdi
leaq 0x20d1c(%rip), %rsi     # 0x74b0e
callq 0x27c00
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x53e0b
callq 0x27560
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x53e25
callq 0x27560
movl $0x8, %edi
callq 0x275c0
movq %rax, %r13
movq %rax, %rdi
callq 0x27360
movq %r13, %rdi
movq %r12, %rsi
movq 0x88(%rsp), %rdx
movq 0x90(%rsp), %rcx
callq 0x279a0
movl %eax, %ebx
testb %al, %al
je 0x53e78
movl %ebp, 0x68(%rsp)
movq %r13, 0x70(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x5cade
jmp 0x53ee5
movl $0x25, %edx
movq 0x3b15c(%rip), %rdi     # 0x8efe0
leaq 0x20f31(%rip), %rsi     # 0x74dbc
callq 0x276b0
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x3b13f(%rip), %rdi     # 0x8efe0
callq 0x276b0
movq %rax, %rbp
movl $0xd, %edx
movq %rax, %rdi
leaq 0x1dfb8(%rip), %rsi     # 0x71e70
callq 0x276b0
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq %rbp, %rdi
callq 0x27040
movq %rax, %rdi
callq 0x27410
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x53ef9
callq 0x27560
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x53f0c
callq 0x27560
testb %bl, %bl
je 0x53f45
movq %r13, %rdi
leaq 0xf0(%rsp), %rsi
callq 0x27bd0
jmp 0x53cb2
movq 0x28(%rax), %r13
testq %r13, %r13
jne 0x53f10
jmp 0x53cb2
leaq 0xa0(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x54ee6
leaq 0xf0(%rsp), %rdi
callq 0x27920
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x53f66
callq 0x27560
xorb $0x1, %r15b
leaq 0xa0(%rsp), %rdi
movq 0x10(%rdi), %rsi
callq 0x5c998
movl %r15d, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d7cf(%rip), %rdi     # 0x71766
xorl %eax, %eax
callq 0x278b0
jmp 0x53fe6
jmp 0x5404d
movq %rax, %rbx
leaq 0x100(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x53fc3
callq 0x27560
jmp 0x53fc3
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x54089
callq 0x27560
jmp 0x54089
jmp 0x53fe6
jmp 0x53fe6
movq %rax, %rbx
jmp 0x5409d
movq %rax, %rbx
jmp 0x54089
jmp 0x54052
jmp 0x53ff4
jmp 0x53ff4
movq %rax, %rbx
jmp 0x54069
movq %rax, %rbx
jmp 0x5402e
movq %rax, %rbx
jmp 0x5401a
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x5401a
callq 0x27560
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x5402e
callq 0x27560
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
jmp 0x5405f
movq %rax, %rbx
movq %r13, %rdi
callq 0x27560
jmp 0x54055
movq %rax, %rbx
jmp 0x5407c
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x54069
callq 0x27560
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x5407c
callq 0x27560
leaq 0xf0(%rsp), %rdi
callq 0x27920
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x5409d
callq 0x27560
leaq 0xa0(%rsp), %rdi
movq 0x10(%rdi), %rsi
callq 0x5c998
movq %rbx, %rdi
callq 0x27b10
movq %rax, %rdi
callq 0x2e1cd