// File: /workspace/github_cpp_makefile_star2_O3/zeeev[P]DASVC/bamtools/src/toolkit/bamtools_filter.cpp
// Function: BamTools::FilterTool::FilterToolPrivate::Run()
bool FilterTool::FilterToolPrivate::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 filter ERROR: could not open input BAM file list... Aborting." << endl;
            return false;
        }

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

    // initialize defined properties & user-specified filters
    // quit if failed
    if ( !SetupFilters() )
        return false;

    // open reader without index
    BamMultiReader reader;
    if ( !reader.Open(m_settings->InputFiles) ) {
        cerr << "bamtools filter ERROR: could not open input files for reading." << endl;
        return false;
    }

    // retrieve reader header & reference data
    const string headerText = reader.GetHeaderText();
    filterToolReferences = 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, headerText, filterToolReferences) ) {
        cerr << "bamtools filter ERROR: could not open " << m_settings->OutputFilename << " for writing." << endl;
        reader.Close();
        return false;
    }

    // if no region specified, filter entire file 
    BamAlignment al;
    if ( !m_settings->HasRegion ) {
        while ( reader.GetNextAlignment(al) ) {
            if ( CheckAlignment(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 filter ERROR: set region failed. Check that REGION describes a valid range" << endl;
                    reader.Close();
                    return false;
                } 
              
                // everything checks out, just iterate through specified region, filtering alignments
                while ( reader.GetNextAlignment(al) )
                    if ( CheckAlignment(al) ) 
                        writer.SaveAlignment(al);
                }
            
            // no index data available, we have to iterate through until we
            // find overlapping alignments
            else {
                while ( reader.GetNextAlignment(al) ) {
                    if ( (al.RefID >= region.LeftRefID)  && ((al.Position + al.Length) >= region.LeftPosition) &&
                         (al.RefID <= region.RightRefID) && ( al.Position <= region.RightPosition) ) 
                    {
                        if ( CheckAlignment(al) ) 
                            writer.SaveAlignment(al);
                    }
                }
            }
        } 
        
        // error parsing REGION string
        else {
            cerr << "bamtools filter 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();
            return false;
        }
    }

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x260, %rsp            # imm = 0x260
movq %rdi, %rbx
movq 0x18(%rdi), %r14
cmpb $0x0, (%r14)
jne 0x34549
cmpb $0x0, 0x1(%r14)
jne 0x34549
addq $0x8, %r14
callq 0x641a2
movq %r14, %rdi
movq %rax, %rsi
callq 0x2e190
movq 0x18(%rbx), %r14
cmpb $0x1, 0x1(%r14)
jne 0x3464a
movq 0x20(%r14), %rsi
leaq 0x58(%rsp), %r14
movq %r14, %rdi
movl $0x8, %edx
callq 0x27b90
leaq 0xd0(%rsp), %rdi
callq 0x27130
testb %al, %al
je 0x345dc
leaq 0x40(%rsp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
leaq 0x30(%rsp), %r15
movq 0x58(%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 0x34631
movq 0x18(%rbx), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x2e190
jmp 0x34598
movq 0x5a9fd(%rip), %rbx     # 0x8efe0
leaq 0x3d7d9(%rip), %rsi     # 0x71dc3
movl $0x46, %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 0x5a9cb(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
leaq 0x58(%rsp), %rdi
callq 0x27170
jmp 0x347ea
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x34640
callq 0x27560
leaq 0x58(%rsp), %rdi
callq 0x27170
movq %rbx, %rdi
callq 0x34b22
testb %al, %al
je 0x347ea
movq %rsp, %r14
movq %r14, %rdi
callq 0x27b30
movq 0x18(%rbx), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0x273c0
testb %al, %al
je 0x347f1
leaq 0x30(%rsp), %rdi
movq %rsp, %rsi
callq 0x274d0
leaq 0x58(%rsp), %rdi
movq %rsp, %rsi
callq 0x27270
leaq 0x5b3ea(%rip), %rdi     # 0x8fa88
leaq 0x58(%rsp), %rsi
callq 0x2e1d8
leaq 0x58(%rsp), %rdi
callq 0x2e37a
movq 0x18(%rbx), %r14
callq 0x641aa
movq 0x48(%r14), %rdx
xorl %ebp, %ebp
cmpq 0x8(%rax), %rdx
jne 0x346e7
testq %rdx, %rdx
je 0x346dc
movq (%rax), %rsi
movq 0x40(%r14), %rdi
callq 0x275f0
testl %eax, %eax
jne 0x346e7
movq 0x18(%rbx), %rax
movzbl 0x5(%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 0x18(%rbx), %rsi
addq $0x40, %rsi
leaq 0x5b375(%rip), %rcx     # 0x8fa88
leaq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x279a0
testb %al, %al
je 0x3483e
leaq 0x58(%rsp), %rdi
callq 0x27220
movq 0x18(%rbx), %rdi
cmpb $0x0, 0x3(%rdi)
je 0x348bb
addq $0x60, %rdi
pcmpeqd %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movdqa %xmm0, (%rdx)
movq %rsp, %rsi
callq 0x6643c
testb %al, %al
je 0x348fb
leaq 0x54(%rsp), %rsi
movl $0x1, (%rsi)
movq %rsp, %rdi
callq 0x27760
movq %rsp, %rdi
callq 0x273e0
testb %al, %al
je 0x349a0
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 0x34a1e
addq $0x20, %rbx
movq %rsp, %r14
leaq 0x58(%rsp), %r15
leaq 0x8(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x272d0
testb %al, %al
je 0x34a08
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3bfce
testb %al, %al
je 0x347bb
movq %r12, %rdi
movq %r15, %rsi
callq 0x27bd0
jmp 0x347bb
xorl %ebx, %ebx
jmp 0x34a7b
movq 0x5a7e8(%rip), %rbx     # 0x8efe0
leaq 0x3d60b(%rip), %rsi     # 0x71e0a
movl $0x3e, %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 0x5a7b6(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
xorl %ebx, %ebx
jmp 0x34a73
movq 0x5a79b(%rip), %rdi     # 0x8efe0
leaq 0x3d5fd(%rip), %rsi     # 0x71e49
movl $0x26, %edx
callq 0x276b0
movq 0x18(%rbx), %rax
movq 0x40(%rax), %rsi
movq 0x48(%rax), %rdx
movq 0x5a777(%rip), %rdi     # 0x8efe0
callq 0x276b0
movq %rax, %rbx
leaq 0x3d5f8(%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 0x34a56
addq $0x20, %rbx
movq %rsp, %r14
leaq 0x58(%rsp), %r15
leaq 0x8(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x272d0
testb %al, %al
je 0x34a08
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3bfce
testb %al, %al
je 0x348cc
movq %r12, %rdi
movq %r15, %rsi
callq 0x27bd0
jmp 0x348cc
movq 0x5a6de(%rip), %rdi     # 0x8efe0
leaq 0x3d5c9(%rip), %rsi     # 0x71ed2
movl $0x2f, %edx
callq 0x276b0
movq 0x18(%rbx), %rax
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
movq 0x5a6ba(%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 0x5a684(%rip), %rbx     # 0x8efe0
leaq 0x3c897(%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 0x5a652(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
jmp 0x34a42
addq $0x20, %rbx
movq %rsp, %r14
leaq 0x58(%rsp), %r15
leaq 0x8(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x272d0
testb %al, %al
je 0x34a08
movl 0x100(%rsp), %eax
cmpl 0x10(%rsp), %eax
jl 0x349b1
movl 0x104(%rsp), %ecx
movl 0x78(%rsp), %edx
addl %ecx, %edx
cmpl 0x14(%rsp), %edx
jl 0x349b1
cmpl 0x18(%rsp), %eax
jg 0x349b1
cmpl 0x1c(%rsp), %ecx
jg 0x349b1
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3bfce
testb %al, %al
je 0x349b1
movq %r12, %rdi
movq %r15, %rsi
callq 0x27bd0
jmp 0x349b1
movq %rsp, %rdi
callq 0x27190
movb $0x1, %bl
leaq 0x8(%rsp), %rdi
callq 0x27370
jmp 0x34a4c
movq 0x5a5bb(%rip), %rdi     # 0x8efe0
leaq 0x3d452(%rip), %rsi     # 0x71e7e
movl $0x53, %edx
callq 0x276b0
movq 0x5a5a3(%rip), %rdi     # 0x8efe0
callq 0x27310
movq %rsp, %rdi
callq 0x27190
xorl %ebx, %ebx
leaq 0x58(%rsp), %rdi
callq 0x27920
leaq 0x8(%rsp), %rdi
callq 0x27430
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x34a73
callq 0x27560
movq %rsp, %rdi
callq 0x27110
movl %ebx, %eax
addq $0x260, %rsp            # imm = 0x260
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x34ac2
jmp 0x34ac2
jmp 0x34ab8
jmp 0x34aaa
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x2e37a
jmp 0x34ad9
jmp 0x34abd
jmp 0x34ac2
jmp 0x34aaa
movq %rax, %rbx
jmp 0x34ad9
jmp 0x34ac2
jmp 0x34ac2
movq %rax, %rbx
jmp 0x34b08
movq %rax, %rbx
jmp 0x34acf
movq %rax, %rbx
jmp 0x34aec
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x27920
leaq 0x8(%rsp), %rdi
callq 0x27430
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x34aec
callq 0x27560
movq %rsp, %rdi
callq 0x27110
jmp 0x34b12
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x34b08
callq 0x27560
leaq 0x58(%rsp), %rdi
callq 0x27170
movq %rbx, %rdi
callq 0x27b10
movq %rax, %rdi
callq 0x2e1cd