// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rdi, %rbx
movq 0x68(%rdi), %rax
movsbq 0x900(%rax), %rcx
movq %rcx, 0x10(%rsp)
movslq 0x8fc(%rax), %r15
movl 0x8f8(%rax), %ecx
movq %rcx, 0x30(%rsp)
movl 0x908(%rax), %ebp
testq %r15, %r15
jle 0xccb2
movq 0x70(%rbx), %r12
movl 0x10(%rsp), %r14d
shlq $0x3, %r14
xorl %r13d, %r13d
cmpb $0x0, 0x10(%rsp)
jle 0xccaa
movq (%r12,%r13,8), %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2100
incq %r13
cmpq %r13, %r15
jne 0xcc95
imull 0x38(%rbx), %ebp
testl %ebp, %ebp
jle 0xcccb
movq 0x30(%rbx), %rdi
movl %ebp, %edx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2100
movq %rbx, %rdi
xorl %esi, %esi
callq 0xb8ee
movsd %xmm0, 0x40(%rsp)
movq %rbx, %rdi
callq 0xc57e
cmpl $0x0, 0x30(%rsp)
jle 0xd0ad
movzbl 0x10(%rsp), %r10d
xorl %ebp, %ebp
movq %r10, 0x28(%rsp)
movq 0x68(%rbx), %rdi
movq 0x880(%rdi), %rax
movq 0x890(%rdi), %rcx
movzbl (%rax,%rbp), %eax
movl %eax, 0x8(%rsp)
movslq (%rcx,%rbp,4), %rax
movq %rax, 0x18(%rsp)
movl %ebp, %esi
leaq 0x38(%rsp), %rdx
leaq 0x3(%rsp), %rcx
callq 0x7797
movq 0x38(%rsp), %rax
cmpl $0x0, (%rax)
js 0xd09f
movsbl 0x3(%rsp), %ecx
movl %ecx, 0x4(%rsp)
testl %ecx, %ecx
jle 0xd09f
movsbl 0x8(%rsp), %r11d
movq 0x58(%rbx), %rdx
movl 0x4(%rsp), %esi
xorl %edi, %edi
xorl %ecx, %ecx
movslq (%rax,%rdi,4), %r8
xorl %r9d, %r9d
cmpb $0x0, (%rdx,%r8)
setg %r9b
addl %r9d, %ecx
incq %rdi
cmpq %rdi, %rsi
jne 0xcd60
cmpb $0x0, 0x4(%rsp)
jle 0xd09f
cmpl 0x4(%rsp), %ecx
jne 0xd09f
xorl %esi, %esi
movq %rbp, 0x48(%rsp)
movl %r11d, 0xc(%rsp)
movq 0x38(%rsp), %rax
movslq (%rax,%rsi,4), %r14
movq 0x68(%rbx), %rax
movq 0xb0(%rbx), %rcx
movq 0x8b0(%rax), %rdx
movslq (%rdx,%rbp,4), %rdx
movq %rsi, 0x50(%rsp)
movslq %esi, %rsi
addq %rdx, %rsi
movq %rsi, %r12
movslq (%rcx,%rsi,4), %r15
cmpq $-0x1, %r15
je 0xcecb
cmpb $0x0, 0x10(%rsp)
jle 0xd082
xorl %ebp, %ebp
xorl %r13d, %r13d
movl 0x8(%rsp), %r8d
testb %r8b, %r8b
js 0xce25
movq 0x28(%rbx), %rcx
movsbl 0x900(%rax), %edx
movsbl 0x901(%rax), %eax
movl 0x38(%rbx), %esi
imull %r14d, %esi
leal 0x1(%rdx), %edi
imull %edx, %edi
imull %r13d, %eax
addl %r8d, %esi
addl %edi, %esi
addl %eax, %esi
movslq %esi, %rax
movsd (%rcx,%rax,8), %xmm0
jmp 0xce2d
movsd 0x5b23(%rip), %xmm0     # 0x12950
movq 0x18(%rsp), %rdx
movq 0x90(%rbx), %rax
movq (%rax,%r12,8), %rax
mulsd (%rax,%r13,8), %xmm0
movq 0x70(%rbx), %rax
movq 0x78(%rbx), %rcx
movq (%rax,%r15,8), %rax
mulsd (%rax,%rbp,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movq (%rcx,%rdx,8), %rax
movsd (%rax,%r14,8), %xmm0
callq 0x6158
movsd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x30(%rbx), %rcx
movq 0x68(%rbx), %rax
movsbl 0x900(%rax), %edx
movl 0x38(%rbx), %esi
imull %r14d, %esi
addl %r13d, %esi
addl %edx, %esi
imull %ebp, %edx
addl %esi, %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
incq %r13
movq 0x28(%rsp), %r10
cmpq %r13, %r10
movl 0xc(%rsp), %r11d
jne 0xcde7
incq %rbp
cmpq %r10, %rbp
jne 0xcde4
jmp 0xcf91
cmpb $0x0, 0x10(%rsp)
jle 0xd082
xorl %r15d, %r15d
movl 0x8(%rsp), %r8d
testb %r8b, %r8b
js 0xcf1b
movq 0x28(%rbx), %rax
movq 0x68(%rbx), %rcx
movsbl 0x900(%rcx), %edx
movsbl 0x901(%rcx), %ecx
movl 0x38(%rbx), %esi
imull %r14d, %esi
leal 0x1(%rdx), %edi
imull %edx, %edi
imull %r15d, %ecx
addl %r8d, %esi
addl %ecx, %esi
addl %edi, %esi
movslq %esi, %rcx
movsd (%rax,%rcx,8), %xmm0
jmp 0xcf23
movsd 0x5a2d(%rip), %xmm0     # 0x12950
movq 0x18(%rsp), %rcx
movq 0x90(%rbx), %rax
movq (%rax,%r12,8), %rax
mulsd (%rax,%r15,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x78(%rbx), %rax
movq (%rax,%rcx,8), %rax
movsd (%rax,%r14,8), %xmm0
callq 0x6158
movsd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x30(%rbx), %rax
movl 0x38(%rbx), %ecx
imull %r14d, %ecx
addl %r15d, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
incq %r15
movq 0x28(%rsp), %r10
cmpq %r15, %r10
movl 0xc(%rsp), %r11d
jne 0xced9
cmpb $0x0, 0x10(%rsp)
movq 0x48(%rsp), %rbp
movq 0x18(%rsp), %r13
jle 0xd082
movq 0x68(%rbx), %rax
movb 0x900(%rax), %dl
movb 0x901(%rax), %cl
movl 0x38(%rbx), %eax
xorl %r15d, %r15d
movq 0x70(%rbx), %rsi
movq (%rsi,%r12,8), %rsi
movsd (%rsi,%r15,8), %xmm0
movq 0x90(%rbx), %rsi
movq (%rsi,%r12,8), %rsi
mulsd (%rsi,%r15,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x28(%rbx), %rsi
movq 0x78(%rbx), %rdi
movq (%rdi,%r13,8), %rdi
movsd (%rdi,%r14,8), %xmm0
imull %r14d, %eax
movsbl %dl, %edx
leal 0x1(%rdx), %edi
imull %edx, %edi
movsbl %cl, %ecx
imull %r15d, %ecx
addl %r11d, %eax
addl %ecx, %eax
addl %edi, %eax
cltq
mulsd (%rsi,%rax,8), %xmm0
callq 0x6158
movl 0xc(%rsp), %r11d
movq 0x28(%rsp), %r10
movsd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x30(%rbx), %rsi
movq 0x68(%rbx), %rax
movsbl 0x900(%rax), %edx
movsbl 0x901(%rax), %ecx
movl 0x38(%rbx), %eax
movl %eax, %edi
imull %r14d, %edi
leal 0x1(%rdx), %r8d
imull %edx, %r8d
movl %ecx, %r9d
imull %r15d, %r9d
addl %r11d, %edi
addl %r9d, %edi
addl %r8d, %edi
movslq %edi, %rdi
movsd (%rsi,%rdi,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %r15
cmpq %r15, %r10
jne 0xcfbc
movq 0x50(%rsp), %rsi
incq %rsi
movsbq 0x3(%rsp), %rax
cmpq %rax, %rsi
jge 0xd09f
cmpb %al, 0x4(%rsp)
je 0xcd9c
incq %rbp
cmpq 0x30(%rsp), %rbp
jne 0xccfb
movsd 0x40(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq