// File: myudelson[P]hmm-scalable/HMMProblemPiGKww.cpp
// Function: HMMProblemPiGKww::setGradPI(FitBit*)
void HMMProblemPiGKww::setGradPI(FitBit *fb){
    if(this->p->block_fitting[0]>0) return;
    NDAT t = 0, ndat = 0;
    NPAR i, o;
    NUMBER combined, deriv_logit;
    //    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] );
    
//    // a logit(k) + b logit(u)
//    if(kg_flag == 0) { // k
//        for(i=0; i<this->p->nS; i++) {
//            combined = getPI(dt,i);//sigmoid( logit(this->pi[k][i]) + logit(this->PIg[g][i]) );
//            deriv_logit = this->ww[0] / safe0num( this->pi[ dt->k ][i] * ( 1 - this->pi[ dt->k ][i] ) );
//			fb->gradPI[i] -= combined * (1-combined) * deriv_logit * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param) + L2penalty(this->p,this->pi[ dt->k ][i], 0.5); // PENALTY;
//        }
//    }
//    else // g
//        for(i=0; i<this->p->nS; i++) {
//            combined = getPI(dt,i);
//            deriv_logit = this->ww[1] / safe0num( this->PIg[ dt->g ][i] * ( 1 - this->PIg[ dt->g ][i] ) );
//			fb->gradPI[i] -= combined * (1-combined) * deriv_logit * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param) + L2penalty(this->p,this->PIg[ dt->g ][i], 0.5); // PENALTY;
//        }
    
    // logit1(a) logit(k) + logit1(b) logit(u)
        for(i=0; i<fb->nS; i++) {
            combined = getPI(dt,i);//sigmoid( logit(this->pi[k][i]) + logit(this->PIg[g][i]) );
            deriv_logit = (1+logit(this->ww[0])) / safe0num( fb->pi[i] * ( 1 - fb->pi[i] ) );
			fb->gradPI[i] -= combined * (1-combined) * deriv_logit * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param) +
                L2penalty(fb->Cslice,fb->pi[i], 0.5); // PENALTY;
            fprintf(stderr,"Fix L2penalty here");
        }
    }
    
//    // x / sqrt(1+ x^2)
//    if(kg_flag == 0) { // k
//        for(i=0; i<this->p->nS; i++) {
//            combined = getPI(dt,i);//sigmoid( logit(this->pi[k][i]) + logit(this->PIg[g][i]) );
//            deriv_logit = (1+sqrt(3)*(this->ww[0]-0.5)/sqrt(1-pow(this->ww[0]-0.5,2))) / safe0num( this->pi[ dt->k ][i] * ( 1 - this->pi[ dt->k ][i] ) );
//			fb->gradPI[i] -= combined * (1-combined) * deriv_logit * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param) + L2penalty(this->p,this->pi[ dt->k ][i], 0.5); // PENALTY;
//        }
//    }
//    else // g
//        for(i=0; i<this->p->nS; i++) {
//            combined = getPI(dt,i);
//            deriv_logit = (1+sqrt(3)*(this->ww[1]-0.5)/sqrt(1-pow(this->ww[1]-0.5,2))) / safe0num( this->PIg[ dt->g ][i] * ( 1 - this->PIg[ dt->g ][i] ) );
//			fb->gradPI[i] -= combined * (1-combined) * deriv_logit * dt->beta[t][i] * ((o<0)?1:getB(dt,i,o)) / safe0num(dt->p_O_param) + L2penalty(this->p,this->PIg[ dt->g ][i], 0.5); // PENALTY;
//        }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
jle 0x2b090
jmp 0x2b2db
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xe8(%rcx), %eax
jge 0x2b2db
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x2b0de
jmp 0x2b2cd
movq -0x48(%rbp), %rax
movq -0x38(%rbp), %rcx
movl (%rcx), %ecx
addl -0x18(%rbp), %ecx
movl %ecx, -0x18(%rbp)
movq 0x88(%rax), %rax
movq 0x870(%rax), %rax
movq -0x38(%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 0x2b2cb
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rsi
movb -0x19(%rbp), %cl
movq (%rdi), %rax
movsbl %cl, %edx
callq *0x10(%rax)
movq -0x48(%rbp), %rax
movsd %xmm0, -0x28(%rbp)
movq 0x98(%rax), %rax
movsd (%rax), %xmm0
callq 0x9b80
movaps %xmm0, %xmm1
movsd 0x248a6(%rip), %xmm0    # 0x4fa08
addsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movsbq -0x19(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movsbq -0x19(%rbp), %rcx
movsd 0x24876(%rip), %xmm1    # 0x4fa08
subsd (%rax,%rcx,8), %xmm1
mulsd %xmm1, %xmm0
callq 0x9a40
movaps %xmm0, %xmm1
movsd -0x58(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x28(%rbp), %xmm0
movsd 0x2484a(%rip), %xmm1    # 0x4fa08
subsd -0x28(%rbp), %xmm1
mulsd %xmm1, %xmm0
mulsd -0x30(%rbp), %xmm0
movq -0x38(%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, -0x50(%rbp)
movsbl -0x1a(%rbp), %eax
cmpl $0x0, %eax
jge 0x2b203
movsd 0x2480c(%rip), %xmm0    # 0x4fa08
movsd %xmm0, -0x60(%rbp)
jmp 0x2b222
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rsi
movb -0x19(%rbp), %dl
movb -0x1a(%rbp), %cl
movq (%rdi), %rax
movsbl %dl, %edx
movsbl %cl, %ecx
callq *0x20(%rax)
movsd %xmm0, -0x60(%rbp)
movsd -0x50(%rbp), %xmm0
movsd -0x60(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x38(%rbp), %rax
movsd 0x48(%rax), %xmm0
callq 0x9a40
movaps %xmm0, %xmm1
movsd -0x70(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x10(%rbp), %rax
movsbl 0xfa(%rax), %eax
cvtsi2sd %eax, %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movsbq -0x19(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd 0x247fb(%rip), %xmm2    # 0x4fa78
callq 0xc360
movsd -0x68(%rbp), %xmm1
addsd %xmm0, %xmm1
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movsbq -0x19(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x2fd2b(%rip), %rax     # 0x5afd8
movq (%rax), %rdi
leaq 0x257ab(%rip), %rsi     # 0x50a62
movb $0x0, %al
callq 0x5440
movb -0x19(%rbp), %al
addb $0x1, %al
movb %al, -0x19(%rbp)
jmp 0x2b116
jmp 0x2b2cd
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2b0a5
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)