// File: /workspace/github_cpp_makefile_star2_O1/bobverity[P]MavericK/MCMCobject_admixture.cpp
// Function: MCMCobject_admixture::chooseBestLabelPermutation(globals&, int)
void MCMCobject_admixture::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<geneCopies; i++) {
                costMat[k1][k2] += Qmatrix_gene_new[i][k1]*(logQmatrix_gene_new[i][k1]-logQmatrix_gene_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) {
        
        groupIndex=-1;
        for (int ind=0; ind<n; ind++) {
            for (int l=0; l<loci; l++) {
                for (int p=0; p<ploidy_vec[ind]; p++) {
                    groupIndex++;
                    linearGroup[groupIndex] = bestPerm[linearGroup[groupIndex]-1]+1;
                    group[ind][l][p] = linearGroup[groupIndex];
                }
            }
        }
        
        // 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 admix counts to reflect swapped labels
        old_admixCounts = admixCounts;
        for (int i=0; i<n; i++) {
            for (int k=0; k<K; k++) {
                admixCounts[i][k] = old_admixCounts[i][bestPermOrder[k]];
            }
        }
        
        // update logQmatrix_gene_new to reflect swapped labels. No need to swap Qmatrix_gene_new, as this is not used again until after it is recalcualted
        logQmatrix_gene_old = logQmatrix_gene_new;
        for (int i=0; i<geneCopies; i++) {
            for (int k=0; k<K; k++) {
                logQmatrix_gene_new[i][k] = logQmatrix_gene_old[i][bestPermOrder[k]];
            }
        }
        
    }
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rax
movq %rdi, %rbx
movslq 0x1c(%rdi), %rcx
testq %rcx, %rcx
jle 0xa8de
movq 0x340(%rbx), %rdx
movl 0x70(%rbx), %esi
movq %rsi, %rdi
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %rdi
xorl %r8d, %r8d
leaq (%r8,%r8,2), %r9
movq (%rdx,%r9,8), %r9
movq 0x298(%rbx), %r10
movq 0x2b0(%rbx), %r11
movq 0x2c8(%rbx), %r14
xorl %r15d, %r15d
movq $0x0, (%r9,%r15,8)
testl %esi, %esi
jle 0xa8ce
movsd (%r9,%r15,8), %xmm0
xorl %r12d, %r12d
movq (%r11,%r12), %r13
movq (%r10,%r12), %rbp
movsd (%rbp,%r8,8), %xmm1
movq (%r14,%r12), %rbp
subsd (%rbp,%r15,8), %xmm1
mulsd (%r13,%r8,8), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, (%r9,%r15,8)
addq $0x18, %r12
cmpq %r12, %rdi
jne 0xa89a
incq %r15
cmpq %rcx, %r15
jne 0xa885
incq %r8
cmpq %rcx, %r8
jne 0xa865
leaq 0x340(%rbx), %rsi
leaq 0x388(%rbx), %rdx
leaq 0x3a0(%rbx), %rcx
leaq 0x3b8(%rbx), %r8
leaq 0x3d0(%rbx), %r9
movzbl 0x2c3c(%rax), %r10d
addq $0x970, %rax            # imm = 0x970
movq %rsp, %r14
movq %r14, %rdi
pushq %rax
pushq %r10
callq 0x611f
addq $0x10, %rsp
movq 0x358(%rbx), %rdi
movaps (%r14), %xmm0
movq 0x10(%r14), %rax
movq %rdi, (%r14)
movups 0x360(%rbx), %xmm1
movups %xmm0, 0x358(%rbx)
movq %rax, 0x368(%rbx)
movups %xmm1, 0x8(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
movq $0x0, 0x10(%r14)
testq %rdi, %rdi
je 0xa966
callq 0x3310
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xa974
callq 0x3310
cmpl $0x0, 0x1c(%rbx)
jle 0xabe6
movq 0x358(%rbx), %rax
movq 0x370(%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 0xa995
testb $0x1, %dil
je 0xabe6
movl $0xffffffff, 0x108(%rbx) # imm = 0xFFFFFFFF
cmpl $0x0, 0x20(%rbx)
jle 0xaa6f
movq 0x40(%rbx), %rax
xorl %ecx, %ecx
cmpl $0x0, 0x24(%rbx)
jle 0xaa5f
movq 0xc0(%rbx), %rdx
movq 0x358(%rbx), %rsi
leaq (%rcx,%rcx,2), %rdi
shlq $0x3, %rdi
addq 0xd8(%rbx), %rdi
xorl %r8d, %r8d
cmpl $0x0, (%rax,%rcx,4)
jle 0xaa53
movq (%rdi), %r9
leaq (%r8,%r8,2), %r10
movq (%r9,%r10,8), %r9
xorl %r10d, %r10d
movslq 0x108(%rbx), %r11
leaq 0x1(%r11), %r14
movl %r14d, 0x108(%rbx)
movslq 0x4(%rdx,%r11,4), %r14
movl -0x4(%rsi,%r14,4), %ebp
incl %ebp
movl %ebp, 0x4(%rdx,%r11,4)
movslq 0x108(%rbx), %r11
movl (%rdx,%r11,4), %r11d
movl %r11d, (%r9,%r10,4)
incq %r10
movslq (%rax,%rcx,4), %r11
cmpq %r11, %r10
jl 0xaa15
incq %r8
movslq 0x24(%rbx), %r9
cmpq %r9, %r8
jl 0xaa01
incq %rcx
movslq 0x20(%rbx), %rdx
cmpq %rdx, %rcx
jl 0xa9db
leaq 0x110(%rbx), %rsi
leaq 0x158(%rbx), %rdi
callq 0xb17e
leaq 0x128(%rbx), %rsi
leaq 0x170(%rbx), %rdi
callq 0xb748
cmpl $0x0, 0x1c(%rbx)
jle 0xab03
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x370(%rbx), %rax
movslq (%rax,%r15,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0x158(%rbx), %rsi
movq 0x110(%rbx), %rdi
addq %r14, %rdi
callq 0xb748
movq 0x370(%rbx), %rax
movslq (%rax,%r15,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0x170(%rbx), %rsi
movq 0x128(%rbx), %rdi
addq %r14, %rdi
callq 0xb2d4
incq %r15
movslq 0x1c(%rbx), %rax
addq $0x18, %r14
cmpq %rax, %r15
jl 0xaaa1
leaq 0x188(%rbx), %rsi
leaq 0x1d0(%rbx), %rdi
callq 0xb748
cmpl $0x0, 0x20(%rbx)
jle 0xab74
movq 0x188(%rbx), %rax
movq 0x1d0(%rbx), %rcx
movq 0x370(%rbx), %rdx
xorl %esi, %esi
cmpl $0x0, 0x1c(%rbx)
jle 0xab68
leaq (,%rsi,8), %rdi
leaq (%rdi,%rdi,2), %r8
movq (%rcx,%r8), %rdi
movq (%rax,%r8), %r8
xorl %r9d, %r9d
movslq (%rdx,%r9,4), %r10
movl (%rdi,%r10,4), %r10d
movl %r10d, (%r8,%r9,4)
incq %r9
movslq 0x1c(%rbx), %r10
cmpq %r10, %r9
jl 0xab50
incq %rsi
movslq 0x20(%rbx), %rdi
cmpq %rdi, %rsi
jl 0xab33
leaq 0x298(%rbx), %rsi
leaq 0x280(%rbx), %rdi
callq 0xb8aa
movslq 0x70(%rbx), %rax
testq %rax, %rax
jle 0xabe6
movl 0x1c(%rbx), %ecx
movq 0x280(%rbx), %rdx
movq 0x370(%rbx), %rsi
movq 0x298(%rbx), %rdi
xorl %r8d, %r8d
testl %ecx, %ecx
jle 0xabde
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 0xabc6
incq %r8
cmpq %rax, %r8
jne 0xabab
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop