// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %rdi, %rbx
movq 0x68(%rdi), %rax
movq 0x70(%rdi), %rdi
movzbl 0x900(%rax), %r14d
movl 0x8f8(%rax), %r15d
movl 0x8fc(%rax), %esi
movl 0x908(%rax), %ebp
movsbl %r14b, %edx
callq 0xed66
movq 0x30(%rbx), %rdi
imull 0x38(%rbx), %ebp
movl %ebp, %esi
callq 0xedbf
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %esi, %esi
callq 0xa6be
movsd %xmm0, 0x50(%rsp)
movq %rbx, %rdi
callq 0xb29c
testb %r14b, %r14b
movl $0x0, %ebp
movq %r14, 0x70(%rsp)
cmovgl %r14d, %ebp
testl %r15d, %r15d
cmovlel %r12d, %r15d
movq %r15, 0x58(%rsp)
movq %rbp, 0x40(%rsp)
cmpq 0x58(%rsp), %r12
je 0xbcc8
movq 0x68(%rbx), %rdi
movq 0x880(%rdi), %rax
movq 0x890(%rdi), %rcx
movb (%rax,%r12), %al
movb %al, 0xe(%rsp)
movslq (%rcx,%r12,4), %rax
movq %rax, 0x28(%rsp)
movl %r12d, %esi
leaq 0x48(%rsp), %rdx
leaq 0xf(%rsp), %rcx
callq 0x6a19
movq 0x48(%rsp), %rcx
cmpl $0x0, (%rcx)
js 0xbcc0
movzbl 0xf(%rsp), %eax
movq 0x58(%rbx), %rdx
xorl %esi, %esi
testb %al, %al
movl %eax, %edi
cmovlel %esi, %edi
movzbl %dil, %edi
xorl %r10d, %r10d
cmpq %rsi, %rdi
je 0xb9d3
movslq (%rcx,%rsi,4), %r8
xorl %r9d, %r9d
cmpb $0x0, (%rdx,%r8)
setg %r9b
addl %r9d, %r10d
incq %rsi
jmp 0xb9b6
xorl %edx, %edx
movq %r12, 0x60(%rsp)
movl %r10d, 0x3c(%rsp)
movsbq %al, %rcx
cmpq %rcx, %rdx
jge 0xbcc0
movsbl %al, %eax
cmpl %eax, %r10d
jne 0xbcc0
movq 0x48(%rsp), %rax
movslq (%rax,%rdx,4), %r13
movq 0x68(%rbx), %rax
movq 0xb0(%rbx), %rcx
movq 0x8b0(%rax), %rax
movslq (%rax,%r12,4), %rax
movq %rdx, 0x68(%rsp)
movslq %edx, %rdx
addq %rax, %rdx
movq %rdx, 0x30(%rsp)
movslq (%rcx,%rdx,4), %rcx
movsbl 0xe(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq %rcx, 0x20(%rsp)
cmpq $-0x1, %rcx
je 0xbb35
pushq $0x1
popq %r14
xorl %r12d, %r12d
cmpq %rbp, %r12
je 0xbbe2
xorl %ebp, %ebp
cmpq %rbp, 0x70(%rsp)
je 0xbb25
movq 0x90(%rbx), %rax
movq 0x30(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%rbp,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x4ecd(%rip), %xmm0     # 0x10950
cmpb $0x0, 0xe(%rsp)
js 0xbaa9
movq 0x28(%rbx), %r15
movsbl %bpl, %edx
movq %rbx, %rdi
movl %r13d, %esi
movl 0x1c(%rsp), %ecx
callq 0xa35c
cltq
movsd (%r15,%rax,8), %xmm0
movsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movq 0x70(%rbx), %rax
movq 0x78(%rbx), %rcx
movq 0x20(%rsp), %rdx
movq (%rax,%rdx,8), %rax
mulsd (%rax,%r12,8), %xmm1
movsd %xmm1, 0x10(%rsp)
movq 0x28(%rsp), %rax
movq (%rcx,%rax,8), %rax
movsd (%rax,%r13,8), %xmm0
callq 0x55a1
movsd 0x10(%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 %r14d, %ecx
addl %ebp, %edx
addl %ecx, %edx
movslq %edx, %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
incq %rbp
jmp 0xba55
incq %r12
incl %r14d
movq 0x40(%rsp), %rbp
jmp 0xba4a
xorl %r14d, %r14d
cmpq %r14, %rbp
je 0xbbe2
movq 0x90(%rbx), %rax
movq 0x30(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x4deb(%rip), %xmm0     # 0x10950
cmpb $0x0, 0xe(%rsp)
js 0xbb8b
movq 0x28(%rbx), %r15
movsbl %r14b, %edx
movq %rbx, %rdi
movl %r13d, %esi
movl 0x1c(%rsp), %ecx
callq 0xa35c
cltq
movsd (%r15,%rax,8), %xmm0
movsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq 0x78(%rbx), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r13,8), %xmm0
callq 0x55a1
movsd 0x10(%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
jmp 0xbb38
xorl %r14d, %r14d
cmpq %r14, %rbp
je 0xbca5
movq 0x70(%rbx), %rax
movq 0x30(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r14,8), %xmm0
movq 0x90(%rbx), %rax
movq (%rax,%rcx,8), %rax
mulsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x28(%rbx), %r12
movq 0x78(%rbx), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r13,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movsbl %r14b, %r15d
movq %rbx, %rdi
movl %r13d, %esi
movl %r15d, %edx
movl 0x1c(%rsp), %ebp
movl %ebp, %ecx
callq 0xa35c
cltq
movsd 0x20(%rsp), %xmm0
mulsd (%r12,%rax,8), %xmm0
callq 0x55a1
movsd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq 0x30(%rbx), %r12
movq %rbx, %rdi
movl %r13d, %esi
movl %r15d, %edx
movl %ebp, %ecx
movq 0x40(%rsp), %rbp
callq 0xa35c
cltq
movsd (%r12,%rax,8), %xmm0
subsd 0x10(%rsp), %xmm0
movsd %xmm0, (%r12,%rax,8)
incq %r14
jmp 0xbbe5
movq 0x68(%rsp), %rdx
incq %rdx
movb 0xf(%rsp), %al
movq 0x60(%rsp), %r12
movl 0x3c(%rsp), %r10d
jmp 0xb9df
incq %r12
jmp 0xb94f
movsd 0x50(%rsp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop