// File: zeeev[P]DASVC/bamtools/src/toolkit/bamtools_resolve.cpp
// Function: ReadGroupResolver::DetermineTopModels(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void ReadGroupResolver::DetermineTopModels(const string& readGroupName) {

    // sort models (from most common to least common)
    sort( Models.begin(), Models.end(), std::greater<ModelType>() );

    // store top 2 models for later
    TopModelId     = Models[0].ID;
    NextTopModelId = Models[1].ID;

    // make sure that the 2 most common models are some threshold more common
    // than the remaining models
    const unsigned int activeModelCountSum = Models[0].size() + Models[1].size();
    if ( activeModelCountSum == 0 ) return; // skip if no data in this read group
    const unsigned int unusedModelCountSum = Models[2].size() + Models[3].size() +
                                             Models[4].size() + Models[5].size() +
                                             Models[6].size() + Models[7].size();    
    const double unusedPercentage = (double)unusedModelCountSum / (double)activeModelCountSum;
    if ( unusedPercentage > UnusedModelThreshold ) {
        cerr << "WARNING: " << readGroupName << " does not have clearly defined 'top models'" << endl
             << "         The fraction of alignments in bottom 6 models (" << unusedPercentage
             << ") exceeds threshold: " << UnusedModelThreshold << endl;
        IsAmbiguous = true;
    }

    // emit a warning if the best alignment models are non-standard
    const bool isModel1Top = (TopModelId == 1) || (NextTopModelId == 1);
    const bool isModel2Top = (TopModelId == 2) || (NextTopModelId == 2);
    const bool isModel4Top = (TopModelId == 4) || (NextTopModelId == 4);
    const bool isModel5Top = (TopModelId == 5) || (NextTopModelId == 5);
    const bool isModel6Top = (TopModelId == 6) || (NextTopModelId == 6);
    const bool isModel8Top = (TopModelId == 8) || (NextTopModelId == 8);

    bool isMatePair  = ( isModel4Top && isModel5Top ? true : false );
    bool isPairedEnd = ( isModel2Top && isModel6Top ? true : false );
    bool isSolidPair = ( isModel1Top && isModel8Top ? true : false );

    if ( !isMatePair && !isPairedEnd && !isSolidPair ) {
        cerr << "WARNING: Found a non-standard alignment model configuration. " << endl
             << "         Using alignment models " << TopModelId << " & " << NextTopModelId
             << endl;
    }

    // store only the fragments from the best alignment models, then sort
    vector<int32_t> fragments;
    fragments.reserve( Models[0].size() + Models[1].size() );
    fragments.insert( fragments.end(), Models[0].begin(), Models[0].end() );
    fragments.insert( fragments.end(), Models[1].begin(), Models[1].end() );
    sort ( fragments.begin(), fragments.end() );

    // clear out Model fragment data, not needed anymore
    Models.clear();

    // skip if no fragments found for this read group
    if ( fragments.empty() ) {
        HasData = false;
        return;
    } else
        HasData = true;

    // calculate & store the min,median, & max fragment lengths
    const unsigned int numFragmentLengths = fragments.size();
    const double halfNonConfidenceInterval = (1.0 - ReadGroupResolver::ConfidenceInterval)/2.0;
    const unsigned int minIndex    = (unsigned int)(numFragmentLengths * halfNonConfidenceInterval);
    const unsigned int medianIndex = (unsigned int)(numFragmentLengths * 0.5);
    const unsigned int maxIndex    = (unsigned int)(numFragmentLengths * (1.0-halfNonConfidenceInterval));

    MinFragmentLength    = fragments[minIndex];
    MedianFragmentLength = fragments[medianIndex];
    MaxFragmentLength    = fragments[maxIndex];
}

// Assembly:
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, 0x1b0(%rsp)
movq %rsi, 0x1a8(%rsp)
movq 0x1b0(%rsp), %rdi
movq %rdi, 0xd8(%rsp)
addq $0x18, %rdi
callq 0xcf030
movq 0xd8(%rsp), %rdi
movq %rax, 0x1a0(%rsp)
addq $0x18, %rdi
callq 0xcf060
movq %rax, 0x198(%rsp)
movq 0x1a0(%rsp), %rdi
movq 0x198(%rsp), %rsi
callq 0xceff0
movq 0xd8(%rsp), %rdi
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xcf090
movq 0xd8(%rsp), %rdi
movw (%rax), %ax
movw %ax, 0xc(%rdi)
addq $0x18, %rdi
movl $0x1, %esi
callq 0xcf090
movq 0xd8(%rsp), %rdi
movw (%rax), %ax
movw %ax, 0xe(%rdi)
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xcf090
movq %rax, %rdi
callq 0xced10
movq 0xd8(%rsp), %rdi
movq %rax, 0xe0(%rsp)
addq $0x18, %rdi
movl $0x1, %esi
callq 0xcf090
movq %rax, %rdi
callq 0xced10
movq %rax, %rcx
movq 0xe0(%rsp), %rax
addq %rcx, %rax
movl %eax, 0x190(%rsp)
cmpl $0x0, 0x190(%rsp)
jne 0xc78e6
jmp 0xc81eb
movq 0xd8(%rsp), %rdi
addq $0x18, %rdi
movq %rdi, 0xc8(%rsp)
movl $0x2, %esi
callq 0xcf090
movq %rax, %rdi
callq 0xced10
movq 0xc8(%rsp), %rdi
movq %rax, 0xb8(%rsp)
movl $0x3, %esi
callq 0xcf090
movq %rax, %rdi
callq 0xced10
movq 0xc8(%rsp), %rdi
movq %rax, %rcx
movq 0xb8(%rsp), %rax
addl %ecx, %eax
movl %eax, 0xc0(%rsp)
movl $0x4, %esi
callq 0xcf090
movq %rax, %rdi
callq 0xced10
movq 0xc8(%rsp), %rdi
movq %rax, %rcx
movl 0xc0(%rsp), %eax
addl %ecx, %eax
movl %eax, 0xc4(%rsp)
movl $0x5, %esi
callq 0xcf090
movq %rax, %rdi
callq 0xced10
movq 0xc8(%rsp), %rdi
movq %rax, %rcx
movl 0xc4(%rsp), %eax
addl %ecx, %eax
movl %eax, 0xd0(%rsp)
movl $0x6, %esi
callq 0xcf090
movq %rax, %rdi
callq 0xced10
movq 0xc8(%rsp), %rdi
movq %rax, %rcx
movl 0xd0(%rsp), %eax
addl %ecx, %eax
movl %eax, 0xd4(%rsp)
movl $0x7, %esi
callq 0xcf090
movq %rax, %rdi
callq 0xced10
movq %rax, %rcx
movl 0xd4(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x18c(%rsp)
movl 0x18c(%rsp), %eax
cvtsi2sd %rax, %xmm0
movl 0x190(%rsp), %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x180(%rsp)
movsd 0x180(%rsp), %xmm0
ucomisd 0x9dda4(%rip), %xmm0    # 0x1657d0
jbe 0xc7acd
movq 0x9d5a7(%rip), %rdi     # 0x164fe0
leaq 0x690fc(%rip), %rsi     # 0x130b3c
callq 0x86700
movq %rax, %rdi
movq 0x1a8(%rsp), %rsi
callq 0x86640
movq %rax, %rdi
leaq 0x690e7(%rip), %rsi     # 0x130b46
callq 0x86700
movq %rax, %rdi
movq 0x9d502(%rip), %rsi     # 0x164f70
callq 0x86770
movq %rax, %rdi
leaq 0x690f5(%rip), %rsi     # 0x130b72
callq 0x86700
movq %rax, %rdi
movsd 0x180(%rsp), %xmm0
callq 0x86eb0
movq %rax, %rdi
leaq 0x6910e(%rip), %rsi     # 0x130bab
callq 0x86700
movq %rax, %rdi
movsd 0x9dd23(%rip), %xmm0    # 0x1657d0
callq 0x86eb0
movq %rax, %rdi
movq 0x9d4b4(%rip), %rsi     # 0x164f70
callq 0x86770
movq 0xd8(%rsp), %rax
movb $0x1, 0x10(%rax)
movq 0xd8(%rsp), %rax
movzwl 0xc(%rax), %ecx
movb $0x1, %al
cmpl $0x1, %ecx
movb %al, 0xb7(%rsp)
je 0xc7b00
movq 0xd8(%rsp), %rax
movzwl 0xe(%rax), %eax
cmpl $0x1, %eax
sete %al
movb %al, 0xb7(%rsp)
movq 0xd8(%rsp), %rax
movb 0xb7(%rsp), %cl
andb $0x1, %cl
movb %cl, 0x17f(%rsp)
movzwl 0xc(%rax), %ecx
movb $0x1, %al
cmpl $0x2, %ecx
movb %al, 0xb6(%rsp)
je 0xc7b44
movq 0xd8(%rsp), %rax
movzwl 0xe(%rax), %eax
cmpl $0x2, %eax
sete %al
movb %al, 0xb6(%rsp)
movq 0xd8(%rsp), %rax
movb 0xb6(%rsp), %cl
andb $0x1, %cl
movb %cl, 0x17e(%rsp)
movzwl 0xc(%rax), %ecx
movb $0x1, %al
cmpl $0x4, %ecx
movb %al, 0xb5(%rsp)
je 0xc7b88
movq 0xd8(%rsp), %rax
movzwl 0xe(%rax), %eax
cmpl $0x4, %eax
sete %al
movb %al, 0xb5(%rsp)
movq 0xd8(%rsp), %rax
movb 0xb5(%rsp), %cl
andb $0x1, %cl
movb %cl, 0x17d(%rsp)
movzwl 0xc(%rax), %ecx
movb $0x1, %al
cmpl $0x5, %ecx
movb %al, 0xb4(%rsp)
je 0xc7bcc
movq 0xd8(%rsp), %rax
movzwl 0xe(%rax), %eax
cmpl $0x5, %eax
sete %al
movb %al, 0xb4(%rsp)
movq 0xd8(%rsp), %rax
movb 0xb4(%rsp), %cl
andb $0x1, %cl
movb %cl, 0x17c(%rsp)
movzwl 0xc(%rax), %ecx
movb $0x1, %al
cmpl $0x6, %ecx
movb %al, 0xb3(%rsp)
je 0xc7c10
movq 0xd8(%rsp), %rax
movzwl 0xe(%rax), %eax
cmpl $0x6, %eax
sete %al
movb %al, 0xb3(%rsp)
movq 0xd8(%rsp), %rax
movb 0xb3(%rsp), %cl
andb $0x1, %cl
movb %cl, 0x17b(%rsp)
movzwl 0xc(%rax), %ecx
movb $0x1, %al
cmpl $0x8, %ecx
movb %al, 0xb2(%rsp)
je 0xc7c54
movq 0xd8(%rsp), %rax
movzwl 0xe(%rax), %eax
cmpl $0x8, %eax
sete %al
movb %al, 0xb2(%rsp)
movb 0xb2(%rsp), %al
andb $0x1, %al
movb %al, 0x17a(%rsp)
xorl %eax, %eax
testb $0x1, 0x17d(%rsp)
movb %al, 0xb1(%rsp)
je 0xc7c85
movb 0x17c(%rsp), %al
movb %al, 0xb1(%rsp)
movb 0xb1(%rsp), %al
andb $0x1, %al
negb %al
andb $0x1, %al
movb %al, 0x179(%rsp)
xorl %eax, %eax
testb $0x1, 0x17e(%rsp)
movb %al, 0xb0(%rsp)
je 0xc7cba
movb 0x17b(%rsp), %al
movb %al, 0xb0(%rsp)
movb 0xb0(%rsp), %al
andb $0x1, %al
negb %al
andb $0x1, %al
movb %al, 0x178(%rsp)
xorl %eax, %eax
testb $0x1, 0x17f(%rsp)
movb %al, 0xaf(%rsp)
je 0xc7cef
movb 0x17a(%rsp), %al
movb %al, 0xaf(%rsp)
movb 0xaf(%rsp), %al
andb $0x1, %al
negb %al
andb $0x1, %al
movb %al, 0x177(%rsp)
testb $0x1, 0x179(%rsp)
jne 0xc7da0
testb $0x1, 0x178(%rsp)
jne 0xc7da0
testb $0x1, 0x177(%rsp)
jne 0xc7da0
movq 0x9d2b0(%rip), %rdi     # 0x164fe0
leaq 0x68e8a(%rip), %rsi     # 0x130bc1
callq 0x86700
movq %rax, %rdi
movq 0x9d22a(%rip), %rsi     # 0x164f70
callq 0x86770
movq %rax, %rdi
leaq 0x68eaa(%rip), %rsi     # 0x130bff
callq 0x86700
movq %rax, %rdi
movq 0xd8(%rsp), %rax
movzwl 0xc(%rax), %esi
callq 0x86cb0
movq %rax, %rdi
leaq 0x67ff8(%rip), %rsi     # 0x12fd70
callq 0x86700
movq %rax, %rdi
movq 0xd8(%rsp), %rax
movzwl 0xe(%rax), %esi
callq 0x86cb0
movq %rax, %rdi
movq 0x9d1d5(%rip), %rsi     # 0x164f70
callq 0x86770
leaq 0x158(%rsp), %rdi
callq 0xcf0b0
movq 0xd8(%rsp), %rdi
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xcf090
movq %rax, 0xa0(%rsp)
jmp 0xc7dcc
movq 0xa0(%rsp), %rdi
callq 0xced10
movq %rax, 0x98(%rsp)
jmp 0xc7de3
movq 0xd8(%rsp), %rdi
addq $0x18, %rdi
movl $0x1, %esi
callq 0xcf090
movq %rax, 0x90(%rsp)
jmp 0xc7e03
movq 0x90(%rsp), %rdi
callq 0xced10
movq %rax, 0x88(%rsp)
jmp 0xc7e1a
movq 0x88(%rsp), %rax
movq 0x98(%rsp), %rsi
addq %rax, %rsi
leaq 0x158(%rsp), %rdi
callq 0xcf0c0
jmp 0xc7e3c
leaq 0x158(%rsp), %rdi
callq 0xcf230
movq %rax, 0x80(%rsp)
jmp 0xc7e53
movq 0xd8(%rsp), %rdi
movq 0x80(%rsp), %rax
movq %rax, 0x140(%rsp)
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xcf090
movq %rax, 0x78(%rsp)
jmp 0xc7e7f
movq 0x78(%rsp), %rdi
callq 0xcf260
movq %rax, 0x70(%rsp)
jmp 0xc7e90
movq 0xd8(%rsp), %rdi
movq 0x70(%rsp), %rax
movq %rax, 0x138(%rsp)
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xcf090
movq %rax, 0x68(%rsp)
jmp 0xc7eb9
movq 0x68(%rsp), %rdi
callq 0xcf290
movq %rax, 0x60(%rsp)
jmp 0xc7eca
movq 0x60(%rsp), %rax
movq %rax, 0x130(%rsp)
movq 0x140(%rsp), %rsi
movq 0x138(%rsp), %rdx
movq 0x130(%rsp), %rcx
leaq 0x158(%rsp), %rdi
callq 0xcf1d0
jmp 0xc7efe
leaq 0x158(%rsp), %rdi
callq 0xcf230
movq %rax, 0x58(%rsp)
jmp 0xc7f12
movq 0xd8(%rsp), %rdi
movq 0x58(%rsp), %rax
movq %rax, 0x128(%rsp)
addq $0x18, %rdi
movl $0x1, %esi
callq 0xcf090
movq %rax, 0x50(%rsp)
jmp 0xc7f3c
movq 0x50(%rsp), %rdi
callq 0xcf260
movq %rax, 0x48(%rsp)
jmp 0xc7f4d
movq 0xd8(%rsp), %rdi
movq 0x48(%rsp), %rax
movq %rax, 0x120(%rsp)
addq $0x18, %rdi
movl $0x1, %esi
callq 0xcf090
movq %rax, 0x40(%rsp)
jmp 0xc7f77
movq 0x40(%rsp), %rdi
callq 0xcf290
movq %rax, 0x38(%rsp)
jmp 0xc7f88
movq 0x38(%rsp), %rax
movq %rax, 0x118(%rsp)
movq 0x128(%rsp), %rsi
movq 0x120(%rsp), %rdx
movq 0x118(%rsp), %rcx
leaq 0x158(%rsp), %rdi
callq 0xcf1d0
jmp 0xc7fbc
leaq 0x158(%rsp), %rdi
callq 0xcf300
movq %rax, 0x30(%rsp)
jmp 0xc7fd0
movq 0x30(%rsp), %rax
movq %rax, 0x110(%rsp)
leaq 0x158(%rsp), %rdi
callq 0xcf230
movq %rax, 0x28(%rsp)
jmp 0xc7ff1
movq 0x28(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rdi
movq 0x108(%rsp), %rsi
callq 0xcf2c0
jmp 0xc8015
movq 0xd8(%rsp), %rdi
addq $0x18, %rdi
callq 0xcf330
jmp 0xc8028
leaq 0x158(%rsp), %rdi
callq 0xcf350
movb %al, 0x27(%rsp)
jmp 0xc803b
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0xc8045
jmp 0xc8087
movq 0xd8(%rsp), %rax
movb $0x0, 0x11(%rax)
movl $0x1, 0x104(%rsp)
jmp 0xc81de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0x158(%rsp), %rdi
callq 0xcf3e0
jmp 0xc81f3
movq 0xd8(%rsp), %rax
movb $0x1, 0x11(%rax)
leaq 0x158(%rsp), %rdi
callq 0xcf3a0
movq %rax, 0x18(%rsp)
jmp 0xc80a7
movq 0x18(%rsp), %rax
movl %eax, 0x100(%rsp)
movsd 0x9d70d(%rip), %xmm2    # 0x1657c8
movsd 0x6870d(%rip), %xmm1    # 0x1307d0
movaps %xmm1, %xmm0
subsd %xmm2, %xmm0
movsd 0x68706(%rip), %xmm2    # 0x1307d8
mulsd %xmm2, %xmm0
movsd %xmm0, 0xf8(%rsp)
movl 0x100(%rsp), %eax
cvtsi2sd %rax, %xmm0
movsd 0xf8(%rsp), %xmm3
mulsd %xmm3, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, 0xf4(%rsp)
movl 0x100(%rsp), %eax
cvtsi2sd %rax, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, 0xf0(%rsp)
movl 0x100(%rsp), %eax
cvtsi2sd %rax, %xmm0
movsd 0xf8(%rsp), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, 0xec(%rsp)
movl 0xf4(%rsp), %eax
movl %eax, %esi
leaq 0x158(%rsp), %rdi
callq 0xcf3c0
movq %rax, 0x10(%rsp)
jmp 0xc8166
movq 0xd8(%rsp), %rax
movq 0x10(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rax)
movl 0xf0(%rsp), %eax
movl %eax, %esi
leaq 0x158(%rsp), %rdi
callq 0xcf3c0
movq %rax, 0x8(%rsp)
jmp 0xc8194
movq 0xd8(%rsp), %rax
movq 0x8(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, 0x4(%rax)
movl 0xec(%rsp), %eax
movl %eax, %esi
leaq 0x158(%rsp), %rdi
callq 0xcf3c0
movq %rax, (%rsp)
jmp 0xc81c2
movq 0xd8(%rsp), %rax
movq (%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, 0x8(%rax)
movl $0x0, 0x104(%rsp)
leaq 0x158(%rsp), %rdi
callq 0xcf3e0
addq $0x1b8, %rsp            # imm = 0x1B8
retq
jmp 0xc81f5
movq 0x150(%rsp), %rdi
callq 0x86d80
movq %rax, %rdi
callq 0x91290
nopw (%rax,%rax)