// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rsi, %r14
movq %rdi, %r15
movq 0x88(%rdi), %rax
movzbl 0x8b8(%rax), %r12d
movb 0x8b9(%rax), %bl
movb 0x8c8(%rax), %al
movb %al, 0x18(%rsp)
pushq $0x3
popq %rsi
movq %r14, %rdi
callq 0xac12
movsbl %r12b, %r13d
movl %r13d, %edi
callq 0x7dde
movq %rax, 0x28(%rsp)
movl %r13d, %edi
movl %r13d, %esi
callq 0x9328
movq %rax, %rbp
movb %bl, 0x10(%rsp)
movsbl %bl, %esi
movl %r13d, 0xc(%rsp)
movl %r13d, %edi
movq 0x28(%rsp), %rbx
callq 0x9328
movq %r15, 0x20(%rsp)
movq 0x88(%r15), %r10
xorl %ecx, %ecx
testb %r12b, %r12b
movl $0x0, %r13d
cmovgl %r12d, %r13d
xorl %edx, %edx
cmpq %r13, %rdx
je 0x229eb
movq 0x28(%r14), %rsi
movq 0x40(%r14), %rdi
movsd (%rsi,%rdx,8), %xmm0
subsd (%rdi,%rdx,8), %xmm0
movsd %xmm0, (%rbx,%rdx,8)
xorl %esi, %esi
cmpq %rsi, %r12
je 0x22999
movq 0x30(%r14), %rdi
movq 0x48(%r14), %r8
movq (%rdi), %rdi
movq (%rdi,%rdx,8), %rdi
movsd (%rdi,%rsi,8), %xmm0
movq (%r8), %rdi
movq (%rdi,%rdx,8), %rdi
subsd (%rdi,%rsi,8), %xmm0
movq (%rbp,%rdx,8), %rdi
movsd %xmm0, (%rdi,%rsi,8)
incq %rsi
jmp 0x22965
movzbl 0x8b9(%r10), %esi
testb %sil, %sil
cmovlel %ecx, %esi
movzbl %sil, %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x229e3
movq 0x38(%r14), %r8
movq 0x50(%r14), %r9
movq (%r8), %r8
movq (%r8,%rdx,8), %r8
movsd (%r8,%rdi,8), %xmm0
movq (%r9), %r8
movq (%r8,%rdx,8), %r8
subsd (%r8,%rdi,8), %xmm0
movq (%rax,%rdx,8), %r8
movsd %xmm0, (%r8,%rdi,8)
incq %rdi
jmp 0x229ad
incq %rdx
jmp 0x22943
movb 0x10(%rsp), %dl
movzbl %dl, %ecx
xorl %edi, %edi
testb %dl, %dl
cmovlel %edi, %ecx
movzbl %cl, %r15d
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
cmpq %r13, %rdi
je 0x22ab3
movsd (%rbx,%rdi,8), %xmm1
movq 0x58(%r14), %rcx
movq 0x70(%r14), %rdx
movsd (%rcx,%rdi,8), %xmm0
subsd (%rdx,%rdi,8), %xmm0
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm1
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x22a6b
movq (%rbp,%rdi,8), %rdx
movsd (%rdx,%rcx,8), %xmm1
movq 0x60(%r14), %rdx
movq 0x78(%r14), %rsi
movq (%rdx), %rdx
movq (%rdx,%rdi,8), %rdx
movsd (%rdx,%rcx,8), %xmm0
movq (%rsi), %rdx
movq (%rdx,%rdi,8), %rdx
subsd (%rdx,%rcx,8), %xmm0
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm1
incq %rcx
jmp 0x22a2f
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x22aab
movq (%rax,%rdi,8), %rdx
movsd (%rdx,%rcx,8), %xmm1
movq 0x68(%r14), %rdx
movq 0x80(%r14), %rsi
movq (%rdx), %rdx
movq (%rdx,%rdi,8), %rdx
movsd (%rdx,%rcx,8), %xmm0
movq (%rsi), %rdx
movq (%rdx,%rdi,8), %rdx
subsd (%rdx,%rcx,8), %xmm0
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm1
incq %rcx
jmp 0x22a6d
incq %rdi
jmp 0x22a05
movq %rax, 0x30(%rsp)
movq %rbp, 0x10(%rsp)
movapd %xmm1, 0x40(%rsp)
callq 0x67cb
movapd 0x40(%rsp), %xmm1
xorpd 0x9e6a(%rip), %xmm1     # 0x2c940
divsd %xmm0, %xmm1
movzbl 0x18(%rsp), %ebp
xorl %eax, %eax
testb %bpl, %bpl
cmovlel %eax, %ebp
movl $0xff, %edx
movl 0xc(%rsp), %ecx
andl %edx, %ecx
andl %edx, %ebp
cmpq %r13, %rax
je 0x22ba8
movq 0x10(%r14), %rdx
movq 0x58(%r14), %rsi
movsd (%rsi,%rax,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rdx,%rax,8), %xmm0
movsd %xmm0, (%rdx,%rax,8)
xorl %edx, %edx
cmpq %rcx, %rdx
je 0x22b5c
xorl %esi, %esi
cmpq %rsi, %rbp
je 0x22b57
movq 0x18(%r14), %rdi
movq 0x60(%r14), %r8
movq (%rdi,%rsi,8), %rdi
movq (%rdi,%rax,8), %rdi
movq (%r8,%rsi,8), %r8
movq (%r8,%rax,8), %r8
movsd (%r8,%rdx,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rdi,%rdx,8), %xmm0
movsd %xmm0, (%rdi,%rdx,8)
incq %rsi
jmp 0x22b21
incq %rdx
jmp 0x22b1a
xorl %edx, %edx
cmpq %r15, %rdx
je 0x22ba0
xorl %esi, %esi
cmpq %rsi, %rbp
je 0x22b9b
movq 0x20(%r14), %rdi
movq 0x68(%r14), %r8
movq (%rdi,%rsi,8), %rdi
movq (%rdi,%rax,8), %rdi
movq (%r8,%rsi,8), %r8
movq (%r8,%rax,8), %r8
movsd (%r8,%rdx,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rdi,%rdx,8), %xmm0
movsd %xmm0, (%rdi,%rdx,8)
incq %rsi
jmp 0x22b65
incq %rdx
jmp 0x22b5e
incq %rax
jmp 0x22af4
movq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0xe1de
movq 0x10(%r14), %rdi
testb %al, %al
je 0x22cb6
movq %r13, 0x18(%rsp)
movq %r15, %rax
xorl %r15d, %r15d
cmpb $0x0, 0x80(%rax)
movl $0x0, %esi
movl $0x0, %edx
je 0x22bed
movq 0xa0(%rax), %rsi
movq 0xb8(%rax), %rdx
movsbl %r12b, %ecx
callq 0x5d80
xorl %r13d, %r13d
movq %r12, 0x38(%rsp)
movq 0x20(%rsp), %r12
cmpq 0x18(%rsp), %r15
je 0x22d02
xorl %ebx, %ebx
cmpq %rbx, %rbp
je 0x22cae
movq 0x18(%r14), %rax
cmpb $0x0, 0x80(%r12)
movq (%rax,%rbx,8), %rax
movq 0xa8(%r12), %rcx
cmoveq %r13, %rcx
movq (%rax,%r15,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%r15,8), %rsi
movq 0xc0(%r12), %rax
cmoveq %r13, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r15,8), %rdx
movl 0xc(%rsp), %ecx
callq 0x5d80
movq 0x20(%r14), %rax
cmpb $0x0, 0x80(%r12)
movq (%rax,%rbx,8), %rax
movq 0xb0(%r12), %rcx
cmoveq %r13, %rcx
movq (%rax,%r15,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%r15,8), %rsi
movq 0xc8(%r12), %rax
cmoveq %r13, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r15,8), %rdx
movsbl 0x38(%rsp), %ecx
callq 0x5d80
incq %rbx
jmp 0x22c10
incq %r15
jmp 0x22c03
movl 0xc(%rsp), %esi
callq 0x5600
xorl %ebx, %ebx
cmpq %r13, %rbx
je 0x22d02
xorl %r15d, %r15d
cmpq %r15, %rbp
je 0x22cfd
movq 0x18(%r14), %rax
movq (%rax,%r15,8), %rax
movq (%rax,%rbx,8), %rdi
movsbl %r12b, %esi
callq 0x5600
movq 0x20(%r14), %rax
movq (%rax,%r15,8), %rax
movq (%rax,%rbx,8), %rdi
movl 0xc(%rsp), %esi
callq 0x5600
incq %r15
jmp 0x22cc9
incq %rbx
jmp 0x22cc1
movq 0x28(%rsp), %rdi
callq 0x4290
movq 0x30(%rsp), %rdi
movl 0xc(%rsp), %ebx
movl %ebx, %esi
callq 0x7f2c
movq 0x10(%rsp), %rdi
movl %ebx, %esi
callq 0x7f2c
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x2012a
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe400
nop