// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rcx
movb 0x1(%rcx), %cl
movb %cl, -0x15(%rbp)
movq 0x88(%rax), %rcx
movb 0x8b9(%rcx), %cl
movb %cl, -0x16(%rbp)
movq 0x88(%rax), %rax
movb 0x8c8(%rax), %al
movb %al, -0x17(%rbp)
movq -0x10(%rbp), %rdi
movl $0x3, %esi
callq 0x10e30
movq -0x10(%rbp), %rax
movl 0xd0(%rax), %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movl $0x5, %esi
callq 0x102c0
movq -0x60(%rbp), %rdi
movq 0x88(%rdi), %rax
movsd 0x968(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movb $0x0, -0x31(%rbp)
movl -0x1c(%rbp), %esi
movq -0x28(%rbp), %rdx
callq 0x1a9f0
movsd %xmm0, -0x40(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x5, %edx
callq 0x10a20
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movb $0x0, -0x11(%rbp)
movsbl -0x11(%rbp), %eax
movsbl -0x15(%rbp), %ecx
cmpl %ecx, %eax
jge 0x49e10
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x49cb9
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movsbq -0x11(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movsbq -0x11(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x50(%rbp), %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x49d7b
movb $0x0, -0x14(%rbp)
movsbl -0x14(%rbp), %eax
movsbl -0x17(%rbp), %ecx
cmpl %ecx, %eax
jge 0x49d79
movb $0x0, -0x12(%rbp)
movsbl -0x12(%rbp), %eax
movsbl -0x15(%rbp), %ecx
cmpl %ecx, %eax
jge 0x49d6a
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movsbq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x12(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movsbq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x12(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x50(%rbp), %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movb -0x12(%rbp), %al
addb $0x1, %al
movb %al, -0x12(%rbp)
jmp 0x49ce0
jmp 0x49d6c
movb -0x14(%rbp), %al
addb $0x1, %al
movb %al, -0x14(%rbp)
jmp 0x49ccc
jmp 0x49d7b
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x49e01
movb $0x0, -0x13(%rbp)
movsbl -0x13(%rbp), %eax
movsbl -0x16(%rbp), %ecx
cmpl %ecx, %eax
jge 0x49dff
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x13(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x13(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x50(%rbp), %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movb -0x13(%rbp), %al
addb $0x1, %al
movb %al, -0x13(%rbp)
jmp 0x49d8a
jmp 0x49e01
jmp 0x49e03
movb -0x11(%rbp), %al
addb $0x1, %al
movb %al, -0x11(%rbp)
jmp 0x49c51
movl $0x0, -0x54(%rbp)
xorl %eax, %eax
testb $0x1, -0x31(%rbp)
movb %al, -0x61(%rbp)
jne 0x49e40
movq -0x60(%rbp), %rax
movsd -0x30(%rbp), %xmm0
movq 0x88(%rax), %rax
ucomisd 0x970(%rax), %xmm0
seta %al
movb %al, -0x61(%rbp)
movb -0x61(%rbp), %al
testb $0x1, %al
jne 0x49e4c
jmp 0x4a35e
movb $0x0, -0x11(%rbp)
movsbl -0x11(%rbp), %eax
movsbl -0x15(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4a069
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x49ec8
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movsbq -0x11(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd -0x30(%rbp), %xmm0
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movsbq -0x11(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movsbq -0x11(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x49fb0
movb $0x0, -0x14(%rbp)
movsbl -0x14(%rbp), %eax
movsbl -0x17(%rbp), %ecx
cmpl %ecx, %eax
jge 0x49fae
movb $0x0, -0x12(%rbp)
movsbl -0x12(%rbp), %eax
movsbl -0x15(%rbp), %ecx
cmpl %ecx, %eax
jge 0x49f9f
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movsbq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x12(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd -0x30(%rbp), %xmm0
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movsbq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x12(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movsbq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x12(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x12(%rbp), %al
addb $0x1, %al
movb %al, -0x12(%rbp)
jmp 0x49eef
jmp 0x49fa1
movb -0x14(%rbp), %al
addb $0x1, %al
movb %al, -0x14(%rbp)
jmp 0x49edb
jmp 0x49fb0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x4a05a
movb $0x0, -0x13(%rbp)
movsbl -0x13(%rbp), %eax
movsbl -0x16(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4a058
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x13(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd -0x30(%rbp), %xmm0
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x13(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x13(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x13(%rbp), %al
addb $0x1, %al
movb %al, -0x13(%rbp)
jmp 0x49fc3
jmp 0x4a05a
jmp 0x4a05c
movb -0x11(%rbp), %al
addb $0x1, %al
movb %al, -0x11(%rbp)
jmp 0x49e50
movq -0x10(%rbp), %rax
movsbl 0xe0(%rax), %eax
cmpl $0x1, %eax
jne 0x4a2a7
movq -0x60(%rbp), %rdi
callq 0x1a4e0
testb $0x1, %al
jne 0x4a13d
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x4a0aa
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movsbl -0x15(%rbp), %esi
callq 0x7980
movb $0x0, -0x11(%rbp)
movsbl -0x11(%rbp), %eax
movsbl -0x15(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4a138
movb $0x0, -0x14(%rbp)
movsbl -0x14(%rbp), %eax
movsbl -0x17(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4a104
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x4a0f8
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movsbq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movsbl -0x15(%rbp), %esi
callq 0x7980
jmp 0x4a0fa
movb -0x14(%rbp), %al
addb $0x1, %al
movb %al, -0x14(%rbp)
jmp 0x4a0be
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x4a129
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movsbl -0x15(%rbp), %esi
callq 0x7980
jmp 0x4a12b
movb -0x11(%rbp), %al
addb $0x1, %al
movb %al, -0x11(%rbp)
jmp 0x4a0ae
jmp 0x4a2a5
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x4a17e
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x78(%rbp)
callq 0x1a6b0
movq -0x60(%rbp), %rdi
movq %rax, -0x70(%rbp)
callq 0x1a770
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq %rax, %rdx
movsbl -0x15(%rbp), %ecx
callq 0x87e0
movb $0x0, -0x11(%rbp)
movsbl -0x11(%rbp), %eax
movsbl -0x15(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4a2a3
movb $0x0, -0x14(%rbp)
movsbl -0x14(%rbp), %eax
movsbl -0x17(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4a22f
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x4a220
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movsbq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x88(%rbp)
callq 0x43820
movq -0x60(%rbp), %rdi
movsbq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
callq 0x43860
movq -0x88(%rbp), %rdi
movq -0x80(%rbp), %rsi
movsbq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movsbl -0x15(%rbp), %ecx
callq 0x87e0
jmp 0x4a222
movb -0x14(%rbp), %al
addb $0x1, %al
movb %al, -0x14(%rbp)
jmp 0x4a196
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x4a294
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
callq 0x1a730
movq -0x60(%rbp), %rdi
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x90(%rbp)
callq 0x1a7f0
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rsi
movsbq -0x11(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movsbl -0x16(%rbp), %ecx
callq 0x87e0
jmp 0x4a296
movb -0x11(%rbp), %al
addb $0x1, %al
movb %al, -0x11(%rbp)
jmp 0x4a182
jmp 0x4a2a5
jmp 0x4a2a7
movq -0x60(%rbp), %rdi
movl -0x1c(%rbp), %esi
movq -0x28(%rbp), %rdx
callq 0x43a00
movq -0x60(%rbp), %rdi
movl -0x1c(%rbp), %esi
movq -0x28(%rbp), %rdx
callq 0x1a9f0
movq -0x60(%rbp), %rax
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm1
movsd -0x40(%rbp), %xmm2
movq 0x88(%rax), %rax
movsd 0x950(%rax), %xmm0
mulsd -0x30(%rbp), %xmm0
movsd -0x50(%rbp), %xmm3
mulsd %xmm3, %xmm0
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
setae %al
andb $0x1, %al
movb %al, -0x31(%rbp)
testb $0x1, -0x31(%rbp)
je 0x4a31f
movsd 0x56f3(%rip), %xmm0     # 0x4fa08
movsd %xmm0, -0xa0(%rbp)
jmp 0x4a33a
movq -0x60(%rbp), %rax
movq 0x88(%rax), %rax
movsd 0x960(%rax), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0xa0(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x49e17
testb $0x1, -0x31(%rbp)
jne 0x4a389
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movl $0x5, %esi
movl $0x1, %edx
callq 0x10a20
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movl $0x5, %esi
callq 0x106f0
movsd -0x48(%rbp), %xmm0
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)