// File: js850[P]lbfgs_cpp/lbfgs.cpp
// Function: LBFGS_ns::LBFGS::compute_lbfgs_step()
void LBFGS::compute_lbfgs_step()
{
  if (k_ == 0){ 
    double gnorm = vecnorm(g_);
    if (gnorm > 1.) gnorm = 1. / gnorm;
    for (size_t j2 = 0; j2 < x_.size(); ++j2){
      step_[j2] = - gnorm * H0_ * g_[j2];
    }
    return;
  } 

  step_ = g_;

  int jmin = std::max(0, k_ - M_);
  int jmax = k_;
  int i;
  double beta;
  vector<double> alpha(M_);

  // loop backwards through the memory
  for (int j = jmax - 1; j >= jmin; --j){
    i = j % M_;
    //cout << "    i " << i << " j " << j << "\n";
    alpha[i] = rho_[i] * vecdot(s_[i], step_);
    for (size_t j2 = 0; j2 < step_.size(); ++j2){
      step_[j2] -= alpha[i] * y_[i][j2];
    }
  }

  // scale the step size by H0
  for (size_t j2 = 0; j2 < step_.size(); ++j2){
    step_[j2] *= H0_;
  }

  // loop forwards through the memory
  for (int j = jmin; j < jmax; ++j){
    i = j % M_;
    //cout << "    i " << i << " j " << j << "\n";
    beta = rho_[i] * vecdot(y_[i], step_);
    for (size_t j2 = 0; j2 < step_.size(); ++j2){
      step_[j2] += s_[i][j2] * (alpha[i] - beta);
    }
  }

  // invert the step to point downhill
  for (size_t j2 = 0; j2 < x_.size(); ++j2){
    step_[j2] *= -1;
  }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x110(%rbp)
cmpl $0x0, 0xc8(%rax)
jne 0x30b2
movq -0x110(%rbp), %rsi
addq $0x58, %rsi
leaq -0x28(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x4160
movq -0x120(%rbp), %rdi
callq 0x2550
movsd %xmm0, -0x118(%rbp)
jmp 0x2f98
leaq -0x28(%rbp), %rdi
callq 0x19e0
movsd -0x118(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movsd -0x10(%rbp), %xmm0
movsd 0x30dd(%rip), %xmm1     # 0x6098
ucomisd %xmm1, %xmm0
jbe 0x2fef
movsd 0x30cf(%rip), %xmm0     # 0x6098
divsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
jmp 0x2fef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x19e0
jmp 0x366b
movq $0x0, -0x40(%rbp)
movq -0x110(%rbp), %rdi
movq -0x40(%rbp), %rax
movq %rax, -0x128(%rbp)
addq $0x38, %rdi
callq 0x4120
movq %rax, %rcx
movq -0x128(%rbp), %rax
cmpq %rcx, %rax
jae 0x30ad
movq -0x110(%rbp), %rdi
movsd -0x10(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd 0xc0(%rdi), %xmm0
movsd %xmm0, -0x138(%rbp)
addq $0x58, %rdi
movq -0x40(%rbp), %rsi
callq 0x1860
movsd -0x138(%rbp), %xmm0
movq -0x110(%rbp), %rdi
mulsd (%rax), %xmm0
movsd %xmm0, -0x130(%rbp)
addq $0xd0, %rdi
movq -0x40(%rbp), %rsi
callq 0x1860
movsd -0x130(%rbp), %xmm0
movsd %xmm0, (%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2ff7
jmp 0x3659
movq -0x110(%rbp), %rdi
movq %rdi, %rsi
addq $0x58, %rsi
addq $0xd0, %rdi
callq 0x4410
movq -0x110(%rbp), %rax
movl $0x0, -0x48(%rbp)
movl 0x8(%rax), %ecx
movl 0xc8(%rax), %eax
subl %ecx, %eax
movl %eax, -0x4c(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
callq 0x4660
movq %rax, %rcx
movq -0x110(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, -0x44(%rbp)
movl 0xc8(%rax), %ecx
movl %ecx, -0x50(%rbp)
movslq 0x8(%rax), %rax
movq %rax, -0x148(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x17b0
movq -0x148(%rbp), %rsi
movq -0x140(%rbp), %rdx
leaq -0x78(%rbp), %rdi
callq 0x17d0
jmp 0x3141
leaq -0x79(%rbp), %rdi
callq 0x1840
movl -0x50(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x44(%rbp), %eax
jl 0x3374
movq -0x110(%rbp), %rdi
movl -0x80(%rbp), %eax
movl 0x8(%rdi), %ecx
cltd
idivl %ecx
movl %edx, -0x54(%rbp)
addq $0xa8, %rdi
movslq -0x54(%rbp), %rsi
callq 0x1860
movq -0x110(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x150(%rbp)
addq $0x78, %rdi
movslq -0x54(%rbp), %rsi
callq 0x43f0
movq %rax, %rsi
leaq -0x98(%rbp), %rdi
callq 0x4160
jmp 0x31b3
movq -0x110(%rbp), %rsi
addq $0xd0, %rsi
leaq -0xb0(%rbp), %rdi
callq 0x4160
jmp 0x31cf
leaq -0x98(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x2470
movaps %xmm0, %xmm1
movsd -0x150(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x158(%rbp)
movslq -0x54(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x1860
movsd -0x158(%rbp), %xmm0
movsd %xmm0, (%rax)
leaq -0xb0(%rbp), %rdi
callq 0x19e0
leaq -0x98(%rbp), %rdi
callq 0x19e0
movq $0x0, -0xb8(%rbp)
movq -0x110(%rbp), %rdi
movq -0xb8(%rbp), %rax
movq %rax, -0x160(%rbp)
addq $0xd0, %rdi
callq 0x4120
movq %rax, %rcx
movq -0x160(%rbp), %rax
cmpq %rcx, %rax
jae 0x3364
movslq -0x54(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x1860
movq -0x110(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x170(%rbp)
addq $0x90, %rdi
movslq -0x54(%rbp), %rsi
callq 0x43f0
movq %rax, %rdi
movq -0xb8(%rbp), %rsi
callq 0x1860
movq -0x110(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x168(%rbp)
addq $0xd0, %rdi
movq -0xb8(%rbp), %rsi
callq 0x1860
movsd -0x170(%rbp), %xmm0
movsd -0x168(%rbp), %xmm2
movsd (%rax), %xmm1
movq %xmm0, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
xorq %rdx, %rcx
movq %rcx, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movq -0xb8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb8(%rbp)
jmp 0x3235
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x79(%rbp), %rdi
callq 0x1840
jmp 0x366b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x3662
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x19e0
jmp 0x3662
jmp 0x3366
movl -0x80(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x3153
movq $0x0, -0xc0(%rbp)
movq -0x110(%rbp), %rdi
movq -0xc0(%rbp), %rax
movq %rax, -0x178(%rbp)
addq $0xd0, %rdi
callq 0x4120
movq %rax, %rcx
movq -0x178(%rbp), %rax
cmpq %rcx, %rax
jae 0x33fd
movq -0x110(%rbp), %rdi
movsd 0xc0(%rdi), %xmm0
movsd %xmm0, -0x180(%rbp)
addq $0xd0, %rdi
movq -0xc0(%rbp), %rsi
callq 0x1860
movsd -0x180(%rbp), %xmm0
mulsd (%rax), %xmm0
movsd %xmm0, (%rax)
movq -0xc0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc0(%rbp)
jmp 0x337f
movl -0x44(%rbp), %eax
movl %eax, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x35da
movq -0x110(%rbp), %rdi
movl -0xc4(%rbp), %eax
movl 0x8(%rdi), %ecx
cltd
idivl %ecx
movl %edx, -0x54(%rbp)
addq $0xa8, %rdi
movslq -0x54(%rbp), %rsi
callq 0x1860
movq -0x110(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x188(%rbp)
addq $0x90, %rdi
movslq -0x54(%rbp), %rsi
callq 0x43f0
movq %rax, %rsi
leaq -0xe0(%rbp), %rdi
callq 0x4160
jmp 0x346f
movq -0x110(%rbp), %rsi
addq $0xd0, %rsi
leaq -0xf8(%rbp), %rdi
callq 0x4160
jmp 0x348b
leaq -0xe0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x2470
movaps %xmm0, %xmm1
movsd -0x188(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x19e0
leaq -0xe0(%rbp), %rdi
callq 0x19e0
movq $0x0, -0x100(%rbp)
movq -0x110(%rbp), %rdi
movq -0x100(%rbp), %rax
movq %rax, -0x190(%rbp)
addq $0xd0, %rdi
callq 0x4120
movq %rax, %rcx
movq -0x190(%rbp), %rax
cmpq %rcx, %rax
jae 0x35c4
movq -0x110(%rbp), %rdi
addq $0x78, %rdi
movslq -0x54(%rbp), %rsi
callq 0x43f0
movq %rax, %rdi
movq -0x100(%rbp), %rsi
callq 0x1860
movsd (%rax), %xmm0
movsd %xmm0, -0x1a0(%rbp)
movslq -0x54(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x1860
movq -0x110(%rbp), %rdi
movsd (%rax), %xmm0
subsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x198(%rbp)
addq $0xd0, %rdi
movq -0x100(%rbp), %rsi
callq 0x1860
movsd -0x1a0(%rbp), %xmm0
movsd -0x198(%rbp), %xmm2
movsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movq -0x100(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x100(%rbp)
jmp 0x34d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x19e0
jmp 0x3662
jmp 0x35c6
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x3406
movq $0x0, -0x108(%rbp)
movq -0x110(%rbp), %rdi
movq -0x108(%rbp), %rax
movq %rax, -0x1a8(%rbp)
addq $0x38, %rdi
callq 0x4120
movq %rax, %rcx
movq -0x1a8(%rbp), %rax
cmpq %rcx, %rax
jae 0x3650
movq -0x110(%rbp), %rdi
addq $0xd0, %rdi
movq -0x108(%rbp), %rsi
callq 0x1860
movsd 0x2a5c(%rip), %xmm0     # 0x6090
mulsd (%rax), %xmm0
movsd %xmm0, (%rax)
movq -0x108(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x108(%rbp)
jmp 0x35e5
leaq -0x78(%rbp), %rdi
callq 0x19e0
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
leaq -0x78(%rbp), %rdi
callq 0x19e0
movq -0x30(%rbp), %rdi
callq 0x1140
nopw %cs:(%rax,%rax)