// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/HMMProblemSt.cpp
// Function: HMMProblemSt::doConjugateLinearStep(FitBitSt*)
void HMMProblemSt::doConjugateLinearStep(FitBitSt *fb) {
    NPAR i=0;
    NCAT nK = this->task->nK, k, l;
   
    // first scale down gradients -- MOVED TO Cycle Bit
    // fb->scaleGradients(false, this->active_set_block);
    
    // compute beta_gradient_direction
    NUMBER *beta_grad = init1D<NUMBER>(nK);
    NUMBER *beta_grad_den = init1D<NUMBER>(nK);

    for(k=0; k<nK; k++) {
        if(this->active_set_block[k]>0) {
            for(i=0; i<skill1_n; i++) {
                l = skill1_n*k + i;
                if(this->task->solver_setting == 1) { // Polak-Ribiere
                    beta_grad[k]     += fb->GRAD[l]*fb->GRAD[l];
                    beta_grad_den[k] += fb->GRADm1[l]*fb->GRADm1[l];
                } else if(this->task->solver_setting == 2) { // Fletcher-Reeves
                    beta_grad[k]     += -fb->GRAD[l]*(-fb->GRAD[l] + fb->GRADm1[l]);
                    beta_grad_den[k] +=  fb->GRADm1[l]*fb->GRADm1[l];
                } else if(this->task->solver_setting == 3) { // Hestenes-Stiefel
                    beta_grad[k]     += fb->GRAD[l]* (-fb->GRAD[l] + fb->GRADm1[l]); // no -, since neg gradient and - is +
                    beta_grad_den[k] += fb->DIRm1[l]*(-fb->GRAD[l] + fb->GRADm1[l]);
                } else if(this->task->solver_setting == 4) { // Dai-Yuan
                    beta_grad[k]     += -fb->GRAD[l]*fb->GRAD[l];
                    beta_grad_den[k] +=  fb->DIRm1[l]*(-fb->GRAD[l] + fb->GRADm1[l]);
                } else {
                    fprintf(stderr,"Wrong stepping algorithm (%d) specified for Conjugate Gradient Descent\n",this->task->solver_setting);
                    exit(1);
                }
            }
            beta_grad[k] = beta_grad[k] / safe0num(beta_grad_den[k]);
            beta_grad[k] = (beta_grad[k]>=0)?beta_grad[k]:0;
        } // if in the active set
    }// for all skills
    
    // compute new negative direction
    toZero1D(fb->DIR, fb->nPARAM);
    for(k=0; k<nK; k++) {
        if(this->active_set_block[k]>0) {
            for(i=0; i<skill1_n; i++) {
                l = skill1_n*k + i;
//              fb->DIR[l] = -fb->GRAD[l] + beta_grad[k] * fb->DIRm1[l];
                fb->DIR[l] =  fb->GRAD[l] - beta_grad[k] * fb->DIRm1[l]; // negative direction
            }
        }
    }
    fb->scaleGradients(true, this->active_set_block);

    // almost identical code from linear step, but using direction
    doLinearStep(fb, true /*direction*/);
     
    // recycle
    free(beta_grad);
    free(beta_grad_den);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq 0x68(%rdi), %rax
movslq 0x908(%rax), %r13
movl $0x8, %esi
movq %r13, %rdi
callq 0x2110
movq %rax, %r15
movl $0x8, %esi
movq %r13, %rdi
callq 0x2110
movq %rax, %r12
testq %r13, %r13
jle 0xfb5f
xorl %ebp, %ebp
movq 0x58(%r14), %rax
cmpb $0x0, (%rax,%rbp)
jle 0xfb53
movl 0x38(%r14), %eax
testl %eax, %eax
jle 0xfb22
movq 0x68(%r14), %rcx
movzbl 0x50(%rcx), %edi
leal -0x1(%rdi), %ecx
cmpb $0x3, %cl
ja 0xfc37
movl %eax, %ecx
imull %ebp, %ecx
xorl %esi, %esi
movb $0x1, %dl
addl %ecx, %esi
movslq %esi, %rsi
cmpl $0x3, %edi
je 0xfa9a
cmpl $0x2, %edi
je 0xfa64
cmpl $0x1, %edi
jne 0xface
movq 0x30(%rbx), %r8
movsd (%r8,%rsi,8), %xmm0
mulsd %xmm0, %xmm0
addsd (%r15,%rbp,8), %xmm0
movsd %xmm0, (%r15,%rbp,8)
movq 0x38(%rbx), %r8
movsd (%r8,%rsi,8), %xmm0
mulsd %xmm0, %xmm0
addsd (%r12,%rbp,8), %xmm0
movsd %xmm0, (%r12,%rbp,8)
jmp 0xfb14
movq 0x30(%rbx), %r8
movq 0x38(%rbx), %r9
movsd (%r8,%rsi,8), %xmm0
movsd (%r9,%rsi,8), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
movsd (%r15,%rbp,8), %xmm3
mulsd %xmm0, %xmm2
subsd %xmm2, %xmm3
movsd %xmm3, (%r15,%rbp,8)
mulsd %xmm1, %xmm1
jmp 0xfb08
movq 0x30(%rbx), %r8
movq 0x38(%rbx), %r9
movsd (%r8,%rsi,8), %xmm0
movsd (%r9,%rsi,8), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm0
addsd (%r15,%rbp,8), %xmm0
movsd %xmm0, (%r15,%rbp,8)
movq 0x50(%rbx), %r8
mulsd (%r8,%rsi,8), %xmm1
jmp 0xfb08
movq 0x30(%rbx), %r8
movsd (%r8,%rsi,8), %xmm0
movsd (%r15,%rbp,8), %xmm1
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
subsd %xmm2, %xmm1
movsd %xmm1, (%r15,%rbp,8)
movq 0x38(%rbx), %r8
movq 0x50(%rbx), %r9
movsd (%r8,%rsi,8), %xmm1
subsd %xmm0, %xmm1
mulsd (%r9,%rsi,8), %xmm1
addsd (%r12,%rbp,8), %xmm1
movsd %xmm1, (%r12,%rbp,8)
movsbl %dl, %esi
leal 0x1(%rsi), %edx
cmpl %esi, %eax
jg 0xfa13
movsd (%r15,%rbp,8), %xmm0
movaps %xmm0, (%rsp)
movsd (%r12,%rbp,8), %xmm0
callq 0x6046
movapd (%rsp), %xmm1
divsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm1, %xmm0
movlpd %xmm0, (%r15,%rbp,8)
incq %rbp
cmpq %r13, %rbp
jne 0xf9dc
movq 0x48(%rbx), %rbp
movslq 0xc(%rbx), %rdx
testq %rdx, %rdx
jle 0xfb7a
shlq $0x3, %rdx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x2100
testl %r13d, %r13d
jle 0xfbf4
movq 0x58(%r14), %rax
movl 0x38(%r14), %ecx
xorl %edx, %edx
movapd 0x1e0f(%rip), %xmm0     # 0x119a0
testl %ecx, %ecx
setle %sil
cmpb $0x0, (%rax,%rdx)
setle %dil
orb %sil, %dil
jne 0xfbec
movq 0x30(%rbx), %rsi
movq 0x50(%rbx), %rdi
movsd (%r15,%rdx,8), %xmm1
xorpd %xmm0, %xmm1
movl %ecx, %r8d
imull %edx, %r8d
xorl %r10d, %r10d
movb $0x1, %r9b
addl %r8d, %r10d
movslq %r10d, %r10
movsd (%rdi,%r10,8), %xmm2
mulsd %xmm1, %xmm2
addsd (%rsi,%r10,8), %xmm2
movsd %xmm2, (%rbp,%r10,8)
movsbl %r9b, %r10d
incb %r9b
cmpl %r10d, %ecx
jg 0xfbc3
incq %rdx
cmpq %r13, %rdx
jne 0xfb91
movq 0x58(%r14), %rdx
movq %rbx, %rdi
movl $0x1, %esi
callq 0x7fda
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq *0xa0(%rax)
movq %r15, %rdi
callq 0x2240
movq %r12, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2240
callq 0x2ba9