// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
jle 0x2b323
jmp 0x2b665
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xe8(%rcx), %eax
jge 0x2b665
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rax
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x2b371
jmp 0x2b657
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
movl (%rcx), %ecx
addl -0x18(%rbp), %ecx
movl %ecx, -0x18(%rbp)
movq 0x88(%rax), %rax
movq 0x870(%rax), %rax
movq -0x40(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x14(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movb (%rax,%rcx), %al
movb %al, -0x1a(%rbp)
movb $0x0, -0x19(%rbp)
movsbl -0x19(%rbp), %eax
movq -0x10(%rbp), %rcx
movsbl 0x1(%rcx), %ecx
cmpl %ecx, %eax
jge 0x2b655
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rsi
movb -0x19(%rbp), %cl
movq (%rdi), %rax
movsbl %cl, %edx
callq *0x10(%rax)
movq -0x50(%rbp), %rax
movsd %xmm0, -0x28(%rbp)
movq 0x98(%rax), %rcx
movsd (%rcx), %xmm0
movq 0x98(%rax), %rax
movsd 0x24614(%rip), %xmm1    # 0x4fa08
subsd (%rax), %xmm1
mulsd %xmm1, %xmm0
callq 0x9a40
movq -0x50(%rbp), %rax
movaps %xmm0, %xmm1
movsd 0x245f8(%rip), %xmm0    # 0x4fa08
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movq 0x98(%rax), %rcx
movsd 0x8(%rcx), %xmm0
movq 0x98(%rax), %rax
movsd 0x245d4(%rip), %xmm1    # 0x4fa08
subsd 0x8(%rax), %xmm1
mulsd %xmm1, %xmm0
callq 0x9a40
movq -0x50(%rbp), %rax
movaps %xmm0, %xmm1
movsd 0x245b7(%rip), %xmm0    # 0x4fa08
divsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x28(%rbp), %xmm0
movsd 0x245a1(%rip), %xmm1    # 0x4fa08
subsd -0x28(%rbp), %xmm1
mulsd %xmm1, %xmm0
mulsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movq 0x38(%rax), %rax
movq -0x40(%rbp), %rcx
movslq 0x58(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x19(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x9b80
movaps %xmm0, %xmm1
movsd -0x60(%rbp), %xmm0
mulsd %xmm1, %xmm0
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x19(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x58(%rbp)
movsbl -0x1a(%rbp), %eax
cmpl $0x0, %eax
jge 0x2b4dc
movsd 0x24533(%rip), %xmm0    # 0x4fa08
movsd %xmm0, -0x68(%rbp)
jmp 0x2b4fb
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rsi
movb -0x19(%rbp), %dl
movb -0x1a(%rbp), %cl
movq (%rdi), %rax
movsbl %dl, %edx
movsbl %cl, %ecx
callq *0x20(%rax)
movsd %xmm0, -0x68(%rbp)
movsd -0x58(%rbp), %xmm0
movsd -0x68(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x40(%rbp), %rax
movsd 0x48(%rax), %xmm0
callq 0x9a40
movsd -0x80(%rbp), %xmm1
movq -0x50(%rbp), %rax
divsd %xmm0, %xmm1
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
movsd (%rcx), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rcx)
movsd -0x28(%rbp), %xmm0
movsd 0x244be(%rip), %xmm1    # 0x4fa08
subsd -0x28(%rbp), %xmm1
mulsd %xmm1, %xmm0
mulsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movq 0x90(%rax), %rax
movq -0x40(%rbp), %rcx
movslq 0x5c(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x19(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x9b80
movaps %xmm0, %xmm1
movsd -0x78(%rbp), %xmm0
mulsd %xmm1, %xmm0
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x19(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x70(%rbp)
movsbl -0x1a(%rbp), %eax
cmpl $0x0, %eax
jge 0x2b5c5
movsd 0x2444d(%rip), %xmm0    # 0x4fa08
movsd %xmm0, -0x88(%rbp)
jmp 0x2b5e7
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rsi
movb -0x19(%rbp), %dl
movb -0x1a(%rbp), %cl
movq (%rdi), %rax
movsbl %dl, %edx
movsbl %cl, %ecx
callq *0x20(%rax)
movsd %xmm0, -0x88(%rbp)
movsd -0x70(%rbp), %xmm0
movsd -0x88(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x40(%rbp), %rax
movsd 0x48(%rax), %xmm0
callq 0x9a40
movsd -0x90(%rbp), %xmm1
divsd %xmm0, %xmm1
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movsd 0x8(%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
movq 0x2f9a1(%rip), %rax     # 0x5afd8
movq (%rax), %rdi
leaq 0x25434(%rip), %rsi     # 0x50a75
movb $0x0, %al
callq 0x5440
movb -0x19(%rbp), %al
addb $0x1, %al
movb %al, -0x19(%rbp)
jmp 0x2b3a9
jmp 0x2b657
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2b338
addq $0x90, %rsp
popq %rbp
retq
nop