// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/HMMProblemSt.cpp
// Function: HMMProblemSt::computeGradients()
NUMBER HMMProblemSt::computeGradients() {
    NPAR nS = this->task->nS, o = -1;//, isTarget;
    NDAT Nst = this->task->Nst, N = this->task->N, c = 0;
    NCAT g,k, nK = this->task->nK;
    // prepare values
    toZero2D(this->alpha, Nst, nS);
    struct context* ctx = new context;
    
    toZero1D(this->gradient_skill, this->skill1_n*nK);
    
    NUMBER loglik = computeAlphaAndPOParam(NULL); // just alpha and p(O|param)
    computeBeta();
    // this->toFileAlphaBetaObs("trainhmmst_alphabetaobs0.txt", 0); // printing out alpha and beta arrays
    
    // main gradient computation
    for(int t=0; t<N; t++) {
        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(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 tt_m1 = this->backward_ix[tt];

            // grad PI
            if (tt_m1==-1) {
                c += (k==0);
                for(i=0; i<nS; i++) {
                    this->gradient_skill[PI(k,i)] -= this->beta[tt][i] * ((o<0)?1:this->param_skill[B(k,i,o)]) / safe0num(this->po_param_gk[g][k]);
                    //fb->gradPI[i] -= dt->beta[t][i] * ((o<0)?1:fb->B[i][o]) / safe0num(dt->p_O_param);
                }
            } else {
            // grad A
                for(i=0; i<nS; i++)
                    for(j=0; j<nS; j++)
                        this->gradient_skill[A(k,i,j)] -= this->beta[tt][j] * ((o<0)?1:this->param_skill[B(k,j,o)]) * this->alpha[tt_m1][i] / safe0num(this->po_param_gk[g][k]);
            }
            // grad B
            for(i=0; i<nS; i++)
                this->gradient_skill[B(k,i,o)] -= this->alpha[tt][i] * this->beta[tt][i] / safe0num(this->po_param_gk[g][k] * this->param_skill[B(k,i,o)]);
        }// for all skills in a row
    }// for all rows
//    if( this->task->Cslices>0 ) { // penalty
//        fb->addL2Penalty(FBV_PI, this->p, (NUMBER)ndat);
//        fb->addL2Penalty(FBV_A, this->p, (NUMBER)ndat);
//        fb->addL2Penalty(FBV_B, this->p, (NUMBER)ndat);
//    }
    
//    NUMBER sum = 0;
//    for(int l=0;l<(this->skill1_n*nK);l++) sum += this->gradient_skill[l];
    delete ctx;
    
    return loglik;
}

// 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, 0x20(%rsp)
movzbl %cl, %ecx
movq %rcx, 0x10(%rsp)
movslq 0x8fc(%rax), %r15
movl 0x8f8(%rax), %ecx
movq %rcx, 0x40(%rsp)
movl 0x908(%rax), %ebp
testq %r15, %r15
jle 0xcb5e
movq 0x70(%rbx), %r12
movl 0x20(%rsp), %r14d
shlq $0x3, %r14
xorl %r13d, %r13d
cmpb $0x0, 0x10(%rsp)
jle 0xcb56
movq (%r12,%r13,8), %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2100
incq %r13
cmpq %r13, %r15
jne 0xcb41
imull 0x38(%rbx), %ebp
testl %ebp, %ebp
jle 0xcb77
movq 0x30(%rbx), %rdi
movl %ebp, %edx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2100
movq %rbx, %rdi
xorl %esi, %esi
callq 0xb7c2
movsd %xmm0, 0x50(%rsp)
movq %rbx, %rdi
callq 0xc40e
cmpl $0x0, 0x40(%rsp)
movq 0x10(%rsp), %r15
jle 0xcf5d
xorl %r14d, %r14d
movq 0x68(%rbx), %rdi
movq 0x880(%rdi), %rax
movq 0x890(%rdi), %rcx
movzbl (%rax,%r14), %eax
movl %eax, 0xc(%rsp)
movslq (%rcx,%r14,4), %r12
movl %r14d, %esi
leaq 0x48(%rsp), %rdx
leaq 0xb(%rsp), %rcx
callq 0x76ed
movq 0x48(%rsp), %rax
cmpl $0x0, (%rax)
js 0xcf4f
movsbq 0xb(%rsp), %rcx
testq %rcx, %rcx
jle 0xcc11
movq 0x58(%rbx), %rdx
xorl %esi, %esi
xorl %r9d, %r9d
movslq (%rax,%rsi,4), %rdi
xorl %r8d, %r8d
cmpb $0x0, (%rdx,%rdi)
setg %r8b
addl %r8d, %r9d
incq %rsi
cmpq %rsi, %rcx
jne 0xcbf5
jmp 0xcc14
xorl %r9d, %r9d
movsbl 0xb(%rsp), %eax
testl %eax, %eax
jle 0xcf4f
cmpl %eax, %r9d
jne 0xcf4f
movsbl 0xc(%rsp), %ebp
xorl %edx, %edx
movl %r9d, 0x3c(%rsp)
movq %r14, 0x58(%rsp)
movq %r12, 0x28(%rsp)
movl %ebp, 0x38(%rsp)
movq 0x48(%rsp), %rax
movslq (%rax,%rdx,4), %r13
movq 0x68(%rbx), %rax
movq 0xb0(%rbx), %rcx
movq 0x8b0(%rax), %rax
movslq (%rax,%r14,4), %rax
movslq %edx, %r9
addq %rax, %r9
movslq (%rcx,%r9,4), %rbp
cmpq $-0x1, %rbp
movq %r9, 0x30(%rsp)
movq %rdx, 0x60(%rsp)
je 0xcd87
cmpb $0x0, 0x20(%rsp)
movl 0xc(%rsp), %r8d
jle 0xce4b
movl $0x1, %r15d
xorl %r14d, %r14d
xorl %r12d, %r12d
movsd 0x4cab(%rip), %xmm0     # 0x11950
testb %r8b, %r8b
js 0xcce2
movq 0x28(%rbx), %rax
movq 0x68(%rbx), %rcx
movsbl 0x900(%rcx), %edx
movsbl 0x901(%rcx), %ecx
movl 0x38(%rbx), %esi
imull %r13d, %esi
leal 0x1(%rdx), %edi
imull %edx, %edi
movl %r12d, %edx
imull %ecx, %edx
addl %r8d, %esi
addl %edx, %esi
addl %edi, %esi
movslq %esi, %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x90(%rbx), %rax
movq (%rax,%r9,8), %rax
mulsd (%rax,%r12,8), %xmm0
movq 0x70(%rbx), %rax
movq 0x78(%rbx), %rcx
movq (%rax,%rbp,8), %rax
mulsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x28(%rsp), %rax
movq (%rcx,%rax,8), %rax
movsd (%rax,%r13,8), %xmm0
callq 0x6046
movq 0x30(%rsp), %r9
movl 0xc(%rsp), %r8d
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x30(%rbx), %rax
movq 0x68(%rbx), %rcx
movsbl 0x900(%rcx), %ecx
movl 0x38(%rbx), %edx
imull %r13d, %edx
imull %r15d, %ecx
addl %r12d, %edx
addl %ecx, %edx
movslq %edx, %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
incq %r12
cmpq %r12, 0x10(%rsp)
jne 0xcc9d
incq %r14
incl %r15d
cmpq 0x20(%rsp), %r14
jne 0xcc9a
jmp 0xce4b
testb %r15b, %r15b
movl 0xc(%rsp), %r8d
jle 0xce4b
xorl %r14d, %r14d
movsd 0x4bb0(%rip), %xmm0     # 0x11950
testb %r8b, %r8b
js 0xcddd
movq 0x28(%rbx), %rax
movq 0x68(%rbx), %rcx
movsbl 0x900(%rcx), %edx
movsbl 0x901(%rcx), %ecx
movl 0x38(%rbx), %esi
imull %r13d, %esi
leal 0x1(%rdx), %edi
imull %edx, %edi
movl %r14d, %edx
imull %ecx, %edx
addl %r8d, %esi
addl %edx, %esi
addl %edi, %esi
movslq %esi, %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x90(%rbx), %rax
movq (%rax,%r9,8), %rax
mulsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x78(%rbx), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r13,8), %xmm0
callq 0x6046
movq 0x30(%rsp), %r9
movl 0xc(%rsp), %r8d
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x30(%rbx), %rax
movslq 0x38(%rbx), %rcx
imulq %r13, %rcx
addq %r14, %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
incq %r14
cmpq %r14, 0x10(%rsp)
jne 0xcd98
movq 0x10(%rsp), %r15
testb %r15b, %r15b
movq 0x28(%rsp), %r12
movl 0x38(%rsp), %ebp
jle 0xcf2b
xorl %r14d, %r14d
movq 0x70(%rbx), %rax
movq (%rax,%r9,8), %rax
movsd (%rax,%r14,8), %xmm0
movq 0x90(%rbx), %rax
movq (%rax,%r9,8), %rax
mulsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x78(%rbx), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%r13,8), %xmm0
movq 0x28(%rbx), %rax
movq 0x68(%rbx), %rcx
movsbl 0x900(%rcx), %edx
movsbl 0x901(%rcx), %ecx
movl 0x38(%rbx), %esi
imull %r13d, %esi
leal 0x1(%rdx), %edi
imull %edx, %edi
imull %r14d, %ecx
addl %ebp, %esi
addl %ecx, %esi
addl %edi, %esi
movslq %esi, %rcx
mulsd (%rax,%rcx,8), %xmm0
callq 0x6046
movq 0x30(%rsp), %r9
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x30(%rbx), %rax
movq 0x68(%rbx), %rcx
movsbl 0x900(%rcx), %edx
movsbl 0x901(%rcx), %ecx
movl 0x38(%rbx), %esi
imull %r13d, %esi
leal 0x1(%rdx), %edi
imull %edx, %edi
imull %r14d, %ecx
addl %ebp, %esi
addl %ecx, %esi
addl %edi, %esi
movslq %esi, %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
incq %r14
cmpq %r14, %r15
jne 0xce65
movq 0x60(%rsp), %rdx
incq %rdx
movsbq 0xb(%rsp), %rax
cmpq %rax, %rdx
movq 0x58(%rsp), %r14
movl 0x3c(%rsp), %ecx
jge 0xcf4f
cmpl %eax, %ecx
je 0xcc44
incq %r14
cmpq 0x40(%rsp), %r14
jne 0xcba2
movsd 0x50(%rsp), %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq