// File: /workspace/github_cpp_makefile_star2_O3/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 0xfec0
xorl %ebp, %ebp
movapd 0x2c64(%rip), %xmm3     # 0x129a0
movq 0x58(%r14), %rax
cmpb $0x0, (%rax,%rbp)
jle 0xfeb4
movl 0x38(%r14), %eax
testl %eax, %eax
jle 0xfe72
movq 0x68(%r14), %rcx
movzbl 0x50(%rcx), %edi
leal -0x1(%rdi), %ecx
cmpb $0x3, %cl
ja 0xff94
movl %eax, %ecx
imull %ebp, %ecx
movq 0x30(%rbx), %rdx
movsd (%r12,%rbp,8), %xmm0
xorl %r8d, %r8d
movb $0x1, %sil
addl %ecx, %r8d
movslq %r8d, %r8
movsd (%rdx,%r8,8), %xmm4
cmpl $0x3, %edi
je 0xfde9
cmpl $0x2, %edi
je 0xfdb7
cmpl $0x1, %edi
jne 0xfe15
movq 0x38(%rbx), %r9
movhpd (%r9,%r8,8), %xmm4      # xmm4 = xmm4[0],mem[0]
movsd (%r15,%rbp,8), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
mulpd %xmm4, %xmm4
jmp 0xfe47
movq 0x38(%rbx), %r9
movsd (%r9,%r8,8), %xmm1
movapd %xmm1, %xmm2
subsd %xmm4, %xmm2
xorpd %xmm3, %xmm4
unpcklpd %xmm1, %xmm4            # xmm4 = xmm4[0],xmm1[0]
unpcklpd %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0]
mulpd %xmm4, %xmm2
movsd (%r15,%rbp,8), %xmm4
unpcklpd %xmm0, %xmm4            # xmm4 = xmm4[0],xmm0[0]
addpd %xmm2, %xmm4
jmp 0xfe4b
movq 0x38(%rbx), %r9
movq 0x50(%rbx), %r10
movsd (%r9,%r8,8), %xmm1
subsd %xmm4, %xmm1
movhpd (%r10,%r8,8), %xmm4     # xmm4 = xmm4[0],mem[0]
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
mulpd %xmm4, %xmm1
movsd (%r15,%rbp,8), %xmm4
unpcklpd %xmm0, %xmm4            # xmm4 = xmm4[0],xmm0[0]
jmp 0xfe47
movapd %xmm4, %xmm1
xorpd %xmm3, %xmm1
movq 0x38(%rbx), %r9
movq 0x50(%rbx), %r10
movsd (%r9,%r8,8), %xmm2
subsd %xmm4, %xmm2
movhpd (%r10,%r8,8), %xmm1     # xmm1 = xmm1[0],mem[0]
unpcklpd %xmm2, %xmm4            # xmm4 = xmm4[0],xmm2[0]
mulpd %xmm1, %xmm4
movsd (%r15,%rbp,8), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
addpd %xmm1, %xmm4
movlpd %xmm4, (%r15,%rbp,8)
movapd %xmm4, %xmm0
unpckhpd %xmm4, %xmm0            # xmm0 = xmm0[1],xmm4[1]
movsbl %sil, %r8d
leal 0x1(%r8), %esi
cmpl %r8d, %eax
jg 0xfd7f
movsd %xmm0, (%r12,%rbp,8)
jmp 0xfe7e
movsd (%r15,%rbp,8), %xmm4
movhpd (%r12,%rbp,8), %xmm4    # xmm4 = xmm4[0],mem[0]
movapd %xmm4, (%rsp)
movapd %xmm4, %xmm0
unpckhpd %xmm4, %xmm0            # xmm0 = xmm0[1],xmm4[1]
callq 0x6158
movapd (%rsp), %xmm1
divsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm1, %xmm0
movlpd %xmm0, (%r15,%rbp,8)
movapd 0x2aec(%rip), %xmm3     # 0x129a0
incq %rbp
cmpq %r13, %rbp
jne 0xfd3c
movq 0x48(%rbx), %rbp
movslq 0xc(%rbx), %rdx
testq %rdx, %rdx
jle 0xfedb
shlq $0x3, %rdx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x2100
movq 0x58(%r14), %rdx
testl %r13d, %r13d
jle 0xff55
movl 0x38(%r14), %eax
xorl %ecx, %ecx
movapd 0x2aae(%rip), %xmm0     # 0x129a0
testl %eax, %eax
setle %sil
cmpb $0x0, (%rdx,%rcx)
setle %dil
orb %sil, %dil
jne 0xff4d
movq 0x30(%rbx), %rsi
movq 0x50(%rbx), %rdi
movsd (%r15,%rcx,8), %xmm1
xorpd %xmm0, %xmm1
movl %eax, %r8d
imull %ecx, %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, %eax
jg 0xff24
incq %rcx
cmpq %r13, %rcx
jne 0xfef2
movq %rbx, %rdi
movl $0x1, %esi
callq 0x806a
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 0x2bf7
nop