// File: /workspace/github_cpp_makefile_star2_O2/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, 0x10(%rsp)
pushq $0x28
popq %rdi
callq 0x4220
movq %rax, %r14
movl $0x1, (%rax)
andq $0x0, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movq %rax, %rbx
addq $0x18, %rbx
movl 0xe8(%r15), %eax
movl %eax, 0x4(%rsp)
movq 0xf0(%r15), %rax
movq %rax, 0x8(%rsp)
pushq $0x1
popq %r13
pushq $0x2
popq %rdx
movq %r15, %rdi
movl %r13d, %esi
callq 0x88b6
xorl %ecx, %ecx
pushq $0x3
popq %rdx
movl %r13d, %eax
movl %edx, %r13d
movq 0x88(%r12), %rdx
testl %ecx, %ecx
jne 0x11629
cmpl 0x38(%rdx), %eax
jg 0x11629
movl %r13d, %ebp
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x80(%rax)
movl %eax, 0x24(%r14)
movl (%r14), %r13d
cmpl $0x1, %r13d
jne 0x114d4
movl 0x4(%rsp), %esi
movq 0x8(%rsp), %rdx
callq 0xe400
movsd %xmm0, 0x8(%r14)
movsd %xmm0, 0x10(%r14)
cmpl $0x1, %r13d
sete %cl
movq 0x88(%r12), %rax
movzbl 0x3f(%rax), %eax
leal -0x3(%rax), %edx
testb $-0x3, %dl
sete %dl
cmpl $0x2, %eax
je 0x11520
andb %dl, %cl
jne 0x11520
cmpl $0x5, %eax
movl %ebp, %r13d
je 0x11619
cmpl $0x4, %eax
je 0x11604
cmpl $0x3, %eax
jne 0x11537
movq %r12, %rdi
movq %r15, %rsi
callq 0x116a8
jmp 0x11533
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x88(%rax)
movl %ebp, %r13d
movsd %xmm0, (%rbx)
movq %r15, %rdi
movq %r14, %rsi
callq 0x89ea
movzbl %al, %eax
movl %eax, 0x20(%r14)
movq %r15, %rdi
pushq $0x2
popq %rbp
movl %ebp, %esi
pushq $0x7
popq %rdx
callq 0x88b6
movq %r15, %rdi
pushq $0x1
popq %rsi
movl %ebp, %edx
callq 0x88b6
movq 0x88(%r12), %rax
cmpb $0x0, 0x3c(%rax)
je 0x1159d
movb 0x3f(%rax), %al
cmpb $0x3, %al
jne 0x115d5
cmpl $0x1, (%r14)
jne 0x115ae
movq %r15, %rdi
movl %r13d, %esi
pushq $0x6
popq %rbp
movl %ebp, %edx
callq 0x88b6
movq %r15, %rdi
movl %ebp, %esi
callq 0x87c6
jmp 0x115bc
cmpl $0x0, 0x20(%r14)
jne 0x115e7
movl (%r14), %ecx
cmpl 0x38(%rax), %ecx
jne 0x11574
jmp 0x115e7
movq %r15, %rdi
pushq $0x8
popq %rsi
pushq $0x6
popq %rdx
callq 0x88b6
movq %r15, %rdi
movl %r13d, %esi
pushq $0x4
popq %rdx
callq 0x88b6
movq 0x88(%r12), %rax
movb 0x3f(%rax), %al
cmpb $0x5, %al
jne 0x115e7
movq %r15, %rdi
movl %r13d, %esi
pushq $0x4
popq %rdx
callq 0x88b6
movl (%r14), %eax
incl %eax
movl %eax, (%r14)
movsd 0x18(%r14), %xmm0
movsd %xmm0, 0x10(%r14)
movl 0x20(%r14), %ecx
jmp 0x11481
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x90(%rax)
jmp 0x11533
movq %r12, %rdi
movq %r15, %rsi
callq 0x12456
jmp 0x11533
movl 0x4(%rsp), %edi
movq 0x8(%rsp), %rsi
callq 0x7b9b
movl (%r14), %ecx
decl %ecx
movl %ecx, (%r14)
cmpl $0x2, %ecx
jne 0x11665
cmpl $0x0, 0x20(%r14)
pushq $0x2
popq %rcx
je 0x11665
movq %r15, %rdi
movq %r14, %rsi
callq 0x8c26
movl (%r14), %ecx
testb %al, %al
je 0x11665
decl %ecx
movl %ecx, (%r14)
movq 0x10(%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)
pushq $0x28
popq %rsi
movq %r14, %rdi
callq 0x4230
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop