// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movzbl 0x1(%rsi), %eax
movl %eax, 0x8(%rsp)
movq 0x88(%rdi), %rax
movzbl 0x8b9(%rax), %r12d
movl 0x8c8(%rax), %ebp
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0xac12
movl 0xd0(%rbx), %r15d
movq 0xd8(%rbx), %r13
pushq $0x5
popq %rsi
movq %rbx, %rdi
callq 0xa854
movq 0x88(%r14), %rax
movsd 0x968(%rax), %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r14, %rdi
movl %r15d, 0x10(%rsp)
movl %r15d, %esi
movq %r13, 0x40(%rsp)
movq %r13, %rdx
callq 0xe400
movsd %xmm0, 0x18(%rsp)
pushq $0x1
popq %rsi
movq %rbx, %rdi
pushq $0x5
popq %rdx
callq 0xa97e
movl 0x8(%rsp), %edi
xorl %eax, %eax
testb %bpl, %bpl
cmovlel %eax, %ebp
testb %dil, %dil
movl %edi, %r15d
cmovlel %eax, %r15d
testb %r12b, %r12b
movl %r12d, %r9d
cmovlel %eax, %r9d
movl $0xff, %ecx
andl %ecx, %r15d
andl %ecx, %ebp
andl %ecx, %r9d
xorpd %xmm3, %xmm3
cmpq %r15, %rax
je 0x23ca6
cmpq $0x0, 0x10(%rbx)
je 0x23c2e
movq 0x58(%rbx), %rcx
movsd (%rcx,%rax,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm3
cmpq $0x0, 0x18(%rbx)
je 0x23c66
xorl %ecx, %ecx
cmpq %r15, %rcx
je 0x23c66
xorl %edx, %edx
cmpq %rdx, %rbp
je 0x23c61
movq 0x60(%rbx), %rsi
movq (%rsi,%rdx,8), %rsi
movq (%rsi,%rax,8), %rsi
movsd (%rsi,%rcx,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm3
incq %rdx
jmp 0x23c3e
incq %rcx
jmp 0x23c37
cmpq $0x0, 0x20(%rbx)
je 0x23c9e
xorl %ecx, %ecx
cmpq %r9, %rcx
je 0x23c9e
xorl %edx, %edx
cmpq %rdx, %rbp
je 0x23c99
movq 0x68(%rbx), %rsi
movq (%rsi,%rdx,8), %rsi
movq (%rsi,%rax,8), %rsi
movsd (%rsi,%rcx,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm3
incq %rdx
jmp 0x23c76
incq %rcx
jmp 0x23c6f
incq %rax
jmp 0x23c0d
xorpd %xmm1, %xmm1
movsbl %r12b, %eax
movl %eax, 0x14(%rsp)
movsbl %dil, %eax
movl %eax, 0x8(%rsp)
xorl %eax, %eax
movapd 0x20(%rsp), %xmm2
movq %r9, 0x38(%rsp)
movsd %xmm3, 0x30(%rsp)
testb $0x1, %al
jne 0x23fe3
movq 0x88(%r14), %rax
ucomisd 0x970(%rax), %xmm2
jbe 0x23fcf
movapd %xmm2, %xmm0
xorpd 0x8c4a(%rip), %xmm0     # 0x2c940
xorl %eax, %eax
cmpq %r15, %rax
je 0x23de0
movq 0x10(%rbx), %rcx
testq %rcx, %rcx
je 0x23d28
movq 0x58(%rbx), %rdx
movq 0x88(%rbx), %rsi
movsd (%rdx,%rax,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%rsi,%rax,8), %xmm1
movsd %xmm1, (%rcx,%rax,8)
movq 0x18(%rbx), %rcx
testq %rcx, %rcx
je 0x23d80
xorl %edx, %edx
cmpq %r15, %rdx
je 0x23d80
xorl %esi, %esi
cmpq %rsi, %rbp
je 0x23d7b
movq 0x60(%rbx), %rdi
movq 0x90(%rbx), %r8
movq (%r8,%rsi,8), %r8
movq (%r8,%rax,8), %r8
movq (%rdi,%rsi,8), %rdi
movq (%rdi,%rax,8), %rdi
movsd (%rdi,%rdx,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r8,%rdx,8), %xmm1
movq (%rcx,%rsi,8), %rdi
movq (%rdi,%rax,8), %rdi
movsd %xmm1, (%rdi,%rdx,8)
incq %rsi
jmp 0x23d3a
incq %rdx
jmp 0x23d33
movq 0x20(%rbx), %rcx
testq %rcx, %rcx
je 0x23dd8
xorl %edx, %edx
cmpq %r9, %rdx
je 0x23dd8
xorl %esi, %esi
cmpq %rsi, %rbp
je 0x23dd3
movq 0x68(%rbx), %rdi
movq 0x98(%rbx), %r8
movq (%r8,%rsi,8), %r8
movq (%r8,%rax,8), %r8
movq (%rdi,%rsi,8), %rdi
movq (%rdi,%rax,8), %rdi
movsd (%rdi,%rdx,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r8,%rdx,8), %xmm1
movq (%rcx,%rsi,8), %rdi
movq (%rdi,%rax,8), %rdi
movsd %xmm1, (%rdi,%rdx,8)
incq %rsi
jmp 0x23d92
incq %rdx
jmp 0x23d8b
incq %rax
jmp 0x23cf8
movapd %xmm2, 0x20(%rsp)
cmpb $0x1, 0xe0(%rbx)
jne 0x23f55
movq %r14, %rdi
callq 0xe1de
movq 0x10(%rbx), %rdi
testb %al, %al
je 0x23e26
testq %rdi, %rdi
je 0x23e93
cmpb $0x0, 0x80(%r14)
je 0x23e86
movq 0xa0(%r14), %rsi
movq 0xb8(%r14), %rdx
jmp 0x23e8a
testq %rdi, %rdi
je 0x23e34
movl 0x8(%rsp), %esi
callq 0x5600
xorl %r12d, %r12d
cmpq %r15, %r12
je 0x23f55
xorl %r13d, %r13d
cmpq %r13, %rbp
je 0x23e81
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x23e62
movq (%rax,%r13,8), %rax
movq (%rax,%r12,8), %rdi
movl 0x8(%rsp), %esi
callq 0x5600
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x23e7c
movq (%rax,%r13,8), %rax
movq (%rax,%r12,8), %rdi
movl 0x8(%rsp), %esi
callq 0x5600
incq %r13
jmp 0x23e43
incq %r12
jmp 0x23e37
xorl %esi, %esi
xorl %edx, %edx
movl 0x8(%rsp), %ecx
callq 0x5d80
xorl %r13d, %r13d
cmpq %r15, %r13
je 0x23f55
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0x23f4d
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x23ef8
cmpb $0x0, 0x80(%r14)
movq (%rax,%r12,8), %rax
movq 0xa8(%r14), %rcx
movl $0x0, %edx
cmoveq %rdx, %rcx
movq (%rax,%r13,8), %rdi
movq (%rcx,%r12,8), %rax
movq (%rax,%r13,8), %rsi
movq 0xc0(%r14), %rax
cmoveq %rdx, %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r13,8), %rdx
movl 0x8(%rsp), %ecx
callq 0x5d80
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x23f45
cmpb $0x0, 0x80(%r14)
movq (%rax,%r12,8), %rax
movq 0xb0(%r14), %rcx
movl $0x0, %edx
cmoveq %rdx, %rcx
movq (%rax,%r13,8), %rdi
movq (%rcx,%r12,8), %rax
movq (%rax,%r13,8), %rsi
movq 0xc8(%r14), %rax
cmoveq %rdx, %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r13,8), %rdx
movl 0x14(%rsp), %ecx
callq 0x5d80
incq %r12
jmp 0x23ea2
incq %r13
jmp 0x23e96
movq %r14, %rdi
movl 0x10(%rsp), %r12d
movl %r12d, %esi
movq 0x40(%rsp), %r13
movq %r13, %rdx
callq 0x2012a
movq %r14, %rdi
movl %r12d, %esi
movq %r13, %rdx
callq 0xe400
movapd %xmm0, %xmm1
movq 0x88(%r14), %rcx
movsd 0x950(%rcx), %xmm0
movapd 0x20(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x30(%rsp), %xmm3
mulsd %xmm3, %xmm0
addsd 0x18(%rsp), %xmm0
ucomisd %xmm1, %xmm0
setae %al
movsd 0x8939(%rip), %xmm0     # 0x2c8f0
jae 0x23fc1
movsd 0x960(%rcx), %xmm0
divsd %xmm0, %xmm2
movq 0x38(%rsp), %r9
jmp 0x23ccd
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0xa97e
movsd 0x18(%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
pushq $0x5
popq %rsi
movq %rbx, %rdi
callq 0xa904
movsd 0x8(%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop