// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %rdi, 0x8(%rsp)
movq 0x88(%rdi), %rax
movsbl 0x8b8(%rax), %ecx
movl %ecx, 0x4(%rsp)
movslq %ecx, %r12
movzbl 0x8b9(%rax), %ebp
movsbl 0x8c8(%rax), %ebx
movq %rsi, %r15
movq %rsi, %rdi
movl $0x3, %esi
callq 0xc742
movl $0x8, %esi
movq %r12, %rdi
callq 0x4130
movq %rax, 0x20(%rsp)
movl %ebx, %edi
movl %r12d, %esi
movl %r12d, %edx
callq 0xcaae
movq %rax, 0x28(%rsp)
movl %r12d, %edi
movl %r12d, %esi
callq 0xbb5c
movq %rax, 0x18(%rsp)
movl %ebx, 0x14(%rsp)
movzbl %bl, %r13d
testl %r12d, %r12d
jle 0x2ed58
movq 0x28(%r15), %rax
movq 0x40(%r15), %rcx
movq 0x8(%rsp), %rdx
movq 0x88(%rdx), %rdx
movzbl 0x8b9(%rdx), %edx
xorl %esi, %esi
movsd (%rax,%rsi,8), %xmm0
subsd (%rcx,%rsi,8), %xmm0
movq 0x20(%rsp), %rdi
movsd %xmm0, (%rdi,%rsi,8)
testb %r13b, %r13b
jle 0x2ed12
movq 0x30(%r15), %rdi
movq 0x48(%r15), %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 0x28(%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, %r12
jne 0x2ecf0
incq %r9
cmpq %r13, %r9
jne 0x2ecd0
testb %dl, %dl
jle 0x2ed4c
movq 0x38(%r15), %rdi
movq 0x50(%r15), %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 0x2ed32
incq %rsi
cmpq %r12, %rsi
jne 0x2ecac
testb %r12b, %r12b
jle 0x2ee29
movq 0x58(%r15), %rax
movq 0x70(%r15), %rcx
leal -0x1(%rbp), %edx
movsbq %dl, %rdx
xorl %esi, %esi
movq 0x8(%rsp), %rbx
testb %r13b, %r13b
jle 0x2edc5
xorl %edi, %edi
incq %rdi
cmpq %rdi, %r13
jne 0x2ed7e
movq 0x28(%rsp), %r8
movq -0x8(%r8,%rdi,8), %r8
movq (%r8,%rsi,8), %r8
movsd -0x8(%r8,%r12,8), %xmm1
movq 0x60(%r15), %r8
movq 0x78(%r15), %r9
movq -0x8(%r8,%rdi,8), %r8
movq (%r8,%rsi,8), %r8
movsd -0x8(%r8,%r12,8), %xmm0
movq -0x8(%r9,%rdi,8), %rdi
movq (%rdi,%rsi,8), %rdi
subsd -0x8(%rdi,%r12,8), %xmm0
jmp 0x2edd9
movsd (%rax,%rsi,8), %xmm0
subsd (%rcx,%rsi,8), %xmm0
movq 0x20(%rsp), %rdi
movsd (%rdi,%rsi,8), %xmm1
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm1
testb %bpl, %bpl
jle 0x2ee1b
movq 0x18(%rsp), %rdi
movq (%rdi,%rsi,8), %rdi
movq 0x68(%r15), %r8
movq (%r8,%rsi,8), %r8
movsd (%r8,%rdx,8), %xmm0
movq 0x80(%r15), %r8
movq (%r8,%rsi,8), %r8
subsd (%r8,%rdx,8), %xmm0
movsd (%rdi,%rdx,8), %xmm1
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm1
incq %rsi
cmpq %r12, %rsi
jne 0x2ed77
jmp 0x2ee36
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movq 0x8(%rsp), %rbx
movapd %xmm1, 0x30(%rsp)
callq 0x8d88
movapd 0x30(%rsp), %xmm1
testb %r12b, %r12b
jle 0x2ef04
movq 0x10(%r15), %rax
movq 0x58(%r15), %rcx
xorpd 0x5ae0(%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)
testb %r13b, %r13b
jle 0x2eec2
movq 0x18(%r15), %rsi
movq 0x60(%r15), %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, %r12
jne 0x2ee9c
incq %r8
cmpq %r13, %r8
jne 0x2ee89
testb %bpl, %bpl
jle 0x2eef8
movq 0x20(%r15), %rsi
movq 0x68(%r15), %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, %rbp
jne 0x2eeda
incq %rdx
cmpq %r12, %rdx
jne 0x2ee66
movq %rbx, %rdi
callq 0x114a8
movq %r15, %rbp
movq 0x10(%r15), %r14
testb %al, %al
je 0x2f00f
movq %rbx, %rdi
callq 0x11516
movq %rax, %r15
movq %rbx, %rdi
callq 0x1154c
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
movl %r12d, %ecx
callq 0x79f5
testb %r12b, %r12b
movl 0x4(%rsp), %r15d
jle 0x2f062
movzbl 0x14(%rsp), %eax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
movq %r13, 0x40(%rsp)
testb %r13b, %r13b
movq %rbp, %r13
movq 0x8(%rsp), %rbp
movq 0x30(%rsp), %r15
movl $0x0, %edx
jle 0x2efc3
xorl %ebx, %ebx
movq 0x18(%r13), %rax
cmpb $0x0, 0x80(%rbp)
movq (%rax,%rbx,8), %rax
movq 0x98(%rbp), %rcx
cmoveq %rdx, %rcx
movq (%rax,%r14,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%r14,8), %rsi
movq 0xa0(%rbp), %rax
cmoveq %rdx, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r14,8), %rdx
movl %r12d, %ecx
callq 0x79f5
xorl %edx, %edx
incq %rbx
cmpq %rbx, %r15
jne 0x2ef78
movq %r13, %rbx
movq 0x20(%r13), %rax
movq (%rax,%r14,8), %r15
movq %rbp, %rdi
callq 0x1153a
movq (%rax,%r14,8), %r13
movq %rbp, %rdi
callq 0x11570
movq (%rax,%r14,8), %rdx
movq %r15, %rdi
movq %r13, %rsi
movl %r12d, %ecx
callq 0x79f5
incq %r14
cmpq %r12, %r14
movq %rbx, %rbp
movl 0x4(%rsp), %r15d
movq 0x40(%rsp), %r13
jne 0x2ef5f
jmp 0x2f062
movq %r14, %rdi
movl %r12d, %esi
callq 0x692d
testb %r12b, %r12b
movl 0x4(%rsp), %r15d
jle 0x2f062
xorl %r14d, %r14d
testb %r13b, %r13b
jle 0x2f04a
xorl %ebx, %ebx
movq 0x18(%rbp), %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r14,8), %rdi
movl %r15d, %esi
callq 0x692d
incq %rbx
cmpq %rbx, %r13
jne 0x2f02e
movq 0x20(%rbp), %rax
movq (%rax,%r14,8), %rdi
movl %r15d, %esi
callq 0x692d
incq %r14
cmpq %r12, %r14
jne 0x2f027
movq 0x20(%rsp), %rdi
callq 0x42a0
movq 0x28(%rsp), %rdi
movl 0x14(%rsp), %esi
movl %r15d, %edx
callq 0xca4f
movq 0x18(%rsp), %rdi
movl %r15d, %esi
callq 0xa5e0
movl 0xd0(%rbp), %esi
movq 0xd8(%rbp), %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x2bfa8
movl 0xd0(%rbp), %esi
movq 0xd8(%rbp), %rdx
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1161c
nop