// File: /workspace/github_cpp_makefile_star2_O1/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 0x3457f
cmpb $0x0, 0x1(%r14)
jne 0x3457f
addq $0x8, %r14
callq 0x646e2
movq %r14, %rdi
movq %rax, %rsi
callq 0x2e1b0
movq 0x18(%rbx), %rax
cmpb $0x1, 0x1(%rax)
jne 0x34681
movq 0x20(%rax), %rsi
leaq 0x58(%rsp), %r14
movq %r14, %rdi
movl $0x8, %edx
callq 0x27ba0
leaq 0xd0(%rsp), %rdi
callq 0x27130
movl %eax, %ebp
testb %al, %al
je 0x34617
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 0x27bb0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x3465f
movq 0x18(%rbx), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x2e1b0
jmp 0x345d3
movq 0x5b9c2(%rip), %r14     # 0x8ffe0
leaq 0x3e79e(%rip), %rsi     # 0x72dc3
movl $0x46, %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 0x5b990(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
jmp 0x3466e
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x3466e
callq 0x27560
leaq 0x58(%rsp), %rdi
callq 0x27170
testb %bpl, %bpl
je 0x34821
movq %rbx, %rdi
callq 0x34b5a
testb %al, %al
je 0x34821
movq %rsp, %r14
movq %r14, %rdi
callq 0x27b40
movq 0x18(%rbx), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0x273c0
testb %al, %al
je 0x34828
leaq 0x30(%rsp), %rdi
movq %rsp, %rsi
callq 0x274d0
leaq 0x58(%rsp), %rdi
movq %rsp, %rsi
callq 0x27270
leaq 0x5c3bb(%rip), %rdi     # 0x90a90
leaq 0x58(%rsp), %rsi
callq 0x2e1f8
leaq 0x58(%rsp), %rdi
callq 0x2e378
movq 0x18(%rbx), %r14
callq 0x646ea
movq 0x48(%r14), %rdx
xorl %ebp, %ebp
cmpq 0x8(%rax), %rdx
jne 0x3471e
testq %rdx, %rdx
je 0x34713
movq (%rax), %rsi
movq 0x40(%r14), %rdi
callq 0x275f0
testl %eax, %eax
jne 0x3471e
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 0x5c346(%rip), %rcx     # 0x90a90
leaq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x279b0
testb %al, %al
je 0x34875
leaq 0x58(%rsp), %rdi
callq 0x27220
movq 0x18(%rbx), %rdi
cmpb $0x0, 0x3(%rdi)
je 0x348f2
addq $0x60, %rdi
pcmpeqd %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movdqa %xmm0, (%rdx)
movq %rsp, %rsi
callq 0x669d4
testb %al, %al
je 0x34932
leaq 0x54(%rsp), %rsi
movl $0x1, (%rsi)
movq %rsp, %rdi
callq 0x27770
movq %rsp, %rdi
callq 0x273e0
testb %al, %al
je 0x349d7
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 0x34a55
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 0x34a3f
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3c03e
testb %al, %al
je 0x347f2
movq %r12, %rdi
movq %r15, %rsi
callq 0x27be0
jmp 0x347f2
xorl %ebx, %ebx
jmp 0x34ab2
movq 0x5b7b1(%rip), %rbx     # 0x8ffe0
leaq 0x3e5d4(%rip), %rsi     # 0x72e0a
movl $0x3e, %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 0x5b77f(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
xorl %ebx, %ebx
jmp 0x34aaa
movq 0x5b764(%rip), %rdi     # 0x8ffe0
leaq 0x3e5c6(%rip), %rsi     # 0x72e49
movl $0x26, %edx
callq 0x276c0
movq 0x18(%rbx), %rax
movq 0x40(%rax), %rsi
movq 0x48(%rax), %rdx
movq 0x5b740(%rip), %rdi     # 0x8ffe0
callq 0x276c0
movq %rax, %rbx
leaq 0x3e5c1(%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 0x34a8d
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 0x34a3f
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3c03e
testb %al, %al
je 0x34903
movq %r12, %rdi
movq %r15, %rsi
callq 0x27be0
jmp 0x34903
movq 0x5b6a7(%rip), %rdi     # 0x8ffe0
leaq 0x3e592(%rip), %rsi     # 0x72ed2
movl $0x2f, %edx
callq 0x276c0
movq 0x18(%rbx), %rax
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
movq 0x5b683(%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 0x5b64d(%rip), %rbx     # 0x8ffe0
leaq 0x3d860(%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 0x5b61b(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
jmp 0x34a79
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 0x34a3f
movl 0x100(%rsp), %eax
cmpl 0x10(%rsp), %eax
jl 0x349e8
movl 0x104(%rsp), %ecx
movl 0x78(%rsp), %edx
addl %ecx, %edx
cmpl 0x14(%rsp), %edx
jl 0x349e8
cmpl 0x18(%rsp), %eax
jg 0x349e8
cmpl 0x1c(%rsp), %ecx
jg 0x349e8
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3c03e
testb %al, %al
je 0x349e8
movq %r12, %rdi
movq %r15, %rsi
callq 0x27be0
jmp 0x349e8
movq %rsp, %rdi
callq 0x27190
movb $0x1, %bl
leaq 0x8(%rsp), %rdi
callq 0x27370
jmp 0x34a83
movq 0x5b584(%rip), %rdi     # 0x8ffe0
leaq 0x3e41b(%rip), %rsi     # 0x72e7e
movl $0x53, %edx
callq 0x276c0
movq 0x5b56c(%rip), %rdi     # 0x8ffe0
callq 0x27310
movq %rsp, %rdi
callq 0x27190
xorl %ebx, %ebx
leaq 0x58(%rsp), %rdi
callq 0x27930
leaq 0x8(%rsp), %rdi
callq 0x27430
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x34aaa
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 0x34af9
jmp 0x34af9
jmp 0x34aef
jmp 0x34ae1
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x2e378
jmp 0x34b10
jmp 0x34af4
jmp 0x34af9
jmp 0x34ae1
movq %rax, %rbx
jmp 0x34b10
jmp 0x34af9
jmp 0x34af9
movq %rax, %rbx
jmp 0x34b3f
movq %rax, %rbx
jmp 0x34b06
movq %rax, %rbx
jmp 0x34b23
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x27930
leaq 0x8(%rsp), %rdi
callq 0x27430
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x34b23
callq 0x27560
movq %rsp, %rdi
callq 0x27110
jmp 0x34b49
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x34b3f
callq 0x27560
leaq 0x58(%rsp), %rdi
callq 0x27170
movq %rbx, %rdi
callq 0x27b20
movq %rax, %rdi
callq 0x2e1ed
nop