// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rdi, %rbx
movq 0x68(%rdi), %rax
movzbl 0x900(%rax), %r14d
movl 0x8fc(%rax), %r13d
movl 0x8f8(%rax), %r15d
movb 0x41(%rax), %al
movb %al, 0x6(%rsp)
pushq $0x14
popq %rdi
callq 0x31c0
movq %rax, %r12
movq 0x90(%rbx), %rdi
movsbl %r14b, %edx
movl %r13d, %esi
callq 0xed66
movq %r12, %r10
movq %r15, %rsi
xorl %eax, %eax
testb %r14b, %r14b
movq %r14, 0x40(%rsp)
cmovgl %r14d, %eax
movq %rax, 0x18(%rsp)
testl %esi, %esi
jle 0xb54f
decq %rsi
movq 0x68(%rbx), %rdi
movl %esi, %edx
movq 0x880(%rdi), %rax
movq 0x890(%rdi), %rcx
movb (%rax,%rdx), %al
movq %rdx, 0x30(%rsp)
movl (%rcx,%rdx,4), %ecx
movl %ecx, 0x4(%r10)
movb %al, 0x10(%r10)
movl %esi, 0xc(%r10)
movq %rsi, 0x28(%rsp)
leaq 0x20(%rsp), %rdx
leaq 0x7(%rsp), %rcx
movq %r10, %r14
callq 0x6a19
movq %r14, %r10
movq 0x20(%rsp), %rcx
cmpl $0x0, (%rcx)
js 0xb545
movzbl 0x7(%rsp), %eax
movq 0x58(%rbx), %rdx
xorl %esi, %esi
testb %al, %al
movl %eax, %edi
cmovlel %esi, %edi
movzbl %dil, %edi
xorl %r11d, %r11d
cmpq %rsi, %rdi
je 0xb399
movslq (%rcx,%rsi,4), %r8
xorl %r9d, %r9d
cmpb $0x0, (%rdx,%r8)
setg %r9b
addl %r9d, %r11d
incq %rsi
jmp 0xb37c
xorl %r8d, %r8d
movl %r11d, 0x14(%rsp)
movsbq %al, %rcx
cmpq %rcx, %r8
jge 0xb545
movsbl %al, %ecx
cmpl %ecx, %r11d
jne 0xb545
movq 0x20(%rsp), %rcx
movl (%rcx,%r8,4), %ecx
movl %ecx, (%r10)
movq 0x68(%rbx), %rcx
movq 0xb8(%rbx), %rdx
movq 0x8b0(%rcx), %rsi
movq 0x30(%rsp), %rdi
movslq (%rsi,%rdi,4), %rsi
movslq %r8d, %rdi
addq %rsi, %rdi
movq %rdi, 0x8(%rsp)
movslq (%rdx,%rdi,4), %r12
cmpq $-0x1, %r12
je 0xb502
movq %r8, 0x38(%rsp)
movq 0x888(%rcx), %rax
movb (%rax,%r12), %r13b
xorl %ebp, %ebp
cmpq 0x18(%rsp), %rbp
je 0xb4ec
xorl %r15d, %r15d
cmpq %r15, 0x40(%rsp)
je 0xb4b7
movq (%rbx), %rax
movq 0x90(%rbx), %rcx
movq (%rcx,%r12,8), %rcx
movsd (%rcx,%r15,8), %xmm0
movsd %xmm0, 0x50(%rsp)
movsbl %bpl, %edx
movsbl %r15b, %ecx
movq %rbx, %rdi
movq %r10, %rsi
movq %r10, %r14
callq *0x30(%rax)
movapd %xmm0, %xmm2
movsd 0x54f1(%rip), %xmm0     # 0x10950
testb %r13b, %r13b
js 0xb484
movq (%rbx), %rax
movsbl %r13b, %ecx
movsbl %r15b, %edx
movq %rbx, %rdi
movq %r14, %rsi
movsd %xmm2, 0x48(%rsp)
callq *0x38(%rax)
movsd 0x48(%rsp), %xmm2
movsd 0x50(%rsp), %xmm1
mulsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
movq 0x90(%rbx), %rax
movq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rax
addsd (%rax,%rbp,8), %xmm1
movsd %xmm1, (%rax,%rbp,8)
incq %r15
movq %r14, %r10
jmp 0xb41a
cmpb $0x1, 0x6(%rsp)
jne 0xb4e4
movq 0x90(%rbx), %rax
movq 0xa8(%rbx), %rcx
movq 0x8(%rsp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq (%rax,%rdx,8), %rax
mulsd (%rax,%rbp,8), %xmm0
movsd %xmm0, (%rax,%rbp,8)
incq %rbp
jmp 0xb40c
movb 0x7(%rsp), %al
movl 0x14(%rsp), %r11d
movq 0x38(%rsp), %r8
incq %r8
jmp 0xb3a1
movq 0x90(%rbx), %rcx
movq 0xa8(%rbx), %rdx
xorl %esi, %esi
cmpq %rsi, 0x18(%rsp)
je 0xb4fa
movsd 0x542f(%rip), %xmm0     # 0x10950
cmpb $0x1, 0x6(%rsp)
jne 0xb532
movq 0x8(%rsp), %rdi
movsd (%rdx,%rdi,8), %xmm0
movq 0x8(%rsp), %rdi
movq (%rcx,%rdi,8), %rdi
movsd %xmm0, (%rdi,%rsi,8)
incq %rsi
jmp 0xb512
movq 0x28(%rsp), %rsi
jmp 0xb305
pushq $0x14
popq %rsi
movq %r10, %rdi
callq 0x31d0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop