// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq 0x88(%rdi), %rax
movsbl 0x8b8(%rax), %r13d
movsbl 0x8b9(%rax), %r15d
movzbl 0x8c8(%rax), %r12d
movq %rsi, %rdi
movl $0x3, %esi
callq 0xd7a6
movl %r13d, %edi
movl %r13d, %esi
callq 0xbb5c
movq %rax, %rbp
movl %r13d, %edi
movl %r15d, %ebx
movl %r15d, %esi
callq 0xbb5c
movslq %r13d, %rcx
movq %rcx, 0x8(%rsp)
testl %r13d, %r13d
jle 0x28c58
movq 0x10(%rsp), %rcx
movq 0x88(%rcx), %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), %r15
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, %r15
jne 0x28bf8
testb %r11b, %r11b
jle 0x28c4e
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 0x28c34
incq %rsi
cmpq 0x8(%rsp), %rsi
jne 0x28be8
movslq %ebx, %r15
cmpb $0x0, 0x8(%rsp)
jle 0x28cfe
leal -0x1(%r15), %edx
movq 0x60(%r14), %rsi
movq 0x78(%r14), %rcx
movq (%rsi), %r9
movq (%rcx), %rcx
movsbq %dl, %rdx
xorl %esi, %esi
movq 0x10(%rsp), %rbx
testb %r15b, %r15b
jle 0x28cc5
movq (%rax,%rsi,8), %rdi
movq 0x68(%r14), %r8
movq (%r8), %r8
movq (%r8,%rsi,8), %r8
movsd (%r8,%rdx,8), %xmm0
movq 0x80(%r14), %r8
movq (%r8), %r8
movq (%r8,%rsi,8), %r8
subsd (%r8,%rdx,8), %xmm0
movsd (%rdi,%rdx,8), %xmm1
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm1
movq 0x8(%rsp), %r8
jmp 0x28cf4
movq (%rbp,%rsi,8), %rdi
movq 0x8(%rsp), %r8
movsd -0x8(%rdi,%r8,8), %xmm1
movq (%r9,%rsi,8), %rdi
movsd -0x8(%rdi,%r8,8), %xmm0
movq (%rcx,%rsi,8), %rdi
subsd -0x8(%rdi,%r8,8), %xmm0
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm1
incq %rsi
cmpq %rsi, %r8
jne 0x28c83
jmp 0x28d0b
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movq 0x10(%rsp), %rbx
movapd %xmm1, 0x30(%rsp)
movq %rax, 0x20(%rsp)
movq %rbp, 0x28(%rsp)
callq 0x8d88
movapd 0x30(%rsp), %xmm1
movq 0x8(%rsp), %rbp
testb %bpl, %bpl
jle 0x28dfe
movq 0x10(%r14), %rax
movq 0x58(%r14), %rcx
xorpd 0xbbfc(%rip), %xmm1     # 0x34940
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 0x28d9d
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 0x28d6f
incq %rsi
cmpq %rbp, %rsi
jne 0x28d5f
testb %r15b, %r15b
jle 0x28df2
xorl %esi, %esi
testb %r12b, %r12b
jle 0x28dea
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 0x28dbc
incq %rsi
cmpq %r15, %rsi
jne 0x28dac
incq %rdx
cmpq %rbp, %rdx
jne 0x28d4a
movq %rbx, %rdi
callq 0x114a8
movq 0x10(%r14), %rdi
testb %al, %al
je 0x28e27
cmpb $0x0, 0x80(%rbx)
je 0x28e80
movq 0xa0(%rbx), %rsi
movq 0xb8(%rbx), %rdx
jmp 0x28e84
movl %ebp, %esi
callq 0x692d
testb %bpl, %bpl
jle 0x28f5f
xorl %ebx, %ebx
testb %r12b, %r12b
jle 0x28e71
xorl %r15d, %r15d
movq 0x18(%r14), %rax
movq (%rax,%r15,8), %rax
movq (%rax,%rbx,8), %rdi
movl %r13d, %esi
callq 0x692d
movq 0x20(%r14), %rax
movq (%rax,%r15,8), %rax
movq (%rax,%rbx,8), %rdi
movl %r13d, %esi
callq 0x692d
incq %r15
cmpq %r15, %r12
jne 0x28e41
incq %rbx
cmpq 0x8(%rsp), %rbx
jne 0x28e39
jmp 0x28f5f
xorl %esi, %esi
xorl %edx, %edx
movl %ebp, %ecx
callq 0x79f5
testb %bpl, %bpl
jle 0x28f5f
xorl %edx, %edx
xorl %ebp, %ebp
movl %r13d, 0x1c(%rsp)
movq 0x8(%rsp), %r15
testb %r12b, %r12b
movq 0x10(%rsp), %r13
jle 0x28f4e
xorl %ebx, %ebx
movq 0x18(%r14), %rax
cmpb $0x0, 0x80(%r13)
movq (%rax,%rbx,8), %rax
movq 0xa8(%r13), %rsi
cmoveq %rdx, %rsi
movq (%rax,%rbp,8), %rdi
movq (%rsi,%rbx,8), %rax
movq (%rax,%rbp,8), %rsi
movq 0xc0(%r13), %rax
cmoveq %rdx, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%rbp,8), %rdx
movl %r15d, %ecx
callq 0x79f5
movq 0x20(%r14), %rax
cmpb $0x0, 0x80(%r13)
movq (%rax,%rbx,8), %rax
movq 0xb0(%r13), %rcx
movl $0x0, %edx
cmoveq %rdx, %rcx
movq (%rax,%rbp,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%rbp,8), %rsi
movq 0xc8(%r13), %rax
movl $0x0, %ecx
cmoveq %rcx, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%rbp,8), %rdx
movl %r15d, %ecx
callq 0x79f5
xorl %edx, %edx
incq %rbx
cmpq %rbx, %r12
jne 0x28eb2
incq %rbp
cmpq %r15, %rbp
movl 0x1c(%rsp), %r13d
jne 0x28ea2
movq 0x20(%rsp), %rdi
movl %r13d, %esi
callq 0xa5e0
movq 0x28(%rsp), %rdi
movl %r13d, %esi
callq 0xa5e0
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x25d92
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 0x1161c