// File: /workspace/github_cpp_makefile_star2_O1/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
cmpl $0x0, 0x1c(%rdi)
jle 0xe471
xorl %r15d, %r15d
cmpl $0x0, 0x1c(%rbx)
jle 0xe461
leaq (%r15,%r15,2), %r12
xorl %r13d, %r13d
movq 0x248(%rbx), %rax
movq (%rax,%r12,8), %rax
movq $0x0, (%rax,%r13,8)
cmpl $0x0, 0x20(%rbx)
jle 0xe451
xorl %ebp, %ebp
xorl %r14d, %r14d
movq 0x1d0(%rbx), %rax
movq (%rax,%rbp), %rax
movsd (%rax,%r15,8), %xmm0
callq 0x35a0
movq 0x1d0(%rbx), %rax
movq 0x1e8(%rbx), %rcx
movq (%rax,%rbp), %rax
movsd (%rax,%r15,8), %xmm1
movq (%rcx,%rbp), %rax
subsd (%rax,%r13,8), %xmm1
movq 0x248(%rbx), %rax
movq (%rax,%r12,8), %rax
mulsd %xmm0, %xmm1
addsd (%rax,%r13,8), %xmm1
movsd %xmm1, (%rax,%r13,8)
incq %r14
movslq 0x20(%rbx), %rax
addq $0x18, %rbp
cmpq %rax, %r14
jl 0xe3ee
incq %r13
movslq 0x1c(%rbx), %rax
cmpq %rax, %r13
jl 0xe3d0
incq %r15
movslq 0x1c(%rbx), %rax
cmpq %rax, %r15
jl 0xe3bf
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 0x611f
addq $0x10, %rsp
movq 0x260(%rbx), %rdi
movaps (%r15), %xmm0
movq 0x10(%r15), %rax
movq %rdi, (%r15)
movups 0x268(%rbx), %xmm1
movups %xmm0, 0x260(%rbx)
movq %rax, 0x270(%rbx)
movups %xmm1, 0x8(%r15)
xorps %xmm0, %xmm0
movaps %xmm0, (%r15)
movq $0x0, 0x10(%r15)
testq %rdi, %rdi
je 0xe501
callq 0x3310
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xe510
callq 0x3310
cmpl $0x0, 0x1c(%rbx)
jle 0xe692
movq 0x260(%rbx), %rax
movq 0x278(%rbx), %rcx
xorl %edx, %edx
movl $0x1, %esi
xorl %edi, %edi
movslq (%rax,%rdx,4), %r8
movl %edx, (%rcx,%r8,4)
movl (%rax,%rdx,4), %r8d
cmpq %r8, %rdx
movzbl %dil, %edi
cmovnel %esi, %edi
incq %rdx
movslq 0x1c(%rbx), %r8
cmpq %r8, %rdx
jl 0xe531
testb $0x1, %dil
je 0xe692
cmpl $0x0, 0x20(%rbx)
jle 0xe58c
movq 0x90(%rbx), %rax
movq 0x260(%rbx), %rcx
xorl %edx, %edx
movslq (%rax,%rdx,4), %rsi
movl -0x4(%rcx,%rsi,4), %esi
incl %esi
movl %esi, (%rax,%rdx,4)
incq %rdx
movslq 0x20(%rbx), %rsi
cmpq %rsi, %rdx
jl 0xe573
leaq 0xa8(%rbx), %rsi
leaq 0xf0(%rbx), %rdi
callq 0xb17e
leaq 0xc0(%rbx), %rsi
leaq 0x108(%rbx), %rdi
callq 0xb748
cmpl $0x0, 0x1c(%rbx)
jle 0xe620
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 0xb748
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 0xb2d4
incq %r15
movslq 0x1c(%rbx), %rax
addq $0x18, %r14
cmpq %rax, %r15
jl 0xe5be
leaq 0x1d0(%rbx), %rsi
leaq 0x1b8(%rbx), %rdi
callq 0xb8aa
movslq 0x20(%rbx), %rax
testq %rax, %rax
jle 0xe692
movl 0x1c(%rbx), %ecx
movq 0x1b8(%rbx), %rdx
movq 0x278(%rbx), %rsi
movq 0x1d0(%rbx), %rdi
xorl %r8d, %r8d
testl %ecx, %ecx
jle 0xe68a
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 0xe672
incq %r8
cmpq %rax, %r8
jne 0xe657
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop