// File: /workspace/github_cpp_makefile_star2_O2/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)
jle 0x17885
addq $0x18, %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 0x17876
movq 0xf0(%rbx), %rax
movq (%rax,%r12,8), %rbp
cmpl $0x0, 0x4(%rbp)
je 0x178b0
incq %r12
jmp 0x1788e
movq 0x88(%r14), %rax
movq 0x870(%rax), %rax
movq 0x10(%rbp), %rcx
movslq (%rcx), %rcx
movb (%rax,%rcx), %r13b
movsbl %r13b, %eax
movl %eax, 0xc(%rsp)
xorl %r15d, %r15d
movsbq 0x1(%rbx), %rax
cmpq %rax, %r15
jge 0x178ab
movq (%r14), %rax
movsbl %r15b, %edx
movq %r14, %rdi
movq %rbp, %rsi
callq *0x10(%rax)
movsd %xmm0, (%rsp)
movq 0x98(%r14), %rax
movsd (%rax), %xmm0
callq 0x6845
movsd %xmm0, 0x10(%rsp)
movq 0x10(%rbx), %rax
movsd (%rax,%r15,8), %xmm1
movsd 0x14fd5(%rip), %xmm0    # 0x2c8f0
subsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
callq 0x67cb
movsd 0x14fc0(%rip), %xmm1    # 0x2c8f0
movsd 0x10(%rsp), %xmm2
addsd %xmm1, %xmm2
divsd %xmm0, %xmm2
movapd %xmm2, %xmm3
movapd %xmm1, %xmm0
movapd %xmm1, %xmm2
movsd (%rsp), %xmm1
subsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
mulsd %xmm3, %xmm0
movq 0x30(%rbp), %rax
movq (%rax), %rax
mulsd (%rax,%r15,8), %xmm0
movsd %xmm0, (%rsp)
movapd %xmm2, %xmm0
testb %r13b, %r13b
js 0x1798a
movq (%r14), %rax
movsbl %r15b, %edx
movq %r14, %rdi
movq %rbp, %rsi
movl 0xc(%rsp), %ecx
callq *0x20(%rax)
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movsd 0x48(%rbp), %xmm0
callq 0x67cb
movsd (%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movsbl 0xfa(%rbx), %eax
cvtsi2sd %eax, %xmm0
movq 0x10(%rbx), %rax
movsd (%rax,%r15,8), %xmm1
movsd 0x14f53(%rip), %xmm2    # 0x2c920
callq 0x7d80
addsd (%rsp), %xmm0
movq 0x58(%rbx), %rax
movsd (%rax,%r15,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rax,%r15,8)
movq 0x215e6(%rip), %rax     # 0x38fd8
movq (%rax), %rcx
leaq 0x16034(%rip), %rdi     # 0x2da30
pushq $0x12
popq %rsi
pushq $0x1
popq %rdx
callq 0x44b0
incq %r15
jmp 0x178d4
nop