// File: /workspace/github_cpp_makefile_star2_O2/bobverity[P]MavericK/MCMCobject_admixture.cpp
// Function: MCMCobject_admixture::perform_MCMC(globals&, bool, bool, bool, bool, bool, int)
void MCMCobject_admixture::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 at the gene copy level
            group_update();
            
            // update group allocation at individual level. Improves mixing when alpha very small.
            group_update_indLevel();
            
            // if alpha not fixed update by Metropolis step
            if (globals.fixAlpha_on==0)
                alpha_update();
            
        }
        if (rep==burnin)
            thinSwitch = thinning;
        
        // if fix label-switching problem
        if (fixLabels) {
            // calculate logQmatrix_gene_new for this iteration
            produceQmatrix();
            
            // fix label-switching problem
            chooseBestLabelPermutation(globals, rep);
            
            // add logQmatrix_gene_new to logQmatrix_gene_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 admixture proportions 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==true) {
                logLikeJoint_sum += logLikeJoint;
                logLikeJoint_sumSquared += logLikeJoint*logLikeJoint;
            }
        }
        
        // write to outputLikelihoods file
        if (outputLikelihood) {
            globals.outputLikelihood_fileStream << K << "," << mainRep+1 << "," << rep-burnin+1 << "," << logLikeGroup << "," << logLikeJoint << "," << alpha << "\n";
			globals.outputLikelihood_fileStream.flush();
        }
        // write to outputPosteriorGrouping file
        if (outputPosteriorGrouping) {
            globals.outputPosteriorGrouping_fileStream << K << "," << mainRep+1 << "," << rep-burnin+1;
            groupIndex=-1;
            for (int i=0; i<n; i++) {
                for (int l=0; l<loci; l++) {
                    for (int p=0; p<ploidy_vec[i]; p++) {
                        groupIndex++;
                        globals.outputPosteriorGrouping_fileStream << "," << linearGroup[groupIndex];
                    }
                }
            }
            globals.outputPosteriorGrouping_fileStream << "\n";
			globals.outputPosteriorGrouping_fileStream.flush();
        }
        
    } // end of MCMC
    
    
    // finish off Qmatrices
    if (fixLabels) {
        
        // finish off gene level Qmatrices
        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++;
                    for (int k=0; k<K; k++) {
                        Qmatrix_gene[groupIndex][k] = exp(logQmatrix_gene[groupIndex][k] - log(double(samples)));
                    } // k
                } // p
            } // l
        } // ind
        
        
        // calculate individual level Qmatrices
        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++;
                    for (int k=0; k<K; k++) {
                        Qmatrix_ind[ind][k] += Qmatrix_gene[groupIndex][k];
                    }
                }
            }
            for (int k=0; k<K; k++) {
                Qmatrix_ind[ind][k] /= double(ploidy_vec[ind]*loci);
            }
        }
        
        // 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 of 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, 0x24(%rsp)
movl %r8d, %r14d
movl %ecx, 0x1c(%rsp)
movl %edx, 0x10(%rsp)
movq %rdi, %rbx
movl 0x78(%rsp), %eax
leaq 0xb70(%rsi), %rcx
movq %rcx, 0x28(%rsp)
incl %eax
movl %eax, (%rsp)
movq %rsi, 0x8(%rsp)
leaq 0x1f70(%rsi), %rbp
xorl %r13d, %r13d
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
leaq 0x1f947(%rip), %r15     # 0x27240
movl %r8d, 0x20(%rsp)
movl 0xa8(%rbx), %eax
addl 0xa4(%rbx), %eax
cmpl %eax, %r13d
jge 0x7c03
movl 0x4(%rsp), %eax
testl %eax, %eax
movl $0x0, %r12d
cmovgl %eax, %r12d
subl $0x1, %r12d
jb 0x7951
movq %rbx, %rdi
callq 0x7eae
movq %rbx, %rdi
callq 0x81ee
movq 0x8(%rsp), %rax
cmpb $0x0, 0x2bd1(%rax)
jne 0x7923
movq %rbx, %rdi
callq 0x8934
jmp 0x7923
movq %r13, %rax
cmpl 0xa4(%rbx), %eax
jne 0x7966
movl 0xac(%rbx), %eax
movl %eax, 0x4(%rsp)
testb %r14b, %r14b
je 0x7999
movq %rbx, %rdi
callq 0x8ba2
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x8d98
movq %rbx, %rdi
callq 0x9104
cmpl 0xa4(%rbx), %r13d
jl 0x7999
movq %rbx, %rdi
callq 0x917a
movq %rbx, %rdi
callq 0x9230
cmpb $0x0, 0x10(%rsp)
je 0x79b8
movq %rbx, %rdi
callq 0x9340
movq %rbx, %rdi
callq 0x94e2
movq %r13, %rax
subl 0xa4(%rbx), %eax
jl 0x7a6c
movsd 0x1e8(%rbx), %xmm1
movsd 0x1f0(%rbx), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x1f0(%rbx)
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
addsd 0x210(%rbx), %xmm0
movsd %xmm0, 0x210(%rbx)
cmpb $0x0, 0x1c(%rsp)
je 0x7a18
cltq
movq 0x1f8(%rbx), %rcx
movsd %xmm1, (%rcx,%rax,8)
movsd 0x1e8(%rbx), %xmm1
xorpd 0x1f610(%rip), %xmm1    # 0x27030
movsd 0x230(%rbx), %xmm0
callq 0x14ce5
movsd %xmm0, 0x230(%rbx)
cmpb $0x0, 0x10(%rsp)
je 0x7a6c
movsd 0x218(%rbx), %xmm0
movsd 0x220(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x220(%rbx)
mulsd %xmm0, %xmm0
addsd 0x228(%rbx), %xmm0
movsd %xmm0, 0x228(%rbx)
cmpb $0x0, 0x24(%rsp)
je 0x7b23
movl 0x1c(%rbx), %esi
movq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0x3570
movq %rax, %rdi
movq %r15, %rsi
callq 0x3350
movq %rax, %rdi
movl (%rsp), %esi
callq 0x3570
movq %rax, %rdi
movq %r15, %rsi
callq 0x3350
movl %r13d, %esi
subl 0xa4(%rbx), %esi
incl %esi
movq %rax, %rdi
callq 0x3570
movq %rax, %rdi
movq %r15, %rsi
callq 0x3350
movsd 0x1e8(%rbx), %xmm0
movq %rax, %rdi
callq 0x34d0
movq %rax, %rdi
movq %r15, %rsi
callq 0x3350
movsd 0x218(%rbx), %xmm0
movq %rax, %rdi
callq 0x34d0
movq %rax, %rdi
movq %r15, %rsi
callq 0x3350
movsd 0x88(%rbx), %xmm0
movq %rax, %rdi
callq 0x34d0
movq %rax, %rdi
leaq 0x1fda5(%rip), %rsi     # 0x278bb
callq 0x3350
movq %r12, %rdi
callq 0x3230
cmpb $0x0, 0x70(%rsp)
je 0x7bd5
movl 0x1c(%rbx), %esi
movq %rbp, %rdi
callq 0x3570
movq %rax, %rdi
movq %r15, %rsi
callq 0x3350
movq %rax, %rdi
movl (%rsp), %esi
callq 0x3570
movq %rax, %rdi
movq %r15, %rsi
callq 0x3350
incl %r13d
movq %r13, 0x30(%rsp)
movl %r13d, %esi
subl 0xa4(%rbx), %esi
movq %rax, %rdi
callq 0x3570
orl $-0x1, 0x108(%rbx)
xorl %r13d, %r13d
movslq 0x20(%rbx), %rax
cmpq %rax, %r13
jge 0x7bdd
xorl %r12d, %r12d
cmpl 0x24(%rbx), %r12d
jge 0x7bd0
xorl %r14d, %r14d
movq 0x40(%rbx), %rax
cmpl (%rax,%r13,4), %r14d
jge 0x7bcb
incl 0x108(%rbx)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x3350
movslq 0x108(%rbx), %rcx
movq 0xc0(%rbx), %rdx
movl (%rdx,%rcx,4), %esi
movq %rax, %rdi
callq 0x3570
incl %r14d
jmp 0x7b92
incl %r12d
jmp 0x7b89
incq %r13
jmp 0x7b7d
incl %r13d
jmp 0x78fe
movq %rbp, %rdi
leaq 0x1fcd4(%rip), %rsi     # 0x278bb
callq 0x3350
movq %rbp, %rdi
callq 0x3230
movl 0x20(%rsp), %r14d
movq 0x30(%rsp), %r13
jmp 0x78fe
testb %r14b, %r14b
je 0x7e81
orl $-0x1, 0x108(%rbx)
pushq $-0x1
popq %rcx
xorl %r15d, %r15d
movslq 0x20(%rbx), %rax
cmpq %rax, %r15
jge 0x7cc8
xorl %ebp, %ebp
cmpl 0x24(%rbx), %ebp
jge 0x7cc0
xorl %r12d, %r12d
movq 0x40(%rbx), %rax
cmpl (%rax,%r15,4), %r12d
jge 0x7cb9
incl %ecx
movl %ecx, 0x108(%rbx)
xorl %r13d, %r13d
movslq 0x1c(%rbx), %rax
cmpq %rax, %r13
jge 0x7cb1
movslq %ecx, %rax
movq 0x2e0(%rbx), %rcx
imulq $0x18, %rax, %rax
movq (%rcx,%rax), %rax
movsd (%rax,%r13,8), %xmm0
movsd %xmm0, 0x10(%rsp)
cvtsi2sdl 0xa8(%rbx), %xmm0
callq 0x32d0
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x35c0
movslq 0x108(%rbx), %rcx
movq 0x2f8(%rbx), %rax
imulq $0x18, %rcx, %rdx
movq (%rax,%rdx), %rax
movsd %xmm0, (%rax,%r13,8)
incq %r13
jmp 0x7c49
incl %r12d
jmp 0x7c34
incl %ebp
jmp 0x7c28
incq %r15
jmp 0x7c19
orl $-0x1, 0x108(%rbx)
movl 0x24(%rbx), %ecx
xorl %edx, %edx
testl %ecx, %ecx
movl $0x0, %esi
cmovgl %ecx, %esi
movl 0x1c(%rbx), %edi
testl %edi, %edi
cmovlel %edx, %edi
testl %eax, %eax
cmovlel %edx, %eax
pushq $-0x1
popq %r8
cmpq %rax, %rdx
je 0x7db3
movq 0x40(%rbx), %r9
xorl %r10d, %r10d
movl %r8d, %r11d
cmpl %esi, %r10d
je 0x7d71
movl 0x1c(%rbx), %r15d
xorl %ebp, %ebp
testl %r15d, %r15d
cmovlel %ebp, %r15d
movslq %r11d, %r11
cmpl (%r9,%rdx,4), %ebp
jge 0x7d6c
incq %r11
movl %r11d, 0x108(%rbx)
imulq $0x18, %r11, %r8
addq 0x2f8(%rbx), %r8
imulq $0x18, %rdx, %r14
addq 0x310(%rbx), %r14
xorl %r12d, %r12d
cmpq %r12, %r15
je 0x7d65
movq (%r8), %r13
movsd (%r13,%r12,8), %xmm0
movq (%r14), %r13
addsd (%r13,%r12,8), %xmm0
movsd %xmm0, (%r13,%r12,8)
incq %r12
jmp 0x7d40
incl %ebp
movl %r11d, %r8d
jmp 0x7d17
incl %r10d
jmp 0x7d02
movq 0x40(%rbx), %r9
imulq $0x18, %rdx, %r10
addq 0x310(%rbx), %r10
xorl %r11d, %r11d
cmpq %r11, %rdi
je 0x7dab
movl (%r9,%rdx,4), %ebp
imull %ecx, %ebp
cvtsi2sd %ebp, %xmm0
movq (%r10), %r14
movsd (%r14,%r11,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r14,%r11,8)
incq %r11
jmp 0x7d83
incq %rdx
jmp 0x7cef
cmpb $0x1, 0xa0(%rbx)
jne 0x7e81
movl 0x1c(%rbx), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
cmpq %rax, %rdx
je 0x7e20
imulq $0x18, %rdx, %rsi
addq 0x310(%rbx), %rsi
movq 0x8(%rsp), %rdi
movq 0x2cb8(%rdi), %rdi
movq 0x328(%rbx), %r8
xorl %r9d, %r9d
cmpq %r9, %rcx
je 0x7e1b
movq (%rsi), %r10
movsd (%r10,%r9,8), %xmm0
movslq (%rdi,%rdx,4), %r10
imulq $0x18, %r10, %r10
movq (%r8,%r10), %r10
addsd (%r10,%r9,8), %xmm0
movsd %xmm0, (%r10,%r9,8)
incq %r9
jmp 0x7df0
incq %rdx
jmp 0x7dca
movq 0x60(%rbx), %rax
subq 0x58(%rbx), %rax
shrq $0x5, %rax
movl 0x1c(%rbx), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
testl %eax, %eax
cmovlel %edx, %eax
cmpq %rax, %rdx
je 0x7e81
movq 0x8(%rsp), %rsi
movq 0x2ca0(%rsi), %rsi
imulq $0x18, %rdx, %rdi
addq 0x328(%rbx), %rdi
xorl %r8d, %r8d
cmpq %r8, %rcx
je 0x7e7c
cvtsi2sdl (%rsi,%rdx,4), %xmm0
movq (%rdi), %r9
movsd (%r9,%r8,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r9,%r8,8)
incq %r8
jmp 0x7e5a
incq %rdx
jmp 0x7e3b
cvtsi2sdl 0xa8(%rbx), %xmm0
callq 0x32d0
subsd 0x230(%rbx), %xmm0
movsd %xmm0, 0x230(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop