// File: /workspace/github_cpp_makefile_star2_O1/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, 0x48133(%rip)    # 0x90640
movsd 0xa0(%rax), %xmm0
movsd %xmm0, 0x4812b(%rip)    # 0x90648
movq %rsp, %r14
movq %r14, %rdi
callq 0x273f0
movq (%r15), %rsi
addq $0x10, %rsi
movq %r14, %rdi
callq 0x277a0
testb %al, %al
je 0x488a3
leaq 0x410(%rsp), %rdi
movq %rsp, %rsi
callq 0x277d0
leaq 0x410(%rsp), %rsi
movq %r15, %rdi
callq 0x48b60
leaq 0x210(%rsp), %rdi
callq 0x27aa0
movq (%r15), %rsi
addq $0x70, %rsi
leaq 0x210(%rsp), %rdi
callq 0x46d32
testb %al, %al
je 0x48903
leaq 0xb8(%rsp), %rdi
callq 0x27220
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x2f276(%rip), %rdx     # 0x7781b
leaq 0x10(%rsp), %rdi
movq %rdx, %rsi
callq 0x2f1b2
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 0x27a50
movl %eax, %ebp
testb %al, %al
je 0x4896b
movq %r12, %rdi
callq 0x27330
testb %al, %al
je 0x485d2
movq %r12, %rdi
callq 0x270d0
testb %al, %al
je 0x485d2
movq %r12, %rdi
callq 0x276f0
testb %al, %al
je 0x485d2
movl 0x160(%rsp), %eax
cmpl 0x188(%rsp), %eax
jne 0x485d2
movq %r12, %rdi
callq 0x275d0
movq $0x0, 0x18(%rsp)
movq 0x10(%rsp), %rax
movb $0x0, (%rax)
cmpb $0x0, 0x1e8(%rsp)
jne 0x486c9
movq 0x148(%rsp), %rax
testq %rax, %rax
je 0x486c9
movq 0x140(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movl %eax, 0x68(%rsp)
movl $0x0, 0x34(%rsp)
movq %r12, %rdi
leaq 0x48446(%rip), %rsi     # 0x90ab8
leaq 0x40(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x34(%rsp), %r8
callq 0x27710
testb %al, %al
je 0x486c9
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 0x4cda6
movq %rax, %r14
cmpq 0x8(%rsp), %rax
je 0x48a49
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 0x4d208
movq %rax, %r13
leaq 0x78(%r14), %rax
cmpq %rax, %r13
je 0x48793
cmpw %r15w, %bx
movq %rbp, %r15
movq %rsp, %rbx
jb 0x48891
cmpb $0x1, 0x40(%r13)
jne 0x48891
leaq 0x210(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %r12, %rdx
callq 0x46d78
movq %r12, %rdi
callq 0x46367
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 0x48883
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x10(%rdi)
jmp 0x48891
leaq 0xa0(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0xb8(%rsp), %rsi
movq 0xc0(%rsp), %rdx
addq %rsi, %rdx
leaq 0x90(%rsp), %rdi
callq 0x2f122
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %rdx
addq %rsi, %rdx
leaq 0x68(%rsp), %rdi
callq 0x2f122
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 0x2f122
movb 0x88(%rsp), %al
movb %al, 0x60(%rsp)
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x4ce0e
movq %rsp, %rbx
movq %rbp, %r15
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x4884c
callq 0x27560
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x48860
callq 0x27560
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x485d2
callq 0x27560
jmp 0x485d2
addq $0x8, %rdi
leaq 0x40(%rsp), %rdx
callq 0x4baba
movq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x4d270
jmp 0x485d2
movq 0x47736(%rip), %rdi     # 0x8ffe0
leaq 0x2c00b(%rip), %rsi     # 0x748bc
movl $0x37, %edx
callq 0x276c0
movq (%r15), %rax
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rdx
movq 0x47713(%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
xorl %ebp, %ebp
jmp 0x48a2d
movq 0x476d6(%rip), %rdi     # 0x8ffe0
leaq 0x2bfe3(%rip), %rsi     # 0x748f4
movl $0x46, %edx
callq 0x276c0
movq (%r15), %rax
movq 0x70(%rax), %rsi
movq 0x78(%rax), %rdx
movq 0x476b3(%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
movq %rsp, %rdi
callq 0x270b0
xorl %ebp, %ebp
jmp 0x48a13
leaq 0x280(%rsp), %rdi
callq 0x27130
testb %al, %al
je 0x48989
leaq 0x210(%rsp), %rdi
callq 0x27850
movq %rsp, %rdi
callq 0x270b0
movq 0x20(%r15), %rbx
cmpq 0x8(%rsp), %rbx
je 0x489ee
xorl %r14d, %r14d
leaq 0x20(%rbx), %rsi
leaq 0x40(%rbx), %rdi
callq 0x46576
leaq 0x70(%rbx), %rdi
movq 0x80(%rbx), %rsi
callq 0x4b66a
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 0x4899f
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x48a02
callq 0x27560
leaq 0xb8(%rsp), %rdi
callq 0x27930
xorb $0x1, %bpl
leaq 0x210(%rsp), %rdi
callq 0x4aecc
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 0x47590(%rip), %rdi     # 0x8ffe0
leaq 0x2bee4(%rip), %rsi     # 0x7493b
movl $0x54, %edx
callq 0x276c0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x4756e(%rip), %rdi     # 0x8ffe0
callq 0x276c0
movq %rax, %rdi
callq 0x27310
movq %rsp, %rdi
callq 0x270b0
jmp 0x489ee
jmp 0x48b09
jmp 0x48b09
jmp 0x48b09
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x48aae
callq 0x27560
jmp 0x48aae
movq %rax, %rbx
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x48ac7
callq 0x27560
jmp 0x48ac7
movq %rax, %rbx
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x48b0c
callq 0x27560
jmp 0x48b0c
movq %rax, %rbx
jmp 0x48b20
jmp 0x48b09
jmp 0x48b09
jmp 0x48b09
jmp 0x48b09
jmp 0x48b02
jmp 0x48afd
jmp 0x48af8
jmp 0x48b02
movq %rax, %rbx
jmp 0x48b3a
movq %rax, %rbx
jmp 0x48b2d
movq %rax, %rbx
jmp 0x48b47
jmp 0x48b09
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x48b20
callq 0x27560
leaq 0xb8(%rsp), %rdi
callq 0x27930
leaq 0x210(%rsp), %rdi
callq 0x4aecc
leaq 0x410(%rsp), %rdi
callq 0x275a0
movq %rsp, %rdi
callq 0x27620
movq %rbx, %rdi
callq 0x27b20
movq %rax, %rdi
callq 0x2e1ed
nop