// File: /workspace/github_cpp_makefile_star2_O1/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)
jg 0x1c239
movq %rsi, %rbx
cmpl $0x0, 0xe8(%rsi)
jle 0x1c239
movq %rdi, %r14
xorl %r13d, %r13d
movq 0xf0(%rbx), %rax
movq (%rax,%r13,8), %r12
cmpl $0x0, 0x4(%r12)
jne 0x1c226
cmpb $0x0, 0x1(%rbx)
jle 0x1c226
movq 0x88(%r14), %rax
movq 0x870(%rax), %rax
movq 0x10(%r12), %rcx
movslq (%rcx), %rcx
movb (%rax,%rcx), %r15b
movsbl %r15b, %eax
movl %eax, 0x1c(%rsp)
xorl %ebp, %ebp
movq (%r14), %rax
movsbl %bpl, %edx
movq %r14, %rdi
movq %r12, %rsi
callq *0x10(%rax)
movsd %xmm0, 0x8(%rsp)
movq 0x98(%r14), %rax
movsd (%rax), %xmm1
movsd 0x1888e(%rip), %xmm0    # 0x348f0
subsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
callq 0x8d88
movsd %xmm0, 0x10(%rsp)
movq 0x98(%r14), %rax
movsd 0x8(%rax), %xmm1
movsd 0x18867(%rip), %xmm0    # 0x348f0
subsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
callq 0x8d88
movsd %xmm0, 0x20(%rsp)
movq 0x38(%r14), %rax
movslq 0x58(%r12), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%rbp,8), %xmm0
callq 0x8e13
movsd 0x18835(%rip), %xmm3    # 0x348f0
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(%r12), %rax
movq (%rax), %rax
mulsd (%rax,%rbp,8), %xmm2
movapd %xmm3, %xmm0
testb %r15b, %r15b
js 0x1c11b
movq (%r14), %rax
movsbl %bpl, %edx
movq %r14, %rdi
movq %r12, %rsi
movl 0x1c(%rsp), %ecx
movsd %xmm2, 0x10(%rsp)
callq *0x20(%rax)
movsd 0x10(%rsp), %xmm2
mulsd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
movsd 0x187c3(%rip), %xmm0    # 0x348f0
divsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x48(%r12), %xmm0
callq 0x8d88
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(%r12), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%rbp,8), %xmm0
callq 0x8e13
movsd 0x8(%rsp), %xmm1
mulsd 0x20(%rsp), %xmm1
mulsd %xmm0, %xmm1
movq 0x30(%r12), %rax
movq (%rax), %rax
mulsd (%rax,%rbp,8), %xmm1
movsd 0x18752(%rip), %xmm0    # 0x348f0
testb %r15b, %r15b
js 0x1c1c3
movq (%r14), %rax
movsbl %bpl, %edx
movq %r14, %rdi
movq %r12, %rsi
movl 0x1c(%rsp), %ecx
movsd %xmm1, 0x8(%rsp)
callq *0x20(%rax)
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd 0x48(%r12), %xmm0
callq 0x8d88
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x58(%rbx), %rax
movsd 0x8(%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
movq 0x23ddc(%rip), %rax     # 0x3ffd8
movq (%rax), %rcx
movl $0x16, %esi
movl $0x1, %edx
leaq 0x19791(%rip), %rdi     # 0x359a1
callq 0x4480
incq %rbp
movsbq 0x1(%rbx), %rax
cmpq %rax, %rbp
jl 0x1c039
incq %r13
movslq 0xe8(%rbx), %rax
cmpq %rax, %r13
jl 0x1bff4
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq