// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/HMMProblemSlicedAB.cpp
// Function: HMMProblemSlicedAB::GradientDescentBit(FitBitSlicedAB*)
FitResult HMMProblemSlicedAB::GradientDescentBit(FitBitSlicedAB *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);
        
        if(fr->iter==1) {
            fr->pO0 = HMMProblemSlicedAB::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);
                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--;
    
    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, %r12
movq %rsi, %r15
movq %rdi, %rbx
movl $0x28, %edi
callq 0x4210
movq %rax, %r14
movl $0x1, (%rax)
movq $0x0, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movl 0xd0(%r12), %ebp
movq 0xd8(%r12), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0xd4bc
cmpl $0x0, 0x20(%r14)
je 0x27dd0
movq 0x88(%r15), %rdx
movl %ebp, %edi
movq (%rsp), %rsi
callq 0xa39d
movl (%r14), %eax
decl %eax
movl %eax, (%r14)
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, %r13
addq $0x18, %r13
movl (%r14), %eax
movq 0x88(%r15), %rcx
cmpl 0x38(%rcx), %eax
jg 0x27d71
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
callq *0xc0(%rax)
movl %eax, 0x24(%r14)
cmpl $0x1, (%r14)
jne 0x27e19
movq %r15, %rdi
movl %ebp, %esi
movq (%rsp), %rdx
callq 0x1161c
movsd %xmm0, 0x8(%r14)
movsd %xmm0, 0x10(%r14)
movq 0x88(%r15), %rax
movzbl 0x3f(%rax), %eax
cmpl $0x2, %eax
je 0x27e3f
leal -0x3(%rax), %ecx
testb $-0x3, %cl
jne 0x27f1a
cmpl $0x1, (%r14)
jne 0x27f1a
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
callq *0xc8(%rax)
movsd %xmm0, (%r13)
movq %r12, %rdi
movq %r14, %rsi
callq 0xd5ec
movzbl %al, %eax
movl %eax, 0x20(%r14)
movq %r12, %rdi
movl $0x2, %esi
movl $0x7, %edx
callq 0xd4bc
movq %r12, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0xd4bc
movq 0x88(%r15), %rax
cmpb $0x0, 0x3c(%rax)
je 0x27eec
cmpb $0x3, 0x3f(%rax)
jne 0x27ecb
xorl %eax, %eax
cmpl $0x1, (%r14)
setne %al
leal (%rax,%rax,4), %esi
addl $0x3, %esi
movq %r12, %rdi
movl $0x6, %edx
callq 0xd4bc
movq %r12, %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0xd4bc
movq 0x88(%r15), %rax
cmpb $0x5, 0x3f(%rax)
jne 0x27efb
movq %r12, %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0xd4bc
jmp 0x27efb
cmpl $0x0, 0x20(%r14)
jne 0x27efb
movl (%r14), %ecx
cmpl 0x38(%rax), %ecx
jne 0x27e97
incl (%r14)
movsd 0x18(%r14), %xmm0
movsd %xmm0, 0x10(%r14)
cmpl $0x0, 0x20(%r14)
je 0x27dd7
jmp 0x27d71
cmpl $0x5, %eax
je 0x27f51
cmpl $0x4, %eax
je 0x27f3d
cmpl $0x3, %eax
jne 0x27e54
movq %r15, %rdi
movq %r12, %rsi
callq 0x27f62
jmp 0x27e4e
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
callq *0xd0(%rax)
jmp 0x27e4e
movq %r15, %rdi
movq %r12, %rsi
callq 0x28b50
jmp 0x27e4e
nop