// File: /workspace/github_cpp_makefile_star2_O1/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, 0x3c(%rsp)
movl %r8d, 0x10(%rsp)
movl %ecx, 0x38(%rsp)
movl %edx, 0x28(%rsp)
movq %rsi, %r13
movq %rdi, %rbx
movl 0xa8(%rdi), %eax
addl 0xa4(%rdi), %eax
movq %rsi, 0x30(%rsp)
jle 0x9479
movl 0x88(%rsp), %eax
leaq 0xb70(%r13), %rcx
movq %rcx, 0x40(%rsp)
incl %eax
movl %eax, 0x1c(%rsp)
leaq 0x1f70(%r13), %r15
movl $0x1, 0x20(%rsp)
xorl %r12d, %r12d
leaq 0x2e16b(%rip), %rbp     # 0x37290
movl 0x20(%rsp), %eax
movl %eax, %r14d
testl %eax, %eax
jle 0x9157
movq %rbx, %rdi
callq 0x9772
movq %rbx, %rdi
callq 0x9b16
cmpb $0x0, 0x2bd1(%r13)
jne 0x9152
movq %rbx, %rdi
callq 0xa36e
decl %r14d
jne 0x9130
cmpl 0xa4(%rbx), %r12d
jne 0x916a
movl 0xac(%rbx), %eax
movl %eax, 0x20(%rsp)
cmpb $0x0, 0x10(%rsp)
je 0x919d
movq %rbx, %rdi
callq 0xa62a
movq %rbx, %rdi
movq %r13, %rsi
callq 0xa82c
movq %rbx, %rdi
callq 0xabf6
cmpl 0xa4(%rbx), %r12d
jl 0x919d
movq %rbx, %rdi
callq 0xac7c
movq %rbx, %rdi
callq 0xad56
cmpb $0x0, 0x28(%rsp)
je 0x91bc
movq %rbx, %rdi
callq 0xae8a
movq %rbx, %rdi
callq 0xb07a
movl %r12d, %eax
subl 0xa4(%rbx), %eax
jl 0x9270
movsd 0x1e8(%rbx), %xmm0
movsd 0x1f0(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x1f0(%rbx)
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd 0x210(%rbx), %xmm1
movsd %xmm1, 0x210(%rbx)
cmpb $0x0, 0x38(%rsp)
je 0x9214
cltq
movq 0x1f8(%rbx), %rcx
movsd %xmm0, (%rcx,%rax,8)
movsd 0x230(%rbx), %xmm0
movsd 0x1e8(%rbx), %xmm1
xorpd 0x2de14(%rip), %xmm1    # 0x37040
callq 0x1aa59
movsd %xmm0, 0x230(%rbx)
cmpb $0x0, 0x28(%rsp)
je 0x9270
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)
movq %r12, 0x8(%rsp)
cmpb $0x0, 0x3c(%rsp)
je 0x935e
movl 0x1c(%rbx), %esi
movq 0x40(%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
movq 0x8(%rsp), %rax
movl %eax, %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
callq 0x34a0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2e5d1(%rip), %rsi     # 0x37922
callq 0x3390
movq %r14, %rdi
callq 0x31f0
cmpb $0x0, 0x80(%rsp)
je 0x945c
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
movq 0x8(%rsp), %rax
movl %eax, %esi
subl 0xa4(%rbx), %esi
incl %esi
movq %r12, %rdi
callq 0x3550
movl $0xffffffff, 0x108(%rbx) # imm = 0xFFFFFFFF
cmpl $0x0, 0x20(%rbx)
jle 0x943b
xorl %r12d, %r12d
cmpl $0x0, 0x24(%rbx)
jle 0x942f
xorl %r13d, %r13d
movq 0x40(%rbx), %rax
cmpl $0x0, (%rax,%r12,4)
jle 0x9426
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), %rax
cmpl (%rax,%r12,4), %r14d
jl 0x93ea
incl %r13d
cmpl 0x24(%rbx), %r13d
jl 0x93dc
incq %r12
movslq 0x20(%rbx), %rax
cmpq %rax, %r12
jl 0x93d3
movl $0x1, %edx
movq %r15, %rdi
leaq 0x2e4d8(%rip), %rsi     # 0x37922
callq 0x3390
movq %r15, %rdi
callq 0x31f0
movq 0x30(%rsp), %r13
movq 0x8(%rsp), %r12
incl %r12d
movl 0xa8(%rbx), %eax
addl 0xa4(%rbx), %eax
cmpl %eax, %r12d
jl 0x9125
cmpb $0x0, 0x10(%rsp)
je 0x9743
movl $0xffffffff, 0x108(%rbx) # imm = 0xFFFFFFFF
movl 0x20(%rbx), %eax
testl %eax, %eax
jle 0x9567
xorl %r15d, %r15d
cmpl $0x0, 0x24(%rbx)
jle 0x9552
xorl %ebp, %ebp
movq 0x40(%rbx), %rax
cmpl $0x0, (%rax,%r15,4)
jle 0x9547
xorl %r12d, %r12d
incl 0x108(%rbx)
cmpl $0x0, 0x1c(%rbx)
jle 0x9536
xorl %r13d, %r13d
movslq 0x108(%rbx), %rax
movq 0x2e0(%rbx), %rcx
leaq (%rax,%rax,2), %rax
movq (%rcx,%rax,8), %rax
movsd (%rax,%r13,8), %xmm0
movsd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0xa8(%rbx), %xmm0
callq 0x3290
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x35a0
movslq 0x108(%rbx), %rax
movq 0x2f8(%rbx), %rcx
leaq (%rax,%rax,2), %rax
movq (%rcx,%rax,8), %rax
movsd %xmm0, (%rax,%r13,8)
incq %r13
movslq 0x1c(%rbx), %rax
cmpq %rax, %r13
jl 0x94c9
incl %r12d
movq 0x40(%rbx), %rax
cmpl (%rax,%r15,4), %r12d
jl 0x94ba
incl %ebp
cmpl 0x24(%rbx), %ebp
jl 0x94a8
incq %r15
movslq 0x20(%rbx), %rax
cmpq %rax, %r15
jl 0x949c
movq 0x30(%rsp), %r13
movl $0xffffffff, 0x108(%rbx) # imm = 0xFFFFFFFF
movq %rax, 0x10(%rsp)
testl %eax, %eax
jle 0x9674
movl 0x108(%rbx), %ecx
movl 0x1c(%rbx), %edx
movl 0x24(%rbx), %esi
movq 0x40(%rbx), %rdi
movq 0x310(%rbx), %rax
movq %rax, 0x20(%rsp)
movl 0x10(%rsp), %eax
movq %rax, 0x28(%rsp)
xorl %r10d, %r10d
leaq (%r10,%r10,2), %rax
testl %esi, %esi
movq %rax, 0x8(%rsp)
jle 0x9627
movl 0x1c(%rbx), %r15d
xorl %r12d, %r12d
leaq (,%rax,8), %r13
cmpl $0x0, (%rdi,%r10,4)
jle 0x961f
movq 0x2f8(%rbx), %rbp
movq 0x310(%rbx), %r8
addq %r13, %r8
movslq %ecx, %rcx
xorl %r9d, %r9d
incq %rcx
movl %ecx, 0x108(%rbx)
testl %r15d, %r15d
jle 0x9616
leaq (%rcx,%rcx,2), %rax
movq (%rbp,%rax,8), %r14
movq (%r8), %r11
xorl %eax, %eax
movsd (%r14,%rax,8), %xmm0
addsd (%r11,%rax,8), %xmm0
movsd %xmm0, (%r11,%rax,8)
incq %rax
cmpq %rax, %r15
jne 0x95fc
incl %r9d
cmpl (%rdi,%r10,4), %r9d
jl 0x95e0
incl %r12d
cmpl %esi, %r12d
jne 0x95c2
testl %edx, %edx
movq 0x30(%rsp), %r13
jle 0x9666
movl (%rdi,%r10,4), %eax
imull %esi, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq 0x20(%rsp), %rax
movq 0x8(%rsp), %r8
movq (%rax,%r8,8), %r8
xorl %eax, %eax
movsd (%r8,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r8,%rax,8)
incq %rax
cmpq %rax, %rdx
jne 0x964e
incq %r10
cmpq 0x28(%rsp), %r10
jne 0x95a6
cmpb $0x1, 0xa0(%rbx)
jne 0x9743
movq 0x10(%rsp), %rax
testl %eax, %eax
jle 0x96e4
movl 0x1c(%rbx), %ecx
movq 0x310(%rbx), %rdx
movq 0x2cb8(%r13), %rsi
movq 0x328(%rbx), %rdi
movl %eax, %eax
xorl %r8d, %r8d
testl %ecx, %ecx
jle 0x96dc
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 0x96c2
incq %r8
cmpq %rax, %r8
jne 0x96a7
movq 0x60(%rbx), %rax
subq 0x58(%rbx), %rax
shrq $0x5, %rax
testl %eax, %eax
jle 0x9743
movl 0x1c(%rbx), %ecx
movq 0x2ca0(%r13), %rdx
movq 0x328(%rbx), %rsi
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edi, %edi
testl %ecx, %ecx
jle 0x973b
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 0x9723
incq %rdi
cmpq %rax, %rdi
jne 0x970c
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