// File: /workspace/github_cpp_makefile_star2_O1/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 0x88(%rdi), %eax
addl 0x84(%rdi), %eax
jle 0xddab
movl 0x78(%rsp), %ecx
movq 0x10(%rsp), %rax
leaq 0xb70(%rax), %rdx
movq %rdx, 0x30(%rsp)
incl %ecx
movl %ecx, 0x8(%rsp)
leaq 0x1f70(%rax), %r15
xorl %r14d, %r14d
movl $0x1, %r13d
leaq 0x297a2(%rip), %r12     # 0x37290
movl %r13d, %ebp
testl %r13d, %r13d
jle 0xdb02
movq %rbx, %rdi
callq 0xdf3c
decl %ebp
jne 0xdaf6
cmpl 0x84(%rbx), %r14d
jne 0xdb12
movl 0x8c(%rbx), %r13d
cmpb $0x0, 0xc(%rsp)
je 0xdb47
movq %rbx, %rdi
callq 0xe2e2
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0xe39c
movq %rbx, %rdi
callq 0xe6a2
cmpl 0x84(%rbx), %r14d
jl 0xdb47
movq %rbx, %rdi
callq 0xe728
movq %rbx, %rdi
callq 0xe7ae
cmpb $0x0, 0x18(%rsp)
je 0xdb66
movq %rbx, %rdi
callq 0xe8e2
movq %rbx, %rdi
callq 0xea18
movl %r14d, %eax
subl 0x84(%rbx), %eax
jl 0xdc1a
movsd 0x120(%rbx), %xmm0
movsd 0x128(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x128(%rbx)
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd 0x148(%rbx), %xmm1
movsd %xmm1, 0x148(%rbx)
cmpb $0x0, 0x24(%rsp)
je 0xdbbe
cltq
movq 0x130(%rbx), %rcx
movsd %xmm0, (%rcx,%rax,8)
movsd 0x168(%rbx), %xmm0
movsd 0x120(%rbx), %xmm1
xorpd 0x2946a(%rip), %xmm1    # 0x37040
callq 0x1aa59
movsd %xmm0, 0x168(%rbx)
cmpb $0x0, 0x18(%rsp)
je 0xdc1a
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 0xdce6
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 0x29c4e(%rip), %rsi     # 0x37922
callq 0x3390
movq %r13, %rdi
movl 0x28(%rsp), %r13d
callq 0x31f0
cmpb $0x0, 0x70(%rsp)
je 0xdd93
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
movl %r14d, %esi
subl 0x84(%rbx), %esi
incl %esi
movq %rbp, %rdi
callq 0x3550
cmpl $0x0, 0x20(%rbx)
jle 0xdd77
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 0xdd49
movl $0x1, %edx
movq %r15, %rdi
leaq 0x29b9c(%rip), %rsi     # 0x37922
callq 0x3390
movq %r15, %rdi
callq 0x31f0
incl %r14d
movl 0x88(%rbx), %eax
addl 0x84(%rbx), %eax
cmpl %eax, %r14d
jl 0xdaee
cmpb $0x0, 0xc(%rsp)
je 0xdf0c
movl 0x20(%rbx), %edi
testl %edi, %edi
jle 0xde38
xorl %r14d, %r14d
cmpl $0x0, 0x1c(%rbx)
jle 0xde2c
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), %rax
cmpq %rax, %r12
jl 0xddd5
incq %r14
movslq 0x20(%rbx), %rdi
cmpq %rdi, %r14
jl 0xddc0
cmpb $0x1, 0x80(%rbx)
jne 0xdf0c
testl %edi, %edi
jle 0xdea8
movl 0x1c(%rbx), %eax
movq 0x218(%rbx), %rcx
movq 0x10(%rsp), %rdx
movq 0x2cb8(%rdx), %rdx
movq 0x230(%rbx), %rsi
movl %edi, %edi
xorl %r8d, %r8d
testl %eax, %eax
jle 0xdea0
leaq (%r8,%r8,2), %r9
movq (%rcx,%r9,8), %r9
movslq (%rdx,%r8,4), %r10
leaq (%r10,%r10,2), %r10
movq (%rsi,%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, %rax
jne 0xde86
incq %r8
cmpq %rdi, %r8
jne 0xde6b
movq 0x60(%rbx), %rax
subq 0x58(%rbx), %rax
shrq $0x5, %rax
testl %eax, %eax
jle 0xdf0c
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 0xdf04
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 0xdeec
incq %rdi
cmpq %rax, %rdi
jne 0xded5
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