// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/HMMProblem.cpp
// Function: HMMProblem::doBarzilaiBorweinStep(FitBit*)
NUMBER HMMProblem::doBarzilaiBorweinStep(FitBit *fb) {
//NUMBER HMMProblem::doBarzilaiBorweinStep(NCAT xndat, struct data** x_data, NUMBER *a_PI, NUMBER **a_A, NUMBER **a_B, NUMBER *a_PI_m1, NUMBER **a_A_m1, NUMBER **a_B_m1, NUMBER *a_gradPI_m1, NUMBER **a_gradA_m1, NUMBER **a_gradB_m1, NUMBER *a_gradPI, NUMBER **a_gradA, NUMBER **a_gradB, NUMBER *a_dirPI_m1, NUMBER **a_dirA_m1, NUMBER **a_dirB_m1) {
	NPAR i,j,m;
    NPAR nS = this->p->nS, nO = this->p->nO;
	// first scale down gradients
    fb->doLog10ScaleGentleByRow(FBS_GRAD);
    
    // compute s_k_m1
  	NUMBER *s_k_m1_PI = init1D<NUMBER>((NDAT)nS);
	NUMBER **s_k_m1_A = init2D<NUMBER>((NDAT)nS, (NDAT)nS);
	NUMBER **s_k_m1_B = init2D<NUMBER>((NDAT)nS, (NDAT)nO);
	for(i=0; i<nS; i++)
	{
		s_k_m1_PI[i] = fb->PIm1[i] - fb->PIm2[i];
		for(j=0; j<nS; j++) s_k_m1_A[i][j] = fb->Am1[i][j] - fb->Am2[i][j];
		for(m=0; m<this->p->nO; m++) s_k_m1_B[i][m] = fb->Bm1[i][m] - fb->Bm2[i][m];
	}
    // compute alpha_step
    NUMBER alpha_step = 0, alpha_step_num = 0, alpha_step_den = 0;
    // Barzilai Borwein: s' * s / ( s' * (g-g_m1) )
	for(i=0; i<nS; i++)
	{
		alpha_step_num += s_k_m1_PI[i]*s_k_m1_PI[i];
		alpha_step_den += s_k_m1_PI[i]*(fb->gradPI[i] - fb->gradPIm1[i]);
		for(j=0; j<nS; j++) {
            alpha_step_num += s_k_m1_A[i][j]*s_k_m1_A[i][j];
            alpha_step_den += s_k_m1_A[i][j]*(fb->gradA[i][j] - fb->gradAm1[i][j]);
        }
		for(m=0; m<nO; m++) {
            alpha_step_num += s_k_m1_B[i][m]*s_k_m1_B[i][m];
            alpha_step_den += s_k_m1_B[i][m]*(fb->gradB[i][m] - fb->gradBm1[i][m]);
        }
	}
    alpha_step = alpha_step_num / safe0num(alpha_step_den);
    
    // step
    for(i=0; i<nS; i++) {
        fb->pi[i] = fb->pi[i] - alpha_step * fb->gradPI[i];
        for(j=0; j<nS; j++)
            fb->A[i][j] = fb->A[i][j] - alpha_step * fb->gradA[i][j];
        for(m=0; m<nO; m++)
            fb->B[i][m] = fb->B[i][m] - alpha_step * fb->gradB[i][m];
    }
    // scale
    if( !this->hasNon01Constraints() || fb->observe_non01_boundaries==0 ) {
        projectsimplex(fb->pi, nS);
        for(i=0; i<nS; i++) {
            projectsimplex(fb->A[i], nS);
            projectsimplex(fb->B[i], nO);
        }
    } else {
        projectsimplexbounded(fb->pi, this->getLbPI(), this->getUbPI(), nS);
        for(i=0; i<nS; i++) {
            projectsimplexbounded(fb->A[i], this->getLbA()[i], this->getUbA()[i], nS);
            projectsimplexbounded(fb->B[i], this->getLbB()[i], this->getUbB()[i], nO);
        }
    }
	free(s_k_m1_PI);
	free2D<NUMBER>(s_k_m1_B, nS);
	free2D<NUMBER>(s_k_m1_A, nS);

    // recompute alpha and p(O|param)
    computeAlphaAndPOParam(fb->xndat, fb->x_data);
    return HMMProblem::getSumLogPOPara(fb->xndat, fb->x_data);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq 0x88(%rdi), %rax
movzbl 0x8b8(%rax), %ebp
movb 0x8b9(%rax), %r12b
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x8f0e
movsbl %bpl, %r13d
movl %r13d, %edi
callq 0x7dde
movq %rax, 0x28(%rsp)
movl %r13d, %edi
movl %r13d, %esi
callq 0x9328
movq %rax, %r15
movb %r12b, 0x10(%rsp)
movsbl %r12b, %esi
movl %r13d, 0xc(%rsp)
movl %r13d, %edi
movl %esi, 0x1c(%rsp)
callq 0x9328
movq 0x28(%rsp), %r10
movq %rax, %r13
movq %r14, 0x20(%rsp)
movq 0x88(%r14), %rax
xorl %ecx, %ecx
testb %bpl, %bpl
movl $0x0, %r11d
cmovgl %ebp, %r11d
xorl %edx, %edx
cmpq %r11, %rdx
je 0x12584
movq 0x28(%rbx), %rsi
movq 0x40(%rbx), %rdi
movsd (%rsi,%rdx,8), %xmm0
subsd (%rdi,%rdx,8), %xmm0
movsd %xmm0, (%r10,%rdx,8)
xorl %esi, %esi
cmpq %rsi, %rbp
je 0x12538
movq 0x30(%rbx), %rdi
movq 0x48(%rbx), %r8
movq (%rdi,%rdx,8), %rdi
movsd (%rdi,%rsi,8), %xmm0
movq (%r8,%rdx,8), %rdi
subsd (%rdi,%rsi,8), %xmm0
movq (%r15,%rdx,8), %rdi
movsd %xmm0, (%rdi,%rsi,8)
incq %rsi
jmp 0x1250b
movzbl 0x8b9(%rax), %esi
testb %sil, %sil
cmovlel %ecx, %esi
movzbl %sil, %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x1257c
movq 0x38(%rbx), %r8
movq 0x50(%rbx), %r9
movq (%r8,%rdx,8), %r8
movsd (%r8,%rdi,8), %xmm0
movq (%r9,%rdx,8), %r8
subsd (%r8,%rdi,8), %xmm0
movq (%r13,%rdx,8), %r8
movsd %xmm0, (%r8,%rdi,8)
incq %rdi
jmp 0x1254b
incq %rdx
jmp 0x124e8
movb 0x10(%rsp), %dl
movzbl %dl, %ecx
xorl %eax, %eax
testb %dl, %dl
cmovlel %eax, %ecx
movzbl %cl, %r12d
xorpd %xmm3, %xmm3
movl 0x1c(%rsp), %r14d
cmpq %r11, %rax
je 0x12666
movq 0x58(%rbx), %rcx
movq 0x70(%rbx), %rdx
movsd (%rcx,%rax,8), %xmm0
subsd (%rdx,%rax,8), %xmm0
movsd (%r10,%rax,8), %xmm1
movapd %xmm1, %xmm2
unpcklpd %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0]
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
mulpd %xmm2, %xmm1
addpd %xmm1, %xmm3
xorl %ecx, %ecx
cmpq %rcx, %rbp
je 0x12617
movq 0x60(%rbx), %rdx
movq 0x78(%rbx), %rsi
movq (%rdx,%rax,8), %rdx
movsd (%rdx,%rcx,8), %xmm0
movq (%rsi,%rax,8), %rdx
subsd (%rdx,%rcx,8), %xmm0
movq (%r15,%rax,8), %rdx
movsd (%rdx,%rcx,8), %xmm1
movapd %xmm1, %xmm2
unpcklpd %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0]
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
mulpd %xmm2, %xmm1
addpd %xmm1, %xmm3
incq %rcx
jmp 0x125d6
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x1265e
movq 0x68(%rbx), %rdx
movq 0x80(%rbx), %rsi
movq (%rdx,%rax,8), %rdx
movsd (%rdx,%rcx,8), %xmm0
movq (%rsi,%rax,8), %rdx
subsd (%rdx,%rcx,8), %xmm0
movq (%r13,%rax,8), %rdx
movsd (%rdx,%rcx,8), %xmm1
movapd %xmm1, %xmm2
unpcklpd %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0]
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
mulpd %xmm2, %xmm1
addpd %xmm1, %xmm3
incq %rcx
jmp 0x12619
incq %rax
jmp 0x1259f
movapd %xmm3, %xmm0
unpckhpd %xmm3, %xmm0            # xmm0 = xmm0[1],xmm3[1]
movq %r11, 0x10(%rsp)
movapd %xmm3, 0x30(%rsp)
callq 0x67cb
movapd 0x30(%rsp), %xmm1
movq 0x10(%rsp), %r8
xorpd 0x1a2af(%rip), %xmm1    # 0x2c940
divsd %xmm0, %xmm1
movzbl %bpl, %eax
xorl %ecx, %ecx
cmpq %r8, %rcx
je 0x12725
movq 0x10(%rbx), %rdx
movq 0x58(%rbx), %rsi
movsd (%rsi,%rcx,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, (%rdx,%rcx,8)
xorl %edx, %edx
cmpq %rdx, %rax
je 0x126ee
movq 0x18(%rbx), %rsi
movq 0x60(%rbx), %rdi
movq (%rsi,%rcx,8), %rsi
movq (%rdi,%rcx,8), %rdi
movsd (%rdi,%rdx,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rsi,%rdx,8), %xmm0
movsd %xmm0, (%rsi,%rdx,8)
incq %rdx
jmp 0x126c1
xorl %edx, %edx
cmpq %rdx, %r12
je 0x1271d
movq 0x20(%rbx), %rsi
movq 0x68(%rbx), %rdi
movq (%rsi,%rcx,8), %rsi
movq (%rdi,%rcx,8), %rdi
movsd (%rdi,%rdx,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rsi,%rdx,8), %xmm0
movsd %xmm0, (%rsi,%rdx,8)
incq %rdx
jmp 0x126f0
incq %rcx
jmp 0x1269b
movq 0x20(%rsp), %rax
cmpb $0x0, 0x80(%rax)
je 0x127e2
cmpb $0x0, 0xf9(%rbx)
movq 0x10(%rbx), %rdi
je 0x127e6
movq 0x20(%rsp), %rax
movq 0x50(%rax), %rsi
movq 0x68(%rax), %rdx
movl 0xc(%rsp), %ecx
callq 0x5d80
xorl %r12d, %r12d
movq 0x20(%rsp), %r14
cmpq %r12, 0x10(%rsp)
je 0x1281f
movq 0x18(%rbx), %rax
movq (%rax,%r12,8), %rdi
cmpb $0x0, 0x80(%r14)
movq 0x58(%r14), %rax
movl $0x0, %ecx
cmoveq %rcx, %rax
movq (%rax,%r12,8), %rsi
movq 0x70(%r14), %rax
cmoveq %rcx, %rax
movq (%rax,%r12,8), %rdx
movsbl %bpl, %ecx
callq 0x5d80
movq 0x20(%rbx), %rax
movq (%rax,%r12,8), %rdi
cmpb $0x0, 0x80(%r14)
movq 0x60(%r14), %rax
movl $0x0, %ecx
cmoveq %rcx, %rax
movq (%rax,%r12,8), %rsi
movq 0x78(%r14), %rax
cmoveq %rcx, %rax
movq (%rax,%r12,8), %rdx
movl 0x1c(%rsp), %ecx
callq 0x5d80
incq %r12
jmp 0x12766
movq 0x10(%rbx), %rdi
movl 0xc(%rsp), %esi
callq 0x5600
xorl %r12d, %r12d
cmpq %r12, 0x10(%rsp)
je 0x1281f
movq 0x18(%rbx), %rax
movq (%rax,%r12,8), %rdi
movsbl %bpl, %esi
callq 0x5600
movq 0x20(%rbx), %rax
movq (%rax,%r12,8), %rdi
movl %r14d, %esi
callq 0x5600
incq %r12
jmp 0x127f2
movq 0x28(%rsp), %rdi
callq 0x4290
movq %r13, %rdi
movl 0xc(%rsp), %ebp
movl %ebp, %esi
callq 0x7f2c
movq %r15, %rdi
movl %ebp, %esi
callq 0x7f2c
movl 0xe8(%rbx), %esi
movq 0xf0(%rbx), %rdx
movq 0x20(%rsp), %rdi
callq 0xe7b0
movl 0xe8(%rbx), %esi
movq 0xf0(%rbx), %rdx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe400