// File: /workspace/github_cpp_makefile_star2_O3/myudelson[P]hmm-scalable/HMMProblemSlicedA.cpp
// Function: HMMProblemSlicedA::doBarzilaiBorweinStep(FitBitSlicedA*)
NUMBER HMMProblemSlicedA::doBarzilaiBorweinStep(FitBitSlicedA *fb) {
//NUMBER HMMProblemSlicedA::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 = init3D<NUMBER>((NDAT)nZ, (NDAT)nS, (NDAT)nS);
	NUMBER **s_k_m1_B  = init2D<NUMBER>((NDAT)nS, (NDAT)nS);
	for(i=0; i<nS; i++)
	{
		s_k_m1_PI[i] = fb->PIm1[i] - fb->PIm2[i];
        for(z=0; z<nZ; z++)
            for(j=0; j<nS; j++)
                s_k_m1_A[z][i][j] = fb->Am1[z][i][j] - fb->Am2[z][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;
    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(z=0; z<nZ; z++)
            for(j=0; j<nS; j++) {
                alpha_step_num = s_k_m1_A[z][i][j]*s_k_m1_A[z][i][j];
                alpha_step_den = s_k_m1_A[z][i][j]*(fb->gradA[z][i][j] - fb->gradAm1[z][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(z=0; z<nZ; z++)
            for(j=0; j<nS; j++)
                fb->A[z][i][j] = fb->A[z][i][j] - alpha_step * fb->gradA[z][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() ) {
        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[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[i], this->getLbB()[i], this->getUbB()[i], nS);
        }
    }
	free(s_k_m1_PI);
    free3D<NUMBER>(s_k_m1_A, nZ, nS);
	free2D<NUMBER>(s_k_m1_B, nS);

    // recompute alpha and p(O|param)
    computeAlphaAndPOParam(fb->xndat, fb->x_data);
    return HMMProblemSlicedA::getSumLogPOPara(fb->xndat, fb->x_data);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r13
movq 0x88(%rdi), %rax
movsbq 0x8b8(%rax), %r15
movzbl 0x8b9(%rax), %r12d
movsbl 0x8c8(%rax), %ebx
movq %rsi, 0x8(%rsp)
movq %rsi, %rdi
movl $0x3, %esi
callq 0xc728
movl $0x8, %esi
movq %r15, %rdi
callq 0x4130
movq %rax, %rbp
movl %ebx, %edi
movl %r15d, %esi
movl %r15d, %edx
callq 0xcaae
movq %rax, 0x20(%rsp)
movl %r15d, %edi
movl %r15d, %esi
callq 0xbb36
movq %rax, 0x18(%rsp)
movl %ebx, 0x14(%rsp)
movzbl %bl, %ebx
testq %r15, %r15
movq %rbx, 0x28(%rsp)
jle 0x2ea89
movq 0x8(%rsp), %rcx
movq 0x28(%rcx), %rax
movq 0x40(%rcx), %rcx
movq 0x88(%r13), %rdx
movzbl 0x8b9(%rdx), %edx
xorl %esi, %esi
movsd (%rax,%rsi,8), %xmm0
subsd (%rcx,%rsi,8), %xmm0
movsd %xmm0, (%rbp,%rsi,8)
testb %bl, %bl
jle 0x2e87e
movq 0x8(%rsp), %r8
movq 0x30(%r8), %rdi
movq 0x48(%r8), %r8
xorl %r9d, %r9d
movq (%rdi,%r9,8), %r10
movq (%r10,%rsi,8), %r10
movq (%r8,%r9,8), %r11
movq (%r11,%rsi,8), %r11
movq 0x20(%rsp), %rbx
movq (%rbx,%r9,8), %rbx
movq (%rbx,%rsi,8), %rbx
xorl %r14d, %r14d
movsd (%r10,%r14,8), %xmm0
subsd (%r11,%r14,8), %xmm0
movsd %xmm0, (%rbx,%r14,8)
incq %r14
cmpq %r14, %r15
jne 0x2e857
incq %r9
movq 0x28(%rsp), %rbx
cmpq %rbx, %r9
jne 0x2e837
testb %dl, %dl
jle 0x2e8bd
movq 0x8(%rsp), %r8
movq 0x38(%r8), %rdi
movq 0x50(%r8), %r8
movq (%rdi,%rsi,8), %rdi
movq (%r8,%rsi,8), %r8
movq 0x18(%rsp), %r9
movq (%r9,%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, %rdx
jne 0x2e8a3
incq %rsi
cmpq %r15, %rsi
jne 0x2e813
testb %r15b, %r15b
jle 0x2ea89
movq 0x8(%rsp), %rcx
movq 0x58(%rcx), %rax
movq 0x70(%rcx), %rcx
leal -0x1(%r15), %edx
leal -0x1(%r12), %esi
movzbl %dl, %edx
movzbl %sil, %esi
xorl %edi, %edi
shll $0x3, %edx
shll $0x3, %esi
movq 0x8(%rsp), %r11
testb %bl, %bl
jle 0x2e947
xorl %r8d, %r8d
incq %r8
cmpq %r8, %rbx
jne 0x2e903
movq 0x20(%rsp), %r9
movq -0x8(%r9,%r8,8), %r9
movq (%r9,%rdi,8), %r9
movq 0x60(%r11), %r10
movq -0x8(%r10,%r8,8), %r10
movq (%r10,%rdi,8), %r10
movsd (%r10,%rdx), %xmm0
movq 0x78(%r11), %r10
movq -0x8(%r10,%r8,8), %r8
movq (%r8,%rdi,8), %r8
subsd (%r8,%rdx), %xmm0
movsd (%r9,%rdx), %xmm1
jmp 0x2e957
movsd (%rax,%rdi,8), %xmm0
subsd (%rcx,%rdi,8), %xmm0
movsd (%rbp,%rdi,8), %xmm1
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm1
testb %r12b, %r12b
jle 0x2e99f
movq 0x18(%rsp), %r8
movq (%r8,%rdi,8), %r8
movq 0x8(%rsp), %r10
movq 0x68(%r10), %r9
movq (%r9,%rdi,8), %r9
movsd (%r9,%rsi), %xmm0
movq 0x80(%r10), %r9
movq (%r9,%rdi,8), %r9
subsd (%r9,%rsi), %xmm0
movsd (%r8,%rsi), %xmm1
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm1
incq %rdi
cmpq %r15, %rdi
jne 0x2e8fc
movapd %xmm1, 0x30(%rsp)
callq 0x8df8
movapd 0x30(%rsp), %xmm1
testb %r15b, %r15b
jle 0x2ea92
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0x58(%rcx), %rcx
xorpd 0x4f66(%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)
testb %bl, %bl
jle 0x2ea40
movq 0x8(%rsp), %rdi
movq 0x18(%rdi), %rsi
movq 0x60(%rdi), %rdi
xorl %r8d, %r8d
movq (%rsi,%r8,8), %r9
movq (%r9,%rdx,8), %r9
movq (%rdi,%r8,8), %r10
movq (%r10,%rdx,8), %r10
xorl %r11d, %r11d
movsd (%r10,%r11,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%r9,%r11,8), %xmm0
movsd %xmm0, (%r9,%r11,8)
incq %r11
cmpq %r11, %r15
jne 0x2ea1a
incq %r8
cmpq %rbx, %r8
jne 0x2ea07
testb %r12b, %r12b
jle 0x2ea7b
movq 0x8(%rsp), %rdi
movq 0x20(%rdi), %rsi
movq 0x68(%rdi), %rdi
movq (%rsi,%rdx,8), %rsi
movq (%rdi,%rdx,8), %rdi
xorl %r8d, %r8d
movsd (%rdi,%r8,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rsi,%r8,8), %xmm0
movsd %xmm0, (%rsi,%r8,8)
incq %r8
cmpq %r8, %r12
jne 0x2ea5d
incq %rdx
cmpq %r15, %rdx
jne 0x2e9e0
jmp 0x2ea92
xorpd %xmm0, %xmm0
callq 0x8df8
movq %r13, %rdi
callq 0x1133a
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %r12
testb %al, %al
movq %r13, 0x30(%rsp)
je 0x2eba8
movq %r13, %rdi
callq 0x113a8
movq %rax, %r14
movq %r13, %rdi
callq 0x113de
movq %r12, %rdi
movq %r14, %rsi
movq %rax, %rdx
movl %r15d, %ecx
callq 0x7997
testb %r15b, %r15b
jle 0x2ec02
movslq %r15d, %rax
movq %rax, 0x48(%rsp)
movzbl 0x14(%rsp), %eax
movq %rax, 0x40(%rsp)
xorl %r14d, %r14d
movq %rbp, 0x50(%rsp)
movq 0x30(%rsp), %r12
testb %bl, %bl
movq 0x8(%rsp), %rbp
movq 0x40(%rsp), %r13
movl $0x0, %edx
jle 0x2eb60
xorl %ebx, %ebx
movq 0x18(%rbp), %rax
cmpb $0x0, 0x80(%r12)
movq (%rax,%rbx,8), %rax
movq 0x98(%r12), %rcx
cmoveq %rdx, %rcx
movq (%rax,%r14,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%r14,8), %rsi
movq 0xa0(%r12), %rax
cmoveq %rdx, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r14,8), %rdx
movl %r15d, %ecx
callq 0x7997
xorl %edx, %edx
incq %rbx
cmpq %rbx, %r13
jne 0x2eb11
movq 0x20(%rbp), %rax
movq (%rax,%r14,8), %rbx
movq %r12, %rdi
callq 0x113cc
movq (%rax,%r14,8), %r13
movq %r12, %rdi
callq 0x11402
movq (%rax,%r14,8), %rdx
movq %rbx, %rdi
movq %r13, %rsi
movl %r15d, %ecx
callq 0x7997
incq %r14
cmpq 0x48(%rsp), %r14
movq 0x50(%rsp), %rbp
movq 0x28(%rsp), %rbx
jne 0x2eafc
jmp 0x2ec02
movq %r12, %rdi
movl %r15d, %esi
callq 0x6836
testb %r15b, %r15b
jle 0x2ec02
xorl %r14d, %r14d
movq 0x8(%rsp), %r12
testb %bl, %bl
movq %rbx, %r13
jle 0x2ebe6
xorl %ebx, %ebx
movq 0x18(%r12), %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r14,8), %rdi
movl %r15d, %esi
callq 0x6836
incq %rbx
cmpq %rbx, %r13
jne 0x2ebc9
movq 0x20(%r12), %rax
movq (%rax,%r14,8), %rdi
movl %r15d, %esi
callq 0x6836
incq %r14
cmpq %r15, %r14
movq %r13, %rbx
jne 0x2ebc0
movq %rbp, %rdi
callq 0x42a0
movq 0x20(%rsp), %rdi
movl 0x14(%rsp), %esi
movl %r15d, %edx
callq 0xca4f
movq 0x18(%rsp), %rdi
movl %r15d, %esi
callq 0xa5dd
movq 0x8(%rsp), %r14
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x2bba0
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 0x114ae