// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r14
movq 0x88(%rdi), %rax
movslq 0x8dc(%rax), %rcx
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
testq %rcx, %rcx
jle 0x164bf
movq 0x8f0(%rax), %rdx
movsd %xmm0, 0x28(%rsp)
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
cmpl $0x0, 0x4(%rdi)
xorpd %xmm0, %xmm0
jne 0x164a7
movsd 0x50(%rdi), %xmm0
movsd 0x28(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x28(%rsp)
incq %rsi
cmpq %rsi, %rcx
jne 0x16494
movsd 0x968(%rax), %xmm0
movapd %xmm0, 0x40(%rsp)
movl %ebp, %r15d
testl %ebp, %ebp
jle 0x165ca
xorpd %xmm1, %xmm1
xorl %r12d, %r12d
movsd %xmm1, 0x8(%rsp)
movq (%rbx,%r12,8), %rdi
movl $0x3, %esi
callq 0xb70e
movq (%rbx,%r12,8), %rdi
movl $0x5, %esi
callq 0xaf10
movq (%rbx,%r12,8), %rdi
movl $0x1, %esi
movl $0x5, %edx
callq 0xb03a
movq (%rbx,%r12,8), %rax
movsbq 0x1(%rax), %rcx
testq %rcx, %rcx
jle 0x165b1
movq 0x10(%rax), %rdx
movq 0x18(%rax), %rsi
movq 0x20(%rax), %rdi
xorl %r8d, %r8d
movsd 0x8(%rsp), %xmm1
testq %rdx, %rdx
je 0x16552
movq 0x58(%rax), %r9
movsd (%r9,%r8,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm1
testq %rsi, %rsi
je 0x16578
movq 0x60(%rax), %r9
movq (%r9,%r8,8), %r9
xorl %r10d, %r10d
movsd (%r9,%r10,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm1
incq %r10
cmpq %r10, %rcx
jne 0x16562
testq %rdi, %rdi
je 0x165a7
movsbq (%rax), %r9
testq %r9, %r9
jle 0x165a7
movq 0x68(%rax), %r10
movq (%r10,%r8,8), %r10
xorl %r11d, %r11d
movsd (%r10,%r11,8), %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm1
incq %r11
cmpq %r11, %r9
jne 0x16591
incq %r8
cmpq %rcx, %r8
jne 0x1653b
jmp 0x165b7
movsd 0x8(%rsp), %xmm1
incq %r12
cmpq %r15, %r12
jne 0x164df
movq 0x88(%r14), %rax
xorpd %xmm3, %xmm3
movapd 0x40(%rsp), %xmm0
ucomisd 0x970(%rax), %xmm0
jbe 0x168f7
xorl %r12d, %r12d
movl %ebp, 0x34(%rsp)
movq %r15, 0x38(%rsp)
movsd %xmm1, 0x8(%rsp)
testl %ebp, %ebp
jle 0x16853
movapd 0x40(%rsp), %xmm1
xorpd 0x1d336(%rip), %xmm1    # 0x33940
xorl %r13d, %r13d
movapd %xmm1, 0x10(%rsp)
movq (%rbx,%r13,8), %rax
movsbq 0x1(%rax), %rcx
testq %rcx, %rcx
jle 0x166e9
movq 0x10(%rax), %rdx
movq 0x18(%rax), %rsi
movq 0x20(%rax), %rdi
xorl %r8d, %r8d
testq %rdx, %rdx
je 0x1665a
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 0x16696
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 0x16678
testq %rdi, %rdi
je 0x166dd
movsbq (%rax), %r9
testq %r9, %r9
jle 0x166dd
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 0x166be
incq %r8
cmpq %rcx, %r8
jne 0x16634
cmpb $0x1, 0xf8(%rax)
jne 0x16835
cmpb $0x1, 0x80(%r14)
jne 0x167cc
cmpb $0x0, 0xf9(%rax)
je 0x167cc
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x16737
movq 0x50(%r14), %rsi
movq 0x68(%r14), %rdx
movsbl %cl, %ecx
callq 0x7997
movapd 0x10(%rsp), %xmm1
movq (%rbx,%r13,8), %rax
movb 0x1(%rax), %cl
testb %cl, %cl
jle 0x16835
xorl %ebp, %ebp
movq 0x18(%rax), %rdx
testq %rdx, %rdx
je 0x1677a
movq (%rdx,%rbp,8), %rdi
cmpb $0x0, 0x80(%r14)
movq 0x58(%r14), %rax
cmoveq %r12, %rax
movq (%rax,%rbp,8), %rsi
movq 0x70(%r14), %rax
cmoveq %r12, %rax
movq (%rax,%rbp,8), %rdx
movsbl %cl, %ecx
callq 0x7997
movq (%rbx,%r13,8), %rax
movq 0x20(%rax), %rcx
testq %rcx, %rcx
je 0x167b3
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 0x7997
movq (%rbx,%r13,8), %rax
movapd 0x10(%rsp), %xmm1
incq %rbp
movsbq 0x1(%rax), %rcx
cmpq %rcx, %rbp
jl 0x16741
jmp 0x16835
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x167ea
movsbl %cl, %esi
callq 0x6836
movapd 0x10(%rsp), %xmm1
movq (%rbx,%r13,8), %rax
movb 0x1(%rax), %cl
testb %cl, %cl
jle 0x16835
xorl %ebp, %ebp
movq 0x18(%rax), %rdx
testq %rdx, %rdx
je 0x16809
movq (%rdx,%rbp,8), %rdi
movsbl %cl, %esi
callq 0x6836
movq (%rbx,%r13,8), %rax
movq 0x20(%rax), %rcx
testq %rcx, %rcx
je 0x16822
movq (%rcx,%rbp,8), %rdi
movsbl (%rax), %esi
callq 0x6836
movq (%rbx,%r13,8), %rax
movapd 0x10(%rsp), %xmm1
incq %rbp
movsbq 0x1(%rax), %rcx
cmpq %rcx, %rbp
jl 0x167f0
incq %r13
cmpq 0x38(%rsp), %r13
jne 0x16613
movq 0x88(%r14), %rax
movl 0x34(%rsp), %ebp
movq 0x38(%rsp), %r15
movl 0x8dc(%rax), %esi
movq 0x8f0(%rax), %rdx
movq %r14, %rdi
callq 0x11908
movq 0x88(%r14), %rax
movslq 0x8dc(%rax), %rcx
testq %rcx, %rcx
jle 0x168af
movq 0x8f0(%rax), %rdx
xorpd %xmm3, %xmm3
xorl %esi, %esi
movsd 0x8(%rsp), %xmm1
movq (%rdx,%rsi,8), %rdi
cmpl $0x0, 0x4(%rdi)
xorpd %xmm0, %xmm0
jne 0x168a1
movsd 0x50(%rdi), %xmm0
addsd %xmm0, %xmm3
incq %rsi
cmpq %rsi, %rcx
jne 0x1688e
jmp 0x168b9
xorpd %xmm3, %xmm3
movsd 0x8(%rsp), %xmm1
movsd 0x950(%rax), %xmm0
movapd 0x40(%rsp), %xmm2
mulsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
addsd 0x28(%rsp), %xmm0
ucomisd %xmm3, %xmm0
jae 0x16925
divsd 0x960(%rax), %xmm2
movapd %xmm2, 0x40(%rsp)
ucomisd 0x970(%rax), %xmm2
ja 0x165f4
testl %ebp, %ebp
jle 0x1694e
movsd %xmm3, 0x10(%rsp)
xorl %r14d, %r14d
movq (%rbx,%r14,8), %rdi
movl $0x5, %esi
movl $0x1, %edx
callq 0xb03a
incq %r14
cmpq %r14, %r15
jne 0x16904
movsd 0x10(%rsp), %xmm3
testl %ebp, %ebp
jle 0x1694e
movsd %xmm3, 0x10(%rsp)
xorl %r14d, %r14d
movq (%rbx,%r14,8), %rdi
movl $0x5, %esi
callq 0xafc0
incq %r14
cmpq %r14, %r15
jne 0x16932
movsd 0x10(%rsp), %xmm3
movapd %xmm3, %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop