// File: /workspace/github_cpp_makefile_star2_O1/bobverity[P]MavericK/MCMCobject_admixture.cpp
// Function: MCMCobject_admixture::group_update()
void MCMCobject_admixture::group_update() {
    
    // update group allocation for this gene copy
    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++;
                
                // subtract this gene copy from allele counts and admix counts
                if (data[ind][l][p]!=0) {   // if not missing data
                    alleleCounts[linearGroup[groupIndex]-1][l][data[ind][l][p]-1]--;
                    alleleCountsTotals[linearGroup[groupIndex]-1][l]--;
                    
                    admixCounts[ind][linearGroup[groupIndex]-1]--;
                    admixCountsTotals[ind]--;
                }
                
                // calculate probability of this gene copy from all demes
                thisData = data[ind][l][p];
                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
                linearGroup[groupIndex] = sample1(probVec, probVecSum);
                group[ind][l][p] = linearGroup[groupIndex];
                
                // add this gene copy to allele counts and admix counts
                if (data[ind][l][p]!=0) {   // if not missing data
                    alleleCounts[linearGroup[groupIndex]-1][l][data[ind][l][p]-1]++;
                    alleleCountsTotals[linearGroup[groupIndex]-1][l]++;
                    
                    admixCounts[ind][linearGroup[groupIndex]-1]++;
                    admixCountsTotals[ind]++;
                }
            } // p
        } // l
    } // ind
    
}

// Assembly:
movl $0xffffffff, 0x108(%rdi) # imm = 0xFFFFFFFF
cmpl $0x0, 0x20(%rdi)
jle 0x9b15
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
leaq 0x260(%rdi), %rax
movq %rax, 0x10(%rsp)
xorl %r8d, %r8d
cmpl $0x0, 0x24(%rbx)
jle 0x9af7
leaq (%r8,%r8,2), %rbp
leaq (,%r8,8), %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
movq %r8, 0x20(%rsp)
movq 0x40(%rbx), %rax
cmpl $0x0, (%rax,%r8,4)
jle 0x9ae7
leaq (%r14,%r14,2), %r15
leaq (,%r14,8), %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x28(%rsp)
xorl %r9d, %r9d
movslq 0x108(%rbx), %rcx
leal 0x1(%rcx), %eax
movl %eax, 0x108(%rbx)
movq (%rbx), %rax
movq (%rax,%rbp,8), %rax
movq (%rax,%r15,8), %rax
movslq (%rax,%r9,4), %rdx
testq %rdx, %rdx
je 0x9880
movq 0xc0(%rbx), %rsi
movq 0x110(%rbx), %rdi
movslq 0x4(%rsi,%rcx,4), %rcx
leaq (%rcx,%rcx,2), %rcx
movq -0x18(%rdi,%rcx,8), %rcx
movq (%rcx,%r15,8), %rcx
decl -0x4(%rcx,%rdx,4)
movslq 0x108(%rbx), %rcx
movslq (%rsi,%rcx,4), %rcx
movq 0x128(%rbx), %rdx
leaq (%rcx,%rcx,2), %rcx
movq -0x18(%rdx,%rcx,8), %rcx
decl (%rcx,%r14,4)
movq 0x188(%rbx), %rcx
movslq 0x108(%rbx), %rdx
movslq (%rsi,%rdx,4), %rdx
movq (%rcx,%rbp,8), %rcx
decl -0x4(%rcx,%rdx,4)
movq 0x1a0(%rbx), %rcx
decl (%rcx,%r8,4)
movq %r9, 0x30(%rsp)
movl (%rax,%r9,4), %eax
movl %eax, 0xb0(%rbx)
movq $0x0, 0x278(%rbx)
cmpl $0x0, 0x1c(%rbx)
jle 0x9a04
xorl %r13d, %r13d
xorl %r12d, %r12d
movslq 0xb0(%rbx), %rax
testq %rax, %rax
je 0x993d
movq 0x110(%rbx), %rcx
movq 0x128(%rbx), %rdx
movq (%rcx,%r13), %rcx
movq (%rcx,%r15,8), %rcx
movslq -0x4(%rcx,%rax,4), %rax
movl %eax, 0xb4(%rbx)
movq (%rdx,%r13), %rcx
movl (%rcx,%r14,4), %ecx
movl %ecx, 0xb8(%rbx)
cmpq $0x270f, %rax           # imm = 0x270F
jg 0x9943
cmpl $0x270f, %ecx           # imm = 0x270F
jg 0x9943
movslq %ecx, %rcx
movsd 0x98(%rbx), %xmm0
leaq 0x38a4d(%rip), %rdx     # 0x42358
movq (%rdx), %rdx
leaq 0x38a2b(%rip), %rsi     # 0x42340
movq (%rsi), %rsi
leaq (%rcx,%rcx,2), %rcx
movq 0x28(%rbx), %rdi
movslq (%rdi,%r14,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 0x998c
xorpd %xmm0, %xmm0
jmp 0x998c
movsd 0x98(%rbx), %xmm0
movsd %xmm0, 0x8(%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,%r14,4), %xmm3
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
divsd %xmm3, %xmm0
callq 0x3290
mulsd 0x8(%rsp), %xmm0
movq 0x238(%rbx), %rax
movsd %xmm0, (%rax,%r12,8)
movq 0x188(%rbx), %rax
movq (%rax,%rbp,8), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%r12,4), %xmm0
movq 0x238(%rbx), %rax
addsd 0x88(%rbx), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd (%rax,%r12,8), %xmm0
callq 0x35a0
mulsd 0x8(%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, %r13
cmpq %rax, %r12
jl 0x98aa
movsd 0x278(%rbx), %xmm0
movq 0x10(%rsp), %rdi
callq 0x1d629
movslq 0x108(%rbx), %rdx
movq 0xc0(%rbx), %rcx
movl %eax, (%rcx,%rdx,4)
movslq 0x108(%rbx), %rax
movl (%rcx,%rax,4), %eax
movq 0xd8(%rbx), %rdx
movq 0x18(%rsp), %rsi
movq (%rdx,%rsi), %rdx
movq 0x28(%rsp), %rdi
movq (%rdx,%rdi), %rdx
movq 0x30(%rsp), %r9
movl %eax, (%rdx,%r9,4)
movq (%rbx), %rax
movq (%rax,%rsi), %rax
movq (%rax,%rdi), %rax
movslq (%rax,%r9,4), %rax
testq %rax, %rax
movq 0x20(%rsp), %r8
je 0x9ad3
movslq 0x108(%rbx), %rdx
movslq (%rcx,%rdx,4), %rdx
movq 0x110(%rbx), %rsi
leaq (%rdx,%rdx,2), %rdx
movq -0x18(%rsi,%rdx,8), %rdx
movq (%rdx,%r15,8), %rdx
incl -0x4(%rdx,%rax,4)
movslq 0x108(%rbx), %rax
movslq (%rcx,%rax,4), %rax
movq 0x128(%rbx), %rdx
leaq (%rax,%rax,2), %rax
movq -0x18(%rdx,%rax,8), %rax
incl (%rax,%r14,4)
movq 0x188(%rbx), %rax
movslq 0x108(%rbx), %rdx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rbp,8), %rax
incl -0x4(%rax,%rcx,4)
movq 0x1a0(%rbx), %rax
incl (%rax,%r8,4)
incq %r9
movq 0x40(%rbx), %rax
movslq (%rax,%r8,4), %rax
cmpq %rax, %r9
jl 0x97f4
incq %r14
movslq 0x24(%rbx), %rax
cmpq %rax, %r14
jl 0x97cd
incq %r8
movslq 0x20(%rbx), %rax
cmpq %rax, %r8
jl 0x97a6
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq