// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %rdx, %r15
movq %rdi, %r14
movq 0x88(%rsi), %rdx
cmpl $0x0, 0x38(%rdx)
jle 0x163db
movl %ecx, %ebx
movq %rsi, %r12
movq %r14, 0x28(%rsp)
movl %ecx, %r14d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movl $0x1, %eax
movl %ecx, 0xc(%rsp)
movq %rax, %rbp
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq *0x98(%rax)
movl %eax, %r13d
movq %rbp, 0x18(%rsp)
cmpl $0x1, %ebp
movsd 0x10(%rsp), %xmm1
jne 0x16285
movq 0x88(%r12), %rcx
movslq 0x8dc(%rcx), %rax
testq %rax, %rax
jle 0x16281
movq 0x8f0(%rcx), %rcx
xorpd %xmm1, %xmm1
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
cmpl $0x0, 0x4(%rsi)
xorpd %xmm0, %xmm0
jne 0x16273
movsd 0x50(%rsi), %xmm0
addsd %xmm0, %xmm1
incq %rdx
cmpq %rdx, %rax
jne 0x16260
jmp 0x16285
xorpd %xmm1, %xmm1
movsd %xmm1, 0x10(%rsp)
testl %ebx, %ebx
jle 0x162b4
xorl %ebx, %ebx
movq (%r15,%rbx,8), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0xb03a
incq %rbx
cmpq %rbx, %r14
jne 0x16291
movl 0xc(%rsp), %ebx
movl %ebx, %ebp
jmp 0x162b6
xorl %ebp, %ebp
cmpl $0x1, 0x18(%rsp)
movl %r13d, 0x8(%rsp)
je 0x162df
movq 0x88(%r12), %rax
cmpb $0x3, 0x3f(%rax)
jne 0x162df
movq %r12, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x16962
jmp 0x162ec
movq %r12, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x1644e
movsd %xmm0, 0x20(%rsp)
movq 0x88(%r12), %rax
movsd 0x28(%rax), %xmm0
movq %r15, %rsi
movl %ebx, %edx
leaq 0x30(%rsp), %rcx
callq 0x17338
movzbl %al, %ebx
movq 0x88(%r12), %rax
testb %bl, %bl
jne 0x16327
movq 0x18(%rsp), %rcx
cmpl 0x38(%rax), %ecx
jne 0x16361
cmpb $0x0, 0x3c(%rax)
jne 0x163b4
movl 0x8dc(%rax), %edx
leaq 0x1e31e(%rip), %rdi     # 0x3465c
movl %ebp, %esi
movl 0x8(%rsp), %ecx
movq 0x18(%rsp), %r8
movsd 0x10(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movl %ebx, %r9d
movb $0x2, %al
callq 0x4050
jmp 0x163b4
cmpl $0x0, 0xc(%rsp)
jle 0x163b4
xorl %eax, %eax
cmpl $0x1, 0x18(%rsp)
setne %al
leal (%rax,%rax,4), %ebp
addl $0x3, %ebp
xorl %r13d, %r13d
movq 0x88(%r12), %rax
cmpb $0x3, 0x3f(%rax)
jne 0x163ac
movq (%r15,%r13,8), %rdi
movl %ebp, %esi
movl $0x6, %edx
callq 0xb03a
movq (%r15,%r13,8), %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0xb03a
incq %r13
cmpq %r13, %r14
jne 0x1637b
movq 0x18(%rsp), %rax
leal 0x1(%rax), %ebp
movq 0x88(%r12), %rdx
testl %ebx, %ebx
jne 0x163f7
cmpl 0x38(%rdx), %eax
movl %ebp, %eax
movl 0xc(%rsp), %ebx
jl 0x16217
xorl %ebx, %ebx
jmp 0x163fc
movl $0x1, %ebp
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %r15d, %r15d
xorl %ebx, %ebx
movsd %xmm0, 0x10(%rsp)
jmp 0x16406
movl $0x1, %ebx
movq 0x28(%rsp), %r14
movl 0x8(%rsp), %r15d
movl 0x8dc(%rdx), %edi
movq 0x8f0(%rdx), %rsi
callq 0xa3aa
movl %ebp, (%r14)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%r14)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x18(%r14)
movl %ebx, 0x20(%r14)
movl %r15d, 0x24(%r14)
movq %r14, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop