// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x40(%rbp)
movq %rdi, -0x38(%rbp)
movq %rsi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x28, %edi
callq 0x52d0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl $0x1, (%rax)
movq -0x20(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rax)
movq -0x20(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x24(%rax)
movq -0x20(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x20(%rcx)
movb %al, -0x41(%rbp)
jne 0x21ea2
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movl (%rax), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
setle %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x21eae
jmp 0x2210f
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
movq (%rdi), %rax
callq *0x98(%rax)
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x20(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x21f13
movq -0x30(%rbp), %rdi
movq 0x88(%rdi), %rax
movl 0x8dc(%rax), %esi
movq 0x88(%rdi), %rax
movq 0x8f0(%rax), %rdx
callq 0x1a9f0
movq -0x20(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movq -0x20(%rbp), %rax
movsd 0x8(%rax), %xmm0
movq -0x20(%rbp), %rax
movsd %xmm0, 0x10(%rax)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x21f48
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0xe4d0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x21f1a
movq -0x20(%rbp), %rax
cmpl $0x1, (%rax)
je 0x21f65
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
je 0x21f80
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0x221a0
movq -0x20(%rbp), %rax
movsd %xmm0, 0x18(%rax)
jmp 0x21f99
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0x22b60
movq -0x20(%rbp), %rax
movsd %xmm0, 0x18(%rax)
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
movq 0x88(%rdi), %rax
movsd 0x28(%rax), %xmm0
leaq -0x28(%rbp), %rcx
callq 0x24190
andb $0x1, %al
movzbl %al, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x21fe5
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movl (%rax), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x22058
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x22053
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x22051
movq -0x30(%rbp), %rax
movl -0x18(%rbp), %esi
movq 0x88(%rax), %rax
movl 0x8dc(%rax), %edx
movq -0x20(%rbp), %rax
movl 0x24(%rax), %ecx
movq -0x20(%rbp), %rax
movl (%rax), %r8d
movq -0x20(%rbp), %rax
movsd 0x8(%rax), %xmm0
movq -0x20(%rbp), %rax
movsd 0x18(%rax), %xmm1
movq -0x20(%rbp), %rax
movl 0x20(%rax), %r9d
leaq 0x2e656(%rip), %rdi     # 0x506a0
movb $0x2, %al
callq 0x5060
jmp 0x22053
jmp 0x220ed
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x220eb
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x220db
movq -0x20(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x220a5
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl $0x3, %esi
movl $0x6, %edx
callq 0xe4d0
jmp 0x220c0
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl $0x8, %esi
movl $0x6, %edx
callq 0xe4d0
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0xe4d0
jmp 0x220dd
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x2205f
jmp 0x220ed
movq -0x20(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movsd 0x18(%rax), %xmm0
movq -0x20(%rbp), %rax
movsd %xmm0, 0x10(%rax)
jmp 0x21e79
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rcx
movl 0x8dc(%rcx), %edi
movq 0x88(%rax), %rcx
movq 0x8f0(%rcx), %rsi
movq 0x88(%rax), %rdx
callq 0xbfd0
movq -0x40(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rcx
movsd 0x8(%rcx), %xmm0
movsd %xmm0, 0x8(%rax)
movq -0x20(%rbp), %rcx
movsd 0x18(%rcx), %xmm0
movsd %xmm0, 0x18(%rax)
movq -0x20(%rbp), %rcx
movl 0x20(%rcx), %ecx
movl %ecx, 0x20(%rax)
movq -0x20(%rbp), %rcx
movl 0x24(%rcx), %ecx
movl %ecx, 0x24(%rax)
movq -0x20(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x22192
movq -0x50(%rbp), %rdi
movl $0x28, %esi
callq 0x52e0
movq -0x38(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)