// File: /workspace/github_cpp_makefile_star2_O2/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, %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, %r13
addq $0x18, %r13
movl 0xd0(%r15), %eax
movl %eax, 0x4(%rsp)
movq 0xd8(%r15), %rax
movq %rax, 0x8(%rsp)
pushq $0x1
popq %rbp
pushq $0x2
popq %rbx
movq %r15, %rdi
movl %ebp, %esi
movl %ebx, %edx
callq 0xa97e
xorl %ecx, %ecx
movl %ebp, %eax
movl 0x4(%rsp), %ebp
movq 0x88(%r12), %rdx
testl %ecx, %ecx
jne 0x21ceb
cmpl 0x38(%rdx), %eax
jg 0x21ceb
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0xc0(%rax)
movl %eax, 0x24(%r14)
cmpl $0x1, (%r14)
jne 0x21bb4
movq %r12, %rdi
movl %ebp, %esi
movq 0x8(%rsp), %rdx
callq 0xe400
movsd %xmm0, 0x8(%r14)
movsd %xmm0, 0x10(%r14)
movq 0x88(%r12), %rax
movzbl 0x3f(%rax), %eax
cmpl $0x2, %eax
je 0x21bdb
leal -0x3(%rax), %ecx
testb $-0x3, %cl
jne 0x21ca3
cmpl $0x1, (%r14)
jne 0x21ca3
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0xc8(%rax)
movsd %xmm0, (%r13)
movq %r15, %rdi
movq %r14, %rsi
callq 0xaab2
movzbl %al, %eax
movl %eax, 0x20(%r14)
movq %r15, %rdi
movl %ebx, %esi
pushq $0x7
popq %rdx
callq 0xa97e
movq %r15, %rdi
pushq $0x1
popq %rsi
movl %ebx, %edx
callq 0xa97e
movq 0x88(%r12), %rax
cmpb $0x0, 0x3c(%rax)
je 0x21c77
movb 0x3f(%rax), %al
cmpb $0x3, %al
jne 0x21c63
xorl %eax, %eax
cmpl $0x1, (%r14)
setne %al
leal 0x3(%rax,%rax,4), %esi
movq %r15, %rdi
pushq $0x6
popq %rdx
callq 0xa97e
movq %r15, %rdi
pushq $0x3
popq %rsi
pushq $0x4
popq %rdx
callq 0xa97e
movq 0x88(%r12), %rax
movb 0x3f(%rax), %al
cmpb $0x5, %al
jne 0x21c86
movq %r15, %rdi
pushq $0x3
popq %rsi
pushq $0x4
popq %rdx
callq 0xa97e
jmp 0x21c86
cmpl $0x0, 0x20(%r14)
jne 0x21c86
movl (%r14), %ecx
cmpl 0x38(%rax), %ecx
jne 0x21c2b
movl (%r14), %eax
incl %eax
movl %eax, (%r14)
movsd 0x18(%r14), %xmm0
movsd %xmm0, 0x10(%r14)
movl 0x20(%r14), %ecx
jmp 0x21b66
cmpl $0x5, %eax
je 0x21cdb
cmpl $0x4, %eax
je 0x21cc6
cmpl $0x3, %eax
jne 0x21bf1
movq %r12, %rdi
movq %r15, %rsi
callq 0x21d42
jmp 0x21beb
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0xd0(%rax)
jmp 0x21beb
movq %r12, %rdi
movq %r15, %rsi
callq 0x228b0
jmp 0x21beb
movl %ebp, %edi
movq 0x8(%rsp), %rsi
callq 0x7b9b
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)
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