// File: /workspace/github_cpp_makefile_star2_O3/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
movl 0x20(%rdi), %ecx
testl %ecx, %ecx
jle 0x97b1
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
leaq 0x260(%rdi), %rax
movq %rax, 0x10(%rsp)
movl 0x24(%rdi), %eax
xorl %r8d, %r8d
testl %eax, %eax
jle 0x9794
movq 0x40(%rbx), %rcx
leaq (%r8,%r8,2), %rbp
xorl %r14d, %r14d
movq %r8, 0x18(%rsp)
cmpl $0x0, (%rcx,%r8,4)
jle 0x9782
movq (%rbx), %rax
movq (%rax,%rbp,8), %rax
leaq (%r14,%r14,2), %r12
movq (%rax,%r12,8), %rax
xorl %r9d, %r9d
movslq 0x108(%rbx), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x108(%rbx)
movslq (%rax,%r9,4), %rdx
testq %rdx, %rdx
je 0x9516
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,%r12,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)
movl (%rax,%r9,4), %eax
jmp 0x9518
xorl %eax, %eax
movq %r9, 0x20(%rsp)
movl %eax, 0xb0(%rbx)
movq $0x0, 0x278(%rbx)
cmpl $0x0, 0x1c(%rbx)
jle 0x9692
movl $0x1, %r15d
xorl %r13d, %r13d
testl %eax, %eax
je 0x95ce
movq 0x110(%rbx), %rcx
movq (%rcx,%r13), %rcx
movq (%rcx,%r12,8), %rcx
cltq
movslq -0x4(%rcx,%rax,4), %rax
movl %eax, 0xb4(%rbx)
movq 0x128(%rbx), %rcx
movq (%rcx,%r13), %rcx
movl (%rcx,%r14,4), %ecx
movl %ecx, 0xb8(%rbx)
movsd 0x98(%rbx), %xmm1
cmpq $0x270f, %rax           # imm = 0x270F
jg 0x95d4
cmpl $0x270f, %ecx           # imm = 0x270F
jg 0x95d4
movslq %ecx, %rcx
leaq 0x37dbc(%rip), %rdx     # 0x41358
movq (%rdx), %rdx
leaq 0x37d9a(%rip), %rsi     # 0x41340
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), %xmm0
mulsd %xmm1, %xmm0
mulsd (%rdx,%rax,8), %xmm1
subsd %xmm0, %xmm1
jmp 0x9619
xorpd %xmm1, %xmm1
jmp 0x9619
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm1, 0x8(%rsp)
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
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movq 0x238(%rbx), %rax
movsd %xmm1, -0x8(%rax,%r15,8)
movq 0x188(%rbx), %rax
movq (%rax,%rbp,8), %rax
xorps %xmm0, %xmm0
cvtsi2sdl -0x4(%rax,%r15,4), %xmm0
addsd 0x88(%rbx), %xmm0
movsd %xmm0, 0x8(%rsp)
movapd %xmm1, %xmm0
callq 0x35a0
mulsd 0x8(%rsp), %xmm0
movq 0x260(%rbx), %rax
movsd %xmm0, -0x8(%rax,%r15,8)
addsd 0x278(%rbx), %xmm0
movsd %xmm0, 0x278(%rbx)
movslq 0x1c(%rbx), %rax
cmpq %rax, %r15
jge 0x9696
movl 0xb0(%rbx), %eax
incq %r15
addq $0x18, %r13
jmp 0x9541
xorpd %xmm0, %xmm0
movq 0x10(%rsp), %rdi
callq 0x1d2d5
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), %r8
leaq (,%r8,8), %rsi
leaq (%rsi,%rsi,2), %rsi
movq (%rdx,%rsi), %rdx
leaq (,%r14,8), %rdi
leaq (%rdi,%rdi,2), %rdi
movq (%rdx,%rdi), %rdx
movq 0x20(%rsp), %r9
movl %eax, (%rdx,%r9,4)
movq (%rbx), %rax
movq (%rax,%rsi), %rax
movq (%rax,%rdi), %rax
movslq (%rax,%r9,4), %rdx
testq %rdx, %rdx
je 0x976b
movslq 0x108(%rbx), %rsi
movslq (%rcx,%rsi,4), %rsi
movq 0x110(%rbx), %rdi
leaq (%rsi,%rsi,2), %rsi
movq -0x18(%rdi,%rsi,8), %rsi
movq (%rsi,%r12,8), %rsi
incl -0x4(%rsi,%rdx,4)
movslq 0x108(%rbx), %rdx
movslq (%rcx,%rdx,4), %rdx
movq 0x128(%rbx), %rsi
leaq (%rdx,%rdx,2), %rdx
movq -0x18(%rsi,%rdx,8), %rdx
incl (%rdx,%r14,4)
movq 0x188(%rbx), %rdx
movslq 0x108(%rbx), %rsi
movslq (%rcx,%rsi,4), %rcx
movq (%rdx,%rbp,8), %rdx
incl -0x4(%rdx,%rcx,4)
movq 0x1a0(%rbx), %rcx
incl (%rcx,%r8,4)
incq %r9
movq 0x40(%rbx), %rcx
movslq (%rcx,%r8,4), %rdx
cmpq %rdx, %r9
jl 0x948f
movl 0x24(%rbx), %eax
incq %r14
movslq %eax, %rdx
cmpq %rdx, %r14
jl 0x9472
movl 0x20(%rbx), %ecx
incq %r8
movslq %ecx, %rdx
cmpq %rdx, %r8
jl 0x945a
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq