// File: /workspace/github_cpp_makefile_star2_O2/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 0x3ef7d
cmpb $0x0, 0x1(%r14)
jne 0x3ef7d
addq $0x8, %r14
callq 0x65ebe
movq %r14, %rdi
movq %rax, %rsi
callq 0x3a16c
movq 0x18(%rbx), %r14
cmpb $0x1, 0x1(%r14)
jne 0x3f02c
movq 0x20(%r14), %rsi
leaq 0x58(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x35bd0
leaq 0xd0(%rsp), %rdi
callq 0x35130
movl %eax, %ebp
testb %al, %al
je 0x3eff2
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x58(%rsp), %r14
leaq 0x30(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x35100
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x3f00f
movq 0x18(%rbx), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x3a16c
jmp 0x3efc7
movq 0x55fe7(%rip), %rdi     # 0x94fe0
leaq 0x32d50(%rip), %rsi     # 0x71d50
callq 0x355b0
movq %rax, %rdi
callq 0x35300
jmp 0x3f019
leaq 0x30(%rsp), %rdi
callq 0x35730
leaq 0x58(%rsp), %rdi
callq 0x35170
testb %bpl, %bpl
je 0x3f1c0
movq %rbx, %rdi
callq 0x3f40e
testb %al, %al
je 0x3f1c0
movq %rsp, %r14
movq %r14, %rdi
callq 0x35b70
movq 0x18(%rbx), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0x353c0
testb %al, %al
je 0x3f1c7
leaq 0x30(%rsp), %rdi
movq %rsp, %rsi
callq 0x354d0
leaq 0x58(%rsp), %rdi
movq %rsp, %rsi
callq 0x35250
leaq 0x56a48(%rip), %rdi     # 0x95ac8
leaq 0x58(%rsp), %rsi
callq 0x3a1a4
leaq 0x58(%rsp), %rdi
callq 0x3a26c
movq 0x18(%rbx), %r14
callq 0x65ec6
addq $0x40, %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x3a2a9
xorl %ecx, %ecx
testb %al, %al
je 0x3f0bd
movq 0x18(%rbx), %rax
movzbl 0x5(%rax), %ecx
xorl $0x1, %ecx
movl %ecx, 0x2c(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x35350
leaq 0x8(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
callq 0x35230
movq 0x18(%rbx), %rsi
addq $0x40, %rsi
leaq 0x569df(%rip), %rcx     # 0x95ac8
leaq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x359d0
testb %al, %al
je 0x3f1e9
leaq 0x58(%rsp), %rdi
callq 0x35200
movq 0x18(%rbx), %rdi
cmpb $0x0, 0x3(%rdi)
je 0x3f232
addq $0x60, %rdi
pcmpeqd %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movdqa %xmm0, (%rdx)
movq %rsp, %rsi
callq 0x67976
testb %al, %al
je 0x3f272
leaq 0x54(%rsp), %rsi
movl $0x1, (%rsi)
movq %rsp, %rdi
callq 0x35780
movq %rsp, %rdi
callq 0x353e0
testb %al, %al
je 0x3f2c4
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 0x3f378
addq $0x20, %rbx
movq %rsp, %r14
leaq 0x58(%rsp), %r15
leaq 0x8(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x352b0
testb %al, %al
je 0x3f32c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x45624
testb %al, %al
je 0x3f191
movq %r12, %rdi
movq %r15, %rsi
callq 0x35c00
jmp 0x3f191
xorl %ebx, %ebx
jmp 0x3f366
movq 0x55e12(%rip), %rdi     # 0x94fe0
leaq 0x32bc2(%rip), %rsi     # 0x71d97
callq 0x355b0
movq %rax, %rdi
callq 0x35300
xorl %ebx, %ebx
jmp 0x3f35e
movq 0x55df0(%rip), %rdi     # 0x94fe0
leaq 0x32bdf(%rip), %rsi     # 0x71dd6
callq 0x355b0
movq 0x18(%rbx), %rsi
addq $0x40, %rsi
movq %rax, %rdi
callq 0x35510
leaq 0x32bea(%rip), %rsi     # 0x71dfd
movq %rax, %rdi
callq 0x355b0
movq %rax, %rdi
callq 0x35300
movq %rsp, %rdi
callq 0x35190
xorl %ebx, %ebx
jmp 0x3f34a
addq $0x20, %rbx
movq %rsp, %r14
leaq 0x58(%rsp), %r15
leaq 0x8(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x352b0
testb %al, %al
je 0x3f32c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x45624
testb %al, %al
je 0x3f243
movq %r12, %rdi
movq %r15, %rsi
callq 0x35c00
jmp 0x3f243
movq 0x55d67(%rip), %rdi     # 0x94fe0
leaq 0x32bdf(%rip), %rsi     # 0x71e5f
callq 0x355b0
movq 0x18(%rbx), %rsi
addq $0x60, %rsi
movq %rax, %rdi
callq 0x35510
movq %rax, %rdi
callq 0x35300
leaq 0x31f60(%rip), %rsi     # 0x71204
movq 0x55d35(%rip), %rdi     # 0x94fe0
callq 0x355b0
movq %rax, %rdi
callq 0x35300
movq %rsp, %rdi
callq 0x35190
xorl %ebx, %ebx
jmp 0x3f340
addq $0x20, %rbx
movq %rsp, %r14
leaq 0x58(%rsp), %r15
leaq 0x8(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
callq 0x352b0
testb %al, %al
je 0x3f32c
movl 0x100(%rsp), %eax
cmpl 0x10(%rsp), %eax
jl 0x3f2d5
movl 0x104(%rsp), %ecx
movl 0x78(%rsp), %edx
addl %ecx, %edx
cmpl 0x14(%rsp), %edx
jl 0x3f2d5
cmpl 0x18(%rsp), %eax
jg 0x3f2d5
cmpl 0x1c(%rsp), %ecx
jg 0x3f2d5
movq %rbx, %rdi
movq %r15, %rsi
callq 0x45624
testb %al, %al
je 0x3f2d5
movq %r12, %rdi
movq %r15, %rsi
callq 0x35c00
jmp 0x3f2d5
movq %rsp, %rdi
callq 0x35190
movb $0x1, %bl
leaq 0x8(%rsp), %rdi
callq 0x35360
leaq 0x58(%rsp), %rdi
callq 0x35940
leaq 0x8(%rsp), %rdi
callq 0x35420
leaq 0x30(%rsp), %rdi
callq 0x35730
movq %rsp, %rdi
callq 0x35110
movl %ebx, %eax
addq $0x260, %rsp            # imm = 0x260
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x32a8c(%rip), %rsi     # 0x71e0b
jmp 0x3f2a4
jmp 0x3f3bb
jmp 0x3f3bb
jmp 0x3f3b6
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x3a26c
jmp 0x3f3d2
jmp 0x3f3bb
jmp 0x3f3aa
jmp 0x3f3a3
jmp 0x3f3b1
jmp 0x3f3a3
movq %rax, %rbx
jmp 0x3f3d2
jmp 0x3f3bb
movq %rax, %rbx
jmp 0x3f3f3
jmp 0x3f3bb
movq %rax, %rbx
jmp 0x3f3dc
movq %rax, %rbx
jmp 0x3f3c8
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x35940
leaq 0x8(%rsp), %rdi
callq 0x35420
leaq 0x30(%rsp), %rdi
callq 0x35730
movq %rsp, %rdi
callq 0x35110
jmp 0x3f3fd
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x35730
leaq 0x58(%rsp), %rdi
callq 0x35170
movq %rbx, %rdi
callq 0x35b50
movq %rax, %rdi
callq 0x3a198
nop