// File: /workspace/github_cpp_makefile_star2_O3/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
subq $0x18, %rsp
movq %rdx, %r15
movq %rsi, %r12
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)
addq $0x18, %rax
movq %rax, 0x10(%rsp)
movl 0xe8(%r15), %r13d
movq 0xf0(%r15), %rbp
movl $0x1, %ebx
movq %r15, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0xb03a
movq 0x88(%r12), %rax
cmpl 0x38(%rax), %ebx
jg 0x14e95
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x80(%rax)
movl %eax, 0x24(%r14)
movl (%r14), %eax
cmpl $0x1, %eax
jne 0x14d13
xorpd %xmm0, %xmm0
testl %r13d, %r13d
jle 0x14d07
xorl %ecx, %ecx
movq (%rbp,%rcx,8), %rdx
cmpl $0x0, 0x4(%rdx)
xorpd %xmm1, %xmm1
jne 0x14cfb
movsd 0x50(%rdx), %xmm1
addsd %xmm1, %xmm0
incq %rcx
cmpq %rcx, %r13
jne 0x14ce7
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 0x14d5b
andb %dl, %cl
jne 0x14d5b
cmpl $0x5, %eax
je 0x14e74
cmpl $0x4, %eax
je 0x14e5f
cmpl $0x3, %eax
jne 0x14d74
movq %r12, %rdi
movq %r15, %rsi
callq 0x14f1e
jmp 0x14d6b
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x88(%rax)
movq 0x10(%rsp), %rax
movsd %xmm0, (%rax)
movq %r15, %rdi
movq %r14, %rsi
callq 0xb196
movzbl %al, %eax
movl %eax, 0x20(%r14)
movq %r15, %rdi
movl $0x2, %esi
movl $0x7, %edx
callq 0xb03a
movq %r15, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0xb03a
movq 0x88(%r12), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x14dcb
cmpl $0x0, 0x20(%r14)
movl (%r14), %ecx
jne 0x14e84
cmpl 0x38(%rax), %ecx
je 0x14e3e
movb 0x3f(%rax), %al
cmpb $0x3, %al
jne 0x14e28
cmpl $0x1, (%r14)
jne 0x14df9
movq %r15, %rdi
movl $0x3, %esi
movl $0x6, %edx
callq 0xb03a
movq %r15, %rdi
movl $0x6, %esi
callq 0xaf4a
jmp 0x14e0b
movq %r15, %rdi
movl $0x8, %esi
movl $0x6, %edx
callq 0xb03a
movq %r15, %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0xb03a
movq 0x88(%r12), %rax
movb 0x3f(%rax), %al
cmpb $0x5, %al
jne 0x14e3e
movq %r15, %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0xb03a
movl (%r14), %ebx
incl %ebx
movl %ebx, (%r14)
movsd 0x18(%r14), %xmm0
movsd %xmm0, 0x10(%r14)
cmpl $0x0, 0x20(%r14)
je 0x14caf
jmp 0x14e95
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x90(%rax)
jmp 0x14d6b
movq %r12, %rdi
movq %r15, %rsi
callq 0x15d98
jmp 0x14d6b
incl %ecx
movl %ecx, (%r14)
movsd 0x18(%r14), %xmm0
movsd %xmm0, 0x10(%r14)
movq 0x88(%r12), %rdx
movl %r13d, %edi
movq %rbp, %rsi
callq 0xa3aa
movl (%r14), %ecx
decl %ecx
movl %ecx, (%r14)
cmpl $0x2, %ecx
jne 0x14eda
cmpl $0x0, 0x20(%r14)
je 0x14ed5
movq %r15, %rdi
movq %r14, %rsi
callq 0xb3e4
movl (%r14), %ecx
testb %al, %al
je 0x14eda
decl %ecx
movl %ecx, (%r14)
jmp 0x14eda
movl $0x2, %ecx
movq 0x8(%rsp), %rbx
movl %ecx, (%rbx)
movsd 0x8(%r14), %xmm0
movsd %xmm0, 0x8(%rbx)
movsd 0x18(%r14), %xmm0
movsd %xmm0, 0x18(%rbx)
movq 0x20(%r14), %rax
movq %rax, 0x20(%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