// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, (%rsp)
movl $0x28, %edi
callq 0x4210
movq %rax, %r14
movl $0x1, (%rax)
movq $0x0, 0x8(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x18(%rax)
movl 0xe8(%r15), %r13d
movq 0xf0(%r15), %rbp
movq %r15, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0xb032
cmpl $0x0, 0x20(%r14)
je 0x14ead
movq 0x88(%r12), %rdx
movl %r13d, %edi
movq %rbp, %rsi
callq 0xa39d
movl (%r14), %eax
decl %eax
movl %eax, (%r14)
cmpl $0x2, %eax
jne 0x14e61
cmpl $0x0, 0x20(%r14)
je 0x14e61
movq %r15, %rdi
movq %r14, %rsi
callq 0xb470
testb %al, %al
je 0x14e61
decl (%r14)
movl (%r14), %eax
movq (%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 $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rbx
addq $0x18, %rbx
movl (%r14), %eax
movq 0x88(%r12), %rcx
cmpl 0x38(%rcx), %eax
jg 0x14e28
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x80(%rax)
movl %eax, 0x24(%r14)
movl (%r14), %eax
cmpl $0x1, %eax
jne 0x14f1b
xorpd %xmm0, %xmm0
testl %r13d, %r13d
jle 0x14f0f
xorl %ecx, %ecx
movq (%rbp,%rcx,8), %rdx
cmpl $0x0, 0x4(%rdx)
xorpd %xmm1, %xmm1
jne 0x14f03
movsd 0x50(%rdx), %xmm1
addsd %xmm1, %xmm0
incq %rcx
cmpq %rcx, %r13
jne 0x14eef
movsd %xmm0, 0x8(%r14)
movsd %xmm0, 0x10(%r14)
cmpl $0x1, %eax
sete %cl
movq 0x88(%r12), %rax
movzbl 0x3f(%rax), %eax
leal -0x3(%rax), %edx
testb $-0x3, %dl
sete %dl
cmpl $0x2, %eax
je 0x14f63
andb %dl, %cl
jne 0x14f63
cmpl $0x5, %eax
je 0x15071
cmpl $0x4, %eax
je 0x1505c
cmpl $0x3, %eax
jne 0x14f77
movq %r12, %rdi
movq %r15, %rsi
callq 0x15082
jmp 0x14f73
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x88(%rax)
movsd %xmm0, (%rbx)
movq %r15, %rdi
movq %r14, %rsi
callq 0xb162
movzbl %al, %eax
movl %eax, 0x20(%r14)
movq %r15, %rdi
movl $0x2, %esi
movl $0x7, %edx
callq 0xb032
movq %r15, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0xb032
movq 0x88(%r12), %rax
cmpb $0x0, 0x3c(%rax)
je 0x14fe8
cmpb $0x3, 0x3f(%rax)
jne 0x1501d
cmpl $0x1, (%r14)
jne 0x14ff9
movq %r15, %rdi
movl $0x3, %esi
movl $0x6, %edx
callq 0xb032
movq %r15, %rdi
movl $0x6, %esi
callq 0xaf42
jmp 0x1500b
cmpl $0x0, 0x20(%r14)
jne 0x1503d
movl (%r14), %ecx
cmpl 0x38(%rax), %ecx
jne 0x14fbb
jmp 0x1503d
movq %r15, %rdi
movl $0x8, %esi
movl $0x6, %edx
callq 0xb032
movq %r15, %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0xb032
movq 0x88(%r12), %rax
cmpb $0x5, 0x3f(%rax)
jne 0x1503d
movq %r15, %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0xb032
incl (%r14)
movsd 0x18(%r14), %xmm0
movsd %xmm0, 0x10(%r14)
cmpl $0x0, 0x20(%r14)
je 0x14eb4
jmp 0x14e28
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x90(%rax)
jmp 0x14f73
movq %r12, %rdi
movq %r15, %rsi
callq 0x15f34
jmp 0x14f73
nop