// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movl %r9d, 0x34(%rsp)
movl %r8d, 0xc(%rsp)
movl %ecx, 0x30(%rsp)
movl %edx, 0x10(%rsp)
movq %rsi, %r12
movq %rdi, %rbx
movl 0xa4(%rdi), %eax
movl 0xa8(%rdi), %ecx
addl %eax, %ecx
movq %rsi, 0x28(%rsp)
jle 0x911c
movl 0x88(%rsp), %ecx
leaq 0xb70(%r12), %rdx
movq %rdx, 0x38(%rsp)
incl %ecx
movl %ecx, 0x1c(%rsp)
leaq 0x1f70(%r12), %r15
movl $0x1, 0x20(%rsp)
xorl %r13d, %r13d
leaq 0x2d4d5(%rip), %rbp     # 0x36290
movl 0x20(%rsp), %ecx
movl %ecx, %r14d
testl %ecx, %ecx
jle 0x8df4
movq %rbx, %rdi
callq 0x9422
movq %rbx, %rdi
callq 0x97b2
cmpb $0x0, 0x2bd1(%r12)
jne 0x8de9
movq %rbx, %rdi
callq 0xa00c
decl %r14d
jne 0x8dc6
movl 0xa4(%rbx), %eax
movq %r13, %rcx
cmpl %eax, %ecx
jne 0x8e05
movl 0xac(%rbx), %eax
movl %eax, 0x20(%rsp)
cmpb $0x0, 0xc(%rsp)
je 0x8e38
movq %rbx, %rdi
callq 0xa2ca
movq %rbx, %rdi
movq %r12, %rsi
callq 0xa4fa
movq %rbx, %rdi
callq 0xa8be
cmpl 0xa4(%rbx), %r13d
jl 0x8e38
movq %rbx, %rdi
callq 0xa950
movq %rbx, %rdi
callq 0xaa3a
cmpb $0x0, 0x10(%rsp)
je 0x8e57
movq %rbx, %rdi
callq 0xab72
movq %rbx, %rdi
callq 0xad58
movq %r13, %rax
subl 0xa4(%rbx), %eax
jl 0x8f0b
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, 0x30(%rsp)
je 0x8eb7
cltq
movq 0x1f8(%rbx), %rcx
movsd %xmm1, (%rcx,%rax,8)
movsd 0x1e8(%rbx), %xmm1
xorpd 0x2d181(%rip), %xmm1    # 0x36040
movsd 0x230(%rbx), %xmm0
callq 0x1a75d
movsd %xmm0, 0x230(%rbx)
cmpb $0x0, 0x10(%rsp)
je 0x8f0b
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, 0x34(%rsp)
je 0x8ff5
movl 0x1c(%rbx), %esi
movq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x3550
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3390
movq %r12, %rdi
movl 0x1c(%rsp), %esi
callq 0x3550
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3390
movl %r13d, %esi
subl 0xa4(%rbx), %esi
incl %esi
movq %r12, %rdi
callq 0x3550
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3390
movsd 0x1e8(%rbx), %xmm0
movq %r12, %rdi
callq 0x34a0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3390
movsd 0x218(%rbx), %xmm0
movq %r12, %rdi
callq 0x34a0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3390
movsd 0x88(%rbx), %xmm0
movq %r12, %rdi
movq 0x28(%rsp), %r12
callq 0x34a0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2d93a(%rip), %rsi     # 0x36922
callq 0x3390
movq %r14, %rdi
callq 0x31f0
cmpb $0x0, 0x80(%rsp)
je 0x9102
movl 0x1c(%rbx), %esi
movq %r15, %rdi
callq 0x3550
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3390
movq %r12, %rdi
movl 0x1c(%rsp), %esi
callq 0x3550
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3390
incl %r13d
movq %r13, 0x40(%rsp)
movl %r13d, %esi
subl 0xa4(%rbx), %esi
movq %r12, %rdi
callq 0x3550
movl $0xffffffff, 0x108(%rbx) # imm = 0xFFFFFFFF
movl 0x20(%rbx), %ecx
testl %ecx, %ecx
jle 0x90da
movl 0x24(%rbx), %eax
xorl %r12d, %r12d
testl %eax, %eax
jle 0x90cf
movq 0x40(%rbx), %rcx
xorl %r13d, %r13d
cmpl $0x0, (%rcx,%r12,4)
jle 0x90c4
xorl %r14d, %r14d
incl 0x108(%rbx)
movl $0x1, %edx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x3390
movslq 0x108(%rbx), %rax
movq 0xc0(%rbx), %rcx
movl (%rcx,%rax,4), %esi
movq %r15, %rdi
callq 0x3550
incl %r14d
movq 0x40(%rbx), %rcx
cmpl (%rcx,%r12,4), %r14d
jl 0x9085
movl 0x24(%rbx), %eax
incl %r13d
cmpl %eax, %r13d
jl 0x907b
movl 0x20(%rbx), %ecx
incq %r12
movslq %ecx, %rdx
cmpq %rdx, %r12
jl 0x9070
movl $0x1, %edx
movq %r15, %rdi
leaq 0x2d839(%rip), %rsi     # 0x36922
callq 0x3390
movq %r15, %rdi
callq 0x31f0
movq 0x28(%rsp), %r12
movq 0x40(%rsp), %r13
jmp 0x9105
incl %r13d
movl 0xa4(%rbx), %eax
movl 0xa8(%rbx), %ecx
addl %eax, %ecx
cmpl %ecx, %r13d
jl 0x8dbb
cmpb $0x0, 0xc(%rsp)
je 0x93f3
movl $0xffffffff, 0x108(%rbx) # imm = 0xFFFFFFFF
movl 0x20(%rbx), %esi
testl %esi, %esi
jle 0x938a
movl 0x24(%rbx), %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r15d, %r15d
testl %edx, %edx
jle 0x9204
movq 0x40(%rbx), %rax
xorl %ebp, %ebp
cmpl $0x0, (%rax,%r15,4)
jle 0x91f7
movl 0x1c(%rbx), %edx
xorl %r12d, %r12d
incl %ecx
movl %ecx, 0x108(%rbx)
testl %edx, %edx
jle 0x91e2
xorl %r13d, %r13d
movslq %ecx, %rax
movq 0x2e0(%rbx), %rcx
leaq (%rax,%rax,2), %rax
movq (%rcx,%rax,8), %rax
movsd (%rax,%r13,8), %xmm0
movsd %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0xa8(%rbx), %xmm0
callq 0x3290
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x35a0
movslq 0x108(%rbx), %rcx
movq 0x2f8(%rbx), %rax
leaq (%rcx,%rcx,2), %rdx
movq (%rax,%rdx,8), %rax
movsd %xmm0, (%rax,%r13,8)
incq %r13
movslq 0x1c(%rbx), %rdx
cmpq %rdx, %r13
jl 0x9175
movq 0x40(%rbx), %rax
incl %r12d
cmpl (%rax,%r15,4), %r12d
jl 0x9166
movl 0x24(%rbx), %edx
movq 0x28(%rsp), %r12
incl %ebp
cmpl %edx, %ebp
jl 0x9155
movl 0x20(%rbx), %esi
incq %r15
movslq %esi, %rax
cmpq %rax, %r15
jl 0x9147
movl $0xffffffff, 0x108(%rbx) # imm = 0xFFFFFFFF
movl %esi, 0xc(%rsp)
testl %esi, %esi
jle 0x9318
movl 0x1c(%rbx), %ecx
movl 0x24(%rbx), %edx
movq 0x40(%rbx), %rsi
movq 0x310(%rbx), %rax
movq %rax, 0x20(%rsp)
movl 0xc(%rsp), %eax
movq %rax, 0x10(%rsp)
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %r10d, %r10d
testl %edx, %edx
jle 0x92ce
movl 0x1c(%rbx), %r11d
movl (%rsi,%r10,4), %r8d
xorl %r15d, %r15d
leaq (%r10,%r10,2), %r12
shlq $0x3, %r12
testl %r8d, %r8d
jle 0x92c6
movq 0x2f8(%rbx), %r13
movq 0x310(%rbx), %rbp
addq %r12, %rbp
movslq %r9d, %r9
xorl %edi, %edi
incq %r9
movl %r9d, 0x108(%rbx)
testl %r11d, %r11d
jle 0x92bb
leaq (%r9,%r9,2), %rax
movq (%r13,%rax,8), %r8
movq (%rbp), %r14
xorl %eax, %eax
movsd (%r8,%rax,8), %xmm0
addsd (%r14,%rax,8), %xmm0
movsd %xmm0, (%r14,%rax,8)
incq %rax
cmpq %rax, %r11
jne 0x92a1
incl %edi
movl (%rsi,%r10,4), %r8d
cmpl %r8d, %edi
jl 0x9283
incl %r15d
cmpl %edx, %r15d
jne 0x9268
testl %ecx, %ecx
movq 0x28(%rsp), %r12
jle 0x930a
movl (%rsi,%r10,4), %eax
imull %edx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
leaq (%r10,%r10,2), %rax
movq 0x20(%rsp), %rdi
movq (%rdi,%rax,8), %rdi
xorl %eax, %eax
movsd (%rdi,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rdi,%rax,8)
incq %rax
cmpq %rax, %rcx
jne 0x92f4
incq %r10
cmpq 0x10(%rsp), %r10
jne 0x9251
cmpb $0x0, 0xa0(%rbx)
movl 0xc(%rsp), %eax
je 0x93f3
testl %eax, %eax
jle 0x9393
movl 0x1c(%rbx), %ecx
movq 0x310(%rbx), %rdx
movq 0x2cb8(%r12), %rsi
movq 0x328(%rbx), %rdi
movl %eax, %eax
xorl %r8d, %r8d
testl %ecx, %ecx
jle 0x9380
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 0x9366
incq %r8
cmpq %rax, %r8
jne 0x934b
jmp 0x9393
cmpb $0x0, 0xa0(%rbx)
je 0x93f3
movq 0x60(%rbx), %rax
subq 0x58(%rbx), %rax
shrq $0x5, %rax
testl %eax, %eax
jle 0x93f3
movl 0x1c(%rbx), %ecx
movq 0x2ca0(%r12), %rdx
movq 0x328(%rbx), %rsi
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edi, %edi
testl %ecx, %ecx
jle 0x93eb
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 0x93d3
incq %rdi
cmpq %rax, %rdi
jne 0x93bc
xorps %xmm0, %xmm0
cvtsi2sdl 0xa8(%rbx), %xmm0
callq 0x3290
subsd 0x230(%rbx), %xmm0
movsd %xmm0, 0x230(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq