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

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

    // open reader without index
    BamMultiReader reader;
    if ( !reader.Open(m_settings->InputFiles) ) {
        cerr << "bamtools count ERROR: could not open input BAM file(s)... Aborting." << endl;
        return false;
    }

    // alignment counter
    BamAlignment al;
    int alignmentCount(0);

    // if no region specified, count entire file
    if ( !m_settings->HasRegion ) {
        while ( reader.GetNextAlignmentCore(al) )
            ++alignmentCount;
    }

    // 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 set region on reader
                if ( !reader.SetRegion(region.LeftRefID, region.LeftPosition, region.RightRefID, region.RightPosition) ) {
                    cerr << "bamtools count ERROR: set region failed. Check that REGION describes a valid range" << endl;
                    reader.Close();
                    return false;
                }

                // everything checks out, just iterate through specified region, counting alignments
                while ( reader.GetNextAlignmentCore(al) )
                    ++alignmentCount;
            }

            // no index data available, we have to iterate through until we
            // find overlapping alignments
            else {
                while ( reader.GetNextAlignmentCore(al) ) {
                    if ( (al.RefID >= region.LeftRefID)  && ( (al.Position + al.Length) >= region.LeftPosition ) &&
                          (al.RefID <= region.RightRefID) && ( al.Position <= region.RightPosition) )
                    {
                        ++alignmentCount;
                    }
                }
            }
        }

        // error parsing REGION string
        else {
            cerr << "bamtools count 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;
        }
    }

    // print results
    cout << alignmentCount << endl;

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rdi, %rbx
movq (%rdi), %r14
cmpb $0x0, (%r14)
jne 0x3bd5f
cmpb $0x0, 0x1(%r14)
jne 0x3bd5f
addq $0x8, %r14
callq 0x65ebe
movq %r14, %rdi
movq %rax, %rsi
callq 0x3a16c
movq (%rbx), %r14
cmpb $0x1, 0x1(%r14)
jne 0x3be0d
movq 0x20(%r14), %rsi
leaq 0x40(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x35bd0
leaq 0xb8(%rsp), %rdi
callq 0x35130
movl %eax, %ebp
testb %al, %al
je 0x3bdd3
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x40(%rsp), %r14
leaq 0x10(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x35100
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x3bdf0
movq (%rbx), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x3a16c
jmp 0x3bda9
movq 0x59206(%rip), %rdi     # 0x94fe0
leaq 0x35a6d(%rip), %rsi     # 0x7184e
callq 0x355b0
movq %rax, %rdi
callq 0x35300
jmp 0x3bdfa
leaq 0x10(%rsp), %rdi
callq 0x35730
leaq 0x40(%rsp), %rdi
callq 0x35170
testb %bpl, %bpl
je 0x3befd
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x35b70
movq (%rbx), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0x353c0
testb %al, %al
je 0x3bedb
leaq 0x40(%rsp), %rdi
callq 0x35200
movq (%rbx), %rdi
cmpb $0x0, 0x2(%rdi)
je 0x3bf04
addq $0x40, %rdi
pcmpeqd %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movdqa %xmm0, (%rdx)
leaq 0x8(%rsp), %rsi
callq 0x67976
testb %al, %al
je 0x3bf27
leaq 0x3c(%rsp), %rsi
movl $0x1, (%rsi)
leaq 0x8(%rsp), %rdi
callq 0x35780
leaq 0x8(%rsp), %rdi
callq 0x353e0
testb %al, %al
je 0x3bf7a
leaq 0x14(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x1c(%rsp), %r8
leaq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x35ad0
testb %al, %al
je 0x3c015
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
leaq 0x40(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x35140
testb %al, %al
je 0x3bfcf
incl %ebx
jmp 0x3bec4
movq 0x590fe(%rip), %rdi     # 0x94fe0
leaq 0x359ab(%rip), %rsi     # 0x71894
callq 0x355b0
movq %rax, %rdi
callq 0x35300
xorl %ebx, %ebx
jmp 0x3bffb
xorl %ebx, %ebx
jmp 0x3c005
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
leaq 0x40(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x35140
testb %al, %al
je 0x3bfcf
incl %ebx
jmp 0x3bf10
movq 0x590b2(%rip), %rdi     # 0x94fe0
leaq 0x359f6(%rip), %rsi     # 0x7192b
callq 0x355b0
movq (%rbx), %rsi
addq $0x40, %rsi
movq %rax, %rdi
callq 0x35510
movq %rax, %rdi
callq 0x35300
leaq 0x352ac(%rip), %rsi     # 0x71204
movq 0x59081(%rip), %rdi     # 0x94fe0
callq 0x355b0
movq %rax, %rdi
callq 0x35300
leaq 0x8(%rsp), %rdi
callq 0x35190
xorl %ebx, %ebx
jmp 0x3bff1
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
leaq 0x40(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x35140
testb %al, %al
je 0x3bfcf
movl 0xe8(%rsp), %eax
cmpl 0x10(%rsp), %eax
jl 0x3bf86
movl 0xec(%rsp), %ecx
movl 0x60(%rsp), %edx
addl %ecx, %edx
cmpl 0x14(%rsp), %edx
setge %dl
cmpl 0x18(%rsp), %eax
setle %al
cmpl 0x1c(%rsp), %ecx
setle %cl
andb %al, %cl
andb %dl, %cl
movzbl %cl, %eax
addl %eax, %ebx
jmp 0x3bf86
movq 0x58fc2(%rip), %rdi     # 0x94f98
movl %ebx, %esi
callq 0x35b00
movq %rax, %rdi
callq 0x35300
movb $0x1, %bl
leaq 0x8(%rsp), %rdi
callq 0x35190
leaq 0x40(%rsp), %rdi
callq 0x35940
leaq 0x8(%rsp), %rdi
callq 0x35110
movl %ebx, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x358bc(%rip), %rsi     # 0x718d8
jmp 0x3bf58
jmp 0x3c039
jmp 0x3c025
movq %rax, %rbx
jmp 0x3c05f
jmp 0x3c039
jmp 0x3c034
jmp 0x3c039
jmp 0x3c039
jmp 0x3c039
movq %rax, %rbx
jmp 0x3c046
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x35940
leaq 0x8(%rsp), %rdi
callq 0x35110
jmp 0x3c069
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x35730
leaq 0x40(%rsp), %rdi
callq 0x35170
movq %rbx, %rdi
callq 0x35b50
movq %rax, %rdi
callq 0x3a198
nop