// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movq 0x88(%rdi), %rax
movsd 0x968(%rax), %xmm0
movapd %xmm0, 0x20(%rsp)
movl 0x8dc(%rax), %esi
movq 0x8f0(%rax), %rdx
callq 0xe400
movsd %xmm0, 0x30(%rsp)
xorl %ebp, %ebp
testl %ebx, %ebx
cmovlel %ebp, %ebx
xorpd %xmm1, %xmm1
pushq $0x5
popq %r12
xorl %r13d, %r13d
movsd %xmm1, 0x18(%rsp)
cmpq %rbx, %r13
je 0x12bd8
movq (%r14,%r13,8), %rdi
pushq $0x3
popq %rsi
callq 0x8f0e
movq (%r14,%r13,8), %rdi
movl %r12d, %esi
callq 0x878c
movq (%r14,%r13,8), %rdi
pushq $0x1
popq %rsi
movl %r12d, %edx
callq 0x88b6
movq (%r14,%r13,8), %rax
movzbl 0x1(%rax), %ecx
testb %cl, %cl
cmovlel %ebp, %ecx
movzbl %cl, %ecx
xorl %edx, %edx
movsd 0x18(%rsp), %xmm1
cmpq %rcx, %rdx
je 0x12bd0
cmpq $0x0, 0x10(%rax)
je 0x12b6e
movq 0x58(%rax), %rsi
movsd (%rsi,%rdx,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm1
cmpq $0x0, 0x18(%rax)
je 0x12b96
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x12b96
movq 0x60(%rax), %rdi
movq (%rdi,%rdx,8), %rdi
movsd (%rdi,%rsi,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm1
incq %rsi
jmp 0x12b77
cmpq $0x0, 0x20(%rax)
je 0x12bcb
movl (%rax), %esi
testb %sil, %sil
cmovlel %ebp, %esi
movzbl %sil, %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x12bcb
movq 0x68(%rax), %r8
movq (%r8,%rdx,8), %r8
movsd (%r8,%rdi,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm1
incq %rdi
jmp 0x12bab
incq %rdx
jmp 0x12b51
incq %r13
jmp 0x12b03
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %r12d, %r12d
xorl %eax, %eax
movapd 0x20(%rsp), %xmm1
testb $0x1, %al
jne 0x12ef2
movq 0x88(%r15), %rax
ucomisd 0x970(%rax), %xmm1
jbe 0x12ed0
movapd %xmm1, %xmm2
xorpd 0x19d2b(%rip), %xmm2    # 0x2c940
xorl %r13d, %r13d
movapd %xmm1, 0x20(%rsp)
movapd %xmm2, (%rsp)
cmpq %rbx, %r13
je 0x12e53
movq (%r14,%r13,8), %rax
movzbl 0x1(%rax), %ecx
testb %cl, %cl
movl %ecx, %edx
cmovlel %r12d, %edx
movzbl %dl, %edx
xorl %esi, %esi
cmpq %rdx, %rsi
je 0x12d0f
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x12c73
movq 0x58(%rax), %r8
movq 0xa0(%rax), %r9
movsd (%r8,%rsi,8), %xmm0
mulsd %xmm2, %xmm0
addsd (%r9,%rsi,8), %xmm0
movsd %xmm0, (%rdi,%rsi,8)
movq 0x18(%rax), %rdi
testq %rdi, %rdi
je 0x12cb6
xorl %r8d, %r8d
cmpq %r8, %rdx
je 0x12cb6
movq 0x60(%rax), %r9
movq 0xa8(%rax), %r10
movq (%r10,%rsi,8), %r10
movq (%r9,%rsi,8), %r9
movsd (%r9,%r8,8), %xmm0
mulsd %xmm2, %xmm0
addsd (%r10,%r8,8), %xmm0
movq (%rdi,%rsi,8), %r9
movsd %xmm0, (%r9,%r8,8)
incq %r8
jmp 0x12c7f
movq 0x20(%rax), %rdi
testq %rdi, %rdi
je 0x12d07
movl (%rax), %r8d
testb %r8b, %r8b
cmovlel %r12d, %r8d
movzbl %r8b, %r8d
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x12d07
movq 0x68(%rax), %r10
movq 0xb0(%rax), %r11
movq (%r11,%rsi,8), %r11
movq (%r10,%rsi,8), %r10
movsd (%r10,%r9,8), %xmm0
mulsd %xmm2, %xmm0
addsd (%r11,%r9,8), %xmm0
movq (%rdi,%rsi,8), %r10
movsd %xmm0, (%r10,%r9,8)
incq %r9
jmp 0x12cd0
incq %rsi
jmp 0x12c41
cmpb $0x1, 0xf8(%rax)
jne 0x12e4b
cmpb $0x0, 0x80(%r15)
movsbl %cl, %ecx
je 0x12de9
cmpb $0x0, 0xf9(%rax)
movq 0x10(%rax), %rdi
je 0x12ded
testq %rdi, %rdi
je 0x12d55
movq 0x50(%r15), %rsi
movq 0x68(%r15), %rdx
callq 0x5d80
movapd (%rsp), %xmm2
xorl %ebp, %ebp
movq (%r14,%r13,8), %rax
movsbq 0x1(%rax), %rcx
cmpq %rcx, %rbp
jge 0x12e4b
movq 0x18(%rax), %rdx
testq %rdx, %rdx
je 0x12da7
movq (%rdx,%rbp,8), %rdi
cmpb $0x0, 0x80(%r15)
movq 0x58(%r15), %rax
cmoveq %r12, %rax
movq (%rax,%rbp,8), %rsi
movq 0x70(%r15), %rax
cmoveq %r12, %rax
movq (%rax,%rbp,8), %rdx
movsbl %cl, %ecx
callq 0x5d80
movapd (%rsp), %xmm2
movq (%r14,%r13,8), %rax
movq 0x20(%rax), %rcx
testq %rcx, %rcx
je 0x12de1
movq (%rcx,%rbp,8), %rdi
cmpb $0x0, 0x80(%r15)
movq 0x60(%r15), %rcx
cmoveq %r12, %rcx
movq (%rcx,%rbp,8), %rsi
movq 0x78(%r15), %rcx
cmoveq %r12, %rcx
movq (%rcx,%rbp,8), %rdx
movsbl (%rax), %ecx
callq 0x5d80
movapd (%rsp), %xmm2
incq %rbp
jmp 0x12d57
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x12dfe
movl %ecx, %esi
callq 0x5600
movapd (%rsp), %xmm2
xorl %ebp, %ebp
movq (%r14,%r13,8), %rax
movsbq 0x1(%rax), %rcx
cmpq %rcx, %rbp
jge 0x12e4b
movq 0x18(%rax), %rdx
testq %rdx, %rdx
je 0x12e2c
movq (%rdx,%rbp,8), %rdi
movsbl %cl, %esi
callq 0x5600
movapd (%rsp), %xmm2
movq (%r14,%r13,8), %rax
movq 0x20(%rax), %rcx
testq %rcx, %rcx
je 0x12e46
movq (%rcx,%rbp,8), %rdi
movsbl (%rax), %esi
callq 0x5600
movapd (%rsp), %xmm2
incq %rbp
jmp 0x12e00
incq %r13
jmp 0x12c23
movq 0x88(%r15), %rax
movl 0x8dc(%rax), %esi
movq 0x8f0(%rax), %rdx
movq %r15, %rdi
callq 0xe7b0
movq 0x88(%r15), %r13
movl 0x8dc(%r13), %esi
movq 0x8f0(%r13), %rdx
callq 0xe400
movapd 0x20(%rsp), %xmm1
movsd 0x950(%r13), %xmm2
mulsd %xmm1, %xmm2
mulsd 0x18(%rsp), %xmm2
addsd 0x30(%rsp), %xmm2
movsd %xmm0, (%rsp)
ucomisd %xmm0, %xmm2
setae %al
movsd 0x19a34(%rip), %xmm0    # 0x2c8f0
jae 0x12ec7
movsd 0x960(%r13), %xmm0
divsd %xmm0, %xmm1
jmp 0x12bec
xorl %r12d, %r12d
pushq $0x5
popq %rbp
pushq $0x1
popq %r15
cmpq %r12, %rbx
je 0x12ef2
movq (%r14,%r12,8), %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x88b6
incq %r12
jmp 0x12eda
xorl %r15d, %r15d
pushq $0x5
popq %rbp
cmpq %r15, %rbx
je 0x12f0d
movq (%r14,%r15,8), %rdi
movl %ebp, %esi
callq 0x883c
incq %r15
jmp 0x12ef8
movsd (%rsp), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop