// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
jle 0x4c690
jmp 0x4c95a
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xe8(%rcx), %eax
jge 0x4c928
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x4c6de
jmp 0x4c91a
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
movl (%rcx), %ecx
addl -0x18(%rbp), %ecx
movl %ecx, -0x18(%rbp)
movq 0x88(%rax), %rax
movq 0x870(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x14(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movb (%rax,%rcx), %al
movb %al, -0x1a(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movb $0x0, -0x19(%rbp)
movq -0x38(%rbp), %rcx
movsbl -0x19(%rbp), %eax
movq 0x88(%rcx), %rcx
movsbl 0x8b8(%rcx), %ecx
cmpl %ecx, %eax
jge 0x4c918
movq -0x38(%rbp), %rax
movq 0x88(%rax), %rcx
movq 0x890(%rcx), %rcx
movq -0x28(%rbp), %rdx
movslq 0x8(%rdx), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x30(%rbp)
movq 0x90(%rax), %rax
movq -0x28(%rbp), %rcx
movslq 0x58(%rcx), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x4c78c
cmpl $0x1, -0x30(%rbp)
jne 0x4c82d
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x19(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x40(%rbp)
movsbl -0x1a(%rbp), %eax
cmpl $0x0, %eax
jge 0x4c7c3
movsd 0x324c(%rip), %xmm0     # 0x4fa08
movsd %xmm0, -0x48(%rbp)
jmp 0x4c7e3
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movsbq -0x19(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x1a(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x40(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x28(%rbp), %rax
movsd 0x48(%rax), %xmm0
callq 0x9a40
movsd -0x50(%rbp), %xmm1
divsd %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)
jmp 0x4c909
movq -0x38(%rbp), %rdi
movq 0x98(%rdi), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rsi
movb -0x19(%rbp), %cl
movq (%rdi), %rax
movsbl %cl, %edx
callq *0x10(%rax)
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movsbq -0x19(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq (%rdi), %rax
xorps %xmm2, %xmm2
xorl %esi, %esi
callq *0x20(%rax)
movq -0x28(%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 0x4c8a4
movsd 0x316b(%rip), %xmm0     # 0x4fa08
movsd %xmm0, -0x68(%rbp)
jmp 0x4c8c4
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movsbq -0x19(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x1a(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x58(%rbp), %xmm0
movsd -0x68(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x28(%rbp), %rax
movsd 0x48(%rax), %xmm0
callq 0x9a40
movsd -0x70(%rbp), %xmm1
divsd %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)
jmp 0x4c90b
movb -0x19(%rbp), %al
addb $0x1, %al
movb %al, -0x19(%rbp)
jmp 0x4c72c
jmp 0x4c91a
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x4c6a5
movq -0x38(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x42(%rax), %eax
cmpl $0x0, %eax
jle 0x4c95a
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rdi
movq 0x88(%rax), %rdx
cvtsi2sdl -0x18(%rbp), %xmm0
movl $0x1, %esi
callq 0xf200
addq $0x70, %rsp
popq %rbp
retq