// File: /workspace/github_cpp_makefile_star2_O1/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 $0x258, %rsp            # imm = 0x258
movq %rdi, %rbx
leaq 0xd8(%rsp), %rbp
movl $0x0, (%rbp)
xorl %eax, %eax
movq %rax, 0x8(%rbp)
movq %rbp, 0x10(%rbp)
movq %rbp, 0x18(%rbp)
movq %rax, 0x20(%rbp)
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x3d61f(%rip), %rsi     # 0x91330
movq 0x3d620(%rip), %rdx     # 0x91338
addq %rsi, %rdx
leaq 0x50(%rsp), %rdi
callq 0x2f122
movq (%rbx), %rsi
cmpb $0x1, 0x2(%rsi)
jne 0x53d3c
addq $0x30, %rsi
leaq 0x50(%rsp), %rdi
callq 0x27350
leaq 0x50(%rsp), %rdi
movl $0x2e, %esi
xorl %edx, %edx
callq 0x27780
testq %rax, %rax
movq %rbx, 0x8(%rsp)
je 0x53dcb
leaq 0x20(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x233fa(%rip), %rsi     # 0x77161
leaq 0x233f4(%rip), %rdx     # 0x77162
leaq 0x10(%rsp), %rdi
callq 0x2f1b2
leaq 0x100(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x4af9e
leaq 0x50(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x27350
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x53db7
callq 0x27560
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0x53dc6
callq 0x27560
movq 0x8(%rsp), %rbx
leaq 0x100(%rsp), %rdi
callq 0x27220
leaq 0x28(%rbx), %rax
movq %rax, 0xa8(%rsp)
leaq 0x8(%rbx), %rax
movq %rax, 0xa0(%rsp)
leaq 0x30(%rbx), %rax
movq %rax, 0x98(%rsp)
leaq 0x50(%rbx), %rax
movq %rax, 0x90(%rsp)
leaq 0x30(%rsp), %r14
movq 0xa8(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x27990
movl %eax, %r15d
testb %al, %al
je 0x54097
movl 0x1a8(%rsp), %r12d
movq 0xe0(%rsp), %rcx
movq %rbp, %rax
testq %rcx, %rcx
je 0x53e5f
movq %rbp, %rax
xorl %edx, %edx
cmpl %r12d, 0x20(%rcx)
setl %dl
cmovgeq %rcx, %rax
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0x53e48
movq %rbp, %rcx
cmpq %rbp, %rax
je 0x53e72
cmpl 0x20(%rax), %r12d
cmovlq %rbp, %rax
movq %rax, %rcx
cmpq %rbp, %rcx
je 0x53e80
movq 0x28(%rcx), %r13
jmp 0x54079
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
cmpl $-0x1, %r12d
je 0x53ee0
movslq %r12d, %rsi
movq 0x8(%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 0x540f4
leaq (%rsi,%rsi,4), %rcx
leaq (%rax,%rcx,8), %rsi
leaq 0x10(%rsp), %rdi
callq 0x27350
jmp 0x53efb
movl $0x8, %r8d
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x21f1f(%rip), %rcx     # 0x75e15
callq 0x27900
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
movq 0xa0(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x4af9e
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
leaq 0x10(%rsp), %rdx
callq 0x4af9e
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rdx
addq %rsi, %rdx
movq %r14, %rbx
movq %r14, %rdi
callq 0x2f122
movq %rbx, %rdi
leaq 0x21ba0(%rip), %rsi     # 0x75af6
callq 0x27c10
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x53f72
callq 0x27560
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x53f8c
callq 0x27560
movl $0x8, %edi
callq 0x275c0
movq %rax, %r13
movq %rax, %rdi
callq 0x27360
movq %r13, %rdi
movq %r14, %rsi
movq 0x98(%rsp), %rdx
movq 0x90(%rsp), %rcx
callq 0x279b0
movl %eax, %ebx
testb %al, %al
je 0x53fe0
movl %r12d, 0x70(%rsp)
movq %r13, 0x78(%rsp)
leaq 0xd0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x5cc12
jmp 0x5404d
movl $0x25, %edx
movq 0x3bff4(%rip), %rdi     # 0x8ffe0
leaq 0x21dd7(%rip), %rsi     # 0x75dca
callq 0x276c0
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x3bfd7(%rip), %rdi     # 0x8ffe0
callq 0x276c0
movq %rax, %r12
movl $0xd, %edx
movq %rax, %rdi
leaq 0x1ee50(%rip), %rsi     # 0x72e70
callq 0x276c0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq %r12, %rdi
callq 0x27040
movq %rax, %rdi
callq 0x27410
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x54061
callq 0x27560
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x54075
callq 0x27560
testb %bl, %bl
je 0x540a9
testq %r13, %r13
je 0x53e0d
movq %r13, %rdi
leaq 0x100(%rsp), %rsi
callq 0x27be0
jmp 0x53e0d
leaq 0xd0(%rsp), %rsi
movq 0x8(%rsp), %rdi
callq 0x5505c
leaq 0x100(%rsp), %rdi
callq 0x27930
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x540ca
callq 0x27560
xorb $0x1, %r15b
leaq 0xd0(%rsp), %rdi
movq 0x10(%rdi), %rsi
callq 0x5cac8
movl %r15d, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e66b(%rip), %rdi     # 0x72766
xorl %eax, %eax
callq 0x278c0
jmp 0x54148
movq %rax, %rbx
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x54122
callq 0x27560
jmp 0x54122
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0x541ef
callq 0x27560
jmp 0x541ef
jmp 0x54148
jmp 0x541b2
jmp 0x54148
movq %rax, %rbx
jmp 0x54203
movq %rax, %rbx
jmp 0x541ef
jmp 0x541b7
jmp 0x54156
jmp 0x54156
movq %rax, %rbx
jmp 0x541ce
movq %rax, %rbx
jmp 0x54193
movq %rax, %rbx
jmp 0x5417c
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x5417c
callq 0x27560
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x54193
callq 0x27560
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
jmp 0x541c4
movq %rax, %rbx
movq %r13, %rdi
callq 0x27560
jmp 0x541ba
movq %rax, %rbx
jmp 0x541e2
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x541ce
callq 0x27560
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x541e2
callq 0x27560
leaq 0x100(%rsp), %rdi
callq 0x27930
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x54203
callq 0x27560
leaq 0xd0(%rsp), %rdi
movq 0x10(%rdi), %rsi
callq 0x5cac8
movq %rbx, %rdi
callq 0x27b20
movq %rax, %rdi
callq 0x2e1ed