// File: /workspace/github_cpp_makefile_star2_O3/bobverity[P]MavericK/MCMCobject_noAdmixture.cpp
// Function: MCMCobject_noAdmixture::chooseBestLabelPermutation(globals&, int)
void MCMCobject_noAdmixture::chooseBestLabelPermutation(globals &globals, int rep) {
    
    // calculate cost matrix from old and new Qmatrices
    for (int k1=0; k1<K; k1++) {
        for (int k2=0; k2<K; k2++) {
            costMat[k1][k2] = 0;
            for (int i=0; i<n; i++) {
                costMat[k1][k2] += exp(logQmatrix_ind_new[i][k1])*(logQmatrix_ind_new[i][k1]-logQmatrix_ind_running[i][k2]);
            }
        }
    }
    
    // find best permutation of current labels
    bestPerm = hungarian(costMat, edgesLeft, edgesRight, blockedLeft, blockedRight, globals.outputLog_on, globals.outputLog_fileStream);

    // define bestPermOrder. If the numbers 1:m_K are placed in best-perm-order then we arrive back at bestPerm. In R terms we would say bestPermOrder=order(bestPerm).
    bool performSwap = false;
    for (int k=0; k<K; k++) {
        bestPermOrder[bestPerm[k]] = k;
        if (bestPerm[k]!=k)
            performSwap = true;
    }
    
    // swap labels if necessary
    if (performSwap) {
        
        // update grouping to reflect swapped labels
        for (int i=0; i<n; i++) {
            group[i] = bestPerm[group[i]-1]+1;
        }
        
        // update allele counts to reflect swapped labels
        old_alleleCounts = alleleCounts;
        old_alleleCountsTotals = alleleCountsTotals;
        for (int k=0; k<K; k++) {
            alleleCounts[k] = old_alleleCounts[bestPermOrder[k]];
            alleleCountsTotals[k] = old_alleleCountsTotals[bestPermOrder[k]];
        }
        
        // update logQmatrix_ind_new to reflect swapped labels
        logQmatrix_ind_old = logQmatrix_ind_new;
        for (int i=0; i<n; i++) {
            for (int k=0; k<K; k++) {
                logQmatrix_ind_new[i][k] = logQmatrix_ind_old[i][bestPermOrder[k]];
            }
        }
        
    }
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, 0x8(%rsp)
movq %rdi, %rbx
movl 0x1c(%rdi), %eax
testl %eax, %eax
jle 0xe01c
xorl %r15d, %r15d
testl %eax, %eax
jle 0xe00d
movq 0x248(%rbx), %rcx
movl 0x20(%rbx), %edx
leaq (%r15,%r15,2), %r12
xorl %r13d, %r13d
movq (%rcx,%r12,8), %rsi
movq $0x0, (%rsi,%r13,8)
testl %edx, %edx
jle 0xdffe
movq 0x1d0(%rbx), %rax
xorl %ebp, %ebp
xorl %r14d, %r14d
movq (%rax,%rbp), %rax
movsd (%rax,%r15,8), %xmm0
callq 0x35a0
movq 0x1d0(%rbx), %rax
movq 0x1e8(%rbx), %rcx
movq (%rax,%rbp), %rdx
movsd (%rdx,%r15,8), %xmm1
movq (%rcx,%rbp), %rcx
subsd (%rcx,%r13,8), %xmm1
movq 0x248(%rbx), %rcx
movq (%rcx,%r12,8), %rdx
mulsd %xmm0, %xmm1
addsd (%rdx,%r13,8), %xmm1
movsd %xmm1, (%rdx,%r13,8)
incq %r14
movslq 0x20(%rbx), %rdx
addq $0x18, %rbp
cmpq %rdx, %r14
jl 0xdf9f
movl 0x1c(%rbx), %eax
incq %r13
movslq %eax, %rsi
cmpq %rsi, %r13
jl 0xdf83
incq %r15
movslq %eax, %rcx
cmpq %rcx, %r15
jl 0xdf6a
leaq 0x248(%rbx), %rsi
leaq 0x290(%rbx), %rdx
leaq 0x2a8(%rbx), %rcx
leaq 0x2c0(%rbx), %r8
leaq 0x2d8(%rbx), %r9
movq 0x8(%rsp), %r10
movzbl 0x2c3c(%r10), %eax
addq $0x970, %r10            # imm = 0x970
leaq 0x10(%rsp), %r15
movq %r15, %rdi
pushq %r10
pushq %rax
callq 0x60b5
addq $0x10, %rsp
movq 0x260(%rbx), %rdi
movaps (%r15), %xmm0
movups %xmm0, 0x260(%rbx)
movq 0x10(%r15), %rax
movq %rax, 0x270(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%r15)
movq $0x0, 0x10(%r15)
testq %rdi, %rdi
je 0xe0ac
callq 0x3310
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xe0ac
callq 0x3310
cmpl $0x0, 0x1c(%rbx)
jle 0xe220
movq 0x260(%rbx), %rax
movq 0x278(%rbx), %rcx
xorl %edx, %edx
xorl %esi, %esi
movslq (%rax,%rdx,4), %rdi
movl %edx, (%rcx,%rdi,4)
movl (%rax,%rdx,4), %edi
cmpq %rdi, %rdx
setne %dil
orb %dil, %sil
incq %rdx
movslq 0x1c(%rbx), %rdi
cmpq %rdi, %rdx
jl 0xe0c8
testb $0x1, %sil
je 0xe220
cmpl $0x0, 0x20(%rbx)
jle 0xe11a
movq 0x90(%rbx), %rcx
xorl %edx, %edx
movslq (%rcx,%rdx,4), %rsi
movl -0x4(%rax,%rsi,4), %esi
incl %esi
movl %esi, (%rcx,%rdx,4)
incq %rdx
movslq 0x20(%rbx), %rsi
cmpq %rsi, %rdx
jl 0xe101
leaq 0xa8(%rbx), %rsi
leaq 0xf0(%rbx), %rdi
callq 0xae62
leaq 0xc0(%rbx), %rsi
leaq 0x108(%rbx), %rdi
callq 0xb486
cmpl $0x0, 0x1c(%rbx)
jle 0xe1ae
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x278(%rbx), %rax
movslq (%rax,%r15,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0xf0(%rbx), %rsi
movq 0xa8(%rbx), %rdi
addq %r14, %rdi
callq 0xb486
movq 0x278(%rbx), %rax
movslq (%rax,%r15,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0x108(%rbx), %rsi
movq 0xc0(%rbx), %rdi
addq %r14, %rdi
callq 0xafde
incq %r15
movslq 0x1c(%rbx), %rax
addq $0x18, %r14
cmpq %rax, %r15
jl 0xe14c
leaq 0x1d0(%rbx), %rsi
leaq 0x1b8(%rbx), %rdi
callq 0xb60e
movslq 0x20(%rbx), %rax
testq %rax, %rax
jle 0xe220
movl 0x1c(%rbx), %ecx
movq 0x1b8(%rbx), %rdx
movq 0x278(%rbx), %rsi
movq 0x1d0(%rbx), %rdi
xorl %r8d, %r8d
testl %ecx, %ecx
jle 0xe218
leaq (,%r8,8), %r9
leaq (%r9,%r9,2), %r10
movq (%rdx,%r10), %r9
movq (%rdi,%r10), %r10
xorl %r11d, %r11d
movslq (%rsi,%r11,4), %rbx
movsd (%r9,%rbx,8), %xmm0
movsd %xmm0, (%r10,%r11,8)
incq %r11
cmpq %r11, %rcx
jne 0xe200
incq %r8
cmpq %rax, %r8
jne 0xe1e5
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop