// File: /workspace/github_cpp_makefile_star2_O3/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
movsbq 0x900(%rax), %rcx
movq %rcx, 0x30(%rsp)
movzbl %cl, %ecx
movq %rcx, 0x8(%rsp)
movslq 0x8fc(%rax), %r15
movl 0x8f8(%rax), %eax
movq %rax, 0x48(%rsp)
testq %r15, %r15
jle 0xc943
movq 0x98(%rbx), %r13
movl 0x30(%rsp), %r12d
leaq (,%r12,8), %r14
xorl %ebp, %ebp
cmpb $0x0, 0x30(%rsp)
jle 0xc8f1
movq (%r13,%rbp,8), %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2100
incq %rbp
cmpq %rbp, %r15
jne 0xc8db
movq 0xa0(%rbx), %rax
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
movq %r15, 0x20(%rsp)
cmpb $0x0, 0x8(%rsp)
jle 0xc936
movq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %r13
xorl %r15d, %r15d
movq (%r13,%r15,8), %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2100
incq %r15
cmpq %r15, %r12
jne 0xc91f
incq %rbp
movq 0x20(%rsp), %r15
cmpq %r15, %rbp
jne 0xc90c
movl $0x14, %edi
callq 0x21c0
movq %rax, %rbp
cmpl $0x0, 0x48(%rsp)
jle 0xcc2a
xorl %r15d, %r15d
movq 0x68(%rbx), %rdi
movq 0x890(%rdi), %rax
movl (%rax,%r15,4), %eax
movl %eax, 0x4(%rbp)
movl %r15d, 0xc(%rbp)
movl %r15d, %esi
leaq 0x50(%rsp), %rdx
leaq 0x3(%rsp), %rcx
callq 0x7797
movq 0x50(%rsp), %rax
cmpl $0x0, (%rax)
js 0xcc1c
movsbl 0x3(%rsp), %ecx
movl %ecx, 0x4(%rsp)
testl %ecx, %ecx
jle 0xcc1c
movq 0x58(%rbx), %rdx
movl 0x4(%rsp), %esi
xorl %edi, %edi
xorl %ecx, %ecx
movslq (%rax,%rdi,4), %r8
xorl %r9d, %r9d
cmpb $0x0, (%rdx,%r8)
setg %r9b
addl %r9d, %ecx
incq %rdi
cmpq %rdi, %rsi
jne 0xc9b1
cmpb $0x0, 0x4(%rsp)
jle 0xcc1c
cmpl 0x4(%rsp), %ecx
jne 0xcc1c
xorl %esi, %esi
movq %r15, 0x58(%rsp)
movq 0x50(%rsp), %rax
movl (%rax,%rsi,4), %eax
movl %eax, (%rbp)
movq 0x68(%rbx), %rax
movq 0xb8(%rbx), %rcx
movq 0x8b0(%rax), %rdx
movslq (%rdx,%r15,4), %rdx
movq %rsi, 0x60(%rsp)
movslq %esi, %r12
addq %rdx, %r12
movslq (%rcx,%r12,4), %rdx
movq %rdx, 0x38(%rsp)
cmpq $-0x1, %rdx
je 0xcbfa
cmpb $0x0, 0x8(%rsp)
jle 0xcbfa
movq 0x888(%rax), %rax
movq 0x38(%rsp), %rcx
movb (%rax,%rcx), %al
movb %al, 0x2(%rsp)
xorps %xmm1, %xmm1
xorl %r15d, %r15d
xorl %r14d, %r14d
movaps %xmm1, 0x20(%rsp)
movq (%rbx), %rax
movq 0x70(%rbx), %rcx
movq (%rcx,%r12,8), %rcx
movsd (%rcx,%r15,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movsbl %r15b, %edx
movsbl %r14b, %ecx
movq %rbx, %rdi
movq %rbp, %rsi
callq *0x30(%rax)
movapd %xmm0, %xmm2
mulsd 0x10(%rsp), %xmm2
movq 0x90(%rbx), %rax
movq 0x38(%rsp), %rcx
movq (%rax,%rcx,8), %rax
mulsd (%rax,%r14,8), %xmm2
movb 0x2(%rsp), %al
movsbl %al, %r13d
movsd 0x5ea5(%rip), %xmm0     # 0x12950
testb %al, %al
js 0xcace
movq (%rbx), %rax
movsbl %r14b, %edx
movq %rbx, %rdi
movq %rbp, %rsi
movl %r13d, %ecx
movsd %xmm2, 0x10(%rsp)
callq *0x38(%rax)
movsd 0x10(%rsp), %xmm2
mulsd %xmm0, %xmm2
movapd 0x20(%rsp), %xmm1
addsd %xmm2, %xmm1
incq %r14
cmpq %r14, 0x8(%rsp)
jne 0xca4e
incq %r15
cmpq 0x8(%rsp), %r15
jne 0xca4b
movl %r13d, 0x44(%rsp)
cmpb $0x0, 0x8(%rsp)
jle 0xcbfa
xorpd %xmm2, %xmm2
cmpltsd %xmm1, %xmm2
andpd %xmm2, %xmm1
movsd 0x5e33(%rip), %xmm0     # 0x12950
andnpd %xmm0, %xmm2
orpd %xmm1, %xmm2
movapd %xmm2, 0x10(%rsp)
xorl %r15d, %r15d
xorl %r14d, %r14d
movq (%rbx), %rax
movq 0x70(%rbx), %rcx
movq (%rcx,%r12,8), %rcx
movsd (%rcx,%r15,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movsbl %r15b, %edx
movsbl %r14b, %ecx
movq %rbx, %rdi
movq %rbp, %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,%r14,8), %xmm1
movsd 0x5dcf(%rip), %xmm0     # 0x12950
cmpb $0x0, 0x2(%rsp)
js 0xcba8
movq (%rbx), %rax
movsbl %r14b, %edx
movq %rbx, %rdi
movq %rbp, %rsi
movl 0x44(%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,%r12,8), %rax
movq (%rax,%r15,8), %rax
movsd %xmm1, (%rax,%r14,8)
movq 0x98(%rbx), %rax
movq (%rax,%r12,8), %rax
addsd (%rax,%r15,8), %xmm1
movsd %xmm1, (%rax,%r15,8)
incq %r14
cmpq %r14, 0x8(%rsp)
jne 0xcb31
incq %r15
cmpq 0x30(%rsp), %r15
jne 0xcb2e
movq 0x60(%rsp), %rsi
incq %rsi
movsbq 0x3(%rsp), %rax
cmpq %rax, %rsi
movq 0x58(%rsp), %r15
jge 0xcc1c
cmpb %al, 0x4(%rsp)
je 0xc9e8
incq %r15
cmpq 0x48(%rsp), %r15
jne 0xc95e
movl $0x14, %esi
movq %rbp, %rdi
callq 0x21d0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq