// File: /workspace/github_cpp_makefile_star2_O1/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 0xc742
movl 0xd0(%rbx), %r13d
movq 0xd8(%rbx), %rax
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
movl $0x5, %esi
callq 0xc350
movq 0x88(%r14), %rax
movsd 0x968(%rax), %xmm0
movapd %xmm0, 0x30(%rsp)
movq %r14, (%rsp)
movq %r14, %rdi
movl %r13d, 0x24(%rsp)
movl %r13d, %esi
movq 0x10(%rsp), %rdx
callq 0x1161c
movsd %xmm0, 0x28(%rsp)
movq %rbx, %rdi
movl $0x1, %esi
movl $0x5, %edx
callq 0xc47a
xorpd %xmm0, %xmm0
xorpd %xmm3, %xmm3
testb %bpl, %bpl
jle 0x3008c
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
xorl %esi, %esi
testq %rcx, %rcx
je 0x3002b
movq 0x58(%rbx), %rdi
movsd (%rdi,%rsi,8), %xmm1
mulsd %xmm1, %xmm1
subsd %xmm1, %xmm3
testb %al, %al
jne 0x3005f
movq 0x60(%rbx), %rdi
xorl %r8d, %r8d
movq (%rdi,%r8,8), %r9
movq (%r9,%rsi,8), %r9
xorl %r10d, %r10d
movsd (%r9,%r10,8), %xmm1
mulsd %xmm1, %xmm1
subsd %xmm1, %xmm3
incq %r10
cmpq %r10, %rbp
jne 0x30041
incq %r8
cmpq %r12, %r8
jne 0x30036
testb %dl, %dl
jne 0x30084
movq 0x68(%rbx), %rdi
movq (%rdi,%rsi,8), %rdi
xorl %r8d, %r8d
movsd (%rdi,%r8,8), %xmm1
mulsd %xmm1, %xmm1
subsd %xmm1, %xmm3
incq %r8
cmpq %r8, %r15
jne 0x3006e
incq %rsi
cmpq %rbp, %rsi
jne 0x30015
movq (%rsp), %rax
movq 0x88(%rax), %rax
movapd 0x30(%rsp), %xmm2
ucomisd 0x970(%rax), %xmm2
jbe 0x303d3
movsd %xmm3, 0x40(%rsp)
testb %bpl, %bpl
jle 0x301c7
testb %r15b, %r15b
setle %dl
testb %r12b, %r12b
setle %sil
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
movapd %xmm2, %xmm0
xorpd 0x4865(%rip), %xmm0     # 0x34940
testq %rcx, %rcx
sete %r8b
movq 0x20(%rbx), %rdi
movq %rdi, 0x18(%rsp)
testq %rdi, %rdi
sete %dil
orb %sil, %r8b
movb %r8b, 0x8(%rsp)
orb %dl, %dil
xorl %r8d, %r8d
testq %rax, %rax
je 0x30126
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 0x3017b
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 0x30155
incq %r11
cmpq %r12, %r11
jne 0x3013b
testb %dil, %dil
jne 0x301bb
movq 0x68(%rbx), %rdx
movq 0x98(%rbx), %rsi
movq (%rsi,%r8,8), %r9
movq (%rdx,%r8,8), %r10
movq 0x18(%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 0x3019e
incq %r8
cmpq %rbp, %r8
jne 0x30100
movapd %xmm2, 0x30(%rsp)
cmpb $0x1, 0xe0(%rbx)
jne 0x30343
movq (%rsp), %rdi
callq 0x114a8
testb %al, %al
je 0x302e1
movq 0x10(%rbx), %r13
testq %r13, %r13
je 0x30221
movq (%rsp), %r14
movq %r14, %rdi
callq 0x11516
movq %rax, 0x8(%rsp)
movq %r14, %rdi
callq 0x1154c
movsbl %bpl, %ecx
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq %rax, %rdx
callq 0x79f5
testb %bpl, %bpl
jle 0x30343
xorl %r13d, %r13d
testb %r12b, %r12b
jle 0x3028d
xorl %r14d, %r14d
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x30285
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 0x79f5
incq %r14
cmpq %r14, %r12
jne 0x30235
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x302d3
movq (%rax,%r13,8), %rax
movq %rax, 0x8(%rsp)
movq (%rsp), %r14
movq %r14, %rdi
callq 0x1153a
movq (%rax,%r13,8), %rax
movq %rax, 0x18(%rsp)
movq %r14, %rdi
callq 0x11570
movq (%rax,%r13,8), %rdx
movsbl %r15b, %ecx
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x79f5
incq %r13
cmpq %rbp, %r13
jne 0x3022d
jmp 0x30343
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x302f3
movsbl %bpl, %esi
callq 0x692d
testb %bpl, %bpl
jle 0x30343
xorl %r13d, %r13d
testb %r12b, %r12b
jle 0x30325
xorl %r14d, %r14d
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x3031d
movq (%rax,%r14,8), %rax
movq (%rax,%r13,8), %rdi
movsbl %bpl, %esi
callq 0x692d
incq %r14
cmpq %r14, %r12
jne 0x30303
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x3033b
movq (%rax,%r13,8), %rdi
movsbl %bpl, %esi
callq 0x692d
incq %r13
cmpq %rbp, %r13
jne 0x302fb
movq (%rsp), %r14
movq %r14, %rdi
movl 0x24(%rsp), %r13d
movl %r13d, %esi
movq 0x10(%rsp), %rdx
callq 0x2bfa8
movq %r14, %rdi
movl %r13d, %esi
movq 0x10(%rsp), %rdx
callq 0x1161c
movq 0x88(%r14), %rcx
movsd 0x950(%rcx), %xmm4
movapd 0x30(%rsp), %xmm2
mulsd %xmm2, %xmm4
movsd 0x40(%rsp), %xmm3
mulsd %xmm3, %xmm4
addsd 0x28(%rsp), %xmm4
ucomisd %xmm0, %xmm4
setae %al
movsd 0x454c(%rip), %xmm1     # 0x348f0
jae 0x303ae
movsd 0x960(%rcx), %xmm1
ucomisd %xmm0, %xmm4
jae 0x303f1
divsd %xmm1, %xmm2
movq (%rsp), %rcx
movq 0x88(%rcx), %rcx
ucomisd 0x970(%rcx), %xmm2
ja 0x300b1
jmp 0x303d5
xorl %eax, %eax
testb %al, %al
jne 0x303f1
movq %rbx, %rdi
movl $0x5, %esi
movl $0x1, %edx
callq 0xc47a
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x5, %esi
callq 0xc400
movsd (%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop