// File: /workspace/github_cpp_makefile_star2_O2/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 %rsi, %r14
movq %rdi, %r15
movq 0x88(%rdi), %rax
movzbl 0x8b8(%rax), %r12d
movb 0x8b9(%rax), %cl
movb %cl, 0x30(%rsp)
movsbl 0x8c8(%rax), %ebp
pushq $0x3
popq %rsi
movq %r14, %rdi
callq 0x9d84
movsbl %r12b, %ebx
movl %ebx, %edi
callq 0x7dde
movq %rax, 0x48(%rsp)
movl %ebp, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0xa0be
movq %rax, %r13
movl %ebx, %edi
movl %ebx, 0xc(%rsp)
movl %ebx, %esi
callq 0x9328
movq 0x48(%rsp), %r10
movq %r15, 0x10(%rsp)
movq 0x88(%r15), %r11
xorl %ecx, %ecx
testb %bpl, %bpl
movl $0x0, %edx
movl %ebp, 0x28(%rsp)
cmovgl %ebp, %edx
testb %r12b, %r12b
movl $0x0, %ebp
cmovgl %r12d, %ebp
movslq %edx, %r15
xorl %edx, %edx
cmpq %rbp, %rdx
je 0x27f7f
movq 0x28(%r14), %rsi
movq 0x40(%r14), %rdi
movsd (%rsi,%rdx,8), %xmm0
subsd (%rdi,%rdx,8), %xmm0
movsd %xmm0, (%r10,%rdx,8)
xorl %esi, %esi
cmpq %r15, %rsi
je 0x27f33
xorl %edi, %edi
cmpq %rdi, %r12
je 0x27f2e
movq 0x30(%r14), %r8
movq 0x48(%r14), %r9
movq (%r8,%rsi,8), %r8
movq (%r8,%rdx,8), %r8
movsd (%r8,%rdi,8), %xmm0
movq (%r9,%rsi,8), %r8
movq (%r8,%rdx,8), %r8
subsd (%r8,%rdi,8), %xmm0
movq (%r13,%rsi,8), %r8
movq (%r8,%rdx,8), %r8
movsd %xmm0, (%r8,%rdi,8)
incq %rdi
jmp 0x27ef1
incq %rsi
jmp 0x27eea
movzbl 0x8b9(%r11), %esi
testb %sil, %sil
cmovlel %ecx, %esi
movzbl %sil, %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x27f77
movq 0x38(%r14), %r8
movq 0x50(%r14), %r9
movq (%r8,%rdx,8), %r8
movsd (%r8,%rdi,8), %xmm0
movq (%r9,%rdx,8), %r8
subsd (%r8,%rdi,8), %xmm0
movq (%rax,%rdx,8), %r8
movsd %xmm0, (%r8,%rdi,8)
incq %rdi
jmp 0x27f47
incq %rdx
jmp 0x27ec7
movzbl 0x30(%rsp), %ecx
xorl %r8d, %r8d
testb %cl, %cl
cmovlel %r8d, %ecx
movzbl %cl, %ebx
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
cmpq %rbp, %r8
je 0x28055
movsd (%r10,%r8,8), %xmm1
movq 0x58(%r14), %rcx
movq 0x70(%r14), %rdx
movsd (%rcx,%r8,8), %xmm0
subsd (%rdx,%r8,8), %xmm0
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm1
xorl %ecx, %ecx
cmpq %r15, %rcx
je 0x28013
xorl %edx, %edx
cmpq %rdx, %r12
je 0x2800e
movq (%r13,%rcx,8), %rsi
movq (%rsi,%r8,8), %rsi
movsd (%rsi,%rdx,8), %xmm1
movq 0x60(%r14), %rsi
movq 0x78(%r14), %rdi
movq (%rsi,%rcx,8), %rsi
movq (%rsi,%r8,8), %rsi
movsd (%rsi,%rdx,8), %xmm0
movq (%rdi,%rcx,8), %rsi
movq (%rsi,%r8,8), %rsi
subsd (%rsi,%rdx,8), %xmm0
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm1
incq %rdx
jmp 0x27fcc
incq %rcx
jmp 0x27fc5
xorl %ecx, %ecx
cmpq %rcx, %rbx
je 0x2804d
movq (%rax,%r8,8), %rdx
movsd (%rdx,%rcx,8), %xmm1
movq 0x68(%r14), %rdx
movq 0x80(%r14), %rsi
movq (%rdx,%r8,8), %rdx
movsd (%rdx,%rcx,8), %xmm0
movq (%rsi,%r8,8), %rdx
subsd (%rdx,%rcx,8), %xmm0
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm1
incq %rcx
jmp 0x28015
incq %r8
jmp 0x27f98
movq %rax, 0x50(%rsp)
movapd %xmm1, 0x30(%rsp)
callq 0x67cb
movapd 0x30(%rsp), %xmm1
xorpd 0x48cd(%rip), %xmm1     # 0x2c940
divsd %xmm0, %xmm1
movzbl %r12b, %eax
xorl %ecx, %ecx
cmpq %rbp, %rcx
je 0x2811c
movq 0x10(%r14), %rdx
movq 0x58(%r14), %rsi
movsd (%rsi,%rcx,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, (%rdx,%rcx,8)
xorl %edx, %edx
cmpq %r15, %rdx
je 0x280e5
xorl %esi, %esi
cmpq %rsi, %rax
je 0x280e0
movq 0x18(%r14), %rdi
movq 0x60(%r14), %r8
movq (%rdi,%rdx,8), %rdi
movq (%rdi,%rcx,8), %rdi
movq (%r8,%rdx,8), %r8
movq (%r8,%rcx,8), %r8
movsd (%r8,%rsi,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rdi,%rsi,8), %xmm0
movsd %xmm0, (%rdi,%rsi,8)
incq %rsi
jmp 0x280aa
incq %rdx
jmp 0x280a3
xorl %edx, %edx
cmpq %rdx, %rbx
je 0x28114
movq 0x20(%r14), %rsi
movq 0x68(%r14), %rdi
movq (%rsi,%rcx,8), %rsi
movq (%rdi,%rcx,8), %rdi
movsd (%rdi,%rdx,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rsi,%rdx,8), %xmm0
movsd %xmm0, (%rsi,%rdx,8)
incq %rdx
jmp 0x280e7
incq %rcx
jmp 0x2807d
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0xe1de
movq 0x10(%r14), %rdi
testb %al, %al
movq %rbp, 0x30(%rsp)
je 0x28223
movq %rdi, 0x20(%rsp)
movq %rbx, %rdi
callq 0xe2a0
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
callq 0xe2d6
movsbl %r12b, %ecx
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rax, %rdx
movl %ecx, 0x2c(%rsp)
callq 0x5d80
movzbl %r15b, %ebx
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpq %rbp, %r12
je 0x28274
xorl %ebp, %ebp
cmpq %rbp, %rbx
je 0x281d4
movq 0x18(%r14), %rax
movq 0x10(%rsp), %rdx
cmpb $0x0, 0x80(%rdx)
movq (%rax,%rbp,8), %rax
movq 0x98(%rdx), %rcx
cmoveq %r15, %rcx
movq (%rax,%r12,8), %rdi
movq (%rcx,%rbp,8), %rax
movq (%rax,%r12,8), %rsi
movq 0xa0(%rdx), %rax
cmoveq %r15, %rax
movq (%rax,%rbp,8), %rax
movq (%rax,%r12,8), %rdx
movl 0xc(%rsp), %ecx
callq 0x5d80
incq %rbp
jmp 0x28183
movq 0x20(%r14), %rax
movq (%rax,%r12,8), %rax
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
callq 0xe2c4
movq (%rax,%r12,8), %rax
movq %rax, 0x18(%rsp)
movq %rbp, %rdi
callq 0xe2fa
movq (%rax,%r12,8), %rdx
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl 0x2c(%rsp), %ecx
callq 0x5d80
incq %r12
movq 0x30(%rsp), %rbp
jmp 0x28178
movl 0xc(%rsp), %esi
callq 0x5600
movzbl %r15b, %ebx
xorl %r15d, %r15d
cmpq %rbp, %r15
je 0x28274
xorl %ebp, %ebp
cmpq %rbp, %rbx
je 0x28259
movq 0x18(%r14), %rax
movq (%rax,%rbp,8), %rax
movq (%rax,%r15,8), %rdi
movsbl %r12b, %esi
callq 0x5600
incq %rbp
jmp 0x2823a
movq 0x20(%r14), %rax
movq (%rax,%r15,8), %rdi
movl 0xc(%rsp), %esi
callq 0x5600
incq %r15
movq 0x30(%rsp), %rbp
jmp 0x28233
movq 0x48(%rsp), %rdi
callq 0x4290
movq %r13, %rdi
movl 0x28(%rsp), %esi
movl 0xc(%rsp), %ebx
movl %ebx, %edx
callq 0xa064
movq 0x50(%rsp), %rdi
movl %ebx, %esi
callq 0x7f2c
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x25848
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