// File: /workspace/github_cpp_makefile_star2_O2/bobverity[P]MavericK/EM_algorithm.cpp
// Function: EM_noAdmix(globals&, int)
void EM_noAdmix(globals &globals, int Kindex) {
    int K = globals.Kmin+Kindex;
    
    // create empty objects
    vector< vector<double> > logProbMat(globals.n,vector<double>(K));
    double logProbSum;
    vector< vector< vector<double> > > logAlleleFreqs(K);
    double logAlleleFreqsSum;
    double logLike=0;
    double logLike_this_i=0;
    double logLike_this_ik=0;
    
    // initialise logAlleleFreqs and zero global maximum likelihood allele frequencies
    globals.max_alleleFreqs = vector< vector< vector<double> > >(K);
    for (int k=0; k<K; k++) {
        logAlleleFreqs[k] = vector< vector<double> >(globals.loci);
        globals.max_alleleFreqs[k] = vector< vector<double> >(globals.loci);
        for (int l=0; l<globals.loci; l++) {
            logAlleleFreqs[k][l] = vector<double>(globals.J[l]);
            globals.max_alleleFreqs[k][l] = vector<double>(globals.J[l]);
        }
    }
    
    // repeat EM multiple times
    globals.maxLike = log(double(0));
    for (int EMrep=0; EMrep<globals.EMrepeats; EMrep++) {
        
        // initialise random allele frequencies
        for (int k=0; k<K; k++) {
            for (int l=0; l<globals.loci; l++) {
                logAlleleFreqsSum = log(double(0));
                for (int j=0; j<globals.J[l]; j++) {
                    logAlleleFreqs[k][l][j] = log(runif1(0.1,0.9));
                    logAlleleFreqsSum = logSum(logAlleleFreqsSum, logAlleleFreqs[k][l][j]);
                }
                for (int j=0; j<globals.J[l]; j++) {
                    logAlleleFreqs[k][l][j] -= logAlleleFreqsSum;
                }
            }
        }
        
        // EM iterations
        for (int EMiter=0; EMiter<globals.EMiterations; EMiter++) {
            
            // calculate assignment probability matrix
            for (int i=0; i<globals.n; i++) {
                logProbSum = log(double(0));
                for (int k=0; k<K; k++) {
                    logProbMat[i][k] = 0;
                    for (int l=0; l<globals.loci; l++) {
                        for (int p=0; p<globals.ploidy_vec[i]; p++) {
                            if (globals.data[i][l][p]!=0) {
                                logProbMat[i][k] += logAlleleFreqs[k][l][globals.data[i][l][p]-1];
                            }
                        }
                    }
                    logProbSum = logSum(logProbSum, logProbMat[i][k]);
                }
                for (int k=0; k<K; k++) {
                    logProbMat[i][k] -= logProbSum;
                }
            }
            
            // zero allele frequencies
            for (int k=0; k<K; k++) {
                for (int l=0; l<globals.loci; l++) {
                    for (int j=0; j<globals.J[l]; j++) {
                        logAlleleFreqs[k][l][j] = log(double(0));
                    }
                }
            }
            
            // add probability-weighted contribution to allele frequencies
            for (int i=0; i<globals.n; i++) {
                for (int l=0; l<globals.loci; l++) {
                    for (int p=0; p<globals.ploidy_vec[i]; p++) {
                        if (globals.data[i][l][p]!=0) {
                            for (int k=0; k<K; k++) {
                                logAlleleFreqs[k][l][globals.data[i][l][p]-1] = logSum(logAlleleFreqs[k][l][globals.data[i][l][p]-1], logProbMat[i][k]);
                            }
                        }
                    }
                }
            }
            
            // normalise allele frequencies
            for (int k=0; k<K; k++) {
                for (int l=0; l<globals.loci; l++) {
                    logAlleleFreqsSum = log(double(0));
                    for (int j=0; j<globals.J[l]; j++) {
                        // control for underflow
                        if (logAlleleFreqs[k][l][j] < -1e300)
                            logAlleleFreqs[k][l][j] = -1e300;
                        logAlleleFreqsSum = logSum(logAlleleFreqsSum, logAlleleFreqs[k][l][j]);
                    }
                    for (int j=0; j<globals.J[l]; j++) {
                        logAlleleFreqs[k][l][j] -= logAlleleFreqsSum;
                    }
                }
            }
            
        } // end of EM iterations loop
        
        // calculate log-likelihood
        logLike = 0;
        for (int i=0; i<globals.n; i++) {
            logLike_this_i = log(double(0));
            for (int k=0; k<K; k++) {
                logLike_this_ik = 0;
                for (int l=0; l<globals.loci; l++) {
                    for (int p=0; p<globals.ploidy_vec[i]; p++) {
                        if (globals.data[i][l][p]!=0) {
                            logLike_this_ik += logAlleleFreqs[k][l][globals.data[i][l][p]-1];
                        }
                    }
                }
                logLike_this_i = logSum(logLike_this_i, logLike_this_ik - log(double(K)));
            }
            logLike += logLike_this_i;
        }
        
        // replace current values if more likely
        if (logLike>globals.maxLike) {
            globals.maxLike = logLike;
            for (int k=0; k<K; k++) {
                for (int l=0; l<globals.loci; l++) {
                    for (int j=0; j<globals.J[l]; j++) {
                        globals.max_alleleFreqs[k][l][j] = exp(logAlleleFreqs[k][l][j]);
                    }
                }
            }
        }
        
    } // end of repeat EM loop
    
    // calculate model comparison statistics
    if (globals.outputComparisonStatistics_on) {
        
        int freeParameters = K*(sum(globals.J)-globals.loci);
        globals.AIC[Kindex] = 2*freeParameters - 2*globals.maxLike;
        globals.BIC[Kindex] = freeParameters*log(double(globals.n)) - 2*globals.maxLike;
        globals.DIC_Spiegelhalter[Kindex] = -4*globals.structure_loglike_mean[Kindex][0] + 2*globals.maxLike;
        globals.DIC_Gelman[Kindex] = -2*globals.structure_loglike_mean[Kindex][0] + 2*globals.structure_loglike_var[Kindex][0];
        
    }
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
movslq 0x2bc8(%rdi), %rcx
movslq %esi, %rax
movq %rax, 0x68(%rsp)
addq %rax, %rcx
movslq 0x2d18(%rdi), %r15
leaq 0x30(%rsp), %rdi
leaq 0x78(%rsp), %rdx
movq %rcx, 0x48(%rsp)
movq %rcx, %rsi
callq 0x4f6c
leaq 0x90(%rsp), %rdi
leaq 0x30(%rsp), %rdx
leaq 0xf(%rsp), %rcx
movq %r15, %rsi
callq 0x4fbe
leaq 0x30(%rsp), %rdi
callq 0x51a2
leaq 0x30(%rsp), %rdi
leaq 0x78(%rsp), %rdx
movq 0x48(%rsp), %r14
movq %r14, %rsi
callq 0x5014
leaq 0x78(%rsp), %rdi
leaq 0xf(%rsp), %rdx
movq %r14, %rsi
callq 0x5014
leaq 0x3048(%rbx), %rdi
leaq 0x78(%rsp), %r13
movq %rdi, 0x58(%rsp)
movq %r13, %rsi
callq 0x54e6
movq %r13, %rdi
callq 0x5066
leaq 0x2d20(%rbx), %rbp
xorl %r15d, %r15d
testl %r14d, %r14d
movl $0x0, %eax
cmovgl %r14d, %eax
movq %rax, 0x10(%rsp)
leaq 0xf(%rsp), %r14
movq %rbp, 0x20(%rsp)
cmpq 0x10(%rsp), %r15
je 0x3b86
movslq 0x2d1c(%rbx), %rsi
movq %r13, %rdi
movq %r14, %rdx
callq 0x5088
movq %r15, 0x28(%rsp)
imulq $0x18, %r15, %r12
movq 0x30(%rsp), %rdi
addq %r12, %rdi
movq %r13, %rsi
callq 0x5566
movq %r13, %rdi
callq 0x50da
movslq 0x2d1c(%rbx), %rsi
movq %r13, %rdi
movq %r14, %rdx
callq 0x5088
movq 0x58(%rsp), %rax
movq (%rax), %rdi
addq %r12, %rdi
movq %r13, %rsi
callq 0x5566
movq %r13, %rdi
callq 0x50da
xorl %r15d, %r15d
xorl %ecx, %ecx
movslq 0x2d1c(%rbx), %rax
cmpq %rax, %rcx
jge 0x3b6f
movq %r12, %r13
movq 0x20(%rsp), %r12
movq (%r12), %rax
movq %rcx, 0x18(%rsp)
movslq (%rax,%rcx,4), %rsi
leaq 0x78(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rdx
callq 0x4f6c
movq 0x30(%rsp), %rax
movq (%rax,%r13), %rdi
addq %r15, %rdi
movq %rbp, %rsi
callq 0x55b8
movq %rbp, %rdi
callq 0x51a2
movq (%r12), %rax
movq 0x18(%rsp), %rcx
movslq (%rax,%rcx,4), %rsi
movq %rbp, %rdi
movq %r14, %rdx
callq 0x4f6c
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r13), %rdi
addq %r15, %rdi
movq %rbp, %rsi
callq 0x55b8
movq %rbp, %rdi
callq 0x51a2
movq 0x18(%rsp), %rcx
incq %rcx
addq $0x18, %r15
movq %r13, %r12
jmp 0x3ad3
movq 0x28(%rsp), %r15
incq %r15
movq 0x20(%rsp), %rbp
leaq 0x78(%rsp), %r13
jmp 0x3a63
xorps %xmm0, %xmm0
callq 0x32d0
movsd %xmm0, 0x3040(%rbx)
movq 0x48(%rsp), %rax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x70(%rsp)
xorl %ecx, %ecx
cmpl 0x2c34(%rbx), %ecx
jge 0x4165
movl %ecx, 0x64(%rsp)
xorl %r15d, %r15d
cmpq 0x10(%rsp), %r15
je 0x3c90
xorl %r13d, %r13d
movslq 0x2d1c(%rbx), %rax
cmpq %rax, %r13
jge 0x3c88
xorpd %xmm0, %xmm0
callq 0x32d0
movsd %xmm0, 0x18(%rsp)
xorl %r12d, %r12d
movq (%rbp), %rax
movslq (%rax,%r13,4), %rax
cmpq %rax, %r12
jge 0x3c46
movsd 0x23411(%rip), %xmm0    # 0x27010
movsd 0x23411(%rip), %xmm1    # 0x27018
callq 0x1707c
callq 0x32d0
movapd %xmm0, %xmm1
movq 0x30(%rsp), %rax
imulq $0x18, %r15, %rcx
movq (%rax,%rcx), %rax
imulq $0x18, %r13, %rcx
movq (%rax,%rcx), %rax
movsd %xmm0, (%rax,%r12,8)
movsd 0x18(%rsp), %xmm0
callq 0x14ce5
movsd %xmm0, 0x18(%rsp)
incq %r12
jmp 0x3bea
imulq $0x18, %r15, %rcx
addq 0x30(%rsp), %rcx
testl %eax, %eax
movl $0x0, %edx
cmovlel %edx, %eax
xorl %edx, %edx
cmpq %rdx, %rax
je 0x3c80
movq (%rcx), %rsi
imulq $0x18, %r13, %rdi
movq (%rsi,%rdi), %rsi
movsd (%rsi,%rdx,8), %xmm0
subsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rsi,%rdx,8)
incq %rdx
jmp 0x3c5b
incq %r13
jmp 0x3bc8
incq %r15
jmp 0x3bba
xorl %eax, %eax
cmpl 0x2c38(%rbx), %eax
jge 0x3fc4
movl %eax, 0x50(%rsp)
xorl %r15d, %r15d
movslq 0x2d18(%rbx), %rax
cmpq %rax, %r15
jge 0x3d96
xorpd %xmm0, %xmm0
callq 0x32d0
imulq $0x18, %r15, %rbp
xorl %r13d, %r13d
movq 0x90(%rsp), %rax
addq %rbp, %rax
cmpq 0x10(%rsp), %r13
je 0x3d6f
movq (%rax), %rax
andq $0x0, (%rax,%r13,8)
movl 0x2d1c(%rbx), %ecx
movq 0x2cd0(%rbx), %rdx
testl %ecx, %ecx
movl $0x0, %esi
cmovlel %esi, %ecx
xorpd %xmm1, %xmm1
xorl %esi, %esi
cmpq %rcx, %rsi
je 0x3d62
movl (%rdx,%r15,4), %edi
movq 0x2d00(%rbx), %r8
addq %rbp, %r8
imulq $0x18, %r13, %r9
addq 0x30(%rsp), %r9
testl %edi, %edi
movl $0x0, %r10d
cmovlel %r10d, %edi
xorl %r10d, %r10d
cmpq %r10, %rdi
je 0x3d5d
movq (%r8), %r11
imulq $0x18, %rsi, %r14
movq (%r11,%r14), %r11
movslq (%r11,%r10,4), %r11
testq %r11, %r11
je 0x3d58
movq (%r9), %r12
movq (%r12,%r14), %r14
addsd -0x8(%r14,%r11,8), %xmm1
movsd %xmm1, (%rax,%r13,8)
incq %r10
jmp 0x3d2b
incq %rsi
jmp 0x3d00
callq 0x14ce5
incq %r13
jmp 0x3cc5
xorl %ecx, %ecx
cmpq %rcx, 0x10(%rsp)
je 0x3d8e
movq (%rax), %rdx
movsd (%rdx,%rcx,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rcx,8)
incq %rcx
jmp 0x3d71
incq %r15
jmp 0x3ca5
xorl %r15d, %r15d
movq 0x20(%rsp), %rbp
cmpq 0x10(%rsp), %r15
je 0x3df7
xorl %r14d, %r14d
movslq 0x2d1c(%rbx), %rax
cmpq %rax, %r14
jge 0x3df2
xorl %r12d, %r12d
movq (%rbp), %rax
movslq (%rax,%r14,4), %rax
cmpq %rax, %r12
jge 0x3ded
xorpd %xmm0, %xmm0
callq 0x32d0
movq 0x30(%rsp), %rax
imulq $0x18, %r15, %rcx
movq (%rax,%rcx), %rax
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %rax
movsd %xmm0, (%rax,%r12,8)
incq %r12
jmp 0x3db7
incq %r14
jmp 0x3da8
incq %r15
jmp 0x3d9e
xorl %ecx, %ecx
movslq 0x2d18(%rbx), %rax
cmpq %rax, %rcx
jge 0x3ef8
xorl %edx, %edx
movq %rcx, 0x28(%rsp)
movslq 0x2d1c(%rbx), %rax
cmpq %rax, %rdx
jge 0x3ef0
xorl %ebp, %ebp
movq %rdx, 0x18(%rsp)
movq 0x2cd0(%rbx), %rax
movslq (%rax,%rcx,4), %rax
cmpq %rax, %rbp
jge 0x3ee3
movq 0x2d00(%rbx), %rax
imulq $0x18, %rcx, %r12
movq (%rax,%r12), %rax
imulq $0x18, %rdx, %r13
movq (%rax,%r13), %rax
cmpl $0x0, (%rax,%rbp,4)
je 0x3ed1
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpq %r14, 0x10(%rsp)
je 0x3ed1
movq 0x30(%rsp), %rax
movq (%rax,%r15), %rax
movq 0x2d00(%rbx), %rcx
movq (%rcx,%r12), %rcx
movq (%rcx,%r13), %rcx
movslq (%rcx,%rbp,4), %rcx
movq (%rax,%r13), %rax
movsd -0x8(%rax,%rcx,8), %xmm0
movq 0x90(%rsp), %rax
movq (%rax,%r12), %rax
movsd (%rax,%r14,8), %xmm1
callq 0x14ce5
movq 0x30(%rsp), %rax
movq (%rax,%r15), %rax
movq 0x2d00(%rbx), %rcx
movq (%rcx,%r12), %rcx
movq (%rcx,%r13), %rcx
movslq (%rcx,%rbp,4), %rcx
movq (%rax,%r13), %rax
movsd %xmm0, -0x8(%rax,%rcx,8)
incq %r14
addq $0x18, %r15
jmp 0x3e5e
incq %rbp
movq 0x28(%rsp), %rcx
movq 0x18(%rsp), %rdx
jmp 0x3e27
incq %rdx
movq 0x20(%rsp), %rbp
jmp 0x3e10
incq %rcx
jmp 0x3df9
xorl %r15d, %r15d
cmpq 0x10(%rsp), %r15
je 0x3fb9
xorl %r13d, %r13d
movslq 0x2d1c(%rbx), %rax
cmpq %rax, %r13
jge 0x3fb1
xorpd %xmm0, %xmm0
callq 0x32d0
xorl %r14d, %r14d
movq (%rbp), %rax
movslq (%rax,%r13,4), %rcx
imulq $0x18, %r15, %rax
addq 0x30(%rsp), %rax
cmpq %rcx, %r14
jge 0x3f7a
movq (%rax), %rax
imulq $0x18, %r13, %rcx
movq (%rax,%rcx), %rax
movsd (%rax,%r14,8), %xmm1
movsd 0x230cc(%rip), %xmm2    # 0x27020
ucomisd %xmm1, %xmm2
jbe 0x3f70
movabsq $-0x1c81bc377ff8a64, %rcx # imm = 0xFE37E43C8800759C
movq %rcx, (%rax,%r14,8)
movsd 0x230b0(%rip), %xmm1    # 0x27020
callq 0x14ce5
incq %r14
jmp 0x3f25
testl %ecx, %ecx
movl $0x0, %edx
cmovlel %edx, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x3fa9
movq (%rax), %rsi
imulq $0x18, %r13, %rdi
movq (%rsi,%rdi), %rsi
movsd (%rsi,%rdx,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rsi,%rdx,8)
incq %rdx
jmp 0x3f86
incq %r13
jmp 0x3f09
incq %r15
jmp 0x3efb
movl 0x50(%rsp), %eax
incl %eax
jmp 0x3c92
xorpd %xmm0, %xmm0
xorl %r15d, %r15d
movslq 0x2d18(%rbx), %rax
cmpq %rax, %r15
jge 0x40c7
movsd %xmm0, 0x50(%rsp)
xorpd %xmm0, %xmm0
callq 0x32d0
movapd %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
xorl %r12d, %r12d
cmpq 0x10(%rsp), %r12
je 0x40bb
movl 0x2d1c(%rbx), %eax
movq 0x2cd0(%rbx), %rcx
imulq $0x18, %r15, %rdx
addq 0x2d00(%rbx), %rdx
imulq $0x18, %r12, %rsi
addq 0x30(%rsp), %rsi
testl %eax, %eax
movl $0x0, %edi
cmovlel %edi, %eax
xorpd %xmm0, %xmm0
xorl %edi, %edi
cmpq %rax, %rdi
je 0x407d
movl (%rcx,%r15,4), %r8d
testl %r8d, %r8d
movl $0x0, %r9d
cmovlel %r9d, %r8d
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x4078
movq (%rdx), %r10
imulq $0x18, %rdi, %r11
movq (%r10,%r11), %r10
movslq (%r10,%r9,4), %r10
testq %r10, %r10
je 0x4073
movq (%rsi), %r14
movq (%r14,%r11), %r11
addsd -0x8(%r11,%r10,8), %xmm0
incq %r9
jmp 0x404c
incq %rdi
jmp 0x4033
movsd %xmm0, 0x28(%rsp)
movsd 0x70(%rsp), %xmm0
movsd %xmm1, 0x18(%rsp)
callq 0x32d0
movsd 0x28(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
callq 0x14ce5
movapd %xmm0, %xmm1
incq %r12
movsd 0x50(%rsp), %xmm0
jmp 0x3ff7
addsd %xmm1, %xmm0
incq %r15
jmp 0x3fcb
ucomisd 0x3040(%rbx), %xmm0
movq 0x48(%rsp), %rax
jbe 0x415a
movsd %xmm0, 0x3040(%rbx)
xorl %r15d, %r15d
cmpq 0x10(%rsp), %r15
je 0x415a
xorl %r14d, %r14d
movslq 0x2d1c(%rbx), %rax
cmpq %rax, %r14
jge 0x4150
xorl %r12d, %r12d
movq (%rbp), %rax
movslq (%rax,%r14,4), %rax
cmpq %rax, %r12
jge 0x414b
movq 0x30(%rsp), %rax
imulq $0x18, %r15, %r13
movq (%rax,%r13), %rax
imulq $0x18, %r14, %rbp
movq (%rax,%rbp), %rax
movsd (%rax,%r12,8), %xmm0
callq 0x35c0
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r13), %rax
movq (%rax,%rbp), %rax
movq 0x20(%rsp), %rbp
movsd %xmm0, (%rax,%r12,8)
incq %r12
jmp 0x40fe
incq %r14
jmp 0x40ef
incq %r15
movq 0x48(%rsp), %rax
jmp 0x40e5
movl 0x64(%rsp), %ecx
incl %ecx
jmp 0x3ba7
cmpb $0x0, 0x2c49(%rbx)
je 0x424e
movq %rbp, %rdi
movq %rax, %r14
callq 0x50fb
subl 0x2d1c(%rbx), %eax
imull %r14d, %eax
leal (%rax,%rax), %ecx
cvtsi2sd %ecx, %xmm0
movsd 0x3040(%rbx), %xmm1
addsd %xmm1, %xmm1
subsd %xmm1, %xmm0
movq 0x3078(%rbx), %rcx
movq 0x68(%rsp), %r14
movsd %xmm0, (%rcx,%r14,8)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
cvtsi2sdl 0x2d18(%rbx), %xmm0
callq 0x32d0
movsd 0x3040(%rbx), %xmm1
mulsd 0x10(%rsp), %xmm0
addsd %xmm1, %xmm1
subsd %xmm1, %xmm0
movq 0x3090(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movq 0x2e60(%rbx), %rax
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %rax
movsd (%rax), %xmm0
movsd 0x3040(%rbx), %xmm1
addsd %xmm1, %xmm1
mulsd 0x22df7(%rip), %xmm0    # 0x27008
addsd %xmm1, %xmm0
movq 0x30a8(%rbx), %rdx
movsd %xmm0, (%rdx,%r14,8)
movsd (%rax), %xmm0
movq 0x2e78(%rbx), %rax
movq (%rax,%rcx), %rax
movsd (%rax), %xmm1
addsd %xmm1, %xmm1
addsd %xmm0, %xmm0
subsd %xmm0, %xmm1
movq 0x30c0(%rbx), %rax
movsd %xmm1, (%rax,%r14,8)
leaq 0x30(%rsp), %rdi
callq 0x5066
leaq 0x90(%rsp), %rdi
callq 0x50da
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x42a2
movq %rax, %rbx
jmp 0x42af
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x51a2
jmp 0x42bc
movq %rax, %rbx
jmp 0x42bc
jmp 0x42a2
jmp 0x42a2
jmp 0x42a2
jmp 0x42a2
jmp 0x42a2
jmp 0x42a2
jmp 0x42a2
jmp 0x42a2
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x5066
leaq 0x90(%rsp), %rdi
callq 0x50da
movq %rbx, %rdi
callq 0x35a0