// File: /workspace/github_cpp_makefile_star2_O3/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
subq $0x18, %rsp
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, 0x10(%rsp)
movl $0x28, %edi
callq 0x4210
movq %rax, %r14
movl $0x1, (%rax)
movq $0x0, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movq %rax, %rbx
addq $0x18, %rbx
movl 0xd0(%r12), %eax
movl %eax, 0xc(%rsp)
movq 0xd8(%r12), %r13
movl $0x1, %ebp
movq %r12, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0xd4e8
movq 0x88(%r15), %rax
cmpl 0x38(%rax), %ebp
jg 0x27a12
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
callq *0xc0(%rax)
movl %eax, 0x24(%r14)
cmpl $0x1, (%r14)
jne 0x278b5
movq %r15, %rdi
movl 0xc(%rsp), %esi
movq %r13, %rdx
callq 0x114ae
movsd %xmm0, 0x8(%r14)
movsd %xmm0, 0x10(%r14)
movq 0x88(%r15), %rax
movzbl 0x3f(%rax), %eax
cmpl $0x2, %eax
je 0x278db
leal -0x3(%rax), %ecx
testb $-0x3, %cl
jne 0x279ba
cmpl $0x1, (%r14)
jne 0x279ba
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
callq *0xc8(%rax)
movsd %xmm0, (%rbx)
movq %r12, %rdi
movq %r14, %rsi
callq 0xd644
movzbl %al, %eax
movl %eax, 0x20(%r14)
movq %r12, %rdi
movl $0x2, %esi
movl $0x7, %edx
callq 0xd4e8
movq %r12, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0xd4e8
movq 0x88(%r15), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x27944
cmpl $0x0, 0x20(%r14)
movl (%r14), %ecx
jne 0x27a01
cmpl 0x38(%rax), %ecx
je 0x27999
movb 0x3f(%rax), %al
cmpb $0x3, %al
jne 0x27983
xorl %eax, %eax
cmpl $0x1, (%r14)
setne %al
leal (%rax,%rax,4), %esi
addl $0x3, %esi
movq %r12, %rdi
movl $0x6, %edx
callq 0xd4e8
movq %r12, %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0xd4e8
movq 0x88(%r15), %rax
movb 0x3f(%rax), %al
cmpb $0x5, %al
jne 0x27999
movq %r12, %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0xd4e8
movl (%r14), %ebp
incl %ebp
movl %ebp, (%r14)
movsd 0x18(%r14), %xmm0
movsd %xmm0, 0x10(%r14)
cmpl $0x0, 0x20(%r14)
je 0x27871
jmp 0x27a12
cmpl $0x5, %eax
je 0x279f1
cmpl $0x4, %eax
je 0x279dd
cmpl $0x3, %eax
jne 0x278ee
movq %r15, %rdi
movq %r12, %rsi
callq 0x27a72
jmp 0x278ea
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
callq *0xd0(%rax)
jmp 0x278ea
movq %r15, %rdi
movq %r12, %rsi
callq 0x2863a
jmp 0x278ea
incl %ecx
movl %ecx, (%r14)
movsd 0x18(%r14), %xmm0
movsd %xmm0, 0x10(%r14)
movq 0x88(%r15), %rdx
movl 0xc(%rsp), %edi
movq %r13, %rsi
callq 0xa3aa
movl (%r14), %eax
decl %eax
movl %eax, (%r14)
movq 0x10(%rsp), %rbx
movl %eax, (%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
nop