// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/HMMProblemComp.cpp
// Function: HMMProblemComp::setGradPI(FitBit*)
void HMMProblemComp::setGradPI(FitBit *fb){
	if(this->p->block_fitting[0]>0) return;
	NDAT t = 0, ndat = 0;
	NPAR i, o;
//	NUMBER combined, deriv_logit;
	struct data* dt;
//	NDAT tag1 = this->p->tag1; // use global tag1 setting
//	this->p->tag1 = 1;
//	this->p->tag1 = 0; // only get parameter values not united with "neighburs", the only difference
	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] );
		dt->t = dt->ix[t]; // statefullness
		for(i=0; i<this->p->nS; i++) {
			NCAT n_skills = this->p->dat_skill_rcount[ dt->t ];
			if( this->is_multi[dt->k] == 0 || n_skills == 1) {
				fb->gradPI[i] -= dt->beta[t][i] * ((o<0)?1:fb->B[i][o]) / safe0num(dt->p_O_param);
			} else {
//				combined = getPI(dt,i);
//				deriv_logit = 1 / safe0num( fb->pi[i] * (1-fb->pi[i]) );
//				fb->gradPI[i] -= combined * (1-combined) * deriv_logit * dt->beta[t][i] * ((o<0)?1:fb->B[i][o]) / safe0num(dt->p_O_param);
				// compute with parameter unite
				fb->gradPI[i] -= this->pu->derivativeUnite(getPI(dt,i), fb->pi[i], 0.0/*NULL*/, 0 /*standard params*/) * dt->beta[t][i] * ((o<0)?1:fb->B[i][o]) / safe0num(dt->p_O_param);
			}
		}
	}
	if( this->p->Cslices>0 ) // penalty
		fb->addL2Penalty(FBV_PI, this->p, (NUMBER)ndat);
//	this->p->tag1 = tag1;
}

// 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 0x31a77
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rbx
movq %rdi, %r14
cmpl $0x0, 0xe8(%rsi)
jle 0x31c03
xorl %edx, %edx
xorl %esi, %esi
movq 0xf0(%rbx), %rax
movq (%rax,%rdx,8), %r15
cmpl $0x0, 0x4(%r15)
jne 0x31be7
movq 0x88(%r14), %rax
movq 0x870(%rax), %rax
movq 0x10(%r15), %rcx
movslq (%rcx), %rcx
movzbl (%rax,%rcx), %ebp
addl (%r15), %esi
movl %ecx, 0x8(%r15)
movq 0x88(%r14), %rax
cmpb $0x0, 0x8b8(%rax)
jle 0x31be7
movl %esi, 0x4(%rsp)
movq %rdx, 0x10(%rsp)
xorl %r13d, %r13d
movq 0x90(%r14), %rcx
movslq 0x58(%r15), %rdx
cmpb $0x0, (%rcx,%rdx)
je 0x31b6d
movq 0x890(%rax), %rax
movslq 0x8(%r15), %rcx
cmpl $0x1, (%rax,%rcx,4)
je 0x31b6d
movq (%r14), %rax
movq 0x98(%r14), %r12
movsbl %r13b, %edx
movq %r14, %rdi
movq %r15, %rsi
callq *0x10(%rax)
movq 0x10(%rbx), %rax
movsd (%rax,%r13,8), %xmm1
movq (%r12), %rax
xorps %xmm2, %xmm2
movq %r12, %rdi
xorl %esi, %esi
callq *0x20(%rax)
movapd %xmm0, %xmm1
movsd 0x2dae(%rip), %xmm0     # 0x348f0
testb %bpl, %bpl
js 0x31b54
movq 0x20(%rbx), %rax
movq (%rax,%r13,8), %rax
movsd (%rax,%rbp,8), %xmm0
movq 0x30(%r15), %rax
movq (%rax), %rax
mulsd (%rax,%r13,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
jmp 0x31b9a
movsd 0x2d7b(%rip), %xmm0     # 0x348f0
testb %bpl, %bpl
js 0x31b87
movq 0x20(%rbx), %rax
movq (%rax,%r13,8), %rax
movsd (%rax,%rbp,8), %xmm0
movq 0x30(%r15), %rax
movq (%rax), %rax
mulsd (%rax,%r13,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x48(%r15), %xmm0
callq 0x8d88
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x58(%rbx), %rax
movsd (%rax,%r13,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%r13,8)
incq %r13
movq 0x88(%r14), %rax
movsbq 0x8b8(%rax), %rcx
cmpq %rcx, %r13
jl 0x31ae4
movq 0x10(%rsp), %rdx
movl 0x4(%rsp), %esi
incq %rdx
movslq 0xe8(%rbx), %rax
cmpq %rax, %rdx
jl 0x31a8e
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
jmp 0x31c07
xorpd %xmm0, %xmm0
movq 0x88(%r14), %rdx
cmpb $0x0, 0x42(%rdx)
jle 0x31a68
movq %rbx, %rdi
movl $0x1, %esi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xb936
nop