// File: /workspace/github_cpp_makefile_star2_O3/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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rdi, %rbx
movq (%rdi), %r14
cmpb $0x0, (%r14)
jne 0x304b0
cmpb $0x0, 0x1(%r14)
jne 0x304b0
addq $0x8, %r14
callq 0x641a2
movq %r14, %rdi
movq %rax, %rsi
callq 0x2e190
movq (%rbx), %r14
cmpb $0x1, 0x1(%r14)
jne 0x305b2
movq 0x20(%r14), %rsi
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movl $0x8, %edx
callq 0x27b90
leaq 0xb8(%rsp), %rdi
callq 0x27130
testb %al, %al
je 0x30542
leaq 0x20(%rsp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
leaq 0x10(%rsp), %r15
movq 0x40(%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 0x30599
movq (%rbx), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x2e190
jmp 0x304ff
movq 0x5ea97(%rip), %rbx     # 0x8efe0
leaq 0x41362(%rip), %rsi     # 0x718b2
movl $0x45, %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 0x5ea65(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
leaq 0x40(%rsp), %rdi
callq 0x27170
xorl %ebx, %ebx
jmp 0x3085b
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x305a8
callq 0x27560
leaq 0x40(%rsp), %rdi
callq 0x27170
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x27b30
movq (%rbx), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0x273c0
testb %al, %al
je 0x30680
leaq 0x40(%rsp), %rdi
callq 0x27220
movq (%rbx), %rdi
cmpb $0x0, 0x2(%rdi)
je 0x306cd
addq $0x40, %rdi
pcmpeqd %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movdqa %xmm0, (%rdx)
leaq 0x8(%rsp), %rsi
callq 0x6643c
testb %al, %al
je 0x306f0
leaq 0x3c(%rsp), %rsi
movl $0x1, (%rsi)
leaq 0x8(%rsp), %rdi
callq 0x27760
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 0x27a80
testb %al, %al
je 0x307f5
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
leaq 0x40(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x27140
testb %al, %al
je 0x307af
incl %ebx
jmp 0x30669
movq 0x5e959(%rip), %rbx     # 0x8efe0
leaq 0x4126a(%rip), %rsi     # 0x718f8
movl $0x43, %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 0x5e927(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
xorl %ebx, %ebx
jmp 0x30851
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
leaq 0x40(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x27140
testb %al, %al
je 0x307af
incl %ebx
jmp 0x306d9
movq 0x5e8e9(%rip), %rdi     # 0x8efe0
leaq 0x41291(%rip), %rsi     # 0x7198f
movl $0x2f, %edx
callq 0x276b0
movq (%rbx), %rax
movq 0x40(%rax), %rsi
movq 0x48(%rax), %rdx
movq 0x5e8c6(%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
movl $0x5b, %edx
leaq 0x40aa5(%rip), %rsi     # 0x711fa
jmp 0x30801
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
leaq 0x40(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x27140
testb %al, %al
je 0x307af
movl 0xe8(%rsp), %eax
cmpl 0x10(%rsp), %eax
jl 0x30766
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 0x30766
movq 0x5e7da(%rip), %rdi     # 0x8ef90
movl %ebx, %esi
callq 0x27ab0
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 0x30847
movl $0x52, %edx
leaq 0x4113b(%rip), %rsi     # 0x7193c
movq 0x5e7d8(%rip), %rbx     # 0x8efe0
movq %rbx, %rdi
callq 0x276b0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x5e7b2(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
leaq 0x8(%rsp), %rdi
callq 0x27190
xorl %ebx, %ebx
leaq 0x40(%rsp), %rdi
callq 0x27920
leaq 0x8(%rsp), %rdi
callq 0x27110
movl %ebx, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r14
popq %r15
retq
jmp 0x30882
jmp 0x30882
jmp 0x3087d
movq %rax, %rbx
jmp 0x308ad
jmp 0x30882
jmp 0x30882
jmp 0x30882
movq %rax, %rbx
jmp 0x3088f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x27920
leaq 0x8(%rsp), %rdi
callq 0x27110
jmp 0x308b7
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x308ad
callq 0x27560
leaq 0x40(%rsp), %rdi
callq 0x27170
movq %rbx, %rdi
callq 0x27b10
movq %rax, %rdi
callq 0x2e1cd
nop