// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/HMMProblemComp.cpp
// Function: HMMProblemComp::setGradB(FitBit*)
void HMMProblemComp::setGradB (FitBit *fb){
	if(this->p->block_fitting[2]>0) return;
	NDAT t, ndat = 0;
	NPAR o, o0, i;//, j;
	struct data* dt;
//	NUMBER combined, deriv_logit;
//	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;
		for(t=0; t<dt->n; t++) { // Levinson MMFST
			dt->t = dt->ix[t]; // statefullness
			o  = this->p->dat_obs[ dt->ix[t] ];//->get( dt->ix[t] );
			o0 = this->p->dat_obs[ dt->ix[0] ];//->get( dt->ix[t] );
			if(o<0) // if no observation -- skip
				continue;
			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->gradB[i][o] -= dt->alpha[t][i] * dt->beta[t][i] / safe0num(dt->p_O_param * fb->B[i][o]);
				} else {
//					combined = getB(dt,i,o);
//					deriv_logit = 1 / safe0num( fb->B[i][o] * (1-fb->B[i][o]) );
//					fb->gradB[i][o] -= combined * (1-combined) * deriv_logit * dt->alpha[t][i] * dt->beta[t][i] / safe0num(dt->p_O_param * ((o<0)?1:fb->B[i][o]));
					// compute with parameter unite
					fb->gradB[i][o] -= this->pu->derivativeUnite(getB(dt,i,o), fb->B[i][o], 0.0/*NULL*/, 0) * dt->alpha[t][i] * dt->beta[t][i] / safe0num(dt->p_O_param * ((o<0)?1:fb->B[i][o]));
				}
			}
			
//			for(j=0; j<this->p->nS; j++)
//				if(t==0) {
//					fb->gradB[j][o] -= (o0==o) * fb->pi[j] * dt->beta[t][j];
//				} else {
//					for(i=0; i<this->p->nS; i++)
//						fb->gradB[j][o] -= ( dt->alpha[t-1][i] * fb->A[i][j] * dt->beta[t][j] /*+ (o0==o) * getPI(dt,j) * dt->beta[0][j]*/ ) / safe0num(dt->p_O_param); // Levinson MMFST
//				}
		}
	}
	if( this->p->Cslices>0 ) // penalty
		fb->addL2Penalty(FBV_B, this->p, (NUMBER)ndat);
//	this->p->tag1 = tag1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq 0x88(%rdi), %rax
cmpb $0x0, 0x983(%rax)
jle 0x31ed7
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %r13
movq %rdi, %r14
cmpl $0x0, 0xe8(%rsi)
jle 0x3207e
xorl %ecx, %ecx
xorl %edx, %edx
movq 0xf0(%r13), %rax
movq (%rax,%rcx,8), %r15
cmpl $0x0, 0x4(%r15)
jne 0x32062
movl (%r15), %eax
addl %eax, %edx
testl %eax, %eax
jle 0x32062
movl %edx, 0xc(%rsp)
movq %rcx, 0x10(%rsp)
xorl %ebp, %ebp
movq 0x10(%r15), %rax
movl (%rax,%rbp,4), %ecx
movl %ecx, 0x8(%r15)
movq 0x88(%r14), %rcx
movq 0x870(%rcx), %rcx
movslq (%rax,%rbp,4), %rax
movsbq (%rcx,%rax), %r12
testq %r12, %r12
js 0x3204a
movq 0x88(%r14), %rax
cmpb $0x0, 0x8b8(%rax)
jle 0x3204a
xorl %ebx, %ebx
movq %rbp, 0x18(%rsp)
movq 0x90(%r14), %rcx
movslq 0x58(%r15), %rdx
cmpb $0x0, (%rcx,%rdx)
je 0x31fd4
movq 0x890(%rax), %rax
movslq 0x8(%r15), %rcx
cmpl $0x1, (%rax,%rcx,4)
je 0x31fd4
movq (%r14), %rax
movq %r13, %rbp
movq 0x98(%r14), %r13
movsbl %bl, %edx
movq %r14, %rdi
movq %r15, %rsi
movl %r12d, %ecx
callq *0x20(%rax)
movq 0x20(%rbp), %rax
movq (%rax,%rbx,8), %rax
movsd (%rax,%r12,8), %xmm1
movq (%r13), %rax
xorps %xmm2, %xmm2
movq %r13, %rdi
movq %rbp, %r13
movq 0x18(%rsp), %rbp
xorl %esi, %esi
callq *0x20(%rax)
movq 0x28(%r15), %rax
movq (%rax,%rbp,8), %rax
mulsd (%rax,%rbx,8), %xmm0
jmp 0x31fe1
movq 0x28(%r15), %rax
movq (%rax,%rbp,8), %rax
movsd (%rax,%rbx,8), %xmm0
movq 0x30(%r15), %rax
movq (%rax,%rbp,8), %rax
mulsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x48(%r15), %xmm0
movq 0x20(%r13), %rax
movq (%rax,%rbx,8), %rax
mulsd (%rax,%r12,8), %xmm0
callq 0x8d88
movsd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x68(%r13), %rax
movq (%rax,%rbx,8), %rax
movsd (%rax,%r12,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%r12,8)
incq %rbx
movq 0x88(%r14), %rax
movsbq 0x8b8(%rax), %rcx
cmpq %rcx, %rbx
jl 0x31f62
incq %rbp
movslq (%r15), %rax
cmpq %rax, %rbp
jl 0x31f1c
movq 0x10(%rsp), %rcx
movl 0xc(%rsp), %edx
incq %rcx
movslq 0xe8(%r13), %rax
cmpq %rax, %rcx
jl 0x31eee
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
jmp 0x32082
xorpd %xmm0, %xmm0
movq 0x88(%r14), %rdx
cmpb $0x0, 0x42(%rdx)
jle 0x31ec8
movq %r13, %rdi
movl $0x3, %esi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xb936