// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq 0x88(%rdi), %rax
movsbq 0x8b8(%rax), %r15
movsbl 0x8b9(%rax), %ebp
movq %rsi, %rdi
movl $0x3, %esi
callq 0xb70e
movl $0x8, %esi
movq %r15, %rdi
callq 0x4130
movq %rax, 0x8(%rsp)
movl %r15d, %edi
movl %r15d, %esi
callq 0xbb36
movq %rax, %r13
movl %r15d, %edi
movl %ebp, %esi
callq 0xbb36
movq %rax, (%rsp)
movzbl %bpl, %ebp
testq %r15, %r15
jle 0x16051
movq 0x28(%rbx), %rax
movq 0x30(%rbx), %rcx
movq 0x40(%rbx), %rdx
movq 0x88(%r14), %rsi
movzbl 0x8b9(%rsi), %esi
movq 0x48(%rbx), %rdi
xorl %r8d, %r8d
movsd (%rax,%r8,8), %xmm0
subsd (%rdx,%r8,8), %xmm0
movq 0x8(%rsp), %r9
movsd %xmm0, (%r9,%r8,8)
movq (%rcx,%r8,8), %r9
movq (%rdi,%r8,8), %r10
movq (%r13,%r8,8), %r11
xorl %r12d, %r12d
movsd (%r9,%r12,8), %xmm0
subsd (%r10,%r12,8), %xmm0
movsd %xmm0, (%r11,%r12,8)
incq %r12
cmpq %r12, %r15
jne 0x15e52
testb %sil, %sil
jle 0x15ea6
movq 0x38(%rbx), %r9
movq 0x50(%rbx), %r10
movq (%r9,%r8,8), %r9
movq (%r10,%r8,8), %r10
movq (%rsp), %r11
movq (%r11,%r8,8), %r11
xorl %r12d, %r12d
movsd (%r9,%r12,8), %xmm0
subsd (%r10,%r12,8), %xmm0
movsd %xmm0, (%r11,%r12,8)
incq %r12
cmpq %r12, %rsi
jne 0x15e8c
incq %r8
cmpq %r15, %r8
jne 0x15e2b
testb %r15b, %r15b
jle 0x16051
movq 0x58(%rbx), %rax
movq 0x60(%rbx), %rcx
movq 0x70(%rbx), %rdx
movq 0x78(%rbx), %rsi
xorpd %xmm3, %xmm3
xorl %edi, %edi
movsd (%rax,%rdi,8), %xmm0
subsd (%rdx,%rdi,8), %xmm0
movq 0x8(%rsp), %r8
movsd (%r8,%rdi,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
movq (%r13,%rdi,8), %r8
movq (%rcx,%rdi,8), %r9
movq (%rsi,%rdi,8), %r10
xorl %r11d, %r11d
movsd (%r9,%r11,8), %xmm0
subsd (%r10,%r11,8), %xmm0
movsd (%r8,%r11,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 %r11
cmpq %r11, %r15
jne 0x15f0a
testb %bpl, %bpl
jle 0x15f89
movq (%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), %xmm0
subsd (%r10,%r11,8), %xmm0
movsd (%r8,%r11,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 %r11
cmpq %r11, %rbp
jne 0x15f5b
incq %rdi
cmpq %r15, %rdi
jne 0x15ed1
movapd %xmm3, %xmm0
unpckhpd %xmm3, %xmm0            # xmm0 = xmm0[1],xmm3[1]
movapd %xmm3, 0x10(%rsp)
callq 0x8df8
movapd 0x10(%rsp), %xmm1
testb %r15b, %r15b
jle 0x1605a
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
xorpd 0x1d979(%rip), %xmm1    # 0x33940
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 0x15ff3
testb %bpl, %bpl
jle 0x16047
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 0x16029
incq %rdi
cmpq %r15, %rdi
jne 0x15fd5
jmp 0x1605a
xorpd %xmm0, %xmm0
callq 0x8df8
movq %r13, 0x10(%rsp)
cmpb $0x1, 0x80(%r14)
jne 0x1610a
cmpb $0x0, 0xf9(%rbx)
je 0x1610a
movq 0x10(%rbx), %rdi
movq 0x50(%r14), %rsi
movq 0x68(%r14), %rdx
movl %r15d, %ecx
callq 0x7997
testb %r15b, %r15b
jle 0x1614c
xorl %r12d, %r12d
movsbl %bpl, %ebp
xorl %r13d, %r13d
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rdi
cmpb $0x0, 0x80(%r14)
movq 0x58(%r14), %rax
cmoveq %r12, %rax
movq (%rax,%r13,8), %rsi
movq 0x70(%r14), %rax
cmoveq %r12, %rax
movq (%rax,%r13,8), %rdx
movl %r15d, %ecx
callq 0x7997
movq 0x20(%rbx), %rax
movq (%rax,%r13,8), %rdi
cmpb $0x0, 0x80(%r14)
movq 0x60(%r14), %rax
cmoveq %r12, %rax
movq (%rax,%r13,8), %rsi
movq 0x78(%r14), %rax
cmoveq %r12, %rax
movq (%rax,%r13,8), %rdx
movl %ebp, %ecx
callq 0x7997
incq %r13
cmpq %r13, %r15
jne 0x160a1
jmp 0x1614c
movq 0x10(%rbx), %rdi
movl %r15d, %esi
callq 0x6836
testb %r15b, %r15b
jle 0x1614c
movslq %r15d, %r12
xorl %r13d, %r13d
movsbl %bpl, %ebp
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rdi
movl %r15d, %esi
callq 0x6836
movq 0x20(%rbx), %rax
movq (%rax,%r13,8), %rdi
movl %ebp, %esi
callq 0x6836
incq %r13
cmpq %r13, %r12
jne 0x16125
movq 0x8(%rsp), %rdi
callq 0x42a0
movq (%rsp), %rdi
movl %r15d, %esi
callq 0xa5dd
movq 0x10(%rsp), %rdi
movl %r15d, %esi
callq 0xa5dd
movl 0xe8(%rbx), %esi
movq 0xf0(%rbx), %rdx
movq %r14, %rdi
callq 0x11908
movslq 0xe8(%rbx), %rax
testq %rax, %rax
jle 0x161be
movq 0xf0(%rbx), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
cmpl $0x0, 0x4(%rsi)
xorpd %xmm1, %xmm1
jne 0x161b0
movsd 0x50(%rsi), %xmm1
addsd %xmm1, %xmm0
incq %rdx
cmpq %rdx, %rax
jne 0x1619d
jmp 0x161c2
xorpd %xmm0, %xmm0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop