// File: /workspace/github_cpp_makefile_star2_O1/bobverity[P]MavericK/MCMCobject_admixture.cpp
// Function: MCMCobject_admixture::group_update_indLevel()
void MCMCobject_admixture::group_update_indLevel() {
    
    double logLike_old;
    double logLike_new;
    double propose_logProb_old;
    double propose_logProb_new;
    double MH_diff;
    double rand1;
    int thisGroup;
    
    // loop over all individuals
    groupIndex=-1;
    for (int ind=0; ind<n; ind++) {
        
        // subtract all gene copies in this individual and calculate likelihood of current grouping at the same time
        logLike_old = 0;
        propose_logProb_old = 0;
        for (int l=0; l<loci; l++) {
            for (int p=0; p<ploidy_vec[ind]; p++) {
                thisData = data[ind][l][p];
                thisGroup = group[ind][l][p];
                
                // subtract this gene copy from allele counts and admix counts
                if (thisData!=0) {   // if not missing data
                    alleleCounts[thisGroup-1][l][thisData-1]--;
                    alleleCountsTotals[thisGroup-1][l]--;
                    
                    admixCounts[ind][thisGroup-1]--;
                    admixCountsTotals[ind]--;
                }
                
                // calculate probability of this gene copy from all demes
                probVecSum = 0;
                for (int k=0; k<K; k++) {
                    if (thisData==0) {
                        logProbVec[k] = 0;
                    } else {
                        thisAlleleCounts = alleleCounts[k][l][thisData-1];
                        thisAlleleCountsTotals = alleleCountsTotals[k][l];
                        if ((thisAlleleCounts<int(1e4)) && (thisAlleleCountsTotals<int(1e4))) {
                            logProbVec[k] = beta*log_lookup_0[thisAlleleCounts] - beta*log_lookup[thisAlleleCountsTotals][J[l]-1];
                        } else {
                            logProbVec[k] = beta*log((thisAlleleCounts + lambda)/double(thisAlleleCountsTotals + J[l]*lambda));
                        }
                    }
                    probVec[k] = double(admixCounts[ind][k]+alpha)*exp(logProbVec[k]);
                    probVecSum += probVec[k];
                }
                
                // calculate probability of chosen grouping
                propose_logProb_old += log(probVec[thisGroup-1]/probVecSum);
                logLike_old += log(probVec[thisGroup-1]);
                
            }
        }
        
        // propose a new grouping and calculate likelihood
        logLike_new = 0;
        propose_logProb_new = 0;
        for (int l=0; l<loci; l++) {
            for (int p=0; p<ploidy_vec[ind]; p++) {
                thisData = data[ind][l][p];
                
                // calculate probability of this gene copy from all demes
                probVecSum = 0;
                for (int k=0; k<K; k++) {
                    if (thisData==0) {
                        logProbVec[k] = 0;
                    } else {
                        thisAlleleCounts = alleleCounts[k][l][thisData-1];
                        thisAlleleCountsTotals = alleleCountsTotals[k][l];
                        if ((thisAlleleCounts<int(1e4)) && (thisAlleleCountsTotals<int(1e4))) {
                            logProbVec[k] = beta*log_lookup_0[thisAlleleCounts] - beta*log_lookup[thisAlleleCountsTotals][J[l]-1];
                        } else {
                            logProbVec[k] = beta*log((thisAlleleCounts + lambda)/double(thisAlleleCountsTotals + J[l]*lambda));
                        }
                    }
                    probVec[k] = double(admixCounts[ind][k]+alpha)*exp(logProbVec[k]);
                    probVecSum += probVec[k];
                }
                
                // resample grouping
                group_propose[ind][l][p] = sample1(probVec, probVecSum);
                
                // calculate probability of new grouping
                propose_logProb_new += log(probVec[group_propose[ind][l][p]-1]/probVecSum);
                logLike_new += log(probVec[group_propose[ind][l][p]-1]);
                
                // add this gene copy to allele counts and admix counts
                if (thisData!=0) {   // if not missing data
                    thisGroup = group_propose[ind][l][p];
                    alleleCounts[thisGroup-1][l][thisData-1]++;
                    alleleCountsTotals[thisGroup-1][l]++;
                    
                    admixCounts[ind][thisGroup-1]++;
                    admixCountsTotals[ind]++;
                }
            }
        }
        
        // Metropolis-Hastings step. If accept then stick with new grouping, otherwise revert back
        MH_diff = (logLike_new - propose_logProb_new) - (logLike_old - propose_logProb_old);
        rand1 = runif1(0,1);
        if (log(rand1)<MH_diff) {
            
            // accept move
            for (int l=0; l<loci; l++) {
                for (int p=0; p<ploidy_vec[ind]; p++) {
                    groupIndex++;
                    linearGroup[groupIndex] = group_propose[ind][l][p];
                    group[ind][l][p] = group_propose[ind][l][p];
                }
            }
            
        } else {
            
            // reject move
            for (int l=0; l<loci; l++) {
                for (int p=0; p<ploidy_vec[ind]; p++) {
                    groupIndex++;
                    thisData = data[ind][l][p];
                    if (thisData!=0) {   // if not missing data
                        
                        // subtract new group
                        thisGroup = group_propose[ind][l][p];
                        alleleCounts[thisGroup-1][l][thisData-1]--;
                        alleleCountsTotals[thisGroup-1][l]--;
                        
                        admixCounts[ind][thisGroup-1]--;
                        admixCountsTotals[ind]--;
                        
                        // reinstate old group
                        thisGroup = group[ind][l][p];
                        alleleCounts[thisGroup-1][l][thisData-1]++;
                        alleleCountsTotals[thisGroup-1][l]++;
                        
                        admixCounts[ind][thisGroup-1]++;
                        admixCountsTotals[ind]++;
                    }
                    
                }
            }
            
        }
        
    }   // end loop over individuals
    
}

// Assembly:
movl $0xffffffff, 0x108(%rdi) # imm = 0xFFFFFFFF
cmpl $0x0, 0x20(%rdi)
jle 0xa36d
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
leaq 0x260(%rdi), %rax
movq %rax, 0x38(%rsp)
xorl %r12d, %r12d
cmpl $0x0, 0x24(%rbx)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r12, 0x30(%rsp)
jle 0x9e2d
leaq (,%r12,8), %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x40(%rsp)
leaq (%r12,%r12,2), %r14
xorl %ebp, %ebp
xorpd %xmm1, %xmm1
movsd %xmm1, 0x10(%rsp)
movq 0x40(%rbx), %rax
cmpl $0x0, (%rax,%r12,4)
jle 0x9e1d
leaq (,%rbp,8), %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x18(%rsp)
leaq (,%rbp,2), %r13
addq %rbp, %r13
xorl %edi, %edi
movq (%rbx), %rax
movq 0xd8(%rbx), %rcx
movq 0x40(%rsp), %rdx
movq (%rax,%rdx), %rax
movq 0x18(%rsp), %rsi
movq (%rax,%rsi), %rax
movslq (%rax,%rdi,4), %rax
movl %eax, 0xb0(%rbx)
movq (%rcx,%rdx), %rcx
movq (%rcx,%rsi), %rcx
movslq (%rcx,%rdi,4), %rsi
testq %rax, %rax
je 0x9c2f
movq 0x110(%rbx), %rcx
leaq -0x8(,%rsi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%rcx,%rdx), %rcx
movq (%rcx,%r13,8), %rcx
decl -0x4(%rcx,%rax,4)
movq 0x128(%rbx), %rax
movq (%rax,%rdx), %rax
decl (%rax,%rbp,4)
movq 0x188(%rbx), %rax
movq (%rax,%r14,8), %rax
decl -0x4(%rax,%rsi,4)
movq 0x1a0(%rbx), %rax
decl (%rax,%r12,4)
movq %rsi, 0x20(%rsp)
movq %rdi, 0x8(%rsp)
movsd %xmm0, 0x28(%rsp)
movq $0x0, 0x278(%rbx)
cmpl $0x0, 0x1c(%rbx)
jle 0x9dae
xorl %r15d, %r15d
xorl %r12d, %r12d
movslq 0xb0(%rbx), %rax
testq %rax, %rax
je 0x9cec
movq 0x110(%rbx), %rcx
movq 0x128(%rbx), %rdx
movq (%rcx,%r15), %rcx
movq (%rcx,%r13,8), %rcx
movslq -0x4(%rcx,%rax,4), %rax
movl %eax, 0xb4(%rbx)
movq (%rdx,%r15), %rcx
movl (%rcx,%rbp,4), %ecx
movl %ecx, 0xb8(%rbx)
cmpq $0x270f, %rax           # imm = 0x270F
jg 0x9cf2
cmpl $0x270f, %ecx           # imm = 0x270F
jg 0x9cf2
movslq %ecx, %rcx
movsd 0x98(%rbx), %xmm0
leaq 0x3869e(%rip), %rdx     # 0x42358
movq (%rdx), %rdx
leaq 0x3867c(%rip), %rsi     # 0x42340
movq (%rsi), %rsi
leaq (%rcx,%rcx,2), %rcx
movq 0x28(%rbx), %rdi
movslq (%rdi,%rbp,4), %rdi
movq (%rsi,%rcx,8), %rcx
movsd -0x8(%rcx,%rdi,8), %xmm1
mulsd %xmm0, %xmm1
mulsd (%rdx,%rax,8), %xmm0
subsd %xmm1, %xmm0
jmp 0x9d38
xorpd %xmm0, %xmm0
jmp 0x9d38
movsd 0x98(%rbx), %xmm0
movsd %xmm0, (%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movsd 0x78(%rbx), %xmm2
movq 0x28(%rbx), %rax
xorps %xmm3, %xmm3
cvtsi2sdl (%rax,%rbp,4), %xmm3
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
divsd %xmm3, %xmm0
callq 0x3290
mulsd (%rsp), %xmm0
movq 0x238(%rbx), %rax
movsd %xmm0, (%rax,%r12,8)
movq 0x188(%rbx), %rax
movq (%rax,%r14,8), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%r12,4), %xmm0
movq 0x238(%rbx), %rax
addsd 0x88(%rbx), %xmm0
movsd %xmm0, (%rsp)
movsd (%rax,%r12,8), %xmm0
callq 0x35a0
mulsd (%rsp), %xmm0
movq 0x260(%rbx), %rax
movsd %xmm0, (%rax,%r12,8)
addsd 0x278(%rbx), %xmm0
movsd %xmm0, 0x278(%rbx)
incq %r12
movslq 0x1c(%rbx), %rax
addq $0x18, %r15
cmpq %rax, %r12
jl 0x9c5a
movq 0x260(%rbx), %rax
movq 0x20(%rsp), %r15
movsd -0x8(%rax,%r15,8), %xmm0
divsd 0x278(%rbx), %xmm0
callq 0x3290
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq 0x260(%rbx), %rax
movsd -0x8(%rax,%r15,8), %xmm0
callq 0x3290
movsd 0x28(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movq 0x8(%rsp), %rdi
incq %rdi
movq 0x40(%rbx), %rax
movq 0x30(%rsp), %r12
movslq (%rax,%r12,4), %rax
cmpq %rax, %rdi
jl 0x9bb1
incq %rbp
movslq 0x24(%rbx), %rax
cmpq %rax, %rbp
jl 0x9b84
cmpl $0x0, 0x24(%rbx)
jle 0xa12f
movsd %xmm0, 0x28(%rsp)
xorpd %xmm1, %xmm1
xorl %ebp, %ebp
xorpd %xmm2, %xmm2
movq 0x40(%rbx), %rax
cmpl $0x0, (%rax,%r12,4)
jle 0xa113
xorl %r13d, %r13d
movsd %xmm2, 0x20(%rsp)
movsd %xmm1, 0x8(%rsp)
movq (%rbx), %rax
leaq (%r12,%r12,2), %r15
movq (%rax,%r15,8), %rax
leaq (%rbp,%rbp,2), %r12
movq (%rax,%r12,8), %rax
movq %r13, 0x18(%rsp)
movl (%rax,%r13,4), %eax
movl %eax, 0xb0(%rbx)
movq $0x0, 0x278(%rbx)
cmpl $0x0, 0x1c(%rbx)
jle 0x9ff7
xorl %r13d, %r13d
xorl %r14d, %r14d
movslq 0xb0(%rbx), %rax
testq %rax, %rax
je 0x9f35
movq 0x110(%rbx), %rcx
movq 0x128(%rbx), %rdx
movq (%rcx,%r13), %rcx
movq (%rcx,%r12,8), %rcx
movslq -0x4(%rcx,%rax,4), %rax
movl %eax, 0xb4(%rbx)
movq (%rdx,%r13), %rcx
movl (%rcx,%rbp,4), %ecx
movl %ecx, 0xb8(%rbx)
cmpq $0x270f, %rax           # imm = 0x270F
jg 0x9f3b
cmpl $0x270f, %ecx           # imm = 0x270F
jg 0x9f3b
movslq %ecx, %rcx
movsd 0x98(%rbx), %xmm0
leaq 0x38455(%rip), %rdx     # 0x42358
movq (%rdx), %rdx
leaq 0x38433(%rip), %rsi     # 0x42340
movq (%rsi), %rsi
leaq (%rcx,%rcx,2), %rcx
movq 0x28(%rbx), %rdi
movslq (%rdi,%rbp,4), %rdi
movq (%rsi,%rcx,8), %rcx
movsd -0x8(%rcx,%rdi,8), %xmm1
mulsd %xmm0, %xmm1
mulsd (%rdx,%rax,8), %xmm0
subsd %xmm1, %xmm0
jmp 0x9f81
xorpd %xmm0, %xmm0
jmp 0x9f81
movsd 0x98(%rbx), %xmm0
movsd %xmm0, (%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movsd 0x78(%rbx), %xmm2
movq 0x28(%rbx), %rax
xorps %xmm3, %xmm3
cvtsi2sdl (%rax,%rbp,4), %xmm3
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
divsd %xmm3, %xmm0
callq 0x3290
mulsd (%rsp), %xmm0
movq 0x238(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
movq 0x188(%rbx), %rax
movq (%rax,%r15,8), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%r14,4), %xmm0
movq 0x238(%rbx), %rax
addsd 0x88(%rbx), %xmm0
movsd %xmm0, (%rsp)
movsd (%rax,%r14,8), %xmm0
callq 0x35a0
mulsd (%rsp), %xmm0
movq 0x260(%rbx), %rax
movsd %xmm0, (%rax,%r14,8)
addsd 0x278(%rbx), %xmm0
movsd %xmm0, 0x278(%rbx)
incq %r14
movslq 0x1c(%rbx), %rax
addq $0x18, %r13
cmpq %rax, %r14
jl 0x9ea3
movsd 0x278(%rbx), %xmm0
movq 0x38(%rsp), %rdi
callq 0x1d629
movq 0xf0(%rbx), %rcx
movq 0x30(%rsp), %r12
leaq (,%r12,8), %rdx
leaq (%rdx,%rdx,2), %r14
movq (%rcx,%r14), %rcx
leaq (,%rbp,8), %rdx
leaq (%rdx,%rdx,2), %r15
movq (%rcx,%r15), %rcx
movq 0x18(%rsp), %r13
movl %eax, (%rcx,%r13,4)
movq 0x260(%rbx), %rcx
cltq
movsd -0x8(%rcx,%rax,8), %xmm0
divsd 0x278(%rbx), %xmm0
callq 0x3290
movsd %xmm0, (%rsp)
movq 0xf0(%rbx), %rax
movq 0x260(%rbx), %rcx
movq (%rax,%r14), %rax
movq (%rax,%r15), %rax
movslq (%rax,%r13,4), %rax
movsd -0x8(%rcx,%rax,8), %xmm0
callq 0x3290
movslq 0xb0(%rbx), %rax
testq %rax, %rax
je 0xa0ea
movq 0xf0(%rbx), %rcx
movq 0x110(%rbx), %rdx
movq (%rcx,%r14), %rcx
movq (%rcx,%r15), %rcx
movslq (%rcx,%r13,4), %rcx
leaq -0x8(,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
movq (%rdx,%rsi), %rdx
movq (%rdx,%r15), %rdx
incl -0x4(%rdx,%rax,4)
movq 0x128(%rbx), %rax
movq (%rax,%rsi), %rax
incl (%rax,%rbp,4)
movq 0x188(%rbx), %rax
movq (%rax,%r14), %rax
incl -0x4(%rax,%rcx,4)
movq 0x1a0(%rbx), %rax
incl (%rax,%r12,4)
movsd 0x20(%rsp), %xmm2
addsd (%rsp), %xmm2
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
incq %r13
movq 0x40(%rbx), %rax
movslq (%rax,%r12,4), %rax
cmpq %rax, %r13
jl 0x9e59
incq %rbp
movslq 0x24(%rbx), %rax
cmpq %rax, %rbp
jl 0x9e47
subsd %xmm2, %xmm1
movsd 0x28(%rsp), %xmm0
jmp 0xa133
xorpd %xmm1, %xmm1
subsd 0x10(%rsp), %xmm0
subsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
xorpd %xmm0, %xmm0
movsd 0x2d111(%rip), %xmm1    # 0x37260
callq 0x1d4cc
callq 0x3290
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movl 0x24(%rbx), %eax
jbe 0xa202
testl %eax, %eax
jle 0xa34f
movq 0x40(%rbx), %rax
movq 0xc0(%rbx), %rcx
leaq (,%r12,8), %rdx
leaq (%rdx,%rdx,2), %rdx
movq 0xf0(%rbx), %rsi
addq %rdx, %rsi
addq 0xd8(%rbx), %rdx
xorl %edi, %edi
cmpl $0x0, (%rax,%r12,4)
jle 0xa1f1
movq (%rsi), %r8
leaq (,%rdi,8), %r9
leaq (%r9,%r9,2), %r9
movq (%r8,%r9), %r8
movq (%rdx), %r10
movq (%r10,%r9), %r9
xorl %r10d, %r10d
movslq 0x108(%rbx), %r11
leaq 0x1(%r11), %r14
movl %r14d, 0x108(%rbx)
movl (%r8,%r10,4), %ebp
movl %ebp, 0x4(%rcx,%r11,4)
movl (%r8,%r10,4), %r11d
movl %r11d, (%r9,%r10,4)
incq %r10
movslq (%rax,%r12,4), %r11
cmpq %r11, %r10
jl 0xa1c2
incq %rdi
movslq 0x24(%rbx), %r8
cmpq %r8, %rdi
jl 0xa19e
jmp 0xa34f
testl %eax, %eax
jle 0xa34f
movq 0x40(%rbx), %rax
leaq (%r12,%r12,2), %rcx
shlq $0x3, %rcx
addq (%rbx), %rcx
movq %rcx, 0x8(%rsp)
xorl %edx, %edx
cmpl $0x0, (%rax,%r12,4)
jle 0xa33f
movq 0x8(%rsp), %rcx
movq (%rcx), %rsi
leaq (%rdx,%rdx,2), %rdi
movq (%rsi,%rdi,8), %rsi
leaq (,%r12,8), %rdi
leaq (%rdi,%rdi,2), %rdi
movq 0xf0(%rbx), %rcx
addq %rdi, %rcx
movq %rcx, (%rsp)
movq 0x110(%rbx), %r9
movq 0x128(%rbx), %r10
movq 0x188(%rbx), %rcx
addq %rdi, %rcx
movq %rcx, 0x10(%rsp)
movq 0x1a0(%rbx), %r14
addq 0xd8(%rbx), %rdi
xorl %r15d, %r15d
incl 0x108(%rbx)
movq %r12, %r11
movslq (%rsi,%r15,4), %r12
movl %r12d, 0xb0(%rbx)
testq %r12, %r12
je 0xa32c
movq (%rsp), %rcx
movq (%rcx), %r13
leaq (,%rdx,8), %rbp
leaq (%rbp,%rbp,2), %rbp
movq (%r13,%rbp), %r13
movslq (%r13,%r15,4), %r13
leaq -0x8(,%r13,8), %rcx
leaq (%rcx,%rcx,2), %rcx
movq (%r9,%rcx), %r8
movq (%r8,%rbp), %r8
decl -0x4(%r8,%r12,4)
movq (%r10,%rcx), %rcx
decl (%rcx,%rdx,4)
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
decl -0x4(%rcx,%r13,4)
decl (%r14,%r11,4)
movq (%rdi), %r8
movq (%r8,%rbp), %r8
movslq (%r8,%r15,4), %r8
leaq -0x8(,%r8,8), %r12
leaq (%r12,%r12,2), %r12
movq (%r9,%r12), %r13
movq (%r13,%rbp), %r13
movslq 0xb0(%rbx), %rbp
incl -0x4(%r13,%rbp,4)
movq (%r10,%r12), %r12
incl (%r12,%rdx,4)
incl -0x4(%rcx,%r8,4)
incl (%r14,%r11,4)
incq %r15
movslq (%rax,%r11,4), %rcx
cmpq %rcx, %r15
movq %r11, %r12
jl 0xa283
incq %rdx
movslq 0x24(%rbx), %rsi
cmpq %rsi, %rdx
jl 0xa220
incq %r12
movslq 0x20(%rbx), %rax
cmpq %rax, %r12
jl 0x9b4a
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq