// File: /workspace/github_cpp_makefile_star2_O1/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
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, 0x18(%rsp)
movslq 0x8fc(%rax), %r14
movl 0x8f8(%rax), %ecx
movq %rcx, 0x10(%rsp)
movb 0x41(%rax), %al
movb %al, 0xf(%rsp)
movl $0x14, %edi
callq 0x21c0
movq %rax, %rbp
testq %r14, %r14
movq %rax, 0x38(%rsp)
jle 0xc495
movq 0x90(%rbx), %r15
movl 0x18(%rsp), %r12d
shlq $0x3, %r12
xorl %r13d, %r13d
cmpb $0x0, 0x18(%rsp)
jle 0xc48d
movq (%r15,%r13,8), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2100
movq 0x38(%rsp), %rbp
incq %r13
cmpq %r13, %r14
jne 0xc473
cmpl $0x0, 0x10(%rsp)
jle 0xc6fa
movzbl 0x18(%rsp), %eax
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %rdx
leaq -0x1(%rdx), %rsi
movq 0x68(%rbx), %rdi
movq 0x880(%rdi), %rax
movq 0x890(%rdi), %rcx
movb -0x1(%rax,%rdx), %al
movq %rdx, 0x48(%rsp)
movl -0x4(%rcx,%rdx,4), %ecx
movl %ecx, 0x4(%rbp)
movb %al, 0x10(%rbp)
movl %esi, 0xc(%rbp)
movq %rsi, 0x10(%rsp)
leaq 0x40(%rsp), %rdx
leaq 0xe(%rsp), %rcx
callq 0x76ed
movq 0x38(%rsp), %rbp
movq 0x40(%rsp), %rax
cmpl $0x0, (%rax)
js 0xc6ee
movsbq 0xe(%rsp), %rcx
testq %rcx, %rcx
jle 0xc532
movq 0x58(%rbx), %rdx
xorl %esi, %esi
xorl %r10d, %r10d
movslq (%rax,%rsi,4), %rdi
xorl %r8d, %r8d
cmpb $0x0, (%rdx,%rdi)
setg %r8b
addl %r8d, %r10d
incq %rsi
cmpq %rsi, %rcx
jne 0xc516
jmp 0xc535
xorl %r10d, %r10d
movsbl 0xe(%rsp), %eax
testl %eax, %eax
jle 0xc6ee
cmpl %eax, %r10d
jne 0xc6ee
xorl %edi, %edi
movl %r10d, 0x34(%rsp)
movq 0x40(%rsp), %rax
movl (%rax,%rdi,4), %eax
movl %eax, (%rbp)
movq 0x68(%rbx), %rax
movq 0xb8(%rbx), %rcx
movq 0x8b0(%rax), %rdx
movq 0x10(%rsp), %rsi
movslq (%rdx,%rsi,4), %rdx
movq %rdi, 0x50(%rsp)
movslq %edi, %rsi
addq %rdx, %rsi
movq %rsi, 0x28(%rsp)
movslq (%rcx,%rsi,4), %r14
cmpq $-0x1, %r14
je 0xc685
cmpb $0x0, 0x20(%rsp)
jle 0xc6cd
movq 0x888(%rax), %rax
movb (%rax,%r14), %r13b
xorl %r12d, %r12d
xorl %r15d, %r15d
movq (%rbx), %rax
movq 0x90(%rbx), %rcx
movq (%rcx,%r14,8), %rcx
movsd (%rcx,%r15,8), %xmm0
movsd %xmm0, 0x60(%rsp)
movsbl %r12b, %edx
movsbl %r15b, %ecx
movq %rbx, %rdi
movq %rbp, %rsi
callq *0x30(%rax)
movapd %xmm0, %xmm2
movsd 0x5367(%rip), %xmm0     # 0x11950
testb %r13b, %r13b
js 0xc60e
movq (%rbx), %rax
movsbl %r13b, %ecx
movsbl %r15b, %edx
movq %rbx, %rdi
movq %rbp, %rsi
movsd %xmm2, 0x58(%rsp)
callq *0x38(%rax)
movsd 0x58(%rsp), %xmm2
movsd 0x60(%rsp), %xmm1
mulsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
movq 0x90(%rbx), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
addsd (%rax,%r12,8), %xmm1
movsd %xmm1, (%rax,%r12,8)
incq %r15
cmpq %r15, 0x20(%rsp)
jne 0xc5b2
cmpb $0x1, 0xf(%rsp)
jne 0xc675
movq 0x90(%rbx), %rax
movq 0xa8(%rbx), %rcx
movq 0x28(%rsp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq (%rax,%rdx,8), %rax
mulsd (%rax,%r12,8), %xmm0
movsd %xmm0, (%rax,%r12,8)
incq %r12
cmpq 0x18(%rsp), %r12
jne 0xc5af
jmp 0xc6cd
cmpb $0x0, 0x20(%rsp)
jle 0xc6cd
movq 0x90(%rbx), %rcx
movq 0xa8(%rbx), %rax
movq 0x28(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
xorl %edx, %edx
movsd 0x52a3(%rip), %xmm0     # 0x11950
cmpb $0x1, 0xf(%rsp)
jne 0xc6be
movq 0x28(%rsp), %rsi
movsd (%rax,%rsi,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, 0x20(%rsp)
jne 0xc6a5
movq 0x50(%rsp), %rdi
incq %rdi
movsbq 0xe(%rsp), %rax
cmpq %rax, %rdi
movl 0x34(%rsp), %r10d
jge 0xc6ee
cmpl %eax, %r10d
je 0xc552
cmpq $0x1, 0x48(%rsp)
jg 0xc4aa
movl $0x14, %esi
movq %rbp, %rdi
callq 0x21d0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq