// File: /workspace/github_cpp_makefile_star2_O3/myudelson[P]hmm-scalable/HMMProblemSlicedAB.cpp
// Function: HMMProblemSlicedAB::doBarzilaiBorweinStep(FitBitSlicedAB*)
NUMBER HMMProblemSlicedAB::doBarzilaiBorweinStep(FitBitSlicedAB *fb) {
//NUMBER HMMProblemSlicedAB::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,z;
    NPAR nS = this->p->nS, nO = this->p->nO, nZ = this->p->nZ;
	// first scale down gradients
    fb->doLog10ScaleGentle(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[0][i][j] - fb->Am2[0][i][j]; // WARNING 0 instead of z
        for(m=0; m<this->p->nO; m++)
            s_k_m1_B[i][m] = fb->Bm1[0][i][m] - fb->Bm2[0][i][m];  // WARNING 0 instead of z
	}
    // compute alpha_step
    NUMBER alpha_step = 0;
    NUMBER alpha_step_num = 0;
    NUMBER 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[0][i][j] - fb->gradAm1[0][i][j]);  // WARNING 0 instead of z
        }
        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[0][i][m] - fb->gradBm1[0][i][m]);  // WARNING 0 instead of z
        }
	}
    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++)
            for(z=0; z<nZ; z++)
                fb->A[z][i][j] = fb->A[z][i][j] - alpha_step * fb->gradA[z][i][j];
        for(m=0; m<nO; m++)
            for(z=0; z<nZ; z++)
                fb->B[z][i][m] = fb->B[z][i][m] - alpha_step * fb->gradB[z][i][m];
    }
    // scale
    if( !this->hasNon01Constraints() ) {
        projectsimplex(fb->pi, nS);
        for(i=0; i<nS; i++) {
            for(z=0; z<nZ; z++){
                projectsimplex(fb->A[z][i], nS);
                projectsimplex(fb->B[z][i], nS);
            }
        }
    } else {
        projectsimplexbounded(fb->pi, this->getLbPI(), this->getUbPI(), nS);
        for(i=0; i<nS; i++) {
            for(z=0; z<nZ; z++){
                projectsimplexbounded(fb->A[z][i], this->getLbA()[z][i], this->getUbA()[z][i], nS); // tag is currently fit slice
                projectsimplexbounded(fb->B[z][i], this->getLbB()[z][i], this->getUbB()[z][i], nS);
            }
        }
    }
	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 HMMProblemSlicedAB::getSumLogPOPara(fb->xndat, fb->x_data);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x88(%rdi), %rax
movsbl 0x8b8(%rax), %r13d
movsbl 0x8b9(%rax), %r15d
movzbl 0x8c8(%rax), %r12d
movq %rsi, %rdi
movl $0x3, %esi
callq 0xd7be
movl %r13d, %edi
movl %r13d, %esi
callq 0xbb36
movq %rax, %rbp
movl %r13d, %edi
movl %r15d, %esi
callq 0xbb36
movslq %r13d, %rcx
movq %rcx, 0x8(%rsp)
testl %r13d, %r13d
movq %rbx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
jle 0x288d0
movslq %r15d, %r15
movq 0x88(%rbx), %rcx
movzbl 0x8b9(%rcx), %r11d
movq 0x30(%r14), %rcx
movq 0x48(%r14), %rdx
movq (%rcx), %rcx
movq (%rdx), %rdx
xorl %esi, %esi
movq 0x8(%rsp), %rbx
movq (%rcx,%rsi,8), %rdi
movq (%rdx,%rsi,8), %r8
movq (%rbp,%rsi,8), %r9
xorl %r10d, %r10d
movsd (%rdi,%r10,8), %xmm0
subsd (%r8,%r10,8), %xmm0
movsd %xmm0, (%r9,%r10,8)
incq %r10
cmpq %r10, %rbx
jne 0x286e5
testb %r11b, %r11b
jle 0x2873b
movq 0x38(%r14), %rdi
movq 0x50(%r14), %r8
movq (%rdi), %rdi
movq (%rdi,%rsi,8), %rdi
movq (%r8), %r8
movq (%r8,%rsi,8), %r8
movq (%rax,%rsi,8), %r9
xorl %r10d, %r10d
movsd (%rdi,%r10,8), %xmm0
subsd (%r8,%r10,8), %xmm0
movsd %xmm0, (%r9,%r10,8)
incq %r10
cmpq %r10, %r11
jne 0x28721
incq %rsi
cmpq 0x8(%rsp), %rsi
jne 0x286d5
movq 0x8(%rsp), %r11
testb %r11b, %r11b
movq 0x18(%rsp), %rbx
jle 0x288d0
leal -0x1(%r11), %ecx
leal -0x1(%r15), %esi
movzbl %cl, %r10d
movq 0x60(%r14), %rcx
movq 0x78(%r14), %rdx
movq (%rcx), %rcx
movq (%rdx), %rdx
movzbl %sil, %esi
xorl %edi, %edi
shll $0x3, %r10d
shll $0x3, %esi
testb %r15b, %r15b
jle 0x287b5
movq (%rax,%rdi,8), %r8
movq 0x68(%r14), %r9
movq (%r9), %r9
movq (%r9,%rdi,8), %r9
movsd (%r9,%rsi), %xmm0
movq 0x80(%r14), %r9
movq (%r9), %r9
movq (%r9,%rdi,8), %r9
subsd (%r9,%rsi), %xmm0
movsd (%r8,%rsi), %xmm1
jmp 0x287d4
movq (%rbp,%rdi,8), %r8
movsd (%r8,%r10), %xmm1
movq (%rcx,%rdi,8), %r8
movsd (%r8,%r10), %xmm0
movq (%rdx,%rdi,8), %r8
subsd (%r8,%r10), %xmm0
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm1
incq %rdi
cmpq %rdi, %r11
jne 0x2877f
movapd %xmm1, 0x30(%rsp)
callq 0x8df8
movapd 0x30(%rsp), %xmm1
cmpb $0x0, 0x8(%rsp)
jle 0x288d9
movq 0x10(%r14), %rax
movq 0x58(%r14), %rcx
xorpd 0xb130(%rip), %xmm1     # 0x33940
divsd %xmm0, %xmm1
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rax,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
xorl %esi, %esi
testb %r12b, %r12b
jle 0x28869
movq 0x18(%r14), %rdi
movq 0x60(%r14), %r8
xorl %r9d, %r9d
movq (%rdi,%r9,8), %r10
movq (%r10,%rdx,8), %r10
movq (%r8,%r9,8), %r11
movq (%r11,%rdx,8), %r11
movsd (%r11,%rsi,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%r10,%rsi,8), %xmm0
movsd %xmm0, (%r10,%rsi,8)
incq %r9
cmpq %r9, %r12
jne 0x2883b
incq %rsi
cmpq 0x8(%rsp), %rsi
jne 0x2882b
testb %r15b, %r15b
jle 0x288c0
xorl %esi, %esi
testb %r12b, %r12b
jle 0x288b8
movq 0x20(%r14), %rdi
movq 0x68(%r14), %r8
xorl %r9d, %r9d
movq (%rdi,%r9,8), %r10
movq (%r10,%rdx,8), %r10
movq (%r8,%r9,8), %r11
movq (%r11,%rdx,8), %r11
movsd (%r11,%rsi,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%r10,%rsi,8), %xmm0
movsd %xmm0, (%r10,%rsi,8)
incq %r9
cmpq %r9, %r12
jne 0x2888a
incq %rsi
cmpq %r15, %rsi
jne 0x2887a
incq %rdx
cmpq 0x8(%rsp), %rdx
jne 0x28816
jmp 0x288d9
xorpd %xmm0, %xmm0
callq 0x8df8
movq %rbx, %rdi
callq 0x1133a
movq 0x10(%r14), %rdi
testb %al, %al
movq %rbp, 0x28(%rsp)
je 0x2890c
cmpb $0x0, 0x80(%rbx)
movq 0x8(%rsp), %rcx
je 0x28969
movq 0xa0(%rbx), %rsi
movq 0xb8(%rbx), %rdx
jmp 0x2896d
movq 0x8(%rsp), %rbx
movl %ebx, %esi
callq 0x6836
testb %bl, %bl
jle 0x28a3d
xorl %ebx, %ebx
testb %r12b, %r12b
jle 0x2895a
xorl %r15d, %r15d
movq 0x18(%r14), %rax
movq (%rax,%r15,8), %rax
movq (%rax,%rbx,8), %rdi
movl %r13d, %esi
callq 0x6836
movq 0x20(%r14), %rax
movq (%rax,%r15,8), %rax
movq (%rax,%rbx,8), %rdi
movl %r13d, %esi
callq 0x6836
incq %r15
cmpq %r15, %r12
jne 0x2892a
incq %rbx
cmpq 0x8(%rsp), %rbx
jne 0x28922
jmp 0x28a3d
xorl %esi, %esi
xorl %edx, %edx
callq 0x7997
movq 0x8(%rsp), %rcx
testb %cl, %cl
jle 0x28a3d
xorl %r15d, %r15d
xorl %ebp, %ebp
movl %r13d, 0x14(%rsp)
testb %r12b, %r12b
movq 0x18(%rsp), %r13
jle 0x28a2c
xorl %ebx, %ebx
movq 0x18(%r14), %rax
cmpb $0x0, 0x80(%r13)
movq (%rax,%rbx,8), %rax
movq 0xa8(%r13), %rdx
cmoveq %r15, %rdx
movq (%rax,%rbp,8), %rdi
movq (%rdx,%rbx,8), %rax
movq (%rax,%rbp,8), %rsi
movq 0xc0(%r13), %rax
cmoveq %r15, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%rbp,8), %rdx
callq 0x7997
movq 0x20(%r14), %rax
cmpb $0x0, 0x80(%r13)
movq (%rax,%rbx,8), %rax
movq 0xb0(%r13), %rcx
cmoveq %r15, %rcx
movq (%rax,%rbp,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%rbp,8), %rsi
movq 0xc8(%r13), %rax
cmoveq %r15, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%rbp,8), %rdx
movl 0x14(%rsp), %ecx
callq 0x7997
movq 0x8(%rsp), %rcx
incq %rbx
cmpq %rbx, %r12
jne 0x28999
incq %rbp
cmpq %rcx, %rbp
movl 0x14(%rsp), %r13d
jne 0x28989
movq 0x20(%rsp), %rdi
movl %r13d, %esi
callq 0xa5dd
movq 0x28(%rsp), %rdi
movl %r13d, %esi
callq 0xa5dd
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x25910
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x114ae