// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
leaq 0x248(%rdi), %rax
movq %rax, 0x8(%rsp)
leaq 0x1d0(%rdi), %rax
movq %rax, 0x10(%rsp)
movl 0x1c(%rdi), %eax
xorl %r13d, %r13d
movslq %eax, %rcx
cmpq %rcx, %r13
jge 0xbf12
imulq $0x18, %r13, %rbp
xorl %r14d, %r14d
movslq %eax, %rcx
cmpq %rcx, %r14
jge 0xbf0a
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbp), %rax
andq $0x0, (%rax,%r14,8)
xorl %r15d, %r15d
xorl %r12d, %r12d
movslq 0x20(%rbx), %rax
cmpq %rax, %r12
jge 0xbeff
movq 0x1d0(%rbx), %rax
movq (%rax,%r15), %rax
movsd (%rax,%r13,8), %xmm0
callq 0x35c0
movq 0x1d0(%rbx), %rax
movq 0x1e8(%rbx), %rcx
movq (%rax,%r15), %rax
movsd (%rax,%r13,8), %xmm1
movq (%rcx,%r15), %rax
subsd (%rax,%r14,8), %xmm1
movq 0x248(%rbx), %rax
movq (%rax,%rbp), %rax
mulsd %xmm0, %xmm1
addsd (%rax,%r14,8), %xmm1
movsd %xmm1, (%rax,%r14,8)
incq %r12
addq $0x18, %r15
jmp 0xbe9a
incq %r14
movl 0x1c(%rbx), %eax
jmp 0xbe77
incq %r13
jmp 0xbe64
leaq 0x290(%rbx), %rdx
leaq 0x2a8(%rbx), %rcx
leaq 0x2c0(%rbx), %r8
leaq 0x2d8(%rbx), %r9
movq 0x18(%rsp), %r10
movzbl 0x2c3c(%r10), %eax
addq $0x970, %r10            # imm = 0x970
leaq 0x20(%rsp), %r13
movq %r13, %rdi
movq 0x8(%rsp), %rsi
pushq %r10
pushq %rax
callq 0x580a
popq %rax
popq %rcx
leaq 0x260(%rbx), %rdi
movq %r13, %rsi
callq 0x6266
movq %r13, %rdi
callq 0x61d0
movq 0x260(%rbx), %rax
movq 0x278(%rbx), %rcx
xorl %edx, %edx
xorl %esi, %esi
movslq 0x1c(%rbx), %rdi
cmpq %rdi, %rdx
jge 0xbfa4
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
jmp 0xbf82
testb $0x1, %sil
je 0xc0cd
movq 0x90(%rbx), %rax
movq 0x260(%rbx), %rcx
xorl %edx, %edx
movslq 0x20(%rbx), %rsi
cmpq %rsi, %rdx
jge 0xbfd9
movslq (%rax,%rdx,4), %rsi
movl -0x4(%rcx,%rsi,4), %esi
incl %esi
movl %esi, (%rax,%rdx,4)
incq %rdx
jmp 0xbfbe
leaq 0xa8(%rbx), %rsi
leaq 0xf0(%rbx), %rdi
callq 0x95d6
leaq 0xc0(%rbx), %rsi
leaq 0x108(%rbx), %rdi
callq 0x9980
xorl %r14d, %r14d
xorl %r15d, %r15d
movslq 0x1c(%rbx), %rax
cmpq %rax, %r15
jge 0xc061
movq 0x278(%rbx), %rax
movslq (%rax,%r15,4), %rax
imulq $0x18, %rax, %rsi
addq 0xf0(%rbx), %rsi
movq 0xa8(%rbx), %rdi
addq %r14, %rdi
callq 0x9980
movq 0x278(%rbx), %rax
movslq (%rax,%r15,4), %rax
imulq $0x18, %rax, %rsi
addq 0x108(%rbx), %rsi
movq 0xc0(%rbx), %rdi
addq %r14, %rdi
callq 0x969e
incq %r15
addq $0x18, %r14
jmp 0xc005
leaq 0x1b8(%rbx), %rdi
movq 0x10(%rsp), %rsi
callq 0x9a48
movl 0x20(%rbx), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movl 0x1c(%rbx), %edx
testl %edx, %edx
cmovlel %ecx, %edx
cmpq %rax, %rcx
je 0xc0cd
imulq $0x18, %rcx, %rsi
movq 0x1b8(%rbx), %rdi
addq %rsi, %rdi
movq 0x278(%rbx), %r8
addq 0x1d0(%rbx), %rsi
xorl %r9d, %r9d
cmpq %r9, %rdx
je 0xc0c8
movslq (%r8,%r9,4), %r10
movq (%rdi), %r11
movsd (%r11,%r10,8), %xmm0
movq (%rsi), %r10
movsd %xmm0, (%r10,%r9,8)
incq %r9
jmp 0xc0a8
incq %rcx
jmp 0xc084
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq