// File: myudelson[P]hmm-scalable/HMMProblem.cpp
// Function: HMMProblem::GradientDescentBit(FitBit*)
FitResult HMMProblem::GradientDescentBit(FitBit *fb) {
    FitResult res;
    FitResult *fr = new FitResult;
    fr->iter = 1;
    fr->pO0  = 0.0;
    fr->pO   = 0.0;
    fr->conv = 0; // converged
    fr->ndat = 0;
    NCAT xndat = fb->xndat;
    struct data **x_data = fb->x_data;
    // inital copy parameter values to the t-1 slice
    fb->copy(FBS_PAR, FBS_PARm1);
    while( !fr->conv && fr->iter<=this->p->maxiter ) {
        fr->ndat = computeGradients(fb);//a_gradPI, a_gradA, a_gradB);
//        this->toFileAlphaBetaObs("trainhmm_alphabetaobs0.txt", 0); // printing out alpha and beta arrays
        
        if(fr->iter==1) {
            fr->pO0 = HMMProblem::getSumLogPOPara(xndat, x_data);
            fr->pOmid = fr->pO0;
        }
        // copy parameter values
//        fb->copy(FBS_PAR, FBS_PARm1);
        // make ste-
        if( this->p->solver==METHOD_GD || (fr->iter==1 && this->p->solver==METHOD_CGD)  || (fr->iter==1 && this->p->solver==METHOD_GBB) )
            fr->pO = doLinearStep(fb); // step for linked skill 0
        else if( this->p->solver==METHOD_CGD )
            fr->pO = doConjugateLinearStep(fb);
        else if( this->p->solver==METHOD_GDL )
            fr->pO = doLagrangeStep(fb);
        else if( this->p->solver==METHOD_GBB )
            fr->pO = doBarzilaiBorweinStep(fb);
        // converge?
        fr->conv = fb->checkConvergence(fr);
        // copy parameter values after we already compared step t-1 with currently computed step t
//        if( this->p->solver==METHOD_GBB )
        fb->copy(FBS_PARm1, FBS_PARm2); // do this for all in order to capture oscillation, e.g. if new param at t is close to param at t-2 (tolerance)
        fb->copy(FBS_PAR, FBS_PARm1);
        // report if converged
        if( !this->p->quiet && ( /*(!conv && iter<this->p->maxiter) ||*/ (fr->conv || fr->iter==this->p->maxiter) )) {
            ;//fr->pO = HMMProblem::getSumLogPOPara(xndat, x_data);
        } else {
            // if Conjugate Gradient
            if (this->p->solver==METHOD_CGD) {
                if( fr->iter==1 ) {
                    fb->copy(FBS_GRAD, FBS_DIRm1); // gradient is not direction, it's negative direction, hence, need to negate it
                    fb->negate(FBS_DIRm1);
                }
                else fb->copy(FBS_DIR,  FBS_DIRm1);
                fb->copy(FBS_GRAD, FBS_GRADm1);
            }
            // if Barzilai Borwein Gradient Method
            if (this->p->solver==METHOD_GBB) {
                fb->copy(FBS_GRAD, FBS_GRADm1);
            }
        }
        fr->iter ++;
        fr->pOmid = fr->pO;
    }// single skill loop
    // cleanup
    RecycleFitData(xndat, x_data, this->p); // recycle memory (Alpha, Beta, p_O_param, Xi, Gamma)
    fr->iter--;
    // this is the case when 2 steps lead to step back to the initial value, i.e. oscillation
    if(fr->iter==2 && fr->conv && fb->checkConvergenceSingle(fr) ) {
        // decrease iteration counter to 1
        fr->iter--;
    }
    
    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)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x28, %edi
callq 0x52d0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
movq -0x18(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rax)
movq -0x18(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x24(%rax)
movq -0x10(%rbp), %rax
movl 0xe8(%rax), %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0xe4d0
movq -0x18(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x20(%rcx)
movb %al, -0x41(%rbp)
jne 0x1f5ee
movq -0x30(%rbp), %rcx
movq -0x18(%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 0x1f5fa
jmp 0x1f867
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x1f648
movq -0x30(%rbp), %rdi
movl -0x1c(%rbp), %esi
movq -0x28(%rbp), %rdx
callq 0x1a9f0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movq -0x18(%rbp), %rax
movsd 0x8(%rax), %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x10(%rax)
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x2, %eax
je 0x1f696
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x1f679
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
je 0x1f696
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x1f6b5
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x5, %eax
jne 0x1f6b5
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq (%rdi), %rax
callq *0x88(%rax)
movq -0x18(%rbp), %rax
movsd %xmm0, 0x18(%rax)
jmp 0x1f741
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x1f6e1
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1f920
movq -0x18(%rbp), %rax
movsd %xmm0, 0x18(%rax)
jmp 0x1f73f
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x4, %eax
jne 0x1f711
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq (%rdi), %rax
callq *0x90(%rax)
movq -0x18(%rbp), %rax
movsd %xmm0, 0x18(%rax)
jmp 0x1f73d
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x5, %eax
jne 0x1f73b
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21680
movq -0x18(%rbp), %rax
movsd %xmm0, 0x18(%rax)
jmp 0x1f73d
jmp 0x1f73f
jmp 0x1f741
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xe630
andb $0x1, %al
movzbl %al, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x10(%rbp), %rdi
movl $0x2, %esi
movl $0x7, %edx
callq 0xe4d0
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0xe4d0
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x1f7b6
movq -0x18(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x1f7b1
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x1f7b6
jmp 0x1f845
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x1f81c
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x1f7f6
movq -0x10(%rbp), %rdi
movl $0x3, %esi
movl $0x6, %edx
callq 0xe4d0
movq -0x10(%rbp), %rdi
movl $0x6, %esi
callq 0xde50
jmp 0x1f809
movq -0x10(%rbp), %rdi
movl $0x8, %esi
movl $0x6, %edx
callq 0xe4d0
movq -0x10(%rbp), %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0xe4d0
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x5, %eax
jne 0x1f843
movq -0x10(%rbp), %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0xe4d0
jmp 0x1f845
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movsd 0x18(%rax), %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x10(%rax)
jmp 0x1f5c5
movq -0x30(%rbp), %rax
movl -0x1c(%rbp), %edi
movq -0x28(%rbp), %rsi
movq 0x88(%rax), %rdx
callq 0xbfd0
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x1f8ba
movq -0x18(%rbp), %rax
cmpl $0x0, 0x20(%rax)
je 0x1f8ba
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xea30
testb $0x1, %al
jne 0x1f8af
jmp 0x1f8ba
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rcx
movsd 0x8(%rcx), %xmm0
movsd %xmm0, 0x8(%rax)
movq -0x18(%rbp), %rcx
movsd 0x18(%rcx), %xmm0
movsd %xmm0, 0x18(%rax)
movq -0x18(%rbp), %rcx
movl 0x20(%rcx), %ecx
movl %ecx, 0x20(%rax)
movq -0x18(%rbp), %rcx
movl 0x24(%rcx), %ecx
movl %ecx, 0x24(%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x1f912
movq -0x50(%rbp), %rdi
movl $0x28, %esi
callq 0x52e0
movq -0x38(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)