// File: /workspace/github_cpp_makefile_star2_O2/zeeev[P]DASVC/bamtools/src/toolkit/bamtools_merge.cpp
// Function: BamTools::MergeTool::MergeToolPrivate::Run()
bool MergeTool::MergeToolPrivate::Run(void) {

    // set to default input if none provided
    if ( !m_settings->HasInput && !m_settings->HasInputFilelist )
        m_settings->InputFiles.push_back(Options::StandardIn());

    // add files in the filelist to the input file list
    if ( m_settings->HasInputFilelist ) {

        ifstream filelist(m_settings->InputFilelist.c_str(), ios::in);
        if ( !filelist.is_open() ) {
            cerr << "bamtools merge ERROR: could not open input BAM file list... Aborting." << endl;
            return false;
        }

        string line;
        while ( getline(filelist, line) )
            m_settings->InputFiles.push_back(line);
    }

    // opens the BAM files (by default without checking for indexes)
    BamMultiReader reader;
    if ( !reader.Open(m_settings->InputFiles) ) {
        cerr << "bamtools merge ERROR: could not open input BAM file(s)... Aborting." << endl;
        return false;
    }

    // retrieve header & reference dictionary info
    std::string mergedHeader = reader.GetHeaderText();
    RefVector references = reader.GetReferenceData();

    // determine compression mode for BamWriter
    bool writeUncompressed = ( m_settings->OutputFilename == Options::StandardOut() &&
                               !m_settings->IsForceCompression );
    BamWriter::CompressionMode compressionMode = BamWriter::Compressed;
    if ( writeUncompressed ) compressionMode = BamWriter::Uncompressed;

    // open BamWriter
    BamWriter writer;
    writer.SetCompressionMode(compressionMode);
    if ( !writer.Open(m_settings->OutputFilename, mergedHeader, references) ) {
        cerr << "bamtools merge ERROR: could not open "
             << m_settings->OutputFilename << " for writing." << endl;
        reader.Close();
        return false;
    }

    // if no region specified, store entire contents of file(s)
    if ( !m_settings->HasRegion ) {
        BamAlignment al;
        while ( reader.GetNextAlignmentCore(al) )
            writer.SaveAlignment(al);
    }

    // otherwise attempt to use region as constraint
    else {

        // if region string parses OK
        BamRegion region;
        if ( Utilities::ParseRegionString(m_settings->Region, reader, region) ) {

            // attempt to find index files
            reader.LocateIndexes();

            // if index data available for all BAM files, we can use SetRegion
            if ( reader.HasIndexes() ) {

                // attempt to use SetRegion(), if failed report error
                if ( !reader.SetRegion(region.LeftRefID,
                                       region.LeftPosition,
                                       region.RightRefID,
                                       region.RightPosition) )
                {
                    cerr << "bamtools merge ERROR: set region failed. Check that REGION describes a valid range"
                         << endl;
                    reader.Close();
                    return false;
                }

                // everything checks out, just iterate through specified region, storing alignments
                BamAlignment al;
                while ( reader.GetNextAlignmentCore(al) )
                    writer.SaveAlignment(al);
            }

            // no index data available, we have to iterate through until we
            // find overlapping alignments
            else {
                BamAlignment al;
                while ( reader.GetNextAlignmentCore(al) ) {
                    if ( (al.RefID >= region.LeftRefID)  && ( (al.Position + al.Length) >= region.LeftPosition ) &&
                         (al.RefID <= region.RightRefID) && ( al.Position <= region.RightPosition) )
                    {
                        writer.SaveAlignment(al);
                    }
                }
            }
        }

        // error parsing REGION string
        else {
            cerr << "bamtools merge ERROR: could not parse REGION - " << m_settings->Region << endl;
            cerr << "Check that REGION is in valid format (see documentation) and that the coordinates are valid"
                 << endl;
            reader.Close();
            writer.Close();
            return false;
        }
    }

    // clean & exit
    reader.Close();
    writer.Close();
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movq %rdi, %rbx
movq (%rdi), %r14
cmpb $0x0, (%r14)
jne 0x4c4ab
cmpb $0x0, 0x1(%r14)
jne 0x4c4ab
addq $0x8, %r14
callq 0x65ebe
movq %r14, %rdi
movq %rax, %rsi
callq 0x3a16c
movq (%rbx), %r14
cmpb $0x1, 0x1(%r14)
jne 0x4c559
movq 0x20(%r14), %rsi
leaq 0x60(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x35bd0
leaq 0xd8(%rsp), %rdi
callq 0x35130
movl %eax, %ebp
testb %al, %al
je 0x4c51f
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x60(%rsp), %r14
leaq 0x28(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x35100
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x4c53c
movq (%rbx), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x3a16c
jmp 0x4c4f5
movq 0x48aba(%rip), %rdi     # 0x94fe0
leaq 0x26469(%rip), %rsi     # 0x72996
callq 0x355b0
movq %rax, %rdi
callq 0x35300
jmp 0x4c546
leaq 0x28(%rsp), %rdi
callq 0x35730
leaq 0x60(%rsp), %rdi
callq 0x35170
testb %bpl, %bpl
je 0x4c6ca
movq %rsp, %r14
movq %r14, %rdi
callq 0x35b70
movq (%rbx), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0x353c0
testb %al, %al
je 0x4c6a8
leaq 0x28(%rsp), %rdi
movq %rsp, %rsi
callq 0x354d0
leaq 0x48(%rsp), %rdi
movq %rsp, %rsi
callq 0x35250
movq (%rbx), %r14
callq 0x65ec6
addq $0x40, %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x3a2a9
xorl %ecx, %ecx
testb %al, %al
je 0x4c5bc
movq (%rbx), %rax
movzbl 0x3(%rax), %ecx
xorl $0x1, %ecx
movl %ecx, 0x24(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x35350
leaq 0x8(%rsp), %rdi
leaq 0x24(%rsp), %rsi
callq 0x35230
movq (%rbx), %rsi
addq $0x40, %rsi
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
leaq 0x48(%rsp), %rcx
callq 0x359d0
testb %al, %al
je 0x4c6d1
movq (%rbx), %rdi
cmpb $0x0, 0x4(%rdi)
je 0x4c719
addq $0x60, %rdi
pcmpeqd %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movdqa %xmm0, (%rdx)
movq %rsp, %rsi
callq 0x67976
testb %al, %al
je 0x4c75b
leaq 0x60(%rsp), %rsi
movl $0x1, (%rsi)
movq %rsp, %rdi
callq 0x35780
movq %rsp, %rdi
callq 0x353e0
testb %al, %al
je 0x4c7b7
leaq 0x14(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x1c(%rsp), %r8
movq %rsp, %rdi
leaq 0x10(%rsp), %rsi
callq 0x35ad0
testb %al, %al
je 0x4c825
leaq 0x60(%rsp), %rdi
callq 0x35200
movq %rsp, %rbx
leaq 0x60(%rsp), %r14
leaq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x35140
testb %al, %al
je 0x4c84d
movq %r15, %rdi
movq %r14, %rsi
callq 0x35c00
jmp 0x4c688
movq 0x48931(%rip), %rdi     # 0x94fe0
leaq 0x26326(%rip), %rsi     # 0x729dc
callq 0x355b0
movq %rax, %rdi
callq 0x35300
xorl %ebx, %ebx
jmp 0x4c889
xorl %ebx, %ebx
jmp 0x4c891
movq 0x48908(%rip), %rdi     # 0x94fe0
leaq 0x26341(%rip), %rsi     # 0x72a20
callq 0x355b0
movq (%rbx), %rsi
addq $0x40, %rsi
movq %rax, %rdi
callq 0x35510
leaq 0x25703(%rip), %rsi     # 0x71dfd
movq %rax, %rdi
callq 0x355b0
movq %rax, %rdi
callq 0x35300
movq %rsp, %rdi
callq 0x35190
xorl %ebx, %ebx
jmp 0x4c86b
leaq 0x60(%rsp), %rdi
callq 0x35200
movq %rsp, %rbx
leaq 0x60(%rsp), %r14
leaq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x35140
testb %al, %al
je 0x4c74c
movq %r15, %rdi
movq %r14, %rsi
callq 0x35c00
jmp 0x4c730
leaq 0x60(%rsp), %rdi
callq 0x35940
jmp 0x4c857
movq 0x4887e(%rip), %rdi     # 0x94fe0
leaq 0x26330(%rip), %rsi     # 0x72a99
callq 0x355b0
movq (%rbx), %rsi
addq $0x60, %rsi
movq %rax, %rdi
callq 0x35510
movq %rax, %rdi
callq 0x35300
movq 0x48854(%rip), %rdi     # 0x94fe0
leaq 0x24a71(%rip), %rsi     # 0x71204
callq 0x355b0
movq %rax, %rdi
callq 0x35300
movq %rsp, %rdi
callq 0x35190
leaq 0x8(%rsp), %rdi
callq 0x35360
jmp 0x4c712
leaq 0x60(%rsp), %rdi
callq 0x35200
movq %rsp, %rbx
leaq 0x60(%rsp), %r14
leaq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x35140
testb %al, %al
je 0x4c819
movl 0x108(%rsp), %eax
cmpl 0x10(%rsp), %eax
jl 0x4c7ce
movl 0x10c(%rsp), %ecx
movl 0x80(%rsp), %edx
addl %ecx, %edx
cmpl 0x14(%rsp), %edx
jl 0x4c7ce
cmpl 0x18(%rsp), %eax
jg 0x4c7ce
cmpl 0x1c(%rsp), %ecx
jg 0x4c7ce
movq %r15, %rdi
movq %r14, %rsi
callq 0x35c00
jmp 0x4c7ce
leaq 0x60(%rsp), %rdi
callq 0x35940
jmp 0x4c857
movq 0x487b4(%rip), %rdi     # 0x94fe0
leaq 0x26213(%rip), %rsi     # 0x72a46
callq 0x355b0
movq %rax, %rdi
callq 0x35300
movq %rsp, %rdi
callq 0x35190
jmp 0x4c712
leaq 0x60(%rsp), %rdi
callq 0x35940
movq %rsp, %rdi
callq 0x35190
movb $0x1, %bl
leaq 0x8(%rsp), %rdi
callq 0x35360
leaq 0x8(%rsp), %rdi
callq 0x35420
leaq 0x48(%rsp), %rdi
callq 0x3a26c
leaq 0x28(%rsp), %rdi
callq 0x35730
movq %rsp, %rdi
callq 0x35110
movl %ebx, %eax
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4c8e3
jmp 0x4c8e3
jmp 0x4c8e3
jmp 0x4c8e3
jmp 0x4c8ab
movq %rax, %rbx
jmp 0x4c92a
jmp 0x4c8e3
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x35940
jmp 0x4c8f5
jmp 0x4c8ca
jmp 0x4c8de
movq %rax, %rbx
jmp 0x4c909
movq %rax, %rbx
jmp 0x4c8ff
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x35940
jmp 0x4c8f5
movq %rax, %rbx
jmp 0x4c913
movq %rax, %rbx
jmp 0x4c8f5
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x35940
leaq 0x8(%rsp), %rdi
callq 0x35420
leaq 0x48(%rsp), %rdi
callq 0x3a26c
leaq 0x28(%rsp), %rdi
callq 0x35730
movq %rsp, %rdi
callq 0x35110
jmp 0x4c934
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x35730
leaq 0x60(%rsp), %rdi
callq 0x35170
movq %rbx, %rdi
callq 0x35b50
movq %rax, %rdi
callq 0x3a198