// File: /workspace/github_cpp_makefile_star2_O3/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 0x43a05
cmpb $0x0, 0x1(%r14)
jne 0x43a05
addq $0x8, %r14
callq 0x641a2
movq %r14, %rdi
movq %rax, %rsi
callq 0x2e190
movq (%rbx), %r14
cmpb $0x1, 0x1(%r14)
jne 0x43b07
movq 0x20(%r14), %rsi
leaq 0x68(%rsp), %r14
movq %r14, %rdi
movl $0x8, %edx
callq 0x27b90
leaq 0xe0(%rsp), %rdi
callq 0x27130
testb %al, %al
je 0x43a97
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 0x27ba0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x43aee
movq (%rbx), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x2e190
jmp 0x43a54
movq 0x4b542(%rip), %rbx     # 0x8efe0
leaq 0x2ef78(%rip), %rsi     # 0x72a1d
movl $0x45, %edx
movq %rbx, %rdi
callq 0x276b0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4b510(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
leaq 0x68(%rsp), %rdi
callq 0x27170
xorl %ebx, %ebx
jmp 0x43f0f
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x43afd
callq 0x27560
leaq 0x68(%rsp), %rdi
callq 0x27170
movq %rsp, %r14
movq %r14, %rdi
callq 0x27b30
movq (%rbx), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0x273c0
testb %al, %al
je 0x43c62
leaq 0x30(%rsp), %rdi
movq %rsp, %rsi
callq 0x274d0
leaq 0x50(%rsp), %rdi
movq %rsp, %rsi
callq 0x27270
movq (%rbx), %r14
callq 0x641aa
movq 0x48(%r14), %rdx
xorl %ebp, %ebp
cmpq 0x8(%rax), %rdx
jne 0x43b76
testq %rdx, %rdx
je 0x43b6c
movq (%rax), %rsi
movq 0x40(%r14), %rdi
callq 0x275f0
testl %eax, %eax
jne 0x43b76
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 0x279a0
testb %al, %al
je 0x43caf
movq (%rbx), %rdi
cmpb $0x0, 0x4(%rdi)
je 0x43d2b
addq $0x60, %rdi
pcmpeqd %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movdqa %xmm0, (%rdx)
movq %rsp, %rsi
callq 0x6643c
testb %al, %al
je 0x43d6d
leaq 0x68(%rsp), %rsi
movl $0x1, (%rsi)
movq %rsp, %rdi
callq 0x27760
movq %rsp, %rdi
callq 0x273e0
testb %al, %al
je 0x43e23
leaq 0x14(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x1c(%rsp), %r8
movq %rsp, %rdi
leaq 0x10(%rsp), %rsi
callq 0x27a80
testb %al, %al
je 0x43e91
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 0x43ec2
movq %r15, %rdi
movq %r14, %rsi
callq 0x27bd0
jmp 0x43c42
movq 0x4b377(%rip), %rbx     # 0x8efe0
leaq 0x2edf3(%rip), %rsi     # 0x72a63
movl $0x43, %edx
movq %rbx, %rdi
callq 0x276b0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4b345(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
xorl %ebx, %ebx
jmp 0x43f07
movq 0x4b32a(%rip), %rdi     # 0x8efe0
leaq 0x2edea(%rip), %rsi     # 0x72aa7
movl $0x25, %edx
callq 0x276b0
movq (%rbx), %rax
movq 0x40(%rax), %rsi
movq 0x48(%rax), %rdx
movq 0x4b307(%rip), %rdi     # 0x8efe0
callq 0x276b0
movq %rax, %rbx
leaq 0x2e188(%rip), %rsi     # 0x71e70
movl $0xd, %edx
movq %rax, %rdi
callq 0x276b0
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 0x43ee0
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 0x43d5e
movq %r15, %rdi
movq %r14, %rsi
callq 0x27bd0
jmp 0x43d42
leaq 0x68(%rsp), %rdi
callq 0x27920
jmp 0x43ecc
movq 0x4b26c(%rip), %rdi     # 0x8efe0
leaq 0x2eda5(%rip), %rsi     # 0x72b20
movl $0x2f, %edx
callq 0x276b0
movq (%rbx), %rax
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
movq 0x4b249(%rip), %rdi     # 0x8efe0
callq 0x276b0
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 0x4b213(%rip), %rbx     # 0x8efe0
leaq 0x2d426(%rip), %rsi     # 0x711fa
movl $0x5b, %edx
movq %rbx, %rdi
callq 0x276b0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4b1e1(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
movq %rsp, %rdi
callq 0x27190
leaq 0x8(%rsp), %rdi
callq 0x27370
jmp 0x43d24
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 0x43e85
movl 0x110(%rsp), %eax
cmpl 0x10(%rsp), %eax
jl 0x43e3a
movl 0x114(%rsp), %ecx
movl 0x88(%rsp), %edx
addl %ecx, %edx
cmpl 0x14(%rsp), %edx
jl 0x43e3a
cmpl 0x18(%rsp), %eax
jg 0x43e3a
cmpl 0x1c(%rsp), %ecx
jg 0x43e3a
movq %r15, %rdi
movq %r14, %rsi
callq 0x27bd0
jmp 0x43e3a
leaq 0x68(%rsp), %rdi
callq 0x27920
jmp 0x43ecc
movq 0x4b148(%rip), %rdi     # 0x8efe0
leaq 0x2ec2e(%rip), %rsi     # 0x72acd
movl $0x52, %edx
callq 0x276b0
movq 0x4b130(%rip), %rdi     # 0x8efe0
callq 0x27310
movq %rsp, %rdi
callq 0x27190
jmp 0x43d24
leaq 0x68(%rsp), %rdi
callq 0x27920
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 0x2e37a
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x43f07
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 0x43f61
jmp 0x43f61
jmp 0x43f61
jmp 0x43f61
jmp 0x43f3e
jmp 0x43f5c
jmp 0x43f61
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x27920
jmp 0x43f73
movq %rax, %rbx
jmp 0x43f7d
movq %rax, %rbx
jmp 0x43f87
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x27920
jmp 0x43f73
movq %rax, %rbx
jmp 0x43fb6
movq %rax, %rbx
jmp 0x43f9a
movq %rax, %rbx
jmp 0x43f73
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x27920
leaq 0x8(%rsp), %rdi
callq 0x27430
leaq 0x50(%rsp), %rdi
callq 0x2e37a
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x43f9a
callq 0x27560
movq %rsp, %rdi
callq 0x27110
jmp 0x43fc0
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x43fb6
callq 0x27560
leaq 0x68(%rsp), %rdi
callq 0x27170
movq %rbx, %rdi
callq 0x27b10
movq %rax, %rdi
callq 0x2e1cd