// File: /workspace/github_cpp_makefile_star2_O3/zeeev[P]DASVC/bamtools/src/toolkit/bamtools_resolve.cpp
// Function: BamTools::ResolveTool::ResolveToolPrivate::MakeStats()
bool ResolveTool::ResolveToolPrivate::MakeStats(void) {

    // pull resolver settings from command-line settings
    ReadGroupResolver::SetConfidenceInterval(m_settings->ConfidenceInterval);
    ReadGroupResolver::SetUnusedModelThreshold(m_settings->UnusedModelThreshold);

    // open our BAM reader
    BamReader bamReader;
    if ( !bamReader.Open(m_settings->InputBamFilename) ) {
        cerr << "bamtools resolve ERROR: could not open input BAM file: "
             << m_settings->InputBamFilename << endl;
        return false;
    }

    // retrieve header & parse for read groups
    const SamHeader& header = bamReader.GetHeader();
    ParseHeader(header);

    // open ReadNamesFileWriter
    ResolveTool::ReadNamesFileWriter readNamesWriter;
    if ( !readNamesWriter.Open(m_settings->ReadNamesFilename) ) {
        cerr << "bamtools resolve ERROR: could not open (temp) output read names file: "
             << m_settings->ReadNamesFilename << endl;
        bamReader.Close();
        return false;
    }

    // read through BAM file
    BamAlignment al;
    string readGroup("");
    map<string, ReadGroupResolver>::iterator rgIter;
    map<string, bool>::iterator readNameIter;
    while ( bamReader.GetNextAlignmentCore(al) ) {

        // skip if alignment is not paired, mapped, nor mate is mapped
        if ( !al.IsPaired() || !al.IsMapped() || !al.IsMateMapped() )
            continue;

        // skip if alignment & mate not on same reference sequence
        if ( al.RefID != al.MateRefID ) continue;

        // flesh out the char data, so we can retrieve its read group ID
        al.BuildCharData();

        // get read group from alignment (OK if empty)
        readGroup.clear();
        al.GetTag(READ_GROUP_TAG, readGroup);

        // look up resolver for read group
        rgIter = m_readGroups.find(readGroup);
        if ( rgIter == m_readGroups.end() )  {
            cerr << "bamtools resolve ERROR - unable to calculate stats, unknown read group encountered: "
                 << readGroup << endl;
            bamReader.Close();
            return false;
        }
        ReadGroupResolver& resolver = (*rgIter).second;

        // determine unique-ness of current alignment
        const bool isCurrentMateUnique = ( al.MapQuality >= m_settings->MinimumMapQuality );

        // look up read name
        readNameIter = resolver.ReadNames.find(al.Name);

        // if read name found (current alignment's mate already parsed)
        if ( readNameIter != resolver.ReadNames.end() ) {

            // if both unique mates are unique, store read name & insert size for later
            const bool isStoredMateUnique  = (*readNameIter).second;
            if ( isCurrentMateUnique && isStoredMateUnique ) {

                // save read name in temp file as candidates for later pair marking
                readNamesWriter.Write(readGroup, al.Name);

                // determine model type & store fragment length for stats calculation
                const uint16_t currentModelType = CalculateModelType(al);
                assert( currentModelType != ModelType::DUMMY_ID );
                resolver.Models[currentModelType].push_back( abs(al.InsertSize) );
            }

            // unique or not, remove read name from map
            resolver.ReadNames.erase(readNameIter);
        }

        // if read name not found, store new entry
        else resolver.ReadNames.insert( make_pair<string, bool>(al.Name, isCurrentMateUnique) );
    }

    // close files
    readNamesWriter.Close();
    bamReader.Close();

    // iterate back through read groups
    map<string, ReadGroupResolver>::iterator rgEnd  = m_readGroups.end();
    for ( rgIter = m_readGroups.begin(); rgIter != rgEnd; ++rgIter ) {
        const string& name = (*rgIter).first;
        ReadGroupResolver& resolver = (*rgIter).second;

        // calculate acceptable orientation & insert sizes for this read group
        resolver.DetermineTopModels(name);

        // clear out left over read names
        // (these have mates that did not pass filters or were already removed as non-unique)
        resolver.ReadNames.clear();
    }

    // if we get here, return success
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x568, %rsp            # imm = 0x568
movq %rdi, %r15
movq (%rdi), %rax
movsd 0x90(%rax), %xmm0
movsd %xmm0, 0x4722d(%rip)    # 0x8f638
movsd 0xa0(%rax), %xmm0
movsd %xmm0, 0x47225(%rip)    # 0x8f640
movq %rsp, %r14
movq %r14, %rdi
callq 0x273f0
movq (%r15), %rsi
addq $0x10, %rsi
movq %r14, %rdi
callq 0x27790
testb %al, %al
je 0x487a1
leaq 0x410(%rsp), %rdi
movq %rsp, %rsi
callq 0x277c0
leaq 0x410(%rsp), %rsi
movq %r15, %rdi
callq 0x48a5e
leaq 0x210(%rsp), %rdi
callq 0x27a90
movq (%r15), %rsi
addq $0x70, %rsi
leaq 0x210(%rsp), %rdi
callq 0x46c2c
testb %al, %al
je 0x48801
leaq 0xb8(%rsp), %rdi
callq 0x27220
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x2e374(%rip), %rdx     # 0x76817
leaq 0x10(%rsp), %rdi
movq %rdx, %rsi
callq 0x2f1a0
leaq 0x8(%r15), %rax
movq %rax, 0xb0(%rsp)
leaq 0x10(%r15), %rax
movq %rax, 0x8(%rsp)
movq %rsp, %rbx
leaq 0xb8(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x27a40
movl %eax, %ebp
testb %al, %al
je 0x48869
movq %r12, %rdi
callq 0x27330
testb %al, %al
je 0x484d0
movq %r12, %rdi
callq 0x270d0
testb %al, %al
je 0x484d0
movq %r12, %rdi
callq 0x276e0
testb %al, %al
je 0x484d0
movl 0x160(%rsp), %eax
cmpl 0x188(%rsp), %eax
jne 0x484d0
movq %r12, %rdi
callq 0x275d0
movq $0x0, 0x18(%rsp)
movq 0x10(%rsp), %rax
movb $0x0, (%rax)
cmpb $0x0, 0x1e8(%rsp)
jne 0x485c7
movq 0x148(%rsp), %rax
testq %rax, %rax
je 0x485c7
movq 0x140(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movl %eax, 0x68(%rsp)
movl $0x0, 0x34(%rsp)
movq %r12, %rdi
leaq 0x47540(%rip), %rsi     # 0x8fab0
leaq 0x40(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x34(%rsp), %r8
callq 0x27700
testb %al, %al
je 0x485c7
movq 0x40(%rsp), %rdi
callq 0x272f0
movq $0x0, 0x18(%rsp)
movq 0x10(%rsp), %rcx
movb $0x0, (%rcx)
movl %eax, %r14d
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x271f0
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0x274c0
movq 0xb0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x4cc54
movq %rax, %r14
cmpq 0x8(%rsp), %rax
je 0x48947
movzwl 0x16a(%rsp), %ebx
movq %r15, %rbp
movq (%r15), %rax
movzwl 0x98(%rax), %r15d
leaq 0x70(%r14), %rdi
movq %rdi, 0x38(%rsp)
movq %r12, %rsi
callq 0x4d0aa
movq %rax, %r13
leaq 0x78(%r14), %rax
cmpq %rax, %r13
je 0x48691
cmpw %r15w, %bx
movq %rbp, %r15
movq %rsp, %rbx
jb 0x4878f
cmpb $0x1, 0x40(%r13)
jne 0x4878f
leaq 0x210(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %r12, %rdx
callq 0x46c72
movq %r12, %rdi
callq 0x4624f
movq 0x58(%r14), %rcx
movzwl %ax, %edx
shll $0x5, %edx
leaq (%rcx,%rdx), %rdi
movl 0x190(%rsp), %esi
movl %esi, %eax
negl %eax
cmovsl %esi, %eax
movl %eax, 0x40(%rsp)
movq 0x10(%rcx,%rdx), %rsi
cmpq 0x18(%rcx,%rdx), %rsi
je 0x48781
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x10(%rdi)
jmp 0x4878f
leaq 0xa0(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0xb8(%rsp), %rsi
movq 0xc0(%rsp), %rdx
addq %rsi, %rdx
leaq 0x90(%rsp), %rdi
callq 0x2f10a
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %rdx
addq %rsi, %rdx
leaq 0x68(%rsp), %rdi
callq 0x2f10a
cmpw %r15w, %bx
setae 0x88(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x2f10a
movb 0x88(%rsp), %al
movb %al, 0x60(%rsp)
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x4ccb8
movq %rsp, %rbx
movq %rbp, %r15
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x4874a
callq 0x27560
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x4875e
callq 0x27560
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x484d0
callq 0x27560
jmp 0x484d0
addq $0x8, %rdi
leaq 0x40(%rsp), %rdx
callq 0x4b9cc
movq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x4d10e
jmp 0x484d0
movq 0x46838(%rip), %rdi     # 0x8efe0
leaq 0x2b125(%rip), %rsi     # 0x738d4
movl $0x37, %edx
callq 0x276b0
movq (%r15), %rax
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rdx
movq 0x46815(%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
xorl %ebp, %ebp
jmp 0x4892b
movq 0x467d8(%rip), %rdi     # 0x8efe0
leaq 0x2b0fd(%rip), %rsi     # 0x7390c
movl $0x46, %edx
callq 0x276b0
movq (%r15), %rax
movq 0x70(%rax), %rsi
movq 0x78(%rax), %rdx
movq 0x467b5(%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
movq %rsp, %rdi
callq 0x270b0
xorl %ebp, %ebp
jmp 0x48911
leaq 0x280(%rsp), %rdi
callq 0x27130
testb %al, %al
je 0x48887
leaq 0x210(%rsp), %rdi
callq 0x27840
movq %rsp, %rdi
callq 0x270b0
movq 0x20(%r15), %rbx
cmpq 0x8(%rsp), %rbx
je 0x488ec
xorl %r14d, %r14d
leaq 0x20(%rbx), %rsi
leaq 0x40(%rbx), %rdi
callq 0x4645e
leaq 0x70(%rbx), %rdi
movq 0x80(%rbx), %rsi
callq 0x4b57c
leaq 0x78(%rbx), %rax
movq %r14, 0x80(%rbx)
movq %rax, 0x88(%rbx)
movq %rax, 0x90(%rbx)
movq %r14, 0x98(%rbx)
movq %rbx, %rdi
callq 0x27420
movq %rax, %rbx
cmpq 0x8(%rsp), %rax
jne 0x4889d
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x48900
callq 0x27560
leaq 0xb8(%rsp), %rdi
callq 0x27920
xorb $0x1, %bpl
leaq 0x210(%rsp), %rdi
callq 0x4adfe
leaq 0x410(%rsp), %rdi
callq 0x275a0
movq %rsp, %rdi
callq 0x27620
movl %ebp, %eax
addq $0x568, %rsp            # imm = 0x568
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x46692(%rip), %rdi     # 0x8efe0
leaq 0x2affe(%rip), %rsi     # 0x73953
movl $0x54, %edx
callq 0x276b0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x46670(%rip), %rdi     # 0x8efe0
callq 0x276b0
movq %rax, %rdi
callq 0x27310
movq %rsp, %rdi
callq 0x270b0
jmp 0x488ec
jmp 0x48a07
jmp 0x48a07
jmp 0x48a07
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x489ac
callq 0x27560
jmp 0x489ac
movq %rax, %rbx
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x489c5
callq 0x27560
jmp 0x489c5
movq %rax, %rbx
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x48a0a
callq 0x27560
jmp 0x48a0a
movq %rax, %rbx
jmp 0x48a1e
jmp 0x48a07
jmp 0x48a07
jmp 0x48a07
jmp 0x48a07
jmp 0x48a00
jmp 0x489fb
jmp 0x489f6
jmp 0x48a00
movq %rax, %rbx
jmp 0x48a38
movq %rax, %rbx
jmp 0x48a2b
movq %rax, %rbx
jmp 0x48a45
jmp 0x48a07
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x48a1e
callq 0x27560
leaq 0xb8(%rsp), %rdi
callq 0x27920
leaq 0x210(%rsp), %rdi
callq 0x4adfe
leaq 0x410(%rsp), %rdi
callq 0x275a0
movq %rsp, %rdi
callq 0x27620
movq %rbx, %rdi
callq 0x27b10
movq %rax, %rdi
callq 0x2e1cd
nop