// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/HMMProblem.cpp
// Function: HMMProblem::doLinearStepBig(FitBit**, int)
NUMBER HMMProblem::doLinearStepBig(FitBit **fbs, NCAT nfbs) {
	NPAR i,j,m;
    NCAT q;
    NUMBER e;
    NUMBER f_xk = 0;
    NUMBER f_xkplus1 = 0;
    NUMBER p_k_by_neg_p_k = 0;

    bool compliesArmijo = false;
    
    e = this->p->ArmijoSeed; // step seed
    f_xk = HMMProblem::getSumLogPOPara(this->p->nSeq, this->p->k_data);
    p_k_by_neg_p_k = 0;
    int iter = 0; // limit iter steps to 20, via ArmijoMinStep (now 10)
    for(q=0; q<nfbs; q++) {
        fbs[q]->doLog10ScaleGentleByRow(FBS_GRAD);
        fbs[q]->init(FBS_PARcopy);
        fbs[q]->copy(FBS_PAR, FBS_PARcopy);
        for(i=0; i<fbs[q]->nS; i++)
        {
            if(fbs[q]->pi != NULL) p_k_by_neg_p_k -= fbs[q]->gradPI[i]*fbs[q]->gradPI[i];
            if(fbs[q]->A  != NULL) for(j=0; j<fbs[q]->nS; j++) p_k_by_neg_p_k -= fbs[q]->gradA[i][j]*fbs[q]->gradA[i][j];
            if(fbs[q]->B  != NULL) for(m=0; m<fbs[q]->nO; m++) p_k_by_neg_p_k -= fbs[q]->gradB[i][m]*fbs[q]->gradB[i][m];
        }
    }
    
    while( !compliesArmijo && e > this->p->ArmijoMinStep) {
        for(q=0;q<nfbs;q++) { // apply step
            // update
            for(i=0; i<fbs[q]->nS; i++) {
                if(fbs[q]->pi != NULL) fbs[q]->pi[i] = fbs[q]->PIcopy[i] - e * fbs[q]->gradPI[i];
                if(fbs[q]->A  != NULL)
                    for(j=0; j<fbs[q]->nS; j++)
                        fbs[q]->A[i][j] = fbs[q]->Acopy[i][j] - e * fbs[q]->gradA[i][j];
                if(fbs[q]->B  != NULL)
                    for(m=0; m<fbs[q]->nO; m++)
                        fbs[q]->B[i][m] = fbs[q]->Bcopy[i][m] - e * fbs[q]->gradB[i][m];
            }
            // project parameters to simplex if needs be
            if(fbs[q]->projecttosimplex==1) {
                // scale
                if( !this->hasNon01Constraints() || fbs[q]->observe_non01_boundaries==0 ) {
                    if(fbs[q]->pi != NULL) projectsimplex(fbs[q]->pi, fbs[q]->nS);
                    for(i=0; i<fbs[q]->nS; i++) {
                        if(fbs[q]->A  != NULL) projectsimplex(fbs[q]->A[i], fbs[q]->nS);
                        if(fbs[q]->B  != NULL) projectsimplex(fbs[q]->B[i], fbs[q]->nO);
                    }
                } else {
                    if(fbs[q]->pi != NULL) projectsimplexbounded(fbs[q]->pi, this->getLbPI(), this->getUbPI(), fbs[q]->nS);
                    
                    for(i=0; i<fbs[q]->nS; i++) {
                        if(fbs[q]->A  != NULL) projectsimplexbounded(fbs[q]->A[i], this->getLbA()[i], this->getUbA()[i], fbs[q]->nS);
                        if(fbs[q]->B  != NULL) projectsimplexbounded(fbs[q]->B[i], this->getLbB()[i], this->getUbB()[i], fbs[q]->nO);
                    }
                }
            }
        } // apply step
        
        // recompute alpha and p(O|param)
        computeAlphaAndPOParam(this->p->nSeq, this->p->k_data);
        
        // compute f(x_{k+1})
        f_xkplus1 = HMMProblem::getSumLogPOPara(this->p->nSeq, this->p->k_data);
        // compute Armijo compliance
        compliesArmijo = (f_xkplus1 <= (f_xk + (this->p->ArmijoC1 * e * p_k_by_neg_p_k)));
        e /= (compliesArmijo)?1:this->p->ArmijoReduceFactor;
        iter++;
    } // armijo loop
    if(!compliesArmijo) { // we couldn't step away from current, copy the inital point back
        e = 0;
        for(q=0;q<nfbs;q++) { // apply step
            fbs[q]->copy(FBS_PARcopy, FBS_PAR);
        }
    }
    for(q=0;q<nfbs;q++) { // apply step
        fbs[q]->destroy(FBS_PARcopy);
    }

    return f_xkplus1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r14
movq 0x88(%rdi), %rax
movslq 0x8dc(%rax), %rcx
xorpd %xmm2, %xmm2
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
testq %rcx, %rcx
jle 0x1662d
movq 0x8f0(%rax), %rdx
movsd %xmm0, 0x28(%rsp)
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
cmpl $0x0, 0x4(%rdi)
xorpd %xmm0, %xmm0
jne 0x16615
movsd 0x50(%rdi), %xmm0
movsd 0x28(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
incq %rsi
cmpq %rsi, %rcx
jne 0x16602
movsd 0x968(%rax), %xmm0
movapd %xmm0, 0x30(%rsp)
testl %ebp, %ebp
jle 0x1672e
movl %ebp, %r15d
xorpd %xmm2, %xmm2
xorl %r12d, %r12d
movsd %xmm2, (%rsp)
movq (%rbx,%r12,8), %rdi
movl $0x3, %esi
callq 0xb768
movq (%rbx,%r12,8), %rdi
movl $0x5, %esi
callq 0xaf08
movq (%rbx,%r12,8), %rdi
movl $0x1, %esi
movl $0x5, %edx
callq 0xb032
movq (%rbx,%r12,8), %rax
movsbq 0x1(%rax), %rcx
testq %rcx, %rcx
jle 0x1671d
movq 0x10(%rax), %rdx
movq 0x18(%rax), %rsi
movq 0x20(%rax), %rdi
xorl %r8d, %r8d
movsd (%rsp), %xmm2
testq %rdx, %rdx
je 0x166be
movq 0x58(%rax), %r9
movsd (%r9,%r8,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm2
testq %rsi, %rsi
je 0x166e4
movq 0x60(%rax), %r9
movq (%r9,%r8,8), %r9
xorl %r10d, %r10d
movsd (%r9,%r10,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm2
incq %r10
cmpq %r10, %rcx
jne 0x166ce
testq %rdi, %rdi
je 0x16713
movsbq (%rax), %r9
testq %r9, %r9
jle 0x16713
movq 0x68(%rax), %r10
movq (%r10,%r8,8), %r10
xorl %r11d, %r11d
movsd (%r10,%r11,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm2
incq %r11
cmpq %r11, %r9
jne 0x166fd
incq %r8
cmpq %rcx, %r8
jne 0x166a7
jmp 0x16722
movsd (%rsp), %xmm2
incq %r12
cmpq %r15, %r12
jne 0x1664d
movq 0x88(%r14), %rax
movapd 0x30(%rsp), %xmm0
ucomisd 0x970(%rax), %xmm0
jbe 0x16a84
movl %ebp, %eax
movq %rax, 0x40(%rsp)
xorl %r12d, %r12d
movl %ebp, 0xc(%rsp)
movsd %xmm2, (%rsp)
testl %ebp, %ebp
jle 0x169b3
movapd 0x30(%rsp), %xmm1
xorpd 0x1e1ce(%rip), %xmm1    # 0x34940
xorl %r13d, %r13d
movapd %xmm1, 0x10(%rsp)
movq (%rbx,%r13,8), %rax
movsbq 0x1(%rax), %rcx
testq %rcx, %rcx
jle 0x16851
movq 0x10(%rax), %rdx
movq 0x18(%rax), %rsi
movq 0x20(%rax), %rdi
xorl %r8d, %r8d
testq %rdx, %rdx
je 0x167c2
movq 0x58(%rax), %r9
movq 0xa0(%rax), %r10
movsd (%r9,%r8,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%r10,%r8,8), %xmm0
movsd %xmm0, (%rdx,%r8,8)
testq %rsi, %rsi
je 0x167fe
movq 0x60(%rax), %r10
movq 0xa8(%rax), %r9
movq (%r9,%r8,8), %r9
movq (%r10,%r8,8), %r10
movq (%rsi,%r8,8), %r11
xorl %ebp, %ebp
movsd (%r10,%rbp,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%r9,%rbp,8), %xmm0
movsd %xmm0, (%r11,%rbp,8)
incq %rbp
cmpq %rbp, %rcx
jne 0x167e0
testq %rdi, %rdi
je 0x16845
movsbq (%rax), %r9
testq %r9, %r9
jle 0x16845
movq 0x68(%rax), %r11
movq 0xb0(%rax), %r10
movq (%r10,%r8,8), %r10
movq (%r11,%r8,8), %r11
movq (%rdi,%r8,8), %rbp
xorl %r15d, %r15d
movsd (%r11,%r15,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%r10,%r15,8), %xmm0
movsd %xmm0, (%rbp,%r15,8)
incq %r15
cmpq %r15, %r9
jne 0x16826
incq %r8
cmpq %rcx, %r8
jne 0x1679c
cmpb $0x1, 0xf8(%rax)
jne 0x169a5
cmpb $0x1, 0x80(%r14)
jne 0x16937
cmpb $0x0, 0xf9(%rax)
je 0x16937
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x16895
movq 0x50(%r14), %rsi
movq 0x68(%r14), %rdx
callq 0x79f5
movapd 0x10(%rsp), %xmm1
movq (%rbx,%r13,8), %rcx
movb 0x1(%rcx), %al
testb %al, %al
jle 0x169a5
xorl %ebp, %ebp
movq 0x18(%rcx), %rcx
testq %rcx, %rcx
je 0x168e1
movq (%rcx,%rbp,8), %rdi
cmpb $0x0, 0x80(%r14)
movq 0x58(%r14), %rcx
cmoveq %r12, %rcx
movq (%rcx,%rbp,8), %rsi
movq 0x70(%r14), %rcx
cmoveq %r12, %rcx
movq (%rcx,%rbp,8), %rdx
movsbl %al, %ecx
callq 0x79f5
movapd 0x10(%rsp), %xmm1
movq (%rbx,%r13,8), %rax
movq 0x20(%rax), %rcx
testq %rcx, %rcx
je 0x16920
movq (%rcx,%rbp,8), %rdi
cmpb $0x0, 0x80(%r14)
movq 0x60(%r14), %rcx
cmoveq %r12, %rcx
movq (%rcx,%rbp,8), %rsi
movq 0x78(%r14), %rcx
cmoveq %r12, %rcx
movq (%rcx,%rbp,8), %rdx
movsbl (%rax), %ecx
callq 0x79f5
movapd 0x10(%rsp), %xmm1
incq %rbp
movq (%rbx,%r13,8), %rcx
movsbq 0x1(%rcx), %rax
cmpq %rax, %rbp
jl 0x168a6
jmp 0x169a5
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x1694d
movl %ecx, %esi
callq 0x692d
movapd 0x10(%rsp), %xmm1
movq (%rbx,%r13,8), %rcx
movb 0x1(%rcx), %al
testb %al, %al
jle 0x169a5
xorl %ebp, %ebp
movq 0x18(%rcx), %rcx
testq %rcx, %rcx
je 0x16975
movq (%rcx,%rbp,8), %rdi
movsbl %al, %esi
callq 0x692d
movapd 0x10(%rsp), %xmm1
movq (%rbx,%r13,8), %rax
movq 0x20(%rax), %rcx
testq %rcx, %rcx
je 0x16994
movq (%rcx,%rbp,8), %rdi
movsbl (%rax), %esi
callq 0x692d
movapd 0x10(%rsp), %xmm1
incq %rbp
movq (%rbx,%r13,8), %rcx
movsbq 0x1(%rcx), %rax
cmpq %rax, %rbp
jl 0x1695a
incq %r13
cmpq 0x40(%rsp), %r13
jne 0x1677b
movq 0x88(%r14), %rax
movl 0x8dc(%rax), %esi
movq 0x8f0(%rax), %rdx
movq %r14, %rdi
callq 0x11a86
movq 0x88(%r14), %rax
movslq 0x8dc(%rax), %rcx
testq %rcx, %rcx
jle 0x16a19
movq 0x8f0(%rax), %rdx
xorpd %xmm3, %xmm3
xorl %esi, %esi
movl 0xc(%rsp), %ebp
movsd (%rsp), %xmm2
movq (%rdx,%rsi,8), %rdi
cmpl $0x0, 0x4(%rdi)
xorpd %xmm0, %xmm0
jne 0x16a0b
movsd 0x50(%rdi), %xmm0
addsd %xmm0, %xmm3
incq %rsi
cmpq %rsi, %rcx
jne 0x169f8
jmp 0x16a26
xorpd %xmm3, %xmm3
movl 0xc(%rsp), %ebp
movsd (%rsp), %xmm2
movsd 0x950(%rax), %xmm0
mulsd 0x30(%rsp), %xmm0
mulsd %xmm2, %xmm0
addsd 0x28(%rsp), %xmm0
ucomisd %xmm3, %xmm0
setae %cl
movsd 0x1dea3(%rip), %xmm1    # 0x348f0
jae 0x16a57
movsd 0x960(%rax), %xmm1
ucomisd %xmm3, %xmm0
jae 0x16abb
movapd 0x30(%rsp), %xmm0
divsd %xmm1, %xmm0
movq 0x88(%r14), %rax
movapd %xmm0, 0x30(%rsp)
ucomisd 0x970(%rax), %xmm0
ja 0x1675c
jmp 0x16a8a
xorpd %xmm3, %xmm3
xorl %ecx, %ecx
testl %ebp, %ebp
movsd %xmm3, 0x10(%rsp)
jle 0x16ac1
testb %cl, %cl
jne 0x16ac1
movl %ebp, %r14d
xorl %r15d, %r15d
movq (%rbx,%r15,8), %rdi
movl $0x5, %esi
movl $0x1, %edx
callq 0xb032
incq %r15
cmpq %r15, %r14
jne 0x16a9e
jmp 0x16ac1
movsd %xmm3, 0x10(%rsp)
testl %ebp, %ebp
jle 0x16ae1
movl %ebp, %r14d
xorl %r15d, %r15d
movq (%rbx,%r15,8), %rdi
movl $0x5, %esi
callq 0xafb8
incq %r15
cmpq %r15, %r14
jne 0x16acb
movsd 0x10(%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq