// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/HMMProblem.cpp
// Function: HMMProblem::GradientDescentBitBig(FitBit**, int)
FitResult HMMProblem::GradientDescentBitBig(FitBit **fbs, NCAT nfbs) {
    NCAT q;
    FitResult res;
    FitResult *fr = new FitResult;
    fr->iter = 1;
    fr->pO0  = 0.0;
    fr->pO   = 0.0;
    fr->conv = 0; // converged
    fr->ndat = 0;
    NUMBER crit;

    while( fr->conv==0 && fr->iter<=this->p->maxiter ) {
        fr->ndat = computeGradientsBig(fbs, nfbs);//a_gradPI, a_gradA, a_gradB);
        
        if(fr->iter==1) {
            fr->pO0 = HMMProblem::getSumLogPOPara(this->p->nSeq, this->p->k_data);
            fr->pOmid = fr->pO0;
        }
        
        for(q=0; q<nfbs; q++) {
            fbs[q]->copy(FBS_PAR, FBS_PARm1);
        }
        // make step
        if( fr->iter==1 || this->p->solver!=METHOD_CGD)
            fr->pO = doLinearStepBig(fbs, nfbs); // step for linked skill 0
        else
            fr->pO= doConjugateLinearStepBig(fbs, nfbs); /* NOT FINISHED*/
        
        fr->conv = checkConvergenceBig(fbs, nfbs, this->p->tol, &crit);
        
        if(fr->conv || fr->iter==this->p->maxiter) {
            if(!this->p->quiet) {
                if( !this->p->quiet )
                    printf("skill %5d, seq %5d, dat %8d, iter#%3d p(O|param)= %15.7f >> %15.7f, conv=%d\n", q, this->p->nSeq, fr->ndat, fr->iter, fr->pO0, fr->pO, fr->conv);
            }
        } else {
            for(q=0; q<nfbs; q++) {
                if (this->p->solver==METHOD_CGD) {
                    if( fr->iter==1 ) fbs[q]->copy(FBS_GRAD, FBS_DIRm1);
                    else              fbs[q]->copy(FBS_DIR,  FBS_DIRm1);
                    fbs[q]->copy(FBS_GRAD, FBS_GRADm1);
                }
            }
        }
        fr->iter ++;
        fr->pOmid = fr->pO;
    }// single skill loop

    RecycleFitData(this->p->nSeq, this->p->k_data, this->p); // recycle memory (Alpha, Beta, p_O_param, Xi, Gamma)
    
    res.iter = fr->iter;
    res.pO0  = fr->pO0;
    res.pO   = fr->pO;
    res.conv = fr->conv;
    res.ndat = fr->ndat;
    delete fr;
    return res;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, %r12d
movq %rdx, %r15
movq $0x0, 0x28(%rsp)
testl %ecx, %ecx
movl $0x0, %r13d
cmovgl %ecx, %r13d
movq %rdi, 0x38(%rsp)
pushq $0x1
popq %rbx
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
pushq $0x2
popq %rbp
movsd %xmm0, 0x20(%rsp)
movl %ebx, %r14d
movl $0x0, 0xc(%rsp)
movl %ecx, 0x34(%rsp)
movq %rsi, 0x18(%rsp)
movq 0x88(%rsi), %rdx
cmpl $0x0, 0xc(%rsp)
jne 0x12a60
cmpl 0x38(%rdx), %r14d
jg 0x12a60
movq (%rsi), %rax
movq %rsi, %rdi
movq %r15, %rsi
movl %r12d, %edx
callq *0x98(%rax)
movq %rax, 0x28(%rsp)
movl %r14d, 0x8(%rsp)
cmpl $0x1, %r14d
jne 0x12931
movq 0x18(%rsp), %rax
movq 0x88(%rax), %rax
movl 0x8dc(%rax), %esi
movq 0x8f0(%rax), %rdx
callq 0xe400
movsd %xmm0, 0x20(%rsp)
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x1294b
movq (%r15,%r14,8), %rdi
movl %ebx, %esi
movl %ebp, %edx
callq 0x88b6
incq %r14
jmp 0x12934
cmpl $0x1, 0x8(%rsp)
movq 0x18(%rsp), %rdi
je 0x12974
movq 0x88(%rdi), %rax
cmpb $0x3, 0x3f(%rax)
jne 0x12974
movq %rdi, %r14
movq %r15, %rsi
movl %r12d, %edx
callq 0x12f22
jmp 0x12982
movq %rdi, %r14
movq %r15, %rsi
movl %r12d, %edx
callq 0x12aae
movsd %xmm0, 0x10(%rsp)
movq 0x88(%r14), %rax
movsd 0x28(%rax), %xmm0
movq %r15, %rsi
movl %r12d, %edx
leaq 0x40(%rsp), %rcx
callq 0x137e4
movzbl %al, %ecx
movq 0x88(%r14), %rax
movl %ecx, 0xc(%rsp)
testb %cl, %cl
movq %r14, %rsi
jne 0x129c2
movl 0x8(%rsp), %ecx
cmpl 0x38(%rax), %ecx
jne 0x12a11
cmpb $0x0, 0x3c(%rax)
jne 0x129ff
movl 0x8dc(%rax), %edx
leaq 0x1ac87(%rip), %rdi     # 0x2d65c
movl %r13d, %esi
movq 0x28(%rsp), %rcx
movl 0x8(%rsp), %r8d
movsd 0x20(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movl 0xc(%rsp), %r9d
movb $0x2, %al
callq 0x4050
movq 0x18(%rsp), %rsi
movl 0x8(%rsp), %r14d
incl %r14d
movl 0x34(%rsp), %r12d
jmp 0x128cf
xorl %eax, %eax
cmpl $0x1, 0x8(%rsp)
setne %al
leal (%rax,%rax,4), %r14d
addl $0x3, %r14d
xorl %r12d, %r12d
cmpq %r12, %r13
je 0x129ff
movq 0x88(%rsi), %rax
cmpb $0x3, 0x3f(%rax)
jne 0x12a5b
movq (%r15,%r12,8), %rdi
movl %r14d, %esi
pushq $0x6
popq %rdx
callq 0x88b6
movq (%r15,%r12,8), %rdi
pushq $0x3
popq %rsi
pushq $0x4
popq %rdx
callq 0x88b6
movq 0x18(%rsp), %rsi
incq %r12
jmp 0x12a26
movl 0x8dc(%rdx), %edi
movq 0x8f0(%rdx), %rsi
callq 0x7b9b
movq 0x38(%rsp), %rax
movl %r14d, (%rax)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x8(%rax)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movl 0xc(%rsp), %ecx
movl %ecx, 0x20(%rax)
movq 0x28(%rsp), %rcx
movl %ecx, 0x24(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq