// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
movl 0x1c(%rdi), %ecx
leaq 0x340(%rdi), %rax
movq %rax, 0x10(%rsp)
leaq 0x298(%rdi), %rax
movq %rax, 0x8(%rsp)
xorl %esi, %esi
testl %ecx, %ecx
movl $0x0, %edi
cmovgl %ecx, %edi
xorl %r8d, %r8d
cmpq %rdi, %r8
je 0x8e69
imulq $0x18, %r8, %r9
addq 0x340(%rbx), %r9
movl 0x70(%rbx), %r10d
testl %r10d, %r10d
cmovlel %esi, %r10d
imulq $0x18, %r10, %r10
xorl %r11d, %r11d
cmpq %rcx, %r11
je 0x8e61
movq (%r9), %r15
andq $0x0, (%r15,%r11,8)
movq 0x298(%rbx), %r12
movq 0x2b0(%rbx), %r13
movq 0x2c8(%rbx), %rbp
xorpd %xmm0, %xmm0
xorl %r14d, %r14d
cmpq %r14, %r10
je 0x8e5c
movq (%r13,%r14), %rax
movq (%r12,%r14), %rdx
movsd (%rdx,%r8,8), %xmm1
movq (%rbp,%r14), %rdx
subsd (%rdx,%r11,8), %xmm1
mulsd (%rax,%r8,8), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, (%r15,%r11,8)
addq $0x18, %r14
jmp 0x8e27
incq %r11
jmp 0x8dfe
incq %r8
jmp 0x8dd8
leaq 0x388(%rbx), %rdx
leaq 0x3a0(%rbx), %rcx
leaq 0x3b8(%rbx), %r8
leaq 0x3d0(%rbx), %r9
movq 0x18(%rsp), %rax
movzbl 0x2c3c(%rax), %r10d
addq $0x970, %rax            # imm = 0x970
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq 0x10(%rsp), %rsi
pushq %rax
pushq %r10
callq 0x580a
popq %rax
popq %rcx
leaq 0x358(%rbx), %rdi
movq %r15, %rsi
callq 0x6266
movq %r15, %rdi
callq 0x61d0
movq 0x358(%rbx), %rax
movq 0x370(%rbx), %rcx
xorl %edx, %edx
xorl %esi, %esi
movslq 0x1c(%rbx), %rdi
cmpq %rdi, %rdx
jge 0x8efa
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 0x8ed8
testb $0x1, %sil
je 0x90f5
orl $-0x1, 0x108(%rbx)
xorl %eax, %eax
movslq 0x20(%rbx), %rcx
cmpq %rcx, %rax
jge 0x8fa0
movq 0x40(%rbx), %rcx
imulq $0x18, %rax, %rdx
xorl %esi, %esi
movslq 0x24(%rbx), %rdi
cmpq %rdi, %rsi
jge 0x8f98
movq 0xc0(%rbx), %rdi
movq 0x358(%rbx), %r8
movq 0xd8(%rbx), %r9
addq %rdx, %r9
imulq $0x18, %rsi, %r10
xorl %r11d, %r11d
movslq (%rcx,%rax,4), %r14
cmpq %r14, %r11
jge 0x8f93
movslq 0x108(%rbx), %r14
leaq 0x1(%r14), %r15
movl %r15d, 0x108(%rbx)
movslq 0x4(%rdi,%r14,4), %r15
movl -0x4(%r8,%r15,4), %ebp
incl %ebp
movl %ebp, 0x4(%rdi,%r14,4)
movslq 0x108(%rbx), %r14
movl (%rdi,%r14,4), %ebp
movq (%r9), %r14
movq (%r14,%r10), %r14
movl %ebp, (%r14,%r11,4)
incq %r11
jmp 0x8f4c
incq %rsi
jmp 0x8f24
incq %rax
jmp 0x8f0d
leaq 0x110(%rbx), %rsi
leaq 0x158(%rbx), %rdi
callq 0x95d6
leaq 0x128(%rbx), %rsi
leaq 0x170(%rbx), %rdi
callq 0x9980
xorl %r15d, %r15d
xorl %r12d, %r12d
movslq 0x1c(%rbx), %rax
cmpq %rax, %r12
jge 0x9028
movq 0x370(%rbx), %rax
movslq (%rax,%r12,4), %rax
imulq $0x18, %rax, %rsi
addq 0x158(%rbx), %rsi
movq 0x110(%rbx), %rdi
addq %r15, %rdi
callq 0x9980
movq 0x370(%rbx), %rax
movslq (%rax,%r12,4), %rax
imulq $0x18, %rax, %rsi
addq 0x170(%rbx), %rsi
movq 0x128(%rbx), %rdi
addq %r15, %rdi
callq 0x969e
incq %r12
addq $0x18, %r15
jmp 0x8fcc
leaq 0x188(%rbx), %rsi
leaq 0x1d0(%rbx), %rdi
callq 0x9980
xorl %eax, %eax
movslq 0x20(%rbx), %rcx
cmpq %rcx, %rax
jge 0x9089
imulq $0x18, %rax, %rcx
movq 0x1d0(%rbx), %rdx
addq %rcx, %rdx
movq 0x370(%rbx), %rsi
addq 0x188(%rbx), %rcx
xorl %edi, %edi
movslq 0x1c(%rbx), %r8
cmpq %r8, %rdi
jge 0x9084
movslq (%rsi,%rdi,4), %r8
movq (%rdx), %r9
movl (%r9,%r8,4), %r8d
movq (%rcx), %r9
movl %r8d, (%r9,%rdi,4)
incq %rdi
jmp 0x9064
incq %rax
jmp 0x903d
leaq 0x280(%rbx), %rdi
movq 0x8(%rsp), %rsi
callq 0x9a48
movl 0x1c(%rbx), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movl 0x70(%rbx), %edx
testl %edx, %edx
cmovlel %ecx, %edx
cmpq %rdx, %rcx
je 0x90f5
imulq $0x18, %rcx, %rsi
movq 0x280(%rbx), %rdi
addq %rsi, %rdi
movq 0x370(%rbx), %r8
addq 0x298(%rbx), %rsi
xorl %r9d, %r9d
cmpq %r9, %rax
je 0x90f0
movslq (%r8,%r9,4), %r10
movq (%rdi), %r11
movsd (%r11,%r10,8), %xmm0
movq (%rsi), %r10
movsd %xmm0, (%r10,%r9,8)
incq %r9
jmp 0x90d0
incq %rcx
jmp 0x90ac
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq