// File: /workspace/github_cpp_makefile_star2_O3/myudelson[P]hmm-scalable/HMMProblemSlicedAB.cpp
// Function: HMMProblemSlicedAB::doLinearStep(FitBitSlicedAB*)
NUMBER HMMProblemSlicedAB::doLinearStep(FitBitSlicedAB *fb) {
	NPAR i,j,m,z;
    NPAR nS = fb->nS, nO = this->p->nO, nZ = this->p->nZ;
    fb->doLog10ScaleGentle(FBS_GRAD);
	
    NCAT xndat = fb->xndat;
    struct data **x_data = fb->x_data;
    fb->init(FBS_PARcopy);
    
	NUMBER e = this->p->ArmijoSeed; // step seed
	bool compliesArmijo = false;
//	bool compliesWolfe2 = false; // second wolfe condition is turned off
	NUMBER f_xk = HMMProblemSlicedAB::getSumLogPOPara(xndat, x_data);
	NUMBER f_xkplus1 = 0;
	
    fb->copy(FBS_PAR, FBS_PARcopy);
	// compute p_k * -p_k
	NUMBER p_k_by_neg_p_k = 0;
    for(i=0; i<nS; i++)
    {
        if(fb->pi != NULL)
            p_k_by_neg_p_k -= fb->gradPI[i]*fb->gradPI[i];
        if(fb->A  != NULL)
            for(j=0; j<nS; j++)
                for(z=0; z<nZ; z++)
                    p_k_by_neg_p_k -= fb->gradA[z][i][j]*fb->gradA[z][i][j];
        if(fb->B  != NULL)
            for(m=0; m<nO; m++)
                for(z=0; z<nZ; z++)
                    p_k_by_neg_p_k -= fb->gradB[z][i][m]*fb->gradB[z][i][m];
    }
	int iter = 0; // limit iter steps to 20, via ArmijoMinStep (now 10)
	while( !(compliesArmijo /*&& compliesWolfe2*/) && e > this->p->ArmijoMinStep) {
		// update
        for(i=0; i<nS; i++) {
            if(fb->pi != NULL) fb->pi[i] = fb->PIcopy[i] - e * fb->gradPI[i];
            if(fb->A  != NULL)
                for(j=0; j<nS; j++)
                    for(z=0; z<nZ; z++)
                        fb->A[z][i][j] = fb->Acopy[z][i][j] - e * fb->gradA[z][i][j];
            if(fb->B  != NULL)
                for(m=0; m<nO; m++)
                    for(z=0; z<nZ; z++)
                        fb->B[z][i][m] = fb->Bcopy[z][i][m] - e * fb->gradB[z][i][m];
        }
        // project parameters to simplex if needs be
        if(fb->projecttosimplex==1) {
            // scale
            if( !this->hasNon01Constraints() ) {
                if(fb->pi != NULL) projectsimplex(fb->pi, nS);
                for(i=0; i<nS; i++) {
                    for(z=0; z<nZ; z++) {
                        if(fb->A  != NULL) projectsimplex(fb->A[z][i], nS);
                        if(fb->B  != NULL) projectsimplex(fb->B[z][i], nS);
                    }
                }
            } else {
                if(fb->pi != NULL) projectsimplexbounded(fb->pi, this->getLbPI(), this->getUbPI(), nS);
                for(i=0; i<nS; i++) {
                    for(z=0; z<nZ; z++) {
                        if(fb->A  != NULL) projectsimplexbounded(fb->A[z][i], this->getLbA()[z][i], this->getUbA()[z][i], nS); // tag is current slice
                        if(fb->B  != NULL) projectsimplexbounded(fb->B[z][i], this->getLbB()[z][i], this->getUbB()[z][i], nO);
                    }
                }
            }
        }
        
		// recompute alpha and p(O|param)
		computeAlphaAndPOParam(xndat, x_data);
		// compute f(x_{k+1})
		f_xkplus1 = HMMProblemSlicedAB::getSumLogPOPara(xndat, x_data);
		// compute Armijo compliance
		compliesArmijo = (f_xkplus1 <= (f_xk + (this->p->ArmijoC1 * e * p_k_by_neg_p_k)));
        
//        // compute Wolfe 2
//        NUMBER p_k_by_neg_p_kp1 = 0;
//        computeGradients(fb);
//        fb->doLog10ScaleGentle(FBS_GRAD);
//        for(i=0; i<nS; i++)
//        {
//            if(fb->pi != NULL) p_k_by_neg_p_kp1 -= fb->gradPI[i]*fb->gradPI[i];
//            if(fb->A  != NULL)
//                for(z=0; z<nZ; z++)
//                    for(j=0; j<nS; j++)
//                        p_k_by_neg_p_kp1 -= fb->gradA[z][i][j]*fb->gradA[z][i][j];
//            if(fb->B  != NULL)
//                for(z=0; z<nZ; z++)
//                    for(m=0; m<nO; m++)
//                        p_k_by_neg_p_kp1 -= fb->gradB[z][i][m]*fb->gradB[z][i][m];
//        }
//        compliesWolfe2 = (p_k_by_neg_p_kp1 >= this->p->ArmijoC2 * p_k_by_neg_p_k); // Wolfe condition
//        // OR
//        compliesWolfe2 = fabs(p_k_by_neg_p_kp1) <= fabs(this->p->ArmijoC2 * p_k_by_neg_p_k); // strong Wolfe condition
        
		e /= (compliesArmijo /*&& compliesWolfe2*/)?1:this->p->ArmijoReduceFactor;
		iter++;
	} // armijo loop
    if(!(compliesArmijo /*&& compliesWolfe2*/)) { // we couldn't step away from current, copy the inital point back
        e = 0;
        fb->copy(FBS_PARcopy, FBS_PAR);
        f_xkplus1 = f_xk;
    }
    fb->destroy(FBS_PARcopy);
    return f_xkplus1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movsbq 0x1(%rsi), %r15
movq 0x88(%rdi), %rax
movzbl 0x8b9(%rax), %r13d
movzbl 0x8c8(%rax), %ebp
movq %rsi, %rdi
movl $0x3, %esi
callq 0xd7be
movl 0xd0(%rbx), %r12d
movq 0xd8(%rbx), %rax
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
movl $0x5, %esi
callq 0xd3be
movq 0x88(%r14), %rax
movsd 0x968(%rax), %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r14, %rdi
movl %r12d, 0x1c(%rsp)
movl %r12d, %esi
movq 0x10(%rsp), %rdx
callq 0x114ae
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rdi
movl $0x1, %esi
movl $0x5, %edx
callq 0xd4e8
testq %r15, %r15
jle 0x29c16
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
cmpq $0x0, 0x20(%rbx)
sete %sil
testb %r13b, %r13b
setle %dl
orb %sil, %dl
xorpd %xmm3, %xmm3
xorl %esi, %esi
movapd 0x20(%rsp), %xmm2
testq %rax, %rax
je 0x29b97
movq 0x58(%rbx), %rdi
movsd (%rdi,%rsi,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm3
testq %rcx, %rcx
je 0x29bd0
xorl %edi, %edi
testb %bpl, %bpl
jle 0x29bc8
movq 0x60(%rbx), %r8
xorl %r9d, %r9d
movq (%r8,%r9,8), %r10
movq (%r10,%rsi,8), %r10
movsd (%r10,%rdi,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm3
incq %r9
cmpq %r9, %rbp
jne 0x29baa
incq %rdi
cmpq %r15, %rdi
jne 0x29b9e
testb %dl, %dl
jne 0x29c08
xorl %edi, %edi
testb %bpl, %bpl
jle 0x29c00
movq 0x68(%rbx), %r8
xorl %r9d, %r9d
movq (%r8,%r9,8), %r10
movq (%r10,%rsi,8), %r10
movsd (%r10,%rdi,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm3
incq %r9
cmpq %r9, %rbp
jne 0x29be2
incq %rdi
cmpq %r13, %rdi
jne 0x29bd6
incq %rsi
cmpq %r15, %rsi
jne 0x29b81
jmp 0x29c20
xorpd %xmm3, %xmm3
movapd 0x20(%rsp), %xmm2
movq 0x88(%r14), %rax
ucomisd 0x970(%rax), %xmm2
jbe 0x29f52
movq %r14, (%rsp)
movsd %xmm3, 0x30(%rsp)
testb %r15b, %r15b
jle 0x29d56
testb %r13b, %r13b
setle %dil
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
movapd %xmm2, %xmm0
xorpd 0x9cdd(%rip), %xmm0     # 0x33940
movq 0x20(%rbx), %rdx
testq %rdx, %rdx
sete %sil
orb %dil, %sil
xorl %edi, %edi
testq %rax, %rax
je 0x29c98
movq 0x58(%rbx), %r8
movq 0x88(%rbx), %r9
movsd (%r8,%rdi,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r9,%rdi,8), %xmm1
movsd %xmm1, (%rax,%rdi,8)
testq %rcx, %rcx
je 0x29cf1
xorl %r8d, %r8d
testb %bpl, %bpl
jle 0x29ce9
movq 0x60(%rbx), %r9
movq 0x90(%rbx), %r10
xorl %r11d, %r11d
movq (%r10,%r11,8), %r14
movq (%r14,%rdi,8), %r14
movq (%r9,%r11,8), %r12
movq (%r12,%rdi,8), %r12
movsd (%r12,%r8,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r14,%r8,8), %xmm1
movq (%rcx,%r11,8), %r14
movq (%r14,%rdi,8), %r14
movsd %xmm1, (%r14,%r8,8)
incq %r11
cmpq %r11, %rbp
jne 0x29cb3
incq %r8
cmpq %r15, %r8
jne 0x29ca0
testb %sil, %sil
jne 0x29d4a
xorl %r8d, %r8d
testb %bpl, %bpl
jle 0x29d42
movq 0x68(%rbx), %r9
movq 0x98(%rbx), %r10
xorl %r11d, %r11d
movq (%r10,%r11,8), %r14
movq (%r14,%rdi,8), %r14
movq (%r9,%r11,8), %r12
movq (%r12,%rdi,8), %r12
movsd (%r12,%r8,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r14,%r8,8), %xmm1
movq (%rdx,%r11,8), %r14
movq (%r14,%rdi,8), %r14
movsd %xmm1, (%r14,%r8,8)
incq %r11
cmpq %r11, %rbp
jne 0x29d0c
incq %r8
cmpq %r13, %r8
jne 0x29cf9
incq %rdi
cmpq %r15, %rdi
jne 0x29c73
movapd %xmm2, 0x20(%rsp)
cmpb $0x1, 0xe0(%rbx)
jne 0x29ee4
movq (%rsp), %r14
movq %r14, %rdi
callq 0x1133a
movq 0x10(%rbx), %rdi
testb %al, %al
je 0x29da0
testq %rdi, %rdi
je 0x29e14
cmpb $0x0, 0x80(%r14)
je 0x29e08
movq 0xa0(%r14), %rsi
movq 0xb8(%r14), %rdx
jmp 0x29e0c
testq %rdi, %rdi
je 0x29dad
movl %r15d, %esi
callq 0x6836
testb %r15b, %r15b
jle 0x29ee4
xorl %r14d, %r14d
testb %bpl, %bpl
jle 0x29dfb
xorl %r12d, %r12d
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x29dda
movq (%rax,%r12,8), %rax
movq (%rax,%r14,8), %rdi
movl %r15d, %esi
callq 0x6836
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x29df3
movq (%rax,%r12,8), %rax
movq (%rax,%r14,8), %rdi
movl %r15d, %esi
callq 0x6836
incq %r12
cmpq %r12, %rbp
jne 0x29dc1
incq %r14
cmpq %r15, %r14
jne 0x29db9
jmp 0x29ee4
xorl %esi, %esi
xorl %edx, %edx
movl %r15d, %ecx
callq 0x7997
testb %r15b, %r15b
jle 0x29ee4
xorl %r12d, %r12d
testb %bpl, %bpl
jle 0x29ed8
xorl %r14d, %r14d
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x29e7c
movq (%rsp), %rdx
cmpb $0x0, 0x80(%rdx)
movq (%rax,%r14,8), %rax
movq 0xa8(%rdx), %rcx
movl $0x0, %r8d
cmoveq %r8, %rcx
movq (%rax,%r12,8), %rdi
movq (%rcx,%r14,8), %rax
movq (%rax,%r12,8), %rsi
movq 0xc0(%rdx), %rax
cmoveq %r8, %rax
movq (%rax,%r14,8), %rax
movq (%rax,%r12,8), %rdx
movl %r15d, %ecx
callq 0x7997
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x29ecc
movq (%rax,%r14,8), %rax
movq (%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq (%rax,%r12,8), %rdi
movq 0xb0(%rcx), %rax
movl $0x0, %edx
cmoveq %rdx, %rax
movq (%rax,%r14,8), %rax
movq 0xc8(%rcx), %rcx
cmoveq %rdx, %rcx
movq (%rax,%r12,8), %rsi
movq (%rcx,%r14,8), %rax
movq (%rax,%r12,8), %rdx
movsbl %r13b, %ecx
callq 0x7997
incq %r14
cmpq %r14, %rbp
jne 0x29e2c
incq %r12
cmpq %r15, %r12
jne 0x29e20
movq (%rsp), %r14
movq %r14, %rdi
movl 0x1c(%rsp), %r12d
movl %r12d, %esi
movq 0x10(%rsp), %rdx
callq 0x25910
movq %r14, %rdi
movl %r12d, %esi
movq 0x10(%rsp), %rdx
callq 0x114ae
movq 0x88(%r14), %rax
movsd 0x950(%rax), %xmm1
movapd 0x20(%rsp), %xmm2
mulsd %xmm2, %xmm1
movsd 0x30(%rsp), %xmm3
mulsd %xmm3, %xmm1
addsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x29f66
divsd 0x960(%rax), %xmm2
ucomisd 0x970(%rax), %xmm2
ja 0x29c3f
movq %rbx, %rdi
movl $0x5, %esi
movl $0x1, %edx
callq 0xd4e8
jmp 0x29f6c
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rdi
movl $0x5, %esi
callq 0xd46e
movsd 0x8(%rsp), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq