// File: /workspace/github_cpp_makefile_star2_O2/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 0x98(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x3deb4(%rip), %rsi     # 0x96368
leaq 0x50(%rsp), %rdi
callq 0x35810
movq (%rbx), %rsi
cmpb $0x1, 0x2(%rsi)
jne 0x584d5
addq $0x30, %rsi
leaq 0x50(%rsp), %rdi
callq 0x35340
leaq 0x50(%rsp), %rdi
pushq $0x2e
popq %rsi
xorl %edx, %edx
callq 0x35790
testq %rax, %rax
je 0x58542
leaq 0x1dc2e(%rip), %rsi     # 0x7611e
leaq 0x10(%rsp), %rdi
leaq 0xe0(%rsp), %rdx
callq 0x35280
leaq 0x100(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x51d62
leaq 0x50(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x35340
leaq 0x100(%rsp), %rdi
callq 0x35730
leaq 0x10(%rsp), %rdi
callq 0x35730
leaq 0x100(%rsp), %rdi
callq 0x35200
leaq 0x28(%rbx), %r14
leaq 0x50(%rbx), %rax
movq %rax, 0x30(%rsp)
leaq 0x8(%rbx), %rax
movq %rax, 0x48(%rsp)
movq %rbx, 0x38(%rsp)
leaq 0x30(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq 0x100(%rsp), %rbp
leaq 0xe0(%rsp), %rbx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x359b0
movl %eax, %r12d
testb %al, %al
je 0x5871e
movl 0x1a8(%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x5f514
leaq 0x98(%rsp), %rcx
cmpq %rcx, %rax
je 0x585d2
movq 0x28(%rax), %r15
testq %r15, %r15
jne 0x5870e
jmp 0x58583
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
andq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
movslq 0xc(%rsp), %rsi
cmpq $-0x1, %rsi
je 0x5860b
movq 0x30(%rsp), %rdi
callq 0x3a2cc
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x35340
jmp 0x5861c
leaq 0x10(%rsp), %rdi
leaq 0x1c7c0(%rip), %rsi     # 0x74dd7
callq 0x35c40
leaq 0xc0(%rsp), %r15
movq %r15, %rdi
movq 0x48(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x51d62
leaq 0x70(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
leaq 0x10(%rsp), %rdx
callq 0x51d62
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x1c487(%rip), %rdx     # 0x74adf
callq 0x3ab7a
movq %r13, %rdi
callq 0x35730
leaq 0xc0(%rsp), %rdi
callq 0x35730
pushq $0x8
popq %rdi
callq 0x355d0
movq %rax, %r15
movq %rax, %rdi
callq 0x35350
movq %r15, %rdi
movq %rbx, %rsi
movq 0x40(%rsp), %rdx
movq 0x30(%rsp), %rcx
callq 0x359d0
movl %eax, %r13d
testb %al, %al
je 0x586c2
movl 0xc(%rsp), %eax
movl %eax, 0x70(%rsp)
movq %r15, 0x78(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x5f566
jmp 0x586f7
movq 0x3c917(%rip), %rdi     # 0x94fe0
leaq 0x1c6bc(%rip), %rsi     # 0x74d8c
callq 0x355b0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x35510
movq %rax, %rdi
leaq 0x19713(%rip), %rsi     # 0x71dfd
callq 0x355b0
movq %rax, %rdi
callq 0x35300
movq %rbx, %rdi
callq 0x35730
leaq 0x10(%rsp), %rdi
callq 0x35730
testb %r13b, %r13b
je 0x58730
movq %r15, %rdi
movq %rbp, %rsi
callq 0x35c00
jmp 0x58583
leaq 0x90(%rsp), %rsi
movq 0x38(%rsp), %rdi
callq 0x5957e
leaq 0x100(%rsp), %rdi
callq 0x35940
leaq 0x50(%rsp), %rdi
callq 0x35730
xorb $0x1, %r12b
leaq 0x90(%rsp), %rdi
callq 0x5f30a
movl %r12d, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x58796
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x35730
jmp 0x5878a
jmp 0x587f9
jmp 0x58796
jmp 0x58796
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x35730
jmp 0x58809
movq %rax, %rbx
jmp 0x58809
movq %rax, %rbx
jmp 0x58813
jmp 0x587dd
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x35730
jmp 0x587c7
movq %rax, %rbx
movq %r15, %rdi
callq 0x35570
jmp 0x587e0
jmp 0x587d6
jmp 0x587f9
jmp 0x587d6
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x35730
jmp 0x587ed
movq %rax, %rbx
jmp 0x587ed
jmp 0x587f9
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x35730
leaq 0x10(%rsp), %rdi
callq 0x35730
jmp 0x587fc
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x35940
leaq 0x50(%rsp), %rdi
callq 0x35730
leaq 0x90(%rsp), %rdi
callq 0x5f30a
movq %rbx, %rdi
callq 0x35b50
movq %rax, %rdi
callq 0x3a198