// File: /workspace/github_cpp_makefile_star2_O3/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, 0x10(%rsp)
movslq 0x8fc(%rax), %r14
movl 0x8f8(%rax), %ebp
movb 0x41(%rax), %al
movb %al, 0xe(%rsp)
movl $0x14, %edi
callq 0x21c0
movq %rax, %r10
testq %r14, %r14
movq %rax, 0x30(%rsp)
jle 0xc600
movq 0x90(%rbx), %r15
movl 0x10(%rsp), %r12d
shlq $0x3, %r12
xorl %r13d, %r13d
cmpb $0x0, 0x10(%rsp)
jle 0xc5f8
movq (%r15,%r13,8), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2100
movq 0x30(%rsp), %r10
incq %r13
cmpq %r13, %r14
jne 0xc5de
movq %rbp, %r12
testl %r12d, %r12d
movq 0x10(%rsp), %r15
jle 0xc867
movzbl %r15b, %eax
movq %rax, 0x20(%rsp)
movq %r12, %rdx
decq %r12
movq 0x68(%rbx), %rdi
movq 0x880(%rdi), %rax
movq 0x890(%rdi), %rcx
movb -0x1(%rax,%rdx), %al
movq %rdx, 0x40(%rsp)
movl -0x4(%rcx,%rdx,4), %ecx
movl %ecx, 0x4(%r10)
movb %al, 0x10(%r10)
movl %r12d, 0xc(%r10)
movl %r12d, %esi
leaq 0x38(%rsp), %rdx
leaq 0xf(%rsp), %rcx
callq 0x7797
movq 0x30(%rsp), %r10
movq 0x38(%rsp), %rax
cmpl $0x0, (%rax)
js 0xc85b
movsbl 0xf(%rsp), %r11d
testl %r11d, %r11d
jle 0xc85b
movq 0x58(%rbx), %rcx
movl %r11d, %esi
xorl %edi, %edi
xorl %edx, %edx
movslq (%rax,%rdi,4), %r8
xorl %r9d, %r9d
cmpb $0x0, (%rcx,%r8)
setg %r9b
addl %r9d, %edx
incq %rdi
cmpq %rdi, %rsi
jne 0xc68a
testb %r11b, %r11b
jle 0xc85b
cmpl %r11d, %edx
jne 0xc85b
movl %r11d, %eax
xorl %edi, %edi
movq %r12, 0x48(%rsp)
movl %r11d, 0x2c(%rsp)
movq 0x38(%rsp), %rcx
movl (%rcx,%rdi,4), %ecx
movl %ecx, (%r10)
movq 0x68(%rbx), %rcx
movq 0xb8(%rbx), %rdx
movq 0x8b0(%rcx), %rsi
movslq (%rsi,%r12,4), %rsi
movslq %edi, %r8
addq %rsi, %r8
movq %r8, 0x18(%rsp)
movslq (%rdx,%r8,4), %r14
cmpq $-0x1, %r14
je 0xc7ff
testb %r15b, %r15b
jle 0xc846
movq %rdi, 0x50(%rsp)
movq 0x888(%rcx), %rax
movb (%rax,%r14), %r13b
movq 0x90(%rbx), %rax
xorl %r12d, %r12d
xorl %r15d, %r15d
movq (%rax,%r14,8), %rax
movsd (%rax,%r15,8), %xmm0
movsd %xmm0, 0x60(%rsp)
movq (%rbx), %rax
movsbl %r12b, %edx
movsbl %r15b, %ecx
movq %rbx, %rdi
movq %r10, %rsi
movq %r10, %rbp
callq *0x30(%rax)
movapd %xmm0, %xmm2
movsd 0x61f7(%rip), %xmm0     # 0x12950
testb %r13b, %r13b
js 0xc77e
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 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
addsd (%rcx,%r12,8), %xmm1
movsd %xmm1, (%rcx,%r12,8)
incq %r15
cmpq %r15, 0x20(%rsp)
movq %rbp, %r10
jne 0xc726
cmpb $0x1, 0xe(%rsp)
jne 0xc7d7
movq 0xa8(%rbx), %rdx
movq 0x18(%rsp), %rsi
mulsd (%rdx,%rsi,8), %xmm1
movsd %xmm1, (%rcx,%r12,8)
incq %r12
cmpq 0x20(%rsp), %r12
jne 0xc723
movb 0xf(%rsp), %al
movq 0x10(%rsp), %r15
movq 0x48(%rsp), %r12
movl 0x2c(%rsp), %r11d
movq 0x50(%rsp), %rdi
jmp 0xc846
testb %r15b, %r15b
jle 0xc846
movq 0x90(%rbx), %rdx
movq 0xa8(%rbx), %rcx
movq 0x18(%rsp), %rsi
movq (%rdx,%rsi,8), %rdx
xorl %esi, %esi
movsd 0x612b(%rip), %xmm0     # 0x12950
cmpb $0x1, 0xe(%rsp)
jne 0xc837
movq 0x18(%rsp), %r8
movsd (%rcx,%r8,8), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
incq %rsi
cmpq %rsi, 0x20(%rsp)
jne 0xc81d
incq %rdi
movsbq %al, %rcx
cmpq %rcx, %rdi
jge 0xc85b
cmpb %al, %r11b
je 0xc6c6
cmpq $0x1, 0x40(%rsp)
jg 0xc61a
movl $0x14, %esi
movq %r10, %rdi
callq 0x21d0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop