// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
movl %ecx, %ebp
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, 0x8(%rsp)
movl $0x28, %edi
callq 0x4210
movq %rax, %r14
movl $0x1, (%rax)
movq $0x0, 0x8(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x18(%rax)
movl %ebp, %r13d
movl (%r14), %eax
movq 0x88(%r15), %rcx
cmpl 0x38(%rcx), %eax
jg 0x16555
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq *0x98(%rax)
movl %eax, 0x24(%r14)
cmpl $0x1, (%r14)
jne 0x1642b
movq 0x88(%r15), %rcx
movslq 0x8dc(%rcx), %rax
testq %rax, %rax
jle 0x1641b
movq 0x8f0(%rcx), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
cmpl $0x0, 0x4(%rsi)
xorpd %xmm1, %xmm1
jne 0x1640d
movsd 0x50(%rsi), %xmm1
addsd %xmm1, %xmm0
incq %rdx
cmpq %rdx, %rax
jne 0x163fa
jmp 0x1641f
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%r14)
movsd %xmm0, 0x10(%r14)
xorl %ebx, %ebx
testl %ebp, %ebp
jle 0x1644c
movq (%r12,%rbx,8), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0xb032
incq %rbx
cmpq %rbx, %r13
jne 0x16431
cmpl $0x1, (%r14)
je 0x1646e
movq 0x88(%r15), %rax
cmpb $0x3, 0x3f(%rax)
jne 0x1646e
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x16af6
jmp 0x1647b
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x165bc
movsd %xmm0, 0x18(%r14)
movq 0x88(%r15), %rax
movsd 0x28(%rax), %xmm0
movq %r12, %rsi
movl %ebp, %edx
leaq 0x10(%rsp), %rcx
callq 0x17526
movzbl %al, %r9d
movl %r9d, 0x20(%r14)
testb %r9b, %r9b
jne 0x164b8
movl (%r14), %eax
movq 0x88(%r15), %rcx
cmpl 0x38(%rcx), %eax
jne 0x164f0
movq 0x88(%r15), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x1653b
movl 0x8dc(%rax), %edx
movl 0x24(%r14), %ecx
movl (%r14), %r8d
movsd 0x8(%r14), %xmm0
movsd 0x18(%r14), %xmm1
leaq 0x1f0e7(%rip), %rdi     # 0x355cc
movl %ebx, %esi
movb $0x2, %al
callq 0x4050
jmp 0x1653b
testl %ebp, %ebp
jle 0x1653b
xorl %ebx, %ebx
movq 0x88(%r15), %rax
cmpb $0x3, 0x3f(%rax)
jne 0x16533
xorl %eax, %eax
cmpl $0x1, (%r14)
setne %al
movq (%r12,%rbx,8), %rdi
leal (%rax,%rax,4), %esi
addl $0x3, %esi
movl $0x6, %edx
callq 0xb032
movq (%r12,%rbx,8), %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0xb032
incq %rbx
cmpq %rbx, %r13
jne 0x164f6
incl (%r14)
movsd 0x18(%r14), %xmm0
movsd %xmm0, 0x10(%r14)
cmpl $0x0, 0x20(%r14)
je 0x163ac
movq 0x88(%r15), %rdx
movl 0x8dc(%rdx), %edi
movq 0x8f0(%rdx), %rsi
callq 0xa39d
movl (%r14), %eax
movq 0x8(%rsp), %rbx
movl %eax, (%rbx)
movsd 0x8(%r14), %xmm0
movsd %xmm0, 0x8(%rbx)
movsd 0x18(%r14), %xmm0
movsd %xmm0, 0x18(%rbx)
movl 0x20(%r14), %eax
movl %eax, 0x20(%rbx)
movl 0x24(%r14), %eax
movl %eax, 0x24(%rbx)
movl $0x28, %esi
movq %r14, %rdi
callq 0x4220
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop