// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq 0x68(%rax), %rcx
movb 0x900(%rcx), %cl
movb %cl, -0x9(%rbp)
movb $-0x1, -0xa(%rbp)
movq 0x68(%rax), %rcx
movl 0x8fc(%rcx), %ecx
movl %ecx, -0x10(%rbp)
movq 0x68(%rax), %rcx
movl 0x8f8(%rcx), %ecx
movl %ecx, -0x14(%rbp)
movq 0x98(%rax), %rdi
movl -0x10(%rbp), %esi
movsbl -0x9(%rbp), %edx
callq 0x19280
movq -0x60(%rbp), %rax
movq 0xa0(%rax), %rdi
movl -0x10(%rbp), %esi
movsbl -0x9(%rbp), %edx
movsbl -0x9(%rbp), %ecx
callq 0x19550
movl $0x14, %edi
callq 0x3270
movq %rax, -0x28(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x13ec3
movq -0x60(%rbp), %rax
movq 0x68(%rax), %rcx
movq 0x890(%rcx), %rcx
movslq -0x34(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x18(%rbp)
movl -0x18(%rbp), %edx
movq -0x28(%rbp), %rcx
movl %edx, 0x4(%rcx)
movl -0x34(%rbp), %edx
movq -0x28(%rbp), %rcx
movl %edx, 0xc(%rcx)
movq 0x68(%rax), %rdi
movl -0x34(%rbp), %esi
leaq -0x40(%rbp), %rdx
leaq -0x41(%rbp), %rcx
callq 0x9ce0
movq -0x40(%rbp), %rax
cmpl $0x0, (%rax)
jge 0x13b08
jmp 0x13eb5
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movsbl -0x41(%rbp), %ecx
cmpl %ecx, %eax
jge 0x13b55
movq -0x60(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x40(%rbp), %rcx
movslq -0x4c(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
setg %al
andb $0x1, %al
movzbl %al, %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x13b16
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %ecx
movsbl -0x41(%rbp), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x61(%rbp)
jge 0x13b7b
movl -0x48(%rbp), %eax
movsbl -0x41(%rbp), %ecx
cmpl %ecx, %eax
sete %al
movb %al, -0x61(%rbp)
movb -0x61(%rbp), %al
testb $0x1, %al
jne 0x13b87
jmp 0x13eb3
movq -0x60(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x50(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x1c(%rbp)
movl -0x1c(%rbp), %edx
movq -0x28(%rbp), %rcx
movl %edx, (%rcx)
movl -0x50(%rbp), %ecx
movq 0x68(%rax), %rdx
movq 0x8b0(%rdx), %rdx
movslq -0x34(%rbp), %rsi
addl (%rdx,%rsi,4), %ecx
movl %ecx, -0x54(%rbp)
movq 0xb8(%rax), %rax
movslq -0x54(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x58(%rbp)
cmpl $-0x1, -0x58(%rbp)
jne 0x13bd6
jmp 0x13ea5
movq -0x60(%rbp), %rax
movq 0x68(%rax), %rax
movq 0x888(%rax), %rax
movslq -0x58(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0xa(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movb $0x0, -0xb(%rbp)
movsbl -0xb(%rbp), %eax
movsbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
jge 0x13cf1
movb $0x0, -0xc(%rbp)
movsbl -0xc(%rbp), %eax
movsbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
jge 0x13ce2
movq -0x60(%rbp), %rdi
movq 0x70(%rdi), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0xb(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x28(%rbp), %rsi
movb -0xb(%rbp), %dl
movb -0xc(%rbp), %cl
movq (%rdi), %rax
movsbl %dl, %edx
movsbl %cl, %ecx
callq *0x30(%rax)
movq -0x60(%rbp), %rax
movaps %xmm0, %xmm1
movsd -0x78(%rbp), %xmm0
mulsd %xmm1, %xmm0
movq 0x90(%rax), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0xc(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x70(%rbp)
movsbl -0xa(%rbp), %eax
cmpl $0x0, %eax
jge 0x13c9a
movsd 0x7dad(%rip), %xmm0     # 0x1ba40
movsd %xmm0, -0x80(%rbp)
jmp 0x13cb9
movq -0x60(%rbp), %rdi
movq -0x28(%rbp), %rsi
movb -0xc(%rbp), %dl
movb -0xa(%rbp), %cl
movq (%rdi), %rax
movsbl %dl, %edx
movsbl %cl, %ecx
callq *0x38(%rax)
movsd %xmm0, -0x80(%rbp)
movsd -0x70(%rbp), %xmm0
movsd -0x80(%rbp), %xmm2
movsd -0x30(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movb -0xc(%rbp), %al
addb $0x1, %al
movb %al, -0xc(%rbp)
jmp 0x13c0f
jmp 0x13ce4
movb -0xb(%rbp), %al
addb $0x1, %al
movb %al, -0xb(%rbp)
jmp 0x13bfb
movb $0x0, -0xb(%rbp)
movsbl -0xb(%rbp), %eax
movsbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
jge 0x13ea3
movb $0x0, -0xc(%rbp)
movsbl -0xc(%rbp), %eax
movsbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
jge 0x13e94
movq -0x60(%rbp), %rdi
movq 0x70(%rdi), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0xb(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x28(%rbp), %rsi
movb -0xb(%rbp), %dl
movb -0xc(%rbp), %cl
movq (%rdi), %rax
movsbl %dl, %edx
movsbl %cl, %ecx
callq *0x30(%rax)
movq -0x60(%rbp), %rax
movaps %xmm0, %xmm1
movsd -0x90(%rbp), %xmm0
mulsd %xmm1, %xmm0
movq 0x90(%rax), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0xc(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x88(%rbp)
movsbl -0xa(%rbp), %eax
cmpl $0x0, %eax
jge 0x13da0
movsd 0x7caa(%rip), %xmm0     # 0x1ba40
movsd %xmm0, -0x98(%rbp)
jmp 0x13dc2
movq -0x60(%rbp), %rdi
movq -0x28(%rbp), %rsi
movb -0xc(%rbp), %dl
movb -0xa(%rbp), %cl
movq (%rdi), %rax
movsbl %dl, %edx
movsbl %cl, %ecx
callq *0x38(%rax)
movsd %xmm0, -0x98(%rbp)
movsd -0x88(%rbp), %xmm0
movsd -0x98(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0x30(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x13dfb
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
jmp 0x13e0d
movsd 0x7c3d(%rip), %xmm0     # 0x1ba40
movsd %xmm0, -0xa8(%rbp)
jmp 0x13e0d
movq -0x60(%rbp), %rax
movsd -0xa0(%rbp), %xmm0
movsd -0xa8(%rbp), %xmm1
divsd %xmm1, %xmm0
movq 0xa0(%rax), %rcx
movslq -0x54(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsbq -0xb(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsbq -0xc(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0xa0(%rax), %rcx
movslq -0x54(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsbq -0xb(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsbq -0xc(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x98(%rax), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0xb(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0xc(%rbp), %al
addb $0x1, %al
movb %al, -0xc(%rbp)
jmp 0x13d09
jmp 0x13e96
movb -0xb(%rbp), %al
addb $0x1, %al
movb %al, -0xb(%rbp)
jmp 0x13cf5
jmp 0x13ea5
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x13b5c
jmp 0x13eb5
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x13aad
movq -0x28(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x13ee5
movq -0xb0(%rbp), %rdi
movl $0x14, %esi
callq 0x3280
addq $0xb0, %rsp
popq %rbp
retq
nop