// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq 0x88(%rdi), %rax
cmpb $0x0, 0x981(%rax)
jg 0x1bfad
movq %rsi, %rbx
cmpl $0x0, 0xe8(%rsi)
jle 0x1bfad
movq %rdi, %r14
xorl %r13d, %r13d
movq 0xf0(%rbx), %rax
movq (%rax,%r13,8), %r12
cmpl $0x0, 0x4(%r12)
jne 0x1bf9a
cmpb $0x0, 0x1(%rbx)
jle 0x1bf9a
movq 0x88(%r14), %rax
movq 0x870(%rax), %rax
movq 0x10(%r12), %rcx
movslq (%rcx), %rcx
movb (%rax,%rcx), %r15b
movsbl %r15b, %eax
movl %eax, 0xc(%rsp)
xorl %ebp, %ebp
movq (%r14), %rax
movsbl %bpl, %edx
movq %r14, %rdi
movq %r12, %rsi
callq *0x10(%rax)
movsd %xmm0, (%rsp)
movq 0x98(%r14), %rax
movsd (%rax), %xmm0
callq 0x8e13
movsd %xmm0, 0x10(%rsp)
movq 0x10(%rbx), %rax
movsd (%rax,%rbp,8), %xmm1
movsd 0x18a55(%rip), %xmm0    # 0x348f0
subsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
callq 0x8d88
movsd 0x18a40(%rip), %xmm2    # 0x348f0
movsd 0x10(%rsp), %xmm1
addsd %xmm2, %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, %xmm3
movapd %xmm2, %xmm1
movsd (%rsp), %xmm0
subsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm3, %xmm1
movq 0x30(%r12), %rax
movq (%rax), %rax
mulsd (%rax,%rbp,8), %xmm1
movapd %xmm2, %xmm0
testb %r15b, %r15b
js 0x1bf0b
movq (%r14), %rax
movsbl %bpl, %edx
movq %r14, %rdi
movq %r12, %rsi
movl 0xc(%rsp), %ecx
movsd %xmm1, (%rsp)
callq *0x20(%rax)
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movsd 0x48(%r12), %xmm0
callq 0x8d88
movsd (%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movsbl 0xfa(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq 0x10(%rbx), %rax
movsd (%rax,%rbp,8), %xmm1
movsd 0x189d3(%rip), %xmm2    # 0x34920
callq 0xa57f
addsd (%rsp), %xmm0
movq 0x58(%rbx), %rax
movsd (%rax,%rbp,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rax,%rbp,8)
movq 0x24068(%rip), %rax     # 0x3ffd8
movq (%rax), %rcx
movl $0x12, %esi
movl $0x1, %edx
leaq 0x19a0a(%rip), %rdi     # 0x3598e
callq 0x4480
incq %rbp
movsbq 0x1(%rbx), %rax
cmpq %rax, %rbp
jl 0x1be5f
incq %r13
movslq 0xe8(%rbx), %rax
cmpq %rax, %r13
jl 0x1be1a
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq