// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/HMMProblemPiGKww.cpp
// Function: HMMProblemPiGKww::setGradWW(FitBit*)
void HMMProblemPiGKww::setGradWW(FitBit *fb){
    if(this->p->block_fitting[0]>0) return;
    NDAT t = 0, ndat = 0;
    NPAR i, o;
    NUMBER combined, deriv_logit0, deriv_logit1/*, deriv_logit2*/;
    //    o = dt->obs[t];
    struct data* dt;
    for(NCAT x=0; x<fb->xndat; x++) {
        dt = fb->x_data[x];
        if( dt->cnt!=0 ) continue;
        ndat += dt->n;
        o = this->p->dat_obs[ dt->ix[t] ];//->get( dt->ix[t] );
        for(i=0; i<fb->nS; i++) {
            combined = getPI(dt,i);//sigmoid( logit(this->pi[k][i]) + logit(this->PIg[g][i]) );
            
//            // a logit(k) + b logit(u)
//            fb->gradPI[0] -= combined * (1-combined) * logit( this->PI [ dt->k ][i] ) * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param);
//            fb->gradPI[1] -= combined * (1-combined) * logit( this->PIg[ dt->g ][i] ) * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param);
//            fb->gradPI[2] -= combined * (1-combined) * 1                              * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param);
            
            
            // logit1(a) logit(k) + logit1(b) logit(u)
            deriv_logit0 = 1/ safe0num( this->ww[0] * (1-this->ww[0]) );
            deriv_logit1 = 1/ safe0num( this->ww[1] * (1-this->ww[1]) );
//            deriv_logit2 = 1/ safe0num( -0.25 + this->ww[2] * this->ww[2] );
            fb->gradPI[0] -= combined * (1-combined) * deriv_logit0 * logit( this->pi [ dt->k ][i] ) * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param);
            fb->gradPI[1] -= combined * (1-combined) * deriv_logit1 * logit( this->PIg[ dt->g ][i] ) * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param);
            fprintf(stderr,"Fix L2penalty here too");
//            fb->gradPI[2] -= combined * (1-combined) * deriv_logit2 * 1                              * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param);
            
//            // x / sqrt( 1 + x^2 )
//            deriv_logit0 = sqrt(3) / safe0num(pow( 1 - pow(this->ww[0]-0.5,2), 1.5 ));
//            deriv_logit1 = sqrt(3) / safe0num(pow( 1 - pow(this->ww[1]-0.5,2), 1.5 ));
////            deriv_logit2 = sqrt(3) / safe0num(pow( 1 - pow(this->ww[2]-0.5,2), 1.5 ));
//            fb->gradPI[0] -= combined * (1-combined) * deriv_logit0 * logit( this->PI [ dt->k ][i] ) * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param);
//            fb->gradPI[1] -= combined * (1-combined) * deriv_logit1 * logit( this->PIg[ dt->g ][i] ) * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param);
////            fb->gradPI[2] -= combined * (1-combined) * deriv_logit2 * 1                              * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq 0x88(%rdi), %rax
cmpb $0x0, 0x981(%rax)
jle 0x17a3d
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rbx
movq %rdi, %r14
xorl %r12d, %r12d
movslq 0xe8(%rbx), %rax
cmpq %rax, %r12
jge 0x17a2e
movq 0xf0(%rbx), %rax
movq (%rax,%r12,8), %rbp
cmpl $0x0, 0x4(%rbp)
je 0x17a68
incq %r12
jmp 0x17a46
movq 0x88(%r14), %rax
movq 0x870(%rax), %rax
movq 0x10(%rbp), %rcx
movslq (%rcx), %rcx
movb (%rax,%rcx), %r13b
movsbl %r13b, %eax
movl %eax, 0x1c(%rsp)
xorl %r15d, %r15d
movsbq 0x1(%rbx), %rax
cmpq %rax, %r15
jge 0x17a63
movq (%r14), %rax
movsbl %r15b, %edx
movq %r14, %rdi
movq %rbp, %rsi
callq *0x10(%rax)
movsd %xmm0, 0x8(%rsp)
movq 0x98(%r14), %rax
movsd (%rax), %xmm1
movsd 0x14e31(%rip), %xmm0    # 0x2c8f0
subsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
callq 0x67cb
movsd %xmm0, 0x10(%rsp)
movq 0x98(%r14), %rax
movsd 0x8(%rax), %xmm1
movsd 0x14e0a(%rip), %xmm0    # 0x2c8f0
subsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
callq 0x67cb
movsd %xmm0, 0x20(%rsp)
movq 0x38(%r14), %rax
movslq 0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r15,8), %xmm0
callq 0x6845
movsd 0x14dd8(%rip), %xmm3    # 0x2c8f0
movapd %xmm3, %xmm2
divsd 0x10(%rsp), %xmm2
movapd %xmm3, %xmm1
movsd 0x8(%rsp), %xmm4
subsd %xmm4, %xmm1
mulsd %xmm4, %xmm1
movsd %xmm1, 0x8(%rsp)
mulsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
movq 0x30(%rbp), %rax
movq (%rax), %rax
mulsd (%rax,%r15,8), %xmm2
movsd %xmm2, 0x10(%rsp)
movapd %xmm3, %xmm0
testb %r13b, %r13b
js 0x17b72
movq (%r14), %rax
movsbl %r15b, %edx
movq %r14, %rdi
movq %rbp, %rsi
movl 0x1c(%rsp), %ecx
callq *0x20(%rax)
movsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x14d66(%rip), %xmm0    # 0x2c8f0
divsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x48(%rbp), %xmm0
callq 0x67cb
movsd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x58(%rbx), %rax
movsd (%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movq 0x90(%r14), %rax
movslq 0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r15,8), %xmm0
callq 0x6845
movsd 0x8(%rsp), %xmm1
mulsd 0x20(%rsp), %xmm1
mulsd %xmm0, %xmm1
movq 0x30(%rbp), %rax
movq (%rax), %rax
mulsd (%rax,%r15,8), %xmm1
movsd %xmm1, 0x8(%rsp)
movsd 0x14cf1(%rip), %xmm0    # 0x2c8f0
testb %r13b, %r13b
js 0x17c18
movq (%r14), %rax
movsbl %r15b, %edx
movq %r14, %rdi
movq %rbp, %rsi
movl 0x1c(%rsp), %ecx
callq *0x20(%rax)
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd 0x48(%rbp), %xmm0
callq 0x67cb
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x58(%rbx), %rax
movsd 0x8(%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
movq 0x21383(%rip), %rax     # 0x38fd8
movq (%rax), %rcx
leaq 0x15de4(%rip), %rdi     # 0x2da43
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x44b0
incq %r15
jmp 0x17a8c