// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x240, %rsp            # imm = 0x240
movq %rdi, %rbx
movq (%rdi), %r14
cmpb $0x0, (%r14)
jne 0x304ae
cmpb $0x0, 0x1(%r14)
jne 0x304ae
addq $0x8, %r14
callq 0x646e2
movq %r14, %rdi
movq %rax, %rsi
callq 0x2e1b0
movq (%rbx), %rax
cmpb $0x1, 0x1(%rax)
jne 0x305ae
movq 0x20(%rax), %rsi
leaq 0x38(%rsp), %r14
movq %r14, %rdi
movl $0x8, %edx
callq 0x27ba0
leaq 0xb0(%rsp), %rdi
callq 0x27130
movl %eax, %ebp
testb %al, %al
je 0x30544
leaq 0x20(%rsp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
leaq 0x10(%rsp), %r15
movq 0x38(%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 0x3058c
movq (%rbx), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x2e1b0
jmp 0x30501
movq 0x5fa95(%rip), %r14     # 0x8ffe0
leaq 0x42360(%rip), %rsi     # 0x728b2
movl $0x45, %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 0x5fa63(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
jmp 0x3059b
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x3059b
callq 0x27560
leaq 0x38(%rsp), %rdi
callq 0x27170
testb %bpl, %bpl
je 0x306c9
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x27b40
movq (%rbx), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0x273c0
testb %al, %al
je 0x3067c
leaq 0x38(%rsp), %rdi
callq 0x27220
movq (%rbx), %rdi
cmpb $0x0, 0x2(%rdi)
je 0x306d0
addq $0x40, %rdi
pcmpeqd %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movdqa %xmm0, (%rdx)
leaq 0x8(%rsp), %rsi
callq 0x669d4
testb %al, %al
je 0x306f3
leaq 0x34(%rsp), %rsi
movl $0x1, (%rsi)
leaq 0x8(%rsp), %rdi
callq 0x27770
leaq 0x8(%rsp), %rdi
callq 0x273e0
testb %al, %al
je 0x3075a
leaq 0x14(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x1c(%rsp), %r8
leaq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x27a90
testb %al, %al
je 0x307b4
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
leaq 0x38(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x27140
testb %al, %al
je 0x307af
incl %ebx
jmp 0x30665
movq 0x5f95d(%rip), %rbx     # 0x8ffe0
leaq 0x4226e(%rip), %rsi     # 0x728f8
movl $0x43, %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 0x5f92b(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
xorl %ebx, %ebx
jmp 0x3085f
xorl %ebx, %ebx
jmp 0x30869
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
leaq 0x38(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x27140
testb %al, %al
je 0x3080d
incl %ebx
jmp 0x306dc
movq 0x5f8e6(%rip), %rdi     # 0x8ffe0
leaq 0x4228e(%rip), %rsi     # 0x7298f
movl $0x2f, %edx
callq 0x276c0
movq (%rbx), %rax
movq 0x40(%rax), %rsi
movq 0x48(%rax), %rdx
movq 0x5f8c3(%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
movl $0x5b, %edx
leaq 0x41aa2(%rip), %rsi     # 0x721fa
jmp 0x307c0
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
leaq 0x38(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x27140
testb %al, %al
je 0x307af
movl 0xe0(%rsp), %eax
cmpl 0x10(%rsp), %eax
jl 0x30766
movl 0xe4(%rsp), %ecx
movl 0x58(%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 0x30766
movb $0x1, %bpl
jmp 0x30808
movl $0x52, %edx
leaq 0x4217c(%rip), %rsi     # 0x7293c
movq 0x5f819(%rip), %rbx     # 0x8ffe0
movq %rbx, %rdi
callq 0x276c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x5f7f3(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
xorl %ebp, %ebp
leaq 0x8(%rsp), %rdi
callq 0x27190
xorl %ebx, %ebx
testb %bpl, %bpl
je 0x30853
movq 0x5f77c(%rip), %rdi     # 0x8ff90
movl %ebx, %esi
callq 0x27ac0
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
movb $0x1, %bl
leaq 0x8(%rsp), %rdi
callq 0x27190
jmp 0x30855
xorl %ebx, %ebx
leaq 0x38(%rsp), %rdi
callq 0x27930
leaq 0x8(%rsp), %rdi
callq 0x27110
movl %ebx, %eax
addq $0x240, %rsp            # imm = 0x240
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x30891
jmp 0x30891
jmp 0x3088c
movq %rax, %rbx
jmp 0x308bc
jmp 0x30891
jmp 0x30891
jmp 0x30891
movq %rax, %rbx
jmp 0x3089e
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x27930
leaq 0x8(%rsp), %rdi
callq 0x27110
jmp 0x308c6
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x308bc
callq 0x27560
leaq 0x38(%rsp), %rdi
callq 0x27170
movq %rbx, %rdi
callq 0x27b20
movq %rax, %rdi
callq 0x2e1ed