// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %rsi, %rbx
movq %rdi, (%rsp)
movq 0x88(%rdi), %rax
movsbq 0x8b8(%rax), %r15
movsbl 0x8b9(%rax), %ebp
movq %rsi, %rdi
movl $0x3, %esi
callq 0xb768
movl $0x8, %esi
movq %r15, %rdi
callq 0x4130
movq %rax, %r13
movl %r15d, %edi
movl %r15d, %esi
callq 0xbb5c
movq %rax, %r12
movl %r15d, %edi
movl %ebp, %esi
callq 0xbb5c
movq %rax, 0x8(%rsp)
movzbl %bpl, %ebp
testq %r15, %r15
jle 0x1604a
movq 0x28(%rbx), %rax
movq 0x30(%rbx), %rcx
movq 0x40(%rbx), %rdx
movq (%rsp), %rsi
movq 0x88(%rsi), %rsi
movzbl 0x8b9(%rsi), %esi
movq 0x48(%rbx), %rdi
xorl %r8d, %r8d
movsd (%rax,%r8,8), %xmm0
subsd (%rdx,%r8,8), %xmm0
movsd %xmm0, (%r13,%r8,8)
movq (%rcx,%r8,8), %r9
movq (%rdi,%r8,8), %r10
movq (%r12,%r8,8), %r11
xorl %r14d, %r14d
movsd (%r9,%r14,8), %xmm0
subsd (%r10,%r14,8), %xmm0
movsd %xmm0, (%r11,%r14,8)
incq %r14
cmpq %r14, %r15
jne 0x15fed
testb %sil, %sil
jle 0x16042
movq 0x38(%rbx), %r9
movq 0x50(%rbx), %r10
movq (%r9,%r8,8), %r9
movq (%r10,%r8,8), %r10
movq 0x8(%rsp), %r11
movq (%r11,%r8,8), %r11
xorl %r14d, %r14d
movsd (%r9,%r14,8), %xmm0
subsd (%r10,%r14,8), %xmm0
movsd %xmm0, (%r11,%r14,8)
incq %r14
cmpq %r14, %rsi
jne 0x16028
incq %r8
cmpq %r15, %r8
jne 0x15fcb
testb %r15b, %r15b
jle 0x16123
movq 0x58(%rbx), %rax
movq 0x60(%rbx), %rcx
movq 0x70(%rbx), %rdx
movq 0x78(%rbx), %rsi
xorpd %xmm0, %xmm0
xorl %edi, %edi
xorpd %xmm3, %xmm3
movsd (%r13,%rdi,8), %xmm1
movsd (%rax,%rdi,8), %xmm2
subsd (%rdx,%rdi,8), %xmm2
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm0
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm3
movq (%r12,%rdi,8), %r8
movq (%rcx,%rdi,8), %r9
movq (%rsi,%rdi,8), %r10
xorl %r11d, %r11d
movsd (%r9,%r11,8), %xmm1
subsd (%r10,%r11,8), %xmm1
movsd (%r8,%r11,8), %xmm2
mulsd %xmm2, %xmm1
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm3
addsd %xmm1, %xmm0
incq %r11
cmpq %r11, %r15
jne 0x1609d
testb %bpl, %bpl
jle 0x16115
movq 0x8(%rsp), %r8
movq (%r8,%rdi,8), %r8
movq 0x68(%rbx), %r9
movq 0x80(%rbx), %r10
movq (%r9,%rdi,8), %r9
movq (%r10,%rdi,8), %r10
xorl %r11d, %r11d
movsd (%r9,%r11,8), %xmm1
subsd (%r10,%r11,8), %xmm1
movsd (%r8,%r11,8), %xmm2
mulsd %xmm2, %xmm1
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm3
addsd %xmm1, %xmm0
incq %r11
cmpq %r11, %rbp
jne 0x160eb
incq %rdi
cmpq %r15, %rdi
jne 0x1606d
jmp 0x1612b
xorpd %xmm3, %xmm3
xorpd %xmm0, %xmm0
movapd %xmm3, 0x10(%rsp)
callq 0x8d88
movapd 0x10(%rsp), %xmm1
testb %r15b, %r15b
jle 0x161dd
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
xorpd 0x1e7eb(%rip), %xmm1    # 0x34940
movq 0x58(%rbx), %rdx
divsd %xmm0, %xmm1
movq 0x60(%rbx), %rsi
xorl %edi, %edi
movsd (%rdx,%rdi,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rax,%rdi,8), %xmm0
movsd %xmm0, (%rax,%rdi,8)
movq (%rcx,%rdi,8), %r8
movq (%rsi,%rdi,8), %r9
xorl %r10d, %r10d
movsd (%r9,%r10,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%r8,%r10,8), %xmm0
movsd %xmm0, (%r8,%r10,8)
incq %r10
cmpq %r10, %r15
jne 0x16181
testb %bpl, %bpl
jle 0x161d5
movq 0x20(%rbx), %r8
movq 0x68(%rbx), %r9
movq (%r8,%rdi,8), %r8
movq (%r9,%rdi,8), %r9
xorl %r10d, %r10d
movsd (%r9,%r10,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%r8,%r10,8), %xmm0
movsd %xmm0, (%r8,%r10,8)
incq %r10
cmpq %r10, %rbp
jne 0x161b7
incq %rdi
cmpq %r15, %rdi
jne 0x16163
movq %r12, 0x20(%rsp)
movq (%rsp), %rax
cmpb $0x1, 0x80(%rax)
jne 0x162a3
cmpb $0x0, 0xf9(%rbx)
je 0x162a3
movq 0x10(%rbx), %rdi
movq (%rsp), %rax
movq 0x50(%rax), %rsi
movq 0x68(%rax), %rdx
movl %r15d, %ecx
callq 0x79f5
testb %r15b, %r15b
jle 0x162e5
xorl %r14d, %r14d
movsbl %bpl, %eax
movl %eax, 0x10(%rsp)
xorl %ebp, %ebp
movq (%rsp), %r12
movq 0x18(%rbx), %rax
movq (%rax,%rbp,8), %rdi
cmpb $0x0, 0x80(%r12)
movq 0x58(%r12), %rax
cmoveq %r14, %rax
movq (%rax,%rbp,8), %rsi
movq 0x70(%r12), %rax
cmoveq %r14, %rax
movq (%rax,%rbp,8), %rdx
movl %r15d, %ecx
callq 0x79f5
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rdi
cmpb $0x0, 0x80(%r12)
movq 0x60(%r12), %rax
cmoveq %r14, %rax
movq (%rax,%rbp,8), %rsi
movq 0x78(%r12), %rax
cmoveq %r14, %rax
movq (%rax,%rbp,8), %rdx
movl 0x10(%rsp), %ecx
callq 0x79f5
incq %rbp
cmpq %rbp, %r15
jne 0x16232
jmp 0x162e5
movq 0x10(%rbx), %rdi
movl %r15d, %esi
callq 0x692d
testb %r15b, %r15b
jle 0x162e5
movslq %r15d, %r14
xorl %r12d, %r12d
movsbl %bpl, %ebp
movq 0x18(%rbx), %rax
movq (%rax,%r12,8), %rdi
movl %r15d, %esi
callq 0x692d
movq 0x20(%rbx), %rax
movq (%rax,%r12,8), %rdi
movl %ebp, %esi
callq 0x692d
incq %r12
cmpq %r12, %r14
jne 0x162be
movq %r13, %rdi
callq 0x42a0
movq 0x8(%rsp), %rdi
movl %r15d, %esi
callq 0xa5e0
movq 0x20(%rsp), %rdi
movl %r15d, %esi
callq 0xa5e0
movl 0xe8(%rbx), %esi
movq 0xf0(%rbx), %rdx
movq (%rsp), %rdi
callq 0x11a86
movslq 0xe8(%rbx), %rax
testq %rax, %rax
jle 0x16357
movq 0xf0(%rbx), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
cmpl $0x0, 0x4(%rsi)
xorpd %xmm1, %xmm1
jne 0x16349
movsd 0x50(%rsi), %xmm1
addsd %xmm1, %xmm0
incq %rdx
cmpq %rdx, %rax
jne 0x16336
jmp 0x1635b
xorpd %xmm0, %xmm0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq