// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
orl $-0x1, 0x108(%rdi)
leaq 0x260(%rdi), %rax
movq %rax, 0x10(%rsp)
xorl %r8d, %r8d
movslq 0x20(%rbx), %rax
cmpq %rax, %r8
jge 0x81df
imulq $0x18, %r8, %rbp
xorl %r14d, %r14d
movq %r8, 0x18(%rsp)
movslq 0x24(%rbx), %rax
cmpq %rax, %r14
jge 0x81d7
imulq $0x18, %r14, %r15
xorl %r9d, %r9d
movq 0x40(%rbx), %rax
movslq (%rax,%r8,4), %rax
cmpq %rax, %r9
jge 0x81cf
movslq 0x108(%rbx), %rcx
leal 0x1(%rcx), %eax
movl %eax, 0x108(%rbx)
movq (%rbx), %rax
movq (%rax,%rbp), %rax
movq (%rax,%r15), %rax
movslq (%rax,%r9,4), %rdx
testq %rdx, %rdx
movq %r9, 0x20(%rsp)
je 0x7faa
movq 0xc0(%rbx), %rsi
movq 0x110(%rbx), %rdi
movslq 0x4(%rsi,%rcx,4), %rcx
imulq $0x18, %rcx, %rcx
movq -0x18(%rdi,%rcx), %rcx
movq (%rcx,%r15), %rcx
decl -0x4(%rcx,%rdx,4)
movslq 0x108(%rbx), %rcx
movslq (%rsi,%rcx,4), %rcx
movq 0x128(%rbx), %rdx
imulq $0x18, %rcx, %rcx
movq -0x18(%rdx,%rcx), %rcx
decl (%rcx,%r14,4)
movq 0x188(%rbx), %rcx
movslq 0x108(%rbx), %rdx
movslq (%rsi,%rdx,4), %rdx
movq (%rcx,%rbp), %rcx
decl -0x4(%rcx,%rdx,4)
movq 0x1a0(%rbx), %rcx
decl (%rcx,%r8,4)
movl (%rax,%r9,4), %eax
jmp 0x7fac
xorl %eax, %eax
movl %eax, 0xb0(%rbx)
andq $0x0, 0x278(%rbx)
xorpd %xmm0, %xmm0
xorl %r13d, %r13d
xorl %r12d, %r12d
movslq 0x1c(%rbx), %rax
cmpq %rax, %r12
jge 0x810a
movslq 0xb0(%rbx), %rax
testq %rax, %rax
je 0x8064
movq 0x110(%rbx), %rcx
movq (%rcx,%r13), %rcx
movq (%rcx,%r15), %rcx
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 0x806a
cmpl $0x270f, %ecx           # imm = 0x270F
jg 0x806a
movslq %ecx, %rcx
leaq 0x2c326(%rip), %rdx     # 0x34358
movq (%rdx), %rdx
leaq 0x2c304(%rip), %rsi     # 0x34340
movq (%rsi), %rsi
imulq $0x18, %rcx, %rcx
movq 0x28(%rbx), %rdi
movslq (%rdi,%r14,4), %rdi
movq (%rsi,%rcx), %rcx
movsd -0x8(%rcx,%rdi,8), %xmm0
mulsd %xmm1, %xmm0
mulsd (%rdx,%rax,8), %xmm1
subsd %xmm0, %xmm1
jmp 0x80a6
xorpd %xmm1, %xmm1
jmp 0x80a6
cvtsi2sd %eax, %xmm0
movsd %xmm1, 0x8(%rsp)
cvtsi2sd %ecx, %xmm1
movsd 0x78(%rbx), %xmm2
movq 0x28(%rbx), %rax
cvtsi2sdl (%rax,%r14,4), %xmm3
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
divsd %xmm3, %xmm0
callq 0x32d0
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movq 0x238(%rbx), %rax
movsd %xmm1, (%rax,%r12,8)
movq 0x188(%rbx), %rax
movq (%rax,%rbp), %rax
cvtsi2sdl (%rax,%r12,4), %xmm0
addsd 0x88(%rbx), %xmm0
movsd %xmm0, 0x8(%rsp)
movapd %xmm1, %xmm0
callq 0x35c0
mulsd 0x8(%rsp), %xmm0
movq 0x260(%rbx), %rax
movsd %xmm0, (%rax,%r12,8)
addsd 0x278(%rbx), %xmm0
movsd %xmm0, 0x278(%rbx)
incq %r12
addq $0x18, %r13
jmp 0x7fc4
movq 0x10(%rsp), %rdi
callq 0x17127
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 (%rdx,%rbp), %rdx
movq (%rdx,%r15), %rdx
movq 0x20(%rsp), %r9
movl %eax, (%rdx,%r9,4)
movq (%rbx), %rax
movq (%rax,%rbp), %rax
movq (%rax,%r15), %rax
movslq (%rax,%r9,4), %rax
testq %rax, %rax
movq 0x18(%rsp), %r8
je 0x81c7
movslq 0x108(%rbx), %rdx
movslq (%rcx,%rdx,4), %rdx
movq 0x110(%rbx), %rsi
imulq $0x18, %rdx, %rdx
movq -0x18(%rsi,%rdx), %rdx
movq (%rdx,%r15), %rdx
incl -0x4(%rdx,%rax,4)
movslq 0x108(%rbx), %rax
movslq (%rcx,%rax,4), %rax
movq 0x128(%rbx), %rdx
imulq $0x18, %rax, %rax
movq -0x18(%rdx,%rax), %rax
incl (%rax,%r14,4)
movq 0x188(%rbx), %rax
movslq 0x108(%rbx), %rdx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rbp), %rax
incl -0x4(%rax,%rcx,4)
movq 0x1a0(%rbx), %rax
incl (%rax,%r8,4)
incq %r9
jmp 0x7f02
incq %r14
jmp 0x7eee
incq %r8
jmp 0x7ed5
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq