// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/HMMProblemSt.cpp
// Function: HMMProblemSt::computeXiGamma()
void HMMProblemSt::computeXiGamma(){
    NPAR nS = this->task->nS, o_tp1 = -1, i, j;
    NDAT Nst = this->task->Nst, N = this->task->N;
    NCAT g,k;
    // prepare values
    toZero2D(this->gamma, Nst, nS);
    toZero3D(this->xi, Nst, nS, nS);
    struct context* ctx = new context;
    NUMBER denom = 0.0;

    for(NDAT t=0; t<N; t++) {
        g = this->task->dat_group[t]; 
        ctx->g=g;
        ctx->t=t;
        // grab skill array (if exists)
        NCAT *ar;
        NPAR n;
        getSkillsAtRow(this->task, t, &ar, &n);
        // deal with null skill
        if(ar[0]<0) { // if no skill label
            // fprintf(stderr, "WARNING! We are not dealing with skill-less observation\n");
            continue;
        }
        
        NCAT n_active = 0;
        for(int l=0; l<n; l++) n_active+=(this->active_set_block[ar[l]]>0);

        for(int l=0; l<n && (n_active==n)/**/; l++) {
            k = ar[l];
            ctx->k=k;
            NDAT tt = l + this->task->dat_skill_rix[t]; // tt – index into stacked skill array
            NDAT next_tt = this->forward_ix[tt];
            if(next_tt==-1) continue;
            o_tp1 = this->task->dat_obs_stacked[next_tt];
            
            denom = 0.0;
            for(i=0; i<nS; i++) {
                for(j=0; j<nS; j++) {
                    denom += this->alpha[tt][i] * getA(ctx,i,j) * this->beta[next_tt][j] * ((o_tp1<0)?1:getB(ctx,j,o_tp1));
                }
            }
            for(i=0; i<nS; i++) {
                for(j=0; j<nS; j++) {
                    this->xi[tt][i][j] = this->alpha[tt][i] * getA(ctx,i,j) * this->beta[next_tt][j] * ((o_tp1<0)?1:getB(ctx,j,o_tp1)) / ((denom>0)?denom:1); //
                    this->gamma[tt][i] += this->xi[tt][i][j];
                }
            }
        } // all row's skills
    } // all N rows
    delete ctx;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
movq 0x68(%rdi), %rax
movq 0x98(%rdi), %rdi
movzbl 0x900(%rax), %r15d
movl 0x8f8(%rax), %r12d
movl 0x8fc(%rax), %ebp
movsbl %r15b, %r14d
movl %ebp, %esi
movl %r14d, %edx
callq 0xed66
movq 0xa0(%rbx), %rdi
movl %ebp, %esi
movl %r14d, %edx
movl %r14d, %ecx
callq 0xeed6
pushq $0x14
popq %rdi
callq 0x31c0
xorl %r13d, %r13d
testb %r15b, %r15b
movl $0x0, %ecx
movq %r15, 0x40(%rsp)
cmovgl %r15d, %ecx
movq %rcx, 0x30(%rsp)
movq %rax, %r15
testl %r12d, %r12d
cmovlel %r13d, %r12d
movq %r12, 0x50(%rsp)
cmpq 0x50(%rsp), %r13
je 0xb8ac
movq 0x68(%rbx), %rdi
movq 0x890(%rdi), %rax
movl (%rax,%r13,4), %eax
movl %eax, 0x4(%r15)
movl %r13d, 0xc(%r15)
movl %r13d, %esi
leaq 0x48(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0x6a19
movq 0x48(%rsp), %rcx
cmpl $0x0, (%rcx)
js 0xb8a4
movzbl 0x7(%rsp), %eax
movq 0x58(%rbx), %rdx
xorl %esi, %esi
testb %al, %al
movl %eax, %edi
cmovlel %esi, %edi
movzbl %dil, %edi
xorl %r10d, %r10d
cmpq %rsi, %rdi
je 0xb665
movslq (%rcx,%rsi,4), %r8
xorl %r9d, %r9d
cmpb $0x0, (%rdx,%r8)
setg %r9b
addl %r9d, %r10d
incq %rsi
jmp 0xb648
xorl %edi, %edi
movq %r13, 0x58(%rsp)
movl %r10d, 0xc(%rsp)
movsbq %al, %rcx
cmpq %rcx, %rdi
jge 0xb8a4
movsbl %al, %ecx
cmpl %ecx, %r10d
jne 0xb8a4
movq 0x48(%rsp), %rcx
movl (%rcx,%rdi,4), %ecx
movl %ecx, (%r15)
movq 0x68(%rbx), %rcx
movq 0xb8(%rbx), %rdx
movq 0x8b0(%rcx), %rsi
movslq (%rsi,%r13,4), %rsi
movslq %edi, %r14
addq %rsi, %r14
movslq (%rdx,%r14,4), %rdx
cmpq $-0x1, %rdx
je 0xb89c
movq %rdi, 0x60(%rsp)
movq 0x888(%rcx), %rax
movq %rdx, 0x38(%rsp)
movb (%rax,%rdx), %r12b
xorpd %xmm2, %xmm2
xorl %ebp, %ebp
movsbl %r12b, %eax
movl %eax, 0x8(%rsp)
cmpq 0x30(%rsp), %rbp
je 0xb793
xorl %r13d, %r13d
cmpq %r13, 0x40(%rsp)
je 0xb78b
movapd %xmm2, 0x20(%rsp)
movq (%rbx), %rax
movq 0x70(%rbx), %rcx
movq (%rcx,%r14,8), %rcx
movsd (%rcx,%rbp,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movsbl %bpl, %edx
movsbl %r13b, %ecx
movq %rbx, %rdi
movq %r15, %rsi
callq *0x30(%rax)
movapd %xmm0, %xmm1
mulsd 0x10(%rsp), %xmm1
movq 0x90(%rbx), %rax
movq 0x38(%rsp), %rcx
movq (%rax,%rcx,8), %rax
mulsd (%rax,%r13,8), %xmm1
movsd 0x5200(%rip), %xmm0     # 0x10950
testb %r12b, %r12b
js 0xb775
movq (%rbx), %rax
movsbl %r13b, %edx
movq %rbx, %rdi
movq %r15, %rsi
movl 0x8(%rsp), %ecx
movsd %xmm1, 0x10(%rsp)
callq *0x38(%rax)
movsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movapd 0x20(%rsp), %xmm2
addsd %xmm1, %xmm2
incq %r13
jmp 0xb6f0
incq %rbp
jmp 0xb6da
xorpd %xmm1, %xmm1
cmpltsd %xmm2, %xmm1
andpd %xmm1, %xmm2
movsd 0x51a8(%rip), %xmm0     # 0x10950
andnpd %xmm0, %xmm1
orpd %xmm2, %xmm1
movapd %xmm1, 0x10(%rsp)
xorl %ebp, %ebp
cmpq 0x30(%rsp), %rbp
je 0xb889
xorl %r13d, %r13d
cmpq %r13, 0x40(%rsp)
je 0xb881
movq (%rbx), %rax
movq 0x70(%rbx), %rcx
movq (%rcx,%r14,8), %rcx
movsd (%rcx,%rbp,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movsbl %bpl, %edx
movsbl %r13b, %ecx
movq %rbx, %rdi
movq %r15, %rsi
callq *0x30(%rax)
movapd %xmm0, %xmm1
mulsd 0x20(%rsp), %xmm1
movq 0x90(%rbx), %rax
movq 0x38(%rsp), %rcx
movq (%rax,%rcx,8), %rax
mulsd (%rax,%r13,8), %xmm1
movsd 0x5130(%rip), %xmm0     # 0x10950
testb %r12b, %r12b
js 0xb845
movq (%rbx), %rax
movsbl %r13b, %edx
movq %rbx, %rdi
movq %r15, %rsi
movl 0x8(%rsp), %ecx
movsd %xmm1, 0x20(%rsp)
callq *0x38(%rax)
movsd 0x20(%rsp), %xmm1
mulsd %xmm0, %xmm1
divsd 0x10(%rsp), %xmm1
movq 0xa0(%rbx), %rax
movq (%rax,%r14,8), %rax
movq (%rax,%rbp,8), %rax
movsd %xmm1, (%rax,%r13,8)
movq 0x98(%rbx), %rax
movq (%rax,%r14,8), %rax
addsd (%rax,%rbp,8), %xmm1
movsd %xmm1, (%rax,%rbp,8)
incq %r13
jmp 0xb7c6
incq %rbp
jmp 0xb7b8
movb 0x7(%rsp), %al
movq 0x58(%rsp), %r13
movl 0xc(%rsp), %r10d
movq 0x60(%rsp), %rdi
incq %rdi
jmp 0xb671
incq %r13
jmp 0xb5ed
pushq $0x14
popq %rsi
movq %r15, %rdi
callq 0x31d0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq