// File: /workspace/github_cpp_makefile_star2_O3/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 0xa5aa
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 0xa59a
xorpd %xmm0, %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 0xa566
incq %r15
cmpq %rcx, %r15
jne 0xa553
incq %r8
cmpq %rcx, %r8
jne 0xa533
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 0x60b5
addq $0x10, %rsp
movq 0x358(%rbx), %rdi
movaps (%r14), %xmm0
movups %xmm0, 0x358(%rbx)
movq 0x10(%r14), %rax
movq %rax, 0x368(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
movq $0x0, 0x10(%r14)
testq %rdi, %rdi
je 0xa631
callq 0x3310
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xa631
callq 0x3310
cmpl $0x0, 0x1c(%rbx)
jle 0xa8af
movq 0x358(%rbx), %rax
movq 0x370(%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 0xa64d
testb $0x1, %sil
je 0xa8af
movl $0xffffffff, 0x108(%rbx) # imm = 0xFFFFFFFF
movl 0x20(%rbx), %esi
testl %esi, %esi
jle 0xa732
movq 0x40(%rbx), %rax
movl 0x24(%rbx), %edx
xorl %ecx, %ecx
testl %edx, %edx
jle 0xa723
movq 0xc0(%rbx), %rsi
movq 0x358(%rbx), %rdi
leaq (%rcx,%rcx,2), %r8
shlq $0x3, %r8
addq 0xd8(%rbx), %r8
movl (%rax,%rcx,4), %r10d
xorl %r9d, %r9d
testl %r10d, %r10d
jle 0xa715
movq (%r8), %rdx
leaq (%r9,%r9,2), %r10
movq (%rdx,%r10,8), %rdx
xorl %r11d, %r11d
movslq 0x108(%rbx), %r10
leaq 0x1(%r10), %r14
movl %r14d, 0x108(%rbx)
movslq 0x4(%rsi,%r10,4), %r14
movl -0x4(%rdi,%r14,4), %ebp
incl %ebp
movl %ebp, 0x4(%rsi,%r10,4)
movslq 0x108(%rbx), %r10
movl (%rsi,%r10,4), %r10d
movl %r10d, (%rdx,%r11,4)
incq %r11
movslq (%rax,%rcx,4), %r10
cmpq %r10, %r11
jl 0xa6d4
movl 0x24(%rbx), %edx
incq %r9
movslq %edx, %r11
cmpq %r11, %r9
jl 0xa6c1
movl 0x20(%rbx), %esi
incq %rcx
movslq %esi, %rdi
cmpq %rdi, %rcx
jl 0xa695
leaq 0x110(%rbx), %rsi
leaq 0x158(%rbx), %rdi
callq 0xae62
leaq 0x128(%rbx), %rsi
leaq 0x170(%rbx), %rdi
callq 0xb486
cmpl $0x0, 0x1c(%rbx)
jle 0xa7c6
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 0xb486
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 0xafde
incq %r15
movslq 0x1c(%rbx), %rax
addq $0x18, %r14
cmpq %rax, %r15
jl 0xa764
leaq 0x188(%rbx), %rsi
leaq 0x1d0(%rbx), %rdi
callq 0xb486
movl 0x20(%rbx), %edi
testl %edi, %edi
jle 0xa83d
movq 0x1d0(%rbx), %rax
movq 0x370(%rbx), %rcx
movq 0x188(%rbx), %rdx
movl 0x1c(%rbx), %r8d
xorl %esi, %esi
testl %r8d, %r8d
jle 0xa832
leaq (,%rsi,8), %rdi
leaq (%rdi,%rdi,2), %r8
movq (%rax,%r8), %rdi
movq (%rdx,%r8), %r9
xorl %r10d, %r10d
movslq (%rcx,%r10,4), %r8
movl (%rdi,%r8,4), %r8d
movl %r8d, (%r9,%r10,4)
incq %r10
movslq 0x1c(%rbx), %r8
cmpq %r8, %r10
jl 0xa817
movl 0x20(%rbx), %edi
incq %rsi
movslq %edi, %r9
cmpq %r9, %rsi
jl 0xa7fb
leaq 0x298(%rbx), %rsi
leaq 0x280(%rbx), %rdi
callq 0xb60e
movslq 0x70(%rbx), %rax
testq %rax, %rax
jle 0xa8af
movl 0x1c(%rbx), %ecx
movq 0x280(%rbx), %rdx
movq 0x370(%rbx), %rsi
movq 0x298(%rbx), %rdi
xorl %r8d, %r8d
testl %ecx, %ecx
jle 0xa8a7
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 0xa88f
incq %r8
cmpq %rax, %r8
jne 0xa874
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq