// File: /workspace/github_cpp_makefile_star2_O3/bobverity[P]MavericK/MCMCobject_noAdmixture.cpp
// Function: MCMCobject_noAdmixture::perform_MCMC(globals&, bool, bool, bool, bool, bool, int)
void MCMCobject_noAdmixture::perform_MCMC(globals &globals, bool drawAlleleFreqs, bool storeLoglike, bool fixLabels, bool outputLikelihood, bool outputPosteriorGrouping, int mainRep) {
    
    // perform MCMC
    int thinSwitch = 1;
    for (int rep=0; rep<(burnin+samples); rep++) {
        
        // thinning loop (becomes active after burn-in)
        for (int thin=0; thin<thinSwitch; thin++) {
            
            // update group allocation of all individuals
            group_update();
            
        }
        if (rep==burnin)
            thinSwitch = thinning;
        
        // if fix label-switching problem
        if (fixLabels) {
            // calculate logQmatrix_ind_new for this iteration
            produceQmatrix();
        
            // fix label-switching problem
            chooseBestLabelPermutation(globals, rep);
        
            // add logQmatrix_ind_new to logQmatrix_ind_running
            updateQmatrix(rep);
            
            // store Qmatrix values if no longer in burn-in
            if (rep>=burnin)
                storeQmatrix();
        }
        
        // calculate marginal likelihood
        d_logLikeGroup();
        
        // optionally draw allele frequencies and calculate joint likelihood
        if (drawAlleleFreqs) {
            drawFreqs();
            d_logLikeJoint();
        }
        
        // add likelihoods to running sums
        if (rep>=burnin) {
            logLikeGroup_sum += logLikeGroup;
            logLikeGroup_sumSquared += logLikeGroup*logLikeGroup;
            
            if (storeLoglike) {
                logLikeGroup_store[rep-burnin] = logLikeGroup;
            }

            harmonic = logSum(harmonic, -logLikeGroup);
            if (drawAlleleFreqs) {
                logLikeJoint_sum += logLikeJoint;
                logLikeJoint_sumSquared += logLikeJoint*logLikeJoint;
            }
        }
        
        // write to outputLikelihoods file
        if (outputLikelihood) {
            globals.outputLikelihood_fileStream << K << "," << mainRep+1 << "," << rep-burnin+1 << "," << logLikeGroup << "," << logLikeJoint << "\n";
            globals.outputLikelihood_fileStream.flush();
        }
        
        // write to outputPosteriorGrouping file
        if (outputPosteriorGrouping) {
            globals.outputPosteriorGrouping_fileStream << K << "," << mainRep+1 << "," << rep-burnin+1;
            for (int i=0; i<n; i++) {
                globals.outputPosteriorGrouping_fileStream << "," << group[i];
            }
            globals.outputPosteriorGrouping_fileStream << "\n";
            globals.outputPosteriorGrouping_fileStream.flush();
        }
            
    } // end of MCMC

    
    // finish off Qmatrices
    if (fixLabels) {
        
        // finish off individual level Qmatrix
        for (int i=0; i<n; i++) {
            for (int k=0; k<K; k++) {
                Qmatrix_ind[i][k] = exp(logQmatrix_ind[i][k] - log(double(samples)));
            }
        }
        
        // calculate population level Qmatrices
        if (outputQmatrix_pop_on) {
            for (int i=0; i<n; i++) {
                for (int k=0; k<K; k++) {
                    Qmatrix_pop[globals.pop_index[i]][k] += Qmatrix_ind[i][k];
                }
            }
            for (int i=0; i<int(uniquePops.size()); i++) {
                for (int k=0; k<K; k++) {
                    Qmatrix_pop[i][k] /= double(globals.uniquePop_counts[i]);
                }
            }
        }
    } // end if fixLabels
    
    // finish off harmonic mean
    harmonic = log(double(samples))-harmonic;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, 0x2c(%rsp)
movl %r8d, 0xc(%rsp)
movl %ecx, 0x24(%rsp)
movl %edx, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
movl 0x84(%rdi), %eax
movl 0x88(%rdi), %ecx
addl %eax, %ecx
jle 0xd964
movl 0x78(%rsp), %edx
movq 0x10(%rsp), %rcx
leaq 0xb70(%rcx), %rsi
movq %rsi, 0x30(%rsp)
incl %edx
movl %edx, 0x8(%rsp)
leaq 0x1f70(%rcx), %r15
xorl %r14d, %r14d
movl $0x1, %r13d
leaq 0x28bf0(%rip), %r12     # 0x36290
movl %r13d, %ebp
testl %r13d, %r13d
jle 0xd6ba
movq %rbx, %rdi
callq 0xdaf8
decl %ebp
jne 0xd6a8
movl 0x84(%rbx), %eax
cmpl %eax, %r14d
jne 0xd6c6
movl 0x8c(%rbx), %r13d
cmpb $0x0, 0xc(%rsp)
je 0xd6fb
movq %rbx, %rdi
callq 0xde8e
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0xdf46
movq %rbx, %rdi
callq 0xe230
cmpl 0x84(%rbx), %r14d
jl 0xd6fb
movq %rbx, %rdi
callq 0xe2c2
movq %rbx, %rdi
callq 0xe354
cmpb $0x0, 0x18(%rsp)
je 0xd71a
movq %rbx, %rdi
callq 0xe48c
movq %rbx, %rdi
callq 0xe5ba
movl %r14d, %eax
subl 0x84(%rbx), %eax
jl 0xd7ce
movsd 0x120(%rbx), %xmm1
movsd 0x128(%rbx), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x128(%rbx)
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
addsd 0x148(%rbx), %xmm0
movsd %xmm0, 0x148(%rbx)
cmpb $0x0, 0x24(%rsp)
je 0xd77a
cltq
movq 0x130(%rbx), %rcx
movsd %xmm1, (%rcx,%rax,8)
movsd 0x120(%rbx), %xmm1
xorpd 0x288be(%rip), %xmm1    # 0x36040
movsd 0x168(%rbx), %xmm0
callq 0x1a75d
movsd %xmm0, 0x168(%rbx)
cmpb $0x0, 0x18(%rsp)
je 0xd7ce
movsd 0x150(%rbx), %xmm0
movsd 0x158(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x158(%rbx)
mulsd %xmm0, %xmm0
addsd 0x160(%rbx), %xmm0
movsd %xmm0, 0x160(%rbx)
cmpb $0x0, 0x2c(%rsp)
je 0xd89a
movl 0x1c(%rbx), %esi
movl %r13d, 0x28(%rsp)
movq 0x30(%rsp), %r13
movq %r13, %rdi
callq 0x3550
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x3390
movq %rbp, %rdi
movl 0x8(%rsp), %esi
callq 0x3550
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x3390
movl %r14d, %esi
subl 0x84(%rbx), %esi
incl %esi
movq %rbp, %rdi
callq 0x3550
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x3390
movsd 0x120(%rbx), %xmm0
movq %rbp, %rdi
callq 0x34a0
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x3390
movsd 0x150(%rbx), %xmm0
movq %rbp, %rdi
callq 0x34a0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2909a(%rip), %rsi     # 0x36922
callq 0x3390
movq %r13, %rdi
movl 0x28(%rsp), %r13d
callq 0x31f0
cmpb $0x0, 0x70(%rsp)
je 0xd94a
movl 0x1c(%rbx), %esi
movq %r15, %rdi
callq 0x3550
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x3390
movq %rbp, %rdi
movl 0x8(%rsp), %esi
callq 0x3550
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x3390
incl %r14d
movl %r14d, %esi
subl 0x84(%rbx), %esi
movq %rbp, %rdi
callq 0x3550
cmpl $0x0, 0x20(%rbx)
jle 0xd92c
xorl %ebp, %ebp
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x3390
movq 0x90(%rbx), %rax
movl (%rax,%rbp,4), %esi
movq %r15, %rdi
callq 0x3550
incq %rbp
movslq 0x20(%rbx), %rax
cmpq %rax, %rbp
jl 0xd8fe
movl $0x1, %edx
movq %r15, %rdi
leaq 0x28fe7(%rip), %rsi     # 0x36922
callq 0x3390
movq %r15, %rdi
callq 0x31f0
jmp 0xd94d
incl %r14d
movl 0x84(%rbx), %eax
movl 0x88(%rbx), %ecx
addl %eax, %ecx
cmpl %ecx, %r14d
jl 0xd6a0
cmpb $0x0, 0xc(%rsp)
je 0xdac8
movl 0x20(%rbx), %eax
testl %eax, %eax
jle 0xd9f4
movl 0x1c(%rbx), %ecx
xorl %r14d, %r14d
testl %ecx, %ecx
jle 0xd9e9
leaq (,%r14,8), %rax
leaq (%rax,%rax,2), %r15
xorl %r12d, %r12d
movq 0x200(%rbx), %rax
movq (%rax,%r15), %rax
movsd (%rax,%r12,8), %xmm0
movsd %xmm0, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x88(%rbx), %xmm0
callq 0x3290
movsd 0x18(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x35a0
movq 0x218(%rbx), %rax
movq (%rax,%r15), %rax
movsd %xmm0, (%rax,%r12,8)
incq %r12
movslq 0x1c(%rbx), %rcx
cmpq %rcx, %r12
jl 0xd98f
movl 0x20(%rbx), %eax
incq %r14
movslq %eax, %rdx
cmpq %rdx, %r14
jl 0xd97c
cmpb $0x1, 0x80(%rbx)
jne 0xdac8
testl %eax, %eax
jle 0xda64
movl 0x1c(%rbx), %ecx
movq 0x218(%rbx), %rdx
movq 0x10(%rsp), %rsi
movq 0x2cb8(%rsi), %rsi
movq 0x230(%rbx), %rdi
movl %eax, %eax
xorl %r8d, %r8d
testl %ecx, %ecx
jle 0xda5c
leaq (%r8,%r8,2), %r9
movq (%rdx,%r9,8), %r9
movslq (%rsi,%r8,4), %r10
leaq (%r10,%r10,2), %r10
movq (%rdi,%r10,8), %r10
xorl %r11d, %r11d
movsd (%r9,%r11,8), %xmm0
addsd (%r10,%r11,8), %xmm0
movsd %xmm0, (%r10,%r11,8)
incq %r11
cmpq %r11, %rcx
jne 0xda42
incq %r8
cmpq %rax, %r8
jne 0xda27
movq 0x60(%rbx), %rax
subq 0x58(%rbx), %rax
shrq $0x5, %rax
testl %eax, %eax
jle 0xdac8
movl 0x1c(%rbx), %ecx
movq 0x10(%rsp), %rdx
movq 0x2ca0(%rdx), %rdx
movq 0x230(%rbx), %rsi
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edi, %edi
testl %ecx, %ecx
jle 0xdac0
xorps %xmm0, %xmm0
cvtsi2sdl (%rdx,%rdi,4), %xmm0
leaq (%rdi,%rdi,2), %r8
movq (%rsi,%r8,8), %r8
xorl %r9d, %r9d
movsd (%r8,%r9,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r8,%r9,8)
incq %r9
cmpq %r9, %rcx
jne 0xdaa8
incq %rdi
cmpq %rax, %rdi
jne 0xda91
xorps %xmm0, %xmm0
cvtsi2sdl 0x88(%rbx), %xmm0
callq 0x3290
subsd 0x168(%rbx), %xmm0
movsd %xmm0, 0x168(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop