// File: /workspace/github_cpp_makefile_star2_O3/myudelson[P]hmm-scalable/HMMProblemComp.cpp
// Function: HMMProblemComp::setGradA(FitBit*)
void HMMProblemComp::setGradA (FitBit *fb){
	if(this->p->block_fitting[1]>0) return;
	NDAT t, ndat = 0;
	NPAR o, i, j;
//	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;
		for(t=1; t<dt->n; t++) {
			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++) {
				for(j=0; j<this->p->nS; j++) {
					NCAT n_skills = this->p->dat_skill_rcount[ dt->t ];
					if( this->is_multi[dt->k] == 0 || n_skills == 1) {
						fb->gradA[i][j] -= dt->beta[t][j] * ((o<0)?1:fb->B[j][o]) * dt->alpha[t-1][i] / safe0num(dt->p_O_param);
					} else {
//						combined = getA(dt,i,j);
//						deriv_logit = 1 / safe0num( fb->A[i][j] * (1-fb->A[i][j]) );
//						fb->gradA[i][j] -= combined * safe0num(1-combined) * deriv_logit * dt->beta[t][j] * ((o<0)?1:fb->B[j][o]) * dt->alpha[t-1][i] / safe0num(dt->p_O_param);
						// compute with parameter unite
						fb->gradA[i][j] -= this->pu->derivativeUnite(getA(dt,i,j), fb->A[i][j], 0.0/*NULL*/, 0) * dt->beta[t][j] * ((o<0)?1:fb->B[j][o]) * dt->alpha[t-1][i] / safe0num(dt->p_O_param);
					}
				}
			}
		}
	}
	if( this->p->Cslices>0 ) // penalty
		fb->addL2Penalty(FBV_A, 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), %rdx
cmpb $0x0, 0x982(%rdx)
jle 0x317cf
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %r12
movl 0xe8(%rsi), %eax
testl %eax, %eax
jle 0x319d6
movq %rdi, %r14
xorl %esi, %esi
movl $0x0, 0x4(%rsp)
movq 0xf0(%r12), %rcx
movq %rsi, 0x10(%rsp)
movq (%rcx,%rsi,8), %r15
cmpl $0x0, 0x4(%r15)
jne 0x319b7
movl (%r15), %ecx
addl %ecx, 0x4(%rsp)
cmpl $0x2, %ecx
jl 0x319b7
movl $0x1, %ebp
movq 0x870(%rdx), %rax
movq 0x10(%r15), %rsi
movslq (%rsi,%rbp,4), %rsi
movzbl (%rax,%rsi), %edi
movl %esi, 0x8(%r15)
movb 0x8b8(%rdx), %al
testb %al, %al
jle 0x319a0
movq %rdi, 0x8(%rsp)
xorl %ebx, %ebx
movq %rbp, 0x18(%rsp)
testb %al, %al
jle 0x31987
xorl %r13d, %r13d
movq 0x90(%r14), %rax
movslq 0x58(%r15), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x318f9
movq 0x890(%rdx), %rax
movslq 0x8(%r15), %rcx
cmpl $0x1, (%rax,%rcx,4)
je 0x318f9
movq %r14, %rdi
movq (%r14), %rax
movq %r12, %rbp
movq 0x98(%r14), %r12
movsbl %bl, %edx
movsbl %r13b, %ecx
movq %r15, %rsi
callq *0x18(%rax)
movq 0x18(%rbp), %rax
movq (%rax,%rbx,8), %rax
movsd (%rax,%r13,8), %xmm1
movq (%r12), %rax
xorps %xmm2, %xmm2
movq %r12, %rdi
movq %rbp, %r12
xorl %esi, %esi
callq *0x20(%rax)
movapd %xmm0, %xmm1
movsd 0x2027(%rip), %xmm0     # 0x338f0
movq 0x8(%rsp), %rcx
testb %cl, %cl
js 0x318e0
movq 0x20(%r12), %rax
movq (%rax,%r13,8), %rax
movsd (%rax,%rcx,8), %xmm0
movq 0x30(%r15), %rax
movq 0x18(%rsp), %rbp
movq (%rax,%rbp,8), %rax
mulsd (%rax,%r13,8), %xmm1
mulsd %xmm0, %xmm1
jmp 0x31926
movsd 0x1fef(%rip), %xmm1     # 0x338f0
movq 0x8(%rsp), %rcx
testb %cl, %cl
js 0x31918
movq 0x20(%r12), %rax
movq (%rax,%r13,8), %rax
movsd (%rax,%rcx,8), %xmm1
movq 0x30(%r15), %rax
movq (%rax,%rbp,8), %rax
mulsd (%rax,%r13,8), %xmm1
movq 0x28(%r15), %rax
movq -0x8(%rax,%rbp,8), %rax
mulsd (%rax,%rbx,8), %xmm1
movsd %xmm1, 0x20(%rsp)
movsd 0x48(%r15), %xmm0
callq 0x8df8
movsd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x60(%r12), %rax
movq (%rax,%rbx,8), %rax
movsd (%rax,%r13,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%r13,8)
incq %r13
movq 0x88(%r14), %rdx
movb 0x8b8(%rdx), %al
movsbq %al, %rcx
cmpq %rcx, %r13
jl 0x3185a
jmp 0x31991
movb 0x8b8(%rdx), %al
movsbq %al, %rcx
incq %rbx
cmpq %rcx, %rbx
jl 0x3184f
movl (%r15), %ecx
incq %rbp
movslq %ecx, %rax
cmpq %rax, %rbp
jl 0x3181e
movl 0xe8(%r12), %eax
movq 0x10(%rsp), %rsi
incq %rsi
movslq %eax, %rcx
cmpq %rcx, %rsi
jl 0x317ed
xorps %xmm0, %xmm0
cvtsi2sdl 0x4(%rsp), %xmm0
jmp 0x319da
xorpd %xmm0, %xmm0
cmpb $0x0, 0x42(%rdx)
jle 0x317c0
movq %r12, %rdi
movl $0x2, %esi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xb90a
nop