// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x270, %rsp            # imm = 0x270
movq %rdi, %rbx
movq (%rdi), %r14
cmpb $0x0, (%r14)
jne 0x43ae2
cmpb $0x0, 0x1(%r14)
jne 0x43ae2
addq $0x8, %r14
callq 0x646e2
movq %r14, %rdi
movq %rax, %rsi
callq 0x2e1b0
movq (%rbx), %rax
cmpb $0x1, 0x1(%rax)
jne 0x43be2
movq 0x20(%rax), %rsi
leaq 0x68(%rsp), %r14
movq %r14, %rdi
movl $0x8, %edx
callq 0x27ba0
leaq 0xe0(%rsp), %rdi
callq 0x27130
movl %eax, %ebp
testb %al, %al
je 0x43b78
leaq 0x40(%rsp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
leaq 0x30(%rsp), %r15
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x27bb0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x43bc0
movq (%rbx), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x2e1b0
jmp 0x43b35
movq 0x4c461(%rip), %r14     # 0x8ffe0
leaq 0x2fe80(%rip), %rsi     # 0x73a06
movl $0x45, %edx
movq %r14, %rdi
callq 0x276c0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4c42f(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
jmp 0x43bcf
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x43bcf
callq 0x27560
leaq 0x68(%rsp), %rdi
callq 0x27170
testb %bpl, %bpl
je 0x43d8a
movq %rsp, %r14
movq %r14, %rdi
callq 0x27b40
movq (%rbx), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0x273c0
testb %al, %al
je 0x43d3d
leaq 0x30(%rsp), %rdi
movq %rsp, %rsi
callq 0x274d0
leaq 0x50(%rsp), %rdi
movq %rsp, %rsi
callq 0x27270
movq (%rbx), %r14
callq 0x646ea
movq 0x48(%r14), %rdx
xorl %ebp, %ebp
cmpq 0x8(%rax), %rdx
jne 0x43c51
testq %rdx, %rdx
je 0x43c47
movq (%rax), %rsi
movq 0x40(%r14), %rdi
callq 0x275f0
testl %eax, %eax
jne 0x43c51
movq (%rbx), %rax
movzbl 0x3(%rax), %ebp
xorl $0x1, %ebp
movl %ebp, 0x2c(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x27360
leaq 0x8(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
callq 0x27250
movq (%rbx), %rsi
addq $0x40, %rsi
leaq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x279b0
testb %al, %al
je 0x43d91
movq (%rbx), %rdi
cmpb $0x0, 0x4(%rdi)
je 0x43e0d
addq $0x60, %rdi
pcmpeqd %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movdqa %xmm0, (%rdx)
movq %rsp, %rsi
callq 0x669d4
testb %al, %al
je 0x43e4f
leaq 0x68(%rsp), %rsi
movl $0x1, (%rsi)
movq %rsp, %rdi
callq 0x27770
movq %rsp, %rdi
callq 0x273e0
testb %al, %al
je 0x43f05
leaq 0x14(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x1c(%rsp), %r8
movq %rsp, %rdi
leaq 0x10(%rsp), %rsi
callq 0x27a90
testb %al, %al
je 0x43f73
leaq 0x68(%rsp), %rdi
callq 0x27220
movq %rsp, %rbx
leaq 0x68(%rsp), %r14
leaq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x27140
testb %al, %al
je 0x43fa4
movq %r15, %rdi
movq %r14, %rsi
callq 0x27be0
jmp 0x43d1d
movq 0x4c29c(%rip), %rbx     # 0x8ffe0
leaq 0x2fd01(%rip), %rsi     # 0x73a4c
movl $0x43, %edx
movq %rbx, %rdi
callq 0x276c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4c26a(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
xorl %ebx, %ebx
jmp 0x43fe9
xorl %ebx, %ebx
jmp 0x43ff1
movq 0x4c248(%rip), %rdi     # 0x8ffe0
leaq 0x2fcf1(%rip), %rsi     # 0x73a90
movl $0x25, %edx
callq 0x276c0
movq (%rbx), %rax
movq 0x40(%rax), %rsi
movq 0x48(%rax), %rdx
movq 0x4c225(%rip), %rdi     # 0x8ffe0
callq 0x276c0
movq %rax, %rbx
leaq 0x2f0a6(%rip), %rsi     # 0x72e70
movl $0xd, %edx
movq %rax, %rdi
callq 0x276c0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq %rbx, %rdi
callq 0x27040
movq %rax, %rdi
callq 0x27410
movq %rsp, %rdi
callq 0x27190
xorl %ebx, %ebx
jmp 0x43fc2
leaq 0x68(%rsp), %rdi
callq 0x27220
movq %rsp, %rbx
leaq 0x68(%rsp), %r14
leaq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x27140
testb %al, %al
je 0x43e40
movq %r15, %rdi
movq %r14, %rsi
callq 0x27be0
jmp 0x43e24
leaq 0x68(%rsp), %rdi
callq 0x27930
jmp 0x43fae
movq 0x4c18a(%rip), %rdi     # 0x8ffe0
leaq 0x2fcac(%rip), %rsi     # 0x73b09
movl $0x2f, %edx
callq 0x276c0
movq (%rbx), %rax
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
movq 0x4c167(%rip), %rdi     # 0x8ffe0
callq 0x276c0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq %rbx, %rdi
callq 0x27040
movq %rax, %rdi
callq 0x27410
movq 0x4c131(%rip), %rbx     # 0x8ffe0
leaq 0x2e344(%rip), %rsi     # 0x721fa
movl $0x5b, %edx
movq %rbx, %rdi
callq 0x276c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4c0ff(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
movq %rsp, %rdi
callq 0x27190
leaq 0x8(%rsp), %rdi
callq 0x27370
jmp 0x43e06
leaq 0x68(%rsp), %rdi
callq 0x27220
movq %rsp, %rbx
leaq 0x68(%rsp), %r14
leaq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x27140
testb %al, %al
je 0x43f67
movl 0x110(%rsp), %eax
cmpl 0x10(%rsp), %eax
jl 0x43f1c
movl 0x114(%rsp), %ecx
movl 0x88(%rsp), %edx
addl %ecx, %edx
cmpl 0x14(%rsp), %edx
jl 0x43f1c
cmpl 0x18(%rsp), %eax
jg 0x43f1c
cmpl 0x1c(%rsp), %ecx
jg 0x43f1c
movq %r15, %rdi
movq %r14, %rsi
callq 0x27be0
jmp 0x43f1c
leaq 0x68(%rsp), %rdi
callq 0x27930
jmp 0x43fae
movq 0x4c066(%rip), %rdi     # 0x8ffe0
leaq 0x2fb35(%rip), %rsi     # 0x73ab6
movl $0x52, %edx
callq 0x276c0
movq 0x4c04e(%rip), %rdi     # 0x8ffe0
callq 0x27310
movq %rsp, %rdi
callq 0x27190
jmp 0x43e06
leaq 0x68(%rsp), %rdi
callq 0x27930
movq %rsp, %rdi
callq 0x27190
movb $0x1, %bl
leaq 0x8(%rsp), %rdi
callq 0x27370
leaq 0x8(%rsp), %rdi
callq 0x27430
leaq 0x50(%rsp), %rdi
callq 0x2e378
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x43fe9
callq 0x27560
movq %rsp, %rdi
callq 0x27110
movl %ebx, %eax
addq $0x270, %rsp            # imm = 0x270
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x44043
jmp 0x44043
jmp 0x44043
jmp 0x44043
jmp 0x44020
jmp 0x4403e
jmp 0x44043
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x27930
jmp 0x44055
movq %rax, %rbx
jmp 0x4405f
movq %rax, %rbx
jmp 0x44069
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x27930
jmp 0x44055
movq %rax, %rbx
jmp 0x44098
movq %rax, %rbx
jmp 0x4407c
movq %rax, %rbx
jmp 0x44055
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x27930
leaq 0x8(%rsp), %rdi
callq 0x27430
leaq 0x50(%rsp), %rdi
callq 0x2e378
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4407c
callq 0x27560
movq %rsp, %rdi
callq 0x27110
jmp 0x440a2
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x44098
callq 0x27560
leaq 0x68(%rsp), %rdi
callq 0x27170
movq %rbx, %rdi
callq 0x27b20
movq %rax, %rdi
callq 0x2e1ed