// File: /workspace/github_cpp_makefile_star2_O3/myudelson[P]hmm-scalable/HMMProblemSlicedA.cpp
// Function: HMMProblemSlicedA::doLinearStep(FitBitSlicedA*)
NUMBER HMMProblemSlicedA::doLinearStep(FitBitSlicedA *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 = HMMProblemSlicedA::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(z=0; z<nZ; z++)
                for(j=0; j<nS; j++)
                    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++)
                p_k_by_neg_p_k -= fb->gradB[i][m]*fb->gradB[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(z=0; z<nZ; z++)
                    for(j=0; j<nS; j++)
                        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++)
                    fb->B[i][m] = fb->Bcopy[i][m] - e * fb->gradB[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[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[i], this->getLbB()[i], this->getUbB()[i], nO);
                }
            }
        }
        
		// recompute alpha and p(O|param)
		computeAlphaAndPOParam(xndat, x_data);
		// compute f(x_{k+1})
		f_xkplus1 = HMMProblemSlicedA::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), %ebp
movq 0x88(%rdi), %rax
movzbl 0x8b9(%rax), %r15d
movzbl 0x8c8(%rax), %r12d
movq %rsi, %rdi
movl $0x3, %esi
callq 0xc728
movl 0xd0(%rbx), %r13d
movq 0xd8(%rbx), %rax
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
movl $0x5, %esi
callq 0xc33a
movq 0x88(%r14), %rax
movsd 0x968(%rax), %xmm0
movapd %xmm0, 0x30(%rsp)
movq %r14, (%rsp)
movq %r14, %rdi
movl %r13d, 0x2c(%rsp)
movl %r13d, %esi
movq 0x18(%rsp), %rdx
callq 0x114ae
movsd %xmm0, 0x10(%rsp)
movq %rbx, %rdi
movl $0x1, %esi
movl $0x5, %edx
callq 0xc464
testb %bpl, %bpl
jle 0x2fc60
cmpq $0x0, 0x18(%rbx)
sete %sil
testb %r12b, %r12b
setle %al
cmpq $0x0, 0x20(%rbx)
movq 0x10(%rbx), %rcx
sete %dil
testb %r15b, %r15b
setle %dl
orb %sil, %al
orb %dil, %dl
xorpd %xmm3, %xmm3
xorl %esi, %esi
movapd 0x30(%rsp), %xmm2
testq %rcx, %rcx
je 0x2fbfd
movq 0x58(%rbx), %rdi
movsd (%rdi,%rsi,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm3
testb %al, %al
jne 0x2fc31
movq 0x60(%rbx), %rdi
xorl %r8d, %r8d
movq (%rdi,%r8,8), %r9
movq (%r9,%rsi,8), %r9
xorl %r10d, %r10d
movsd (%r9,%r10,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm3
incq %r10
cmpq %r10, %rbp
jne 0x2fc13
incq %r8
cmpq %r12, %r8
jne 0x2fc08
testb %dl, %dl
jne 0x2fc56
movq 0x68(%rbx), %rdi
movq (%rdi,%rsi,8), %rdi
xorl %r8d, %r8d
movsd (%rdi,%r8,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm3
incq %r8
cmpq %r8, %r15
jne 0x2fc40
incq %rsi
cmpq %rbp, %rsi
jne 0x2fbe7
jmp 0x2fc6a
xorpd %xmm3, %xmm3
movapd 0x30(%rsp), %xmm2
movq (%rsp), %rax
movq 0x88(%rax), %rax
ucomisd 0x970(%rax), %xmm2
jbe 0x2ff88
movsd %xmm3, 0x40(%rsp)
testb %bpl, %bpl
jle 0x2fd9f
testb %r15b, %r15b
setle %dl
testb %r12b, %r12b
setle %sil
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
movapd %xmm2, %xmm0
xorpd 0x3c8d(%rip), %xmm0     # 0x33940
testq %rcx, %rcx
sete %r8b
movq 0x20(%rbx), %rdi
movq %rdi, 0x20(%rsp)
testq %rdi, %rdi
sete %dil
orb %sil, %r8b
movb %r8b, 0x8(%rsp)
orb %dl, %dil
xorl %r8d, %r8d
testq %rax, %rax
je 0x2fcfe
movq 0x58(%rbx), %rdx
movq 0x88(%rbx), %rsi
movsd (%rdx,%r8,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%rsi,%r8,8), %xmm1
movsd %xmm1, (%rax,%r8,8)
cmpb $0x0, 0x8(%rsp)
jne 0x2fd53
movq 0x60(%rbx), %r9
movq 0x90(%rbx), %r10
xorl %r11d, %r11d
movq (%r10,%r11,8), %rdx
movq (%rdx,%r8,8), %r14
movq (%r9,%r11,8), %rdx
movq (%rdx,%r8,8), %r13
movq (%rcx,%r11,8), %rdx
movq (%rdx,%r8,8), %rsi
xorl %edx, %edx
movsd (%r13,%rdx,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r14,%rdx,8), %xmm1
movsd %xmm1, (%rsi,%rdx,8)
incq %rdx
cmpq %rdx, %rbp
jne 0x2fd2d
incq %r11
cmpq %r12, %r11
jne 0x2fd13
testb %dil, %dil
jne 0x2fd93
movq 0x68(%rbx), %rdx
movq 0x98(%rbx), %rsi
movq (%rsi,%r8,8), %r9
movq (%rdx,%r8,8), %r10
movq 0x20(%rsp), %rdx
movq (%rdx,%r8,8), %rsi
xorl %edx, %edx
movsd (%r10,%rdx,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r9,%rdx,8), %xmm1
movsd %xmm1, (%rsi,%rdx,8)
incq %rdx
cmpq %rdx, %r15
jne 0x2fd76
incq %r8
cmpq %rbp, %r8
jne 0x2fcd8
movapd %xmm2, 0x30(%rsp)
cmpb $0x1, 0xe0(%rbx)
jne 0x2ff1a
movq (%rsp), %rdi
callq 0x1133a
movq 0x10(%rbx), %r13
testb %al, %al
je 0x2feb9
testq %r13, %r13
je 0x2fdf9
movq (%rsp), %r14
movq %r14, %rdi
callq 0x113a8
movq %rax, 0x8(%rsp)
movq %r14, %rdi
callq 0x113de
movsbl %bpl, %ecx
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq %rax, %rdx
callq 0x7997
testb %bpl, %bpl
jle 0x2ff1a
xorl %r13d, %r13d
testb %r12b, %r12b
jle 0x2fe65
xorl %r14d, %r14d
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x2fe5d
movq (%rax,%r14,8), %rax
movq (%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq (%rax,%r13,8), %rdi
movq 0x98(%rcx), %rax
movl $0x0, %edx
cmoveq %rdx, %rax
movq (%rax,%r14,8), %rax
movq 0xa0(%rcx), %rcx
cmoveq %rdx, %rcx
movq (%rax,%r13,8), %rsi
movq (%rcx,%r14,8), %rax
movq (%rax,%r13,8), %rdx
movsbl %bpl, %ecx
callq 0x7997
incq %r14
cmpq %r14, %r12
jne 0x2fe0d
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x2feab
movq (%rax,%r13,8), %rax
movq %rax, 0x8(%rsp)
movq (%rsp), %r14
movq %r14, %rdi
callq 0x113cc
movq (%rax,%r13,8), %rax
movq %rax, 0x20(%rsp)
movq %r14, %rdi
callq 0x11402
movq (%rax,%r13,8), %rdx
movsbl %r15b, %ecx
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x7997
incq %r13
cmpq %rbp, %r13
jne 0x2fe05
jmp 0x2ff1a
testq %r13, %r13
je 0x2feca
movsbl %bpl, %esi
movq %r13, %rdi
callq 0x6836
testb %bpl, %bpl
jle 0x2ff1a
xorl %r13d, %r13d
testb %r12b, %r12b
jle 0x2fefc
xorl %r14d, %r14d
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x2fef4
movq (%rax,%r14,8), %rax
movq (%rax,%r13,8), %rdi
movsbl %bpl, %esi
callq 0x6836
incq %r14
cmpq %r14, %r12
jne 0x2feda
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x2ff12
movq (%rax,%r13,8), %rdi
movsbl %bpl, %esi
callq 0x6836
incq %r13
cmpq %rbp, %r13
jne 0x2fed2
movq (%rsp), %r14
movq %r14, %rdi
movl 0x2c(%rsp), %r13d
movl %r13d, %esi
movq 0x18(%rsp), %rdx
callq 0x2bba0
movq %r14, %rdi
movl %r13d, %esi
movq 0x18(%rsp), %rdx
callq 0x114ae
movq 0x88(%r14), %rax
movsd 0x950(%rax), %xmm1
movapd 0x30(%rsp), %xmm2
mulsd %xmm2, %xmm1
movsd 0x40(%rsp), %xmm3
mulsd %xmm3, %xmm1
addsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x2ff9c
divsd 0x960(%rax), %xmm2
ucomisd 0x970(%rax), %xmm2
ja 0x2fc89
movq %rbx, %rdi
movl $0x5, %esi
movl $0x1, %edx
callq 0xc464
jmp 0x2ffa2
movsd %xmm0, 0x10(%rsp)
movq %rbx, %rdi
movl $0x5, %esi
callq 0xc3ea
movsd 0x10(%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq