// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq 0x68(%rax), %rcx
movb 0x900(%rcx), %cl
movb %cl, -0x9(%rbp)
movb $-0x1, -0xa(%rbp)
movq 0x68(%rax), %rcx
movl 0x8fc(%rcx), %ecx
movl %ecx, -0x10(%rbp)
movq 0x68(%rax), %rcx
movl 0x8f8(%rcx), %ecx
movl %ecx, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movq 0x68(%rax), %rcx
movl 0x908(%rcx), %ecx
movl %ecx, -0x24(%rbp)
movq 0x70(%rax), %rdi
movl -0x10(%rbp), %esi
movsbl -0x9(%rbp), %edx
callq 0x19280
movl $0x14, %edi
callq 0x3270
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, -0x30(%rbp)
movq 0x30(%rax), %rdi
movl 0x38(%rax), %esi
imull -0x24(%rbp), %esi
callq 0x19350
movq -0x68(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x12530
movq -0x68(%rbp), %rdi
movsd %xmm0, -0x38(%rbp)
callq 0x13630
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x144ad
movq -0x68(%rbp), %rax
movq 0x68(%rax), %rcx
movq 0x880(%rcx), %rcx
movslq -0x3c(%rbp), %rdx
movb (%rcx,%rdx), %cl
movb %cl, -0xa(%rbp)
movq 0x68(%rax), %rcx
movq 0x890(%rcx), %rcx
movslq -0x3c(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x1c(%rbp)
movl -0x1c(%rbp), %edx
movq -0x30(%rbp), %rcx
movl %edx, 0x4(%rcx)
movb -0xa(%rbp), %dl
movq -0x30(%rbp), %rcx
movb %dl, 0x10(%rcx)
movl -0x3c(%rbp), %edx
movq -0x30(%rbp), %rcx
movl %edx, 0xc(%rcx)
movq 0x68(%rax), %rdi
movl -0x3c(%rbp), %esi
leaq -0x48(%rbp), %rdx
leaq -0x49(%rbp), %rcx
callq 0x9ce0
movq -0x48(%rbp), %rax
cmpl $0x0, (%rax)
jge 0x14017
jmp 0x1449f
movl $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movsbl -0x49(%rbp), %ecx
cmpl %ecx, %eax
jge 0x14064
movq -0x68(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
movslq -0x54(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
setg %al
andb $0x1, %al
movzbl %al, %eax
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x14025
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %ecx
movsbl -0x49(%rbp), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x69(%rbp)
jge 0x1408a
movl -0x50(%rbp), %eax
movsbl -0x49(%rbp), %ecx
cmpl %ecx, %eax
sete %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x14096
jmp 0x1449d
movq -0x68(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq -0x58(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x20(%rbp)
movl -0x20(%rbp), %edx
movq -0x30(%rbp), %rcx
movl %edx, (%rcx)
movl -0x58(%rbp), %ecx
movq 0x68(%rax), %rdx
movq 0x8b0(%rdx), %rdx
movslq -0x3c(%rbp), %rsi
addl (%rdx,%rsi,4), %ecx
movl %ecx, -0x5c(%rbp)
movq 0xb0(%rax), %rax
movslq -0x5c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x60(%rbp)
cmpl $-0x1, -0x60(%rbp)
jne 0x14215
cmpl $0x0, -0x20(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movb $0x0, -0x4a(%rbp)
movsbl -0x4a(%rbp), %eax
movsbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
jge 0x14210
movq -0x68(%rbp), %rax
movq 0x90(%rax), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x4a(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x78(%rbp)
movsbl -0xa(%rbp), %eax
cmpl $0x0, %eax
jge 0x14144
movsd 0x7903(%rip), %xmm0     # 0x1ba40
movsd %xmm0, -0x80(%rbp)
jmp 0x1417b
movq -0x68(%rbp), %rdi
movq 0x28(%rdi), %rax
movq %rax, -0x88(%rbp)
movl -0x20(%rbp), %esi
movb -0x4a(%rbp), %al
movsbl %al, %edx
movsbl -0xa(%rbp), %ecx
callq 0x11ec0
movl %eax, %ecx
movq -0x88(%rbp), %rax
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x68(%rbp), %rax
movsd -0x78(%rbp), %xmm0
movsd -0x80(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
movq 0x78(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x75f0
movq -0x68(%rbp), %rdi
movaps %xmm0, %xmm1
movsd -0xa0(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movq 0x30(%rdi), %rax
movq %rax, -0x90(%rbp)
movl -0x20(%rbp), %esi
movsbl -0x4a(%rbp), %edx
callq 0x11e40
movsd -0x98(%rbp), %xmm1
movl %eax, %ecx
movq -0x90(%rbp), %rax
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x4a(%rbp), %al
addb $0x1, %al
movb %al, -0x4a(%rbp)
jmp 0x140fa
jmp 0x1437f
movb $0x0, -0x4a(%rbp)
movsbl -0x4a(%rbp), %eax
movsbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1437d
movb $0x0, -0x4b(%rbp)
movsbl -0x4b(%rbp), %eax
movsbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1436e
movq -0x68(%rbp), %rax
movq 0x90(%rax), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x4b(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsbl -0xa(%rbp), %eax
cmpl $0x0, %eax
jge 0x1427d
movsd 0x77cd(%rip), %xmm0     # 0x1ba40
movsd %xmm0, -0xb0(%rbp)
jmp 0x142b7
movq -0x68(%rbp), %rdi
movq 0x28(%rdi), %rax
movq %rax, -0xb8(%rbp)
movl -0x20(%rbp), %esi
movb -0x4b(%rbp), %al
movsbl %al, %edx
movsbl -0xa(%rbp), %ecx
callq 0x11ec0
movl %eax, %ecx
movq -0xb8(%rbp), %rax
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xb0(%rbp)
movq -0x68(%rbp), %rax
movsd -0xa8(%rbp), %xmm0
movsd -0xb0(%rbp), %xmm1
mulsd %xmm1, %xmm0
movq 0x70(%rax), %rcx
movslq -0x60(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsbq -0x4a(%rbp), %rdx
mulsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, -0xd0(%rbp)
movq 0x78(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x75f0
movq -0x68(%rbp), %rdi
movaps %xmm0, %xmm1
movsd -0xd0(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0xc8(%rbp)
movq 0x30(%rdi), %rax
movq %rax, -0xc0(%rbp)
movl -0x20(%rbp), %esi
movb -0x4a(%rbp), %al
movsbl %al, %edx
movsbl -0x4b(%rbp), %ecx
callq 0x11e70
movsd -0xc8(%rbp), %xmm1
movl %eax, %ecx
movq -0xc0(%rbp), %rax
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x4b(%rbp), %al
addb $0x1, %al
movb %al, -0x4b(%rbp)
jmp 0x1422d
jmp 0x14370
movb -0x4a(%rbp), %al
addb $0x1, %al
movb %al, -0x4a(%rbp)
jmp 0x14219
jmp 0x1437f
movb $0x0, -0x4a(%rbp)
movsbl -0x4a(%rbp), %eax
movsbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1448d
movq -0x68(%rbp), %rdi
movq 0x70(%rdi), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x4a(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x90(%rdi), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x4a(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xe8(%rbp)
movq 0x78(%rdi), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x20(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xf8(%rbp)
movq 0x28(%rdi), %rax
movq %rax, -0xf0(%rbp)
movl -0x20(%rbp), %esi
movb -0x4a(%rbp), %al
movsbl %al, %edx
movsbl -0xa(%rbp), %ecx
callq 0x11ec0
movsd -0xf8(%rbp), %xmm0
movl %eax, %ecx
movq -0xf0(%rbp), %rax
movslq %ecx, %rcx
mulsd (%rax,%rcx,8), %xmm0
callq 0x75f0
movq -0x68(%rbp), %rdi
movaps %xmm0, %xmm1
movsd -0xe8(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0xe0(%rbp)
movq 0x30(%rdi), %rax
movq %rax, -0xd8(%rbp)
movl -0x20(%rbp), %esi
movb -0x4a(%rbp), %al
movsbl %al, %edx
movsbl -0xa(%rbp), %ecx
callq 0x11ec0
movsd -0xe0(%rbp), %xmm1
movl %eax, %ecx
movq -0xd8(%rbp), %rax
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x4a(%rbp), %al
addb $0x1, %al
movb %al, -0x4a(%rbp)
jmp 0x14383
jmp 0x1448f
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x1406b
jmp 0x1449f
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x13f9d
movq -0x30(%rbp), %rax
movq %rax, -0x100(%rbp)
cmpq $0x0, %rax
je 0x144cf
movq -0x100(%rbp), %rdi
movl $0x14, %esi
callq 0x3280
movsd -0x38(%rbp), %xmm0
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax)