// File: /workspace/github_cpp_makefile_star2_O3/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 0xa00b
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)
movl 0x24(%rdi), %eax
xorl %edi, %edi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
testl %eax, %eax
movq %rdi, 0x20(%rsp)
jle 0x9dc0
movq 0x40(%rbx), %rcx
leaq (,%rdi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
movq %rdx, 0x40(%rsp)
leaq (%rdi,%rdi,2), %r14
xorl %r15d, %r15d
cmpl $0x0, (%rcx,%rdi,4)
jle 0x9ab2
movsd %xmm1, 0x8(%rsp)
leaq (,%r15,8), %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x28(%rsp)
leaq (%r15,%r15,2), %r13
xorl %r8d, %r8d
movq (%rbx), %rax
movq 0xd8(%rbx), %rcx
movq 0x40(%rsp), %rdx
movq (%rax,%rdx), %rax
movq 0x28(%rsp), %rsi
movq (%rax,%rsi), %rax
movslq (%rax,%r8,4), %rax
movl %eax, 0xb0(%rbx)
movq (%rcx,%rdx), %rcx
movq (%rcx,%rsi), %rcx
movslq (%rcx,%r8,4), %rsi
testq %rax, %rax
je 0x98c3
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,%r15,4)
movq 0x188(%rbx), %rax
movq (%rax,%r14,8), %rax
decl -0x4(%rax,%rsi,4)
movq 0x1a0(%rbx), %rax
decl (%rax,%rdi,4)
movq %rsi, 0x10(%rsp)
movsd %xmm0, 0x30(%rsp)
movq $0x0, 0x278(%rbx)
cmpl $0x0, 0x1c(%rbx)
movq %r8, 0x18(%rsp)
jle 0x9a35
xorl %r12d, %r12d
xorl %ebp, %ebp
movslq 0xb0(%rbx), %rax
testq %rax, %rax
je 0x9980
movq 0x110(%rbx), %rcx
movq (%rcx,%r12), %rcx
movq (%rcx,%r13,8), %rcx
movslq -0x4(%rcx,%rax,4), %rax
movl %eax, 0xb4(%rbx)
movq 0x128(%rbx), %rcx
movq (%rcx,%r12), %rcx
movl (%rcx,%r15,4), %ecx
movl %ecx, 0xb8(%rbx)
movsd 0x98(%rbx), %xmm1
cmpq $0x270f, %rax           # imm = 0x270F
jg 0x9986
cmpl $0x270f, %ecx           # imm = 0x270F
jg 0x9986
movslq %ecx, %rcx
leaq 0x37a0a(%rip), %rdx     # 0x41358
movq (%rdx), %rdx
leaq 0x379e8(%rip), %rsi     # 0x41340
movq (%rsi), %rsi
leaq (%rcx,%rcx,2), %rcx
movq 0x28(%rbx), %rdi
movslq (%rdi,%r15,4), %rdi
movq (%rsi,%rcx,8), %rcx
movsd -0x8(%rcx,%rdi,8), %xmm0
mulsd %xmm1, %xmm0
mulsd (%rdx,%rax,8), %xmm1
subsd %xmm0, %xmm1
jmp 0x99c9
xorpd %xmm1, %xmm1
jmp 0x99c9
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm1, (%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movsd 0x78(%rbx), %xmm2
movq 0x28(%rbx), %rax
xorps %xmm3, %xmm3
cvtsi2sdl (%rax,%r15,4), %xmm3
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
divsd %xmm3, %xmm0
callq 0x3290
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movq 0x238(%rbx), %rax
movsd %xmm1, (%rax,%rbp,8)
movq 0x188(%rbx), %rax
movq (%rax,%r14,8), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%rbp,4), %xmm0
addsd 0x88(%rbx), %xmm0
movsd %xmm0, (%rsp)
movapd %xmm1, %xmm0
callq 0x35a0
mulsd (%rsp), %xmm0
movq 0x260(%rbx), %rax
movsd %xmm0, (%rax,%rbp,8)
addsd 0x278(%rbx), %xmm0
movsd %xmm0, 0x278(%rbx)
incq %rbp
movslq 0x1c(%rbx), %rcx
addq $0x18, %r12
cmpq %rcx, %rbp
jl 0x98ed
jmp 0x9a41
movq 0x38(%rsp), %rax
movq (%rax), %rax
xorpd %xmm0, %xmm0
movq 0x10(%rsp), %r12
movsd -0x8(%rax,%r12,8), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x3290
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movq 0x260(%rbx), %rax
movsd -0x8(%rax,%r12,8), %xmm0
callq 0x3290
movsd 0x30(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movq 0x18(%rsp), %r8
incq %r8
movq 0x40(%rbx), %rcx
movq 0x20(%rsp), %rdi
movslq (%rcx,%rdi,4), %rax
cmpq %rax, %r8
jl 0x9845
movl 0x24(%rbx), %eax
movsd 0x8(%rsp), %xmm1
incq %r15
movslq %eax, %rdx
cmpq %rdx, %r15
jl 0x981d
testl %eax, %eax
jle 0x9dbc
movq 0x40(%rbx), %rcx
xorpd %xmm2, %xmm2
xorl %r15d, %r15d
xorpd %xmm3, %xmm3
movsd %xmm3, 0x10(%rsp)
cmpl $0x0, (%rcx,%rdi,4)
jle 0x9da5
xorl %ebp, %ebp
movsd %xmm0, 0x30(%rsp)
movsd %xmm1, 0x8(%rsp)
movsd %xmm2, 0x18(%rsp)
movq (%rbx), %rax
movq (%rax,%r14,8), %rax
leaq (%r15,%r15,2), %r13
movq (%rax,%r13,8), %rax
movq %rbp, 0x28(%rsp)
movl (%rax,%rbp,4), %eax
movl %eax, 0xb0(%rbx)
movq $0x0, 0x278(%rbx)
cmpl $0x0, 0x1c(%rbx)
jle 0x9c80
movl $0x1, %ebp
xorl %r12d, %r12d
testl %eax, %eax
je 0x9bc3
movq 0x110(%rbx), %rcx
movq (%rcx,%r12), %rcx
movq (%rcx,%r13,8), %rcx
cltq
movslq -0x4(%rcx,%rax,4), %rax
movl %eax, 0xb4(%rbx)
movq 0x128(%rbx), %rcx
movq (%rcx,%r12), %rcx
movl (%rcx,%r15,4), %ecx
movl %ecx, 0xb8(%rbx)
movsd 0x98(%rbx), %xmm1
cmpq $0x270f, %rax           # imm = 0x270F
jg 0x9bc9
cmpl $0x270f, %ecx           # imm = 0x270F
jg 0x9bc9
movslq %ecx, %rcx
leaq 0x377c7(%rip), %rdx     # 0x41358
movq (%rdx), %rdx
leaq 0x377a5(%rip), %rsi     # 0x41340
movq (%rsi), %rsi
leaq (%rcx,%rcx,2), %rcx
movq 0x28(%rbx), %rdi
movslq (%rdi,%r15,4), %rdi
movq (%rsi,%rcx,8), %rcx
movsd -0x8(%rcx,%rdi,8), %xmm0
mulsd %xmm1, %xmm0
mulsd (%rdx,%rax,8), %xmm1
subsd %xmm0, %xmm1
jmp 0x9c0c
xorpd %xmm1, %xmm1
jmp 0x9c0c
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm1, (%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movsd 0x78(%rbx), %xmm2
movq 0x28(%rbx), %rax
xorps %xmm3, %xmm3
cvtsi2sdl (%rax,%r15,4), %xmm3
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
divsd %xmm3, %xmm0
callq 0x3290
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movq 0x238(%rbx), %rax
movsd %xmm1, -0x8(%rax,%rbp,8)
movq 0x188(%rbx), %rax
movq (%rax,%r14,8), %rax
xorps %xmm0, %xmm0
cvtsi2sdl -0x4(%rax,%rbp,4), %xmm0
addsd 0x88(%rbx), %xmm0
movsd %xmm0, (%rsp)
movapd %xmm1, %xmm0
callq 0x35a0
mulsd (%rsp), %xmm0
movq 0x260(%rbx), %rax
movsd %xmm0, -0x8(%rax,%rbp,8)
addsd 0x278(%rbx), %xmm0
movsd %xmm0, 0x278(%rbx)
movslq 0x1c(%rbx), %rax
cmpq %rax, %rbp
jge 0x9c84
movl 0xb0(%rbx), %eax
incq %rbp
addq $0x18, %r12
jmp 0x9b36
xorpd %xmm0, %xmm0
movq 0x38(%rsp), %rdi
callq 0x1d2d5
movq 0xf0(%rbx), %rcx
movq 0x40(%rsp), %r13
movq (%rcx,%r13), %rcx
leaq (,%r15,8), %rdx
leaq (%rdx,%rdx,2), %r12
movq (%rcx,%r12), %rcx
movq 0x28(%rsp), %rbp
movl %eax, (%rcx,%rbp,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,%r13), %rax
movq (%rax,%r12), %rax
movslq (%rax,%rbp,4), %rax
movsd -0x8(%rcx,%rax,8), %xmm0
callq 0x3290
movslq 0xb0(%rbx), %rax
testq %rax, %rax
movq 0x20(%rsp), %rdi
je 0x9d67
movq 0xf0(%rbx), %rcx
movq 0x110(%rbx), %rdx
movq (%rcx,%r13), %rcx
movq (%rcx,%r12), %rcx
movslq (%rcx,%rbp,4), %rcx
leaq -0x8(,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
movq (%rdx,%rsi), %rdx
movq (%rdx,%r12), %rdx
incl -0x4(%rdx,%rax,4)
movq 0x128(%rbx), %rax
movq (%rax,%rsi), %rax
incl (%rax,%r15,4)
movq 0x188(%rbx), %rax
movq (%rax,%r13), %rax
incl -0x4(%rax,%rcx,4)
movq 0x1a0(%rbx), %rax
incl (%rax,%rdi,4)
movsd 0x10(%rsp), %xmm1
addsd (%rsp), %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x18(%rsp), %xmm2
addsd %xmm0, %xmm2
incq %rbp
movq 0x40(%rbx), %rcx
movslq (%rcx,%rdi,4), %rax
cmpq %rax, %rbp
movsd 0x30(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
jl 0x9af6
movl 0x24(%rbx), %eax
incq %r15
movslq %eax, %rdx
cmpq %rdx, %r15
jl 0x9ade
subsd 0x10(%rsp), %xmm2
jmp 0x9dc0
xorpd %xmm2, %xmm2
movq %rdi, %r13
subsd %xmm1, %xmm0
subsd %xmm0, %xmm2
movsd %xmm2, 0x18(%rsp)
xorpd %xmm0, %xmm0
movsd 0x2c483(%rip), %xmm1    # 0x36260
callq 0x1d178
callq 0x3290
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movl 0x24(%rbx), %eax
jbe 0x9e96
testl %eax, %eax
jle 0x9fea
movq 0x40(%rbx), %rcx
movq 0xc0(%rbx), %rdx
leaq (,%r13,8), %rsi
leaq (%rsi,%rsi,2), %rsi
movq 0xf0(%rbx), %rdi
addq %rsi, %rdi
addq 0xd8(%rbx), %rsi
movl (%rcx,%r13,4), %r9d
xorl %r8d, %r8d
testl %r9d, %r9d
jle 0x9e86
movq (%rdi), %rax
leaq (,%r8,8), %r9
leaq (%r9,%r9,2), %r9
movq (%rax,%r9), %rax
movq (%rsi), %r10
movq (%r10,%r9), %r10
xorl %r11d, %r11d
movslq 0x108(%rbx), %r9
leaq 0x1(%r9), %r14
movl %r14d, 0x108(%rbx)
movl (%rax,%r11,4), %ebp
movl %ebp, 0x4(%rdx,%r9,4)
movl %ebp, (%r10,%r11,4)
movq 0x20(%rsp), %r13
incq %r11
movslq (%rcx,%r13,4), %r9
cmpq %r9, %r11
jl 0x9e53
movl 0x24(%rbx), %eax
incq %r8
movslq %eax, %r10
cmpq %r10, %r8
jl 0x9e31
jmp 0x9fea
testl %eax, %eax
jle 0x9fea
movq 0x40(%rbx), %rdx
leaq (,%r13,2), %rsi
addq %r13, %rsi
shlq $0x3, %rsi
addq (%rbx), %rsi
movq %rsi, 0x8(%rsp)
movq %rdx, (%rsp)
movl (%rdx,%r13,4), %r12d
xorl %esi, %esi
testl %r12d, %r12d
jle 0x9fdb
movq 0x8(%rsp), %rax
movq (%rax), %rax
leaq (%rsi,%rsi,2), %rdi
movq (%rax,%rdi,8), %rax
leaq (,%r13,8), %rdi
leaq (%rdi,%rdi,2), %rdi
movq 0xf0(%rbx), %r8
addq %rdi, %r8
movq 0x110(%rbx), %r9
movq 0x128(%rbx), %r10
movq 0x188(%rbx), %r11
addq %rdi, %r11
movq 0x1a0(%rbx), %r14
addq 0xd8(%rbx), %rdi
xorl %r15d, %r15d
incl 0x108(%rbx)
movslq (%rax,%r15,4), %r12
movl %r12d, 0xb0(%rbx)
testq %r12, %r12
je 0x9fc4
movq (%r8), %r13
leaq (,%rsi,8), %rbp
leaq (%rbp,%rbp,2), %rbp
movq (%r13,%rbp), %r13
movslq (%r13,%r15,4), %r13
leaq -0x8(,%r13,8), %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%r9,%rdx), %rcx
movq (%rcx,%rbp), %rcx
decl -0x4(%rcx,%r12,4)
movq (%r10,%rdx), %rcx
decl (%rcx,%rsi,4)
movq (%r11), %rcx
decl -0x4(%rcx,%r13,4)
movq 0x20(%rsp), %rdx
decl (%r14,%rdx,4)
movq (%rdi), %rdx
movq (%rdx,%rbp), %rdx
movslq (%rdx,%r15,4), %rdx
leaq -0x8(,%rdx,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 0x20(%rsp), %rbp
movq (%r10,%r12), %r12
incl (%r12,%rsi,4)
incl -0x4(%rcx,%rdx,4)
movq %rbp, %r13
incl (%r14,%rbp,4)
incq %r15
movq (%rsp), %rcx
movslq (%rcx,%r13,4), %r12
cmpq %r12, %r15
jl 0x9f1b
movl 0x24(%rbx), %eax
incq %rsi
movslq %eax, %rdi
cmpq %rdi, %rsi
jl 0x9ec3
movq %r13, %rdi
incq %rdi
movslq 0x20(%rbx), %rcx
cmpq %rcx, %rdi
jl 0x97e8
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq