// File: myudelson[P]hmm-scalable/HMMProblemSt.cpp
// Function: HMMProblemSt::computeBeta()
void HMMProblemSt::computeBeta() {
    NPAR nS = this->task->nS, o = -1;//, isTarget;
    NDAT Nst = this->task->Nst, N = this->task->N;
    NCAT g,k;
    NPAR f_is_scaled = this->task->is_scaled==1;
    struct context* ctx = new context;
    toZero2D(this->beta, Nst, nS);

    // Backwards pass
    for(int t=(N-1); t>=0; t--) {
        if(t==96) {
            int a =0;
        }
        o = this->task->dat_obs[t]; // observation y in the data is 1-right, 0-wrong; math assumes HMM-Scalable 1-right, 2-wrong, with -1 taken out, so 0-right, 1-wrong
        // isTarget = this->task->metrics_target_obs == o;
        g = this->task->dat_group[t]; // -1, because in data they were 1-starting
        ctx->g=g;
        ctx->o=o;
        ctx->t=t;
        // grab skill array (if exists)
        NCAT *ar;
        NPAR n, i, j;
        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(NPAR 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 fwd_ix = this->forward_ix[tt];
            if(fwd_ix==-1) { // this is the end of student,skill sequence, no forward index (-1)
                for(i=0;i<nS;i++)
                    beta[tt][i] = (f_is_scaled)?this->scale[tt]:1.0;
            } else { // not the end of student,skill sequence
                // o here is o from next step in oo_tt
                NPAR o_next = this->task->dat_obs_stacked[fwd_ix];
                for(i=0; i<nS; i++) {
                    for(j=0; j<nS; j++)
                        this->beta[tt][i] += this->beta[fwd_ix][j] * getA(ctx,i,j) * ((o_next<0)?1:getB(ctx,j,o_next)); // if observatiob unknown use 1
                    if(f_is_scaled) this->beta[tt][i] *= this->scale[tt];
                }
            }
        } // all skills in a row
        // tt_off -= fd->skill_n[t]; // tt offset
    } // all rows in the data
    delete ctx;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %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 0x68(%rax), %rax
movsbl 0x41(%rax), %eax
cmpl $0x1, %eax
sete %al
andb $0x1, %al
movb %al, -0x1d(%rbp)
movl $0x14, %edi
callq 0x3270
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, -0x28(%rbp)
movq 0x90(%rax), %rdi
movl -0x10(%rbp), %esi
movsbl -0x9(%rbp), %edx
callq 0x19280
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jl 0x139e8
cmpl $0x60, -0x2c(%rbp)
jne 0x136cd
movl $0x0, -0x30(%rbp)
movq -0x60(%rbp), %rax
movq 0x68(%rax), %rcx
movq 0x880(%rcx), %rcx
movslq -0x2c(%rbp), %rdx
movb (%rcx,%rdx), %cl
movb %cl, -0xa(%rbp)
movq 0x68(%rax), %rcx
movq 0x890(%rcx), %rcx
movslq -0x2c(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x18(%rbp)
movl -0x18(%rbp), %edx
movq -0x28(%rbp), %rcx
movl %edx, 0x4(%rcx)
movb -0xa(%rbp), %dl
movq -0x28(%rbp), %rcx
movb %dl, 0x10(%rcx)
movl -0x2c(%rbp), %edx
movq -0x28(%rbp), %rcx
movl %edx, 0xc(%rcx)
movq 0x68(%rax), %rdi
movl -0x2c(%rbp), %esi
leaq -0x38(%rbp), %rdx
leaq -0x39(%rbp), %rcx
callq 0x9ce0
movq -0x38(%rbp), %rax
cmpl $0x0, (%rax)
jge 0x1373b
jmp 0x139da
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movsbl -0x39(%rbp), %ecx
cmpl %ecx, %eax
jge 0x13788
movq -0x60(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
movslq -0x44(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
setg %al
andb $0x1, %al
movzbl %al, %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x13749
movb $0x0, -0x45(%rbp)
movsbl -0x45(%rbp), %ecx
movsbl -0x39(%rbp), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x61(%rbp)
jge 0x137ac
movl -0x40(%rbp), %eax
movsbl -0x39(%rbp), %ecx
cmpl %ecx, %eax
sete %al
movb %al, -0x61(%rbp)
movb -0x61(%rbp), %al
testb $0x1, %al
jne 0x137b8
jmp 0x139d8
movq -0x60(%rbp), %rax
movq -0x38(%rbp), %rcx
movsbq -0x45(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x1c(%rbp)
movl -0x1c(%rbp), %edx
movq -0x28(%rbp), %rcx
movl %edx, (%rcx)
movsbl -0x45(%rbp), %ecx
movq 0x68(%rax), %rdx
movq 0x8b0(%rdx), %rdx
movslq -0x2c(%rbp), %rsi
addl (%rdx,%rsi,4), %ecx
movl %ecx, -0x4c(%rbp)
movq 0xb8(%rax), %rax
movslq -0x4c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x50(%rbp)
cmpl $-0x1, -0x50(%rbp)
jne 0x13875
movb $0x0, -0x3a(%rbp)
movsbl -0x3a(%rbp), %eax
movsbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
jge 0x13870
cmpb $0x0, -0x1d(%rbp)
je 0x13835
movq -0x60(%rbp), %rax
movq 0xa8(%rax), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x70(%rbp)
jmp 0x13844
movsd 0x8203(%rip), %xmm0     # 0x1ba40
movsd %xmm0, -0x70(%rbp)
jmp 0x13844
movq -0x60(%rbp), %rax
movsd -0x70(%rbp), %xmm0
movq 0x90(%rax), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x3a(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x3a(%rbp), %al
addb $0x1, %al
movb %al, -0x3a(%rbp)
jmp 0x13808
jmp 0x139c9
movq -0x60(%rbp), %rax
movq 0x68(%rax), %rax
movq 0x888(%rax), %rax
movslq -0x50(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x51(%rbp)
movb $0x0, -0x3a(%rbp)
movsbl -0x3a(%rbp), %eax
movsbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
jge 0x139c7
movb $0x0, -0x3b(%rbp)
movsbl -0x3b(%rbp), %eax
movsbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
jge 0x13980
movq -0x60(%rbp), %rdi
movq 0x90(%rdi), %rax
movslq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x3b(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x28(%rbp), %rsi
movb -0x3a(%rbp), %dl
movb -0x3b(%rbp), %cl
movq (%rdi), %rax
movsbl %dl, %edx
movsbl %cl, %ecx
callq *0x30(%rax)
movaps %xmm0, %xmm1
movsd -0x80(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movsbl -0x51(%rbp), %eax
cmpl $0x0, %eax
jge 0x1391a
movsd 0x8130(%rip), %xmm0     # 0x1ba40
movsd %xmm0, -0x88(%rbp)
jmp 0x1393c
movq -0x60(%rbp), %rdi
movq -0x28(%rbp), %rsi
movb -0x3b(%rbp), %dl
movb -0x51(%rbp), %cl
movq (%rdi), %rax
movsbl %dl, %edx
movsbl %cl, %ecx
callq *0x38(%rax)
movsd %xmm0, -0x88(%rbp)
movsd -0x78(%rbp), %xmm0
movq -0x60(%rbp), %rax
movsd -0x88(%rbp), %xmm2
movq 0x90(%rax), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x3a(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x3b(%rbp), %al
addb $0x1, %al
movb %al, -0x3b(%rbp)
jmp 0x138a6
cmpb $0x0, -0x1d(%rbp)
je 0x139b8
movq -0x60(%rbp), %rax
movq 0xa8(%rax), %rcx
movslq -0x4c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x90(%rax), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x3a(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x139ba
movb -0x3a(%rbp), %al
addb $0x1, %al
movb %al, -0x3a(%rbp)
jmp 0x13892
jmp 0x139c9
jmp 0x139cb
movb -0x45(%rbp), %al
addb $0x1, %al
movb %al, -0x45(%rbp)
jmp 0x1378c
jmp 0x139da
movl -0x2c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x136b6
movq -0x28(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, %rax
je 0x13a0a
movq -0x90(%rbp), %rdi
movl $0x14, %esi
callq 0x3280
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)