// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x40(%rbp)
movq %rdi, -0x38(%rbp)
movq %rsi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x28, %edi
callq 0x52d0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
movq -0x18(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rax)
movq -0x18(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x24(%rax)
movq -0x10(%rbp), %rax
movl 0xd0(%rax), %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x12890
movq -0x18(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x20(%rcx)
movb %al, -0x41(%rbp)
jne 0x3cbde
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
setle %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x3cbea
jmp 0x3ce49
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq (%rdi), %rax
callq *0xc0(%rax)
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x3cc38
movq -0x30(%rbp), %rdi
movl -0x1c(%rbp), %esi
movq -0x28(%rbp), %rdx
callq 0x1a9f0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movq -0x18(%rbp), %rax
movsd 0x8(%rax), %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x10(%rax)
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x2, %eax
je 0x3cc86
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x3cc69
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
je 0x3cc86
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x3cca5
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x5, %eax
jne 0x3cca5
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq (%rdi), %rax
callq *0xc8(%rax)
movq -0x18(%rbp), %rax
movsd %xmm0, 0x18(%rax)
jmp 0x3cd31
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x3ccd1
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3ced0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x18(%rax)
jmp 0x3cd2f
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x4, %eax
jne 0x3cd01
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq (%rdi), %rax
callq *0xd0(%rax)
movq -0x18(%rbp), %rax
movsd %xmm0, 0x18(%rax)
jmp 0x3cd2d
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x5, %eax
jne 0x3cd2b
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3e890
movq -0x18(%rbp), %rax
movsd %xmm0, 0x18(%rax)
jmp 0x3cd2d
jmp 0x3cd2f
jmp 0x3cd31
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x129f0
andb $0x1, %al
movzbl %al, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x10(%rbp), %rdi
movl $0x2, %esi
movl $0x7, %edx
callq 0x12890
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x12890
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x3cda6
movq -0x18(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x3cda1
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x3cda6
jmp 0x3ce27
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x3cdfe
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x3cdd8
movq -0x10(%rbp), %rdi
movl $0x3, %esi
movl $0x6, %edx
callq 0x12890
jmp 0x3cdeb
movq -0x10(%rbp), %rdi
movl $0x8, %esi
movl $0x6, %edx
callq 0x12890
movq -0x10(%rbp), %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0x12890
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x5, %eax
jne 0x3ce25
movq -0x10(%rbp), %rdi
movl $0x3, %esi
movl $0x4, %edx
callq 0x12890
jmp 0x3ce27
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movsd 0x18(%rax), %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x10(%rax)
jmp 0x3cbb5
movq -0x30(%rbp), %rax
movl -0x1c(%rbp), %edi
movq -0x28(%rbp), %rsi
movq 0x88(%rax), %rdx
callq 0xbfd0
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
movl (%rcx), %edx
addl $-0x1, %edx
movl %edx, (%rcx)
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rcx
movsd 0x8(%rcx), %xmm0
movsd %xmm0, 0x8(%rax)
movq -0x18(%rbp), %rcx
movsd 0x18(%rcx), %xmm0
movsd %xmm0, 0x18(%rax)
movq -0x18(%rbp), %rcx
movl 0x20(%rcx), %ecx
movl %ecx, 0x20(%rax)
movq -0x18(%rbp), %rcx
movl 0x24(%rcx), %ecx
movl %ecx, 0x24(%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x3cec3
movq -0x50(%rbp), %rdi
movl $0x28, %esi
callq 0x52e0
movq -0x38(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)