// File: js850[P]lbfgs_cpp/lbfgs.cpp
// Function: LBFGS_ns::LBFGS::update_memory(std::vector<double, std::allocator<double>>&, std::vector<double, std::allocator<double>>&, std::vector<double, std::allocator<double>>&, std::vector<double, std::allocator<double>>&)
void LBFGS::update_memory(
          std::vector<double> & xold,
          std::vector<double> & gold,
          std::vector<double> & xnew,
          std::vector<double> & gnew
          )
{
  // update the lbfgs memory
  // This updates s_, y_, rho_, and H0_, and k_
  int klocal = k_ % M_;
  for (size_t j2 = 0; j2 < x_.size(); ++j2){
    y_[klocal][j2] = gnew[j2] - gold[j2];
    s_[klocal][j2] = xnew[j2] - xold[j2];
  }

  double ys = vecdot(y_[klocal], s_[klocal]);
  if (ys == 0.) {
    // should print a warning here
    cout << "warning: resetting YS to 1.\n";
    ys = 1.;
  }

  rho_[klocal] = 1. / ys;

  double yy = vecdot(y_[klocal], y_[klocal]);
  if (yy == 0.) {
    // should print a warning here
    cout << "warning: resetting YY to 1.\n";
    yy = 1.;
  }
  H0_ = ys / yy;
//  cout << "    setting H0 " << H0_ 
//    << " ys " << ys 
//    << " yy " << yy 
//    << " rho[i] " << rho_[klocal] 
//    << "\n";

  // increment k
  k_ += 1;
  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0xc0(%rbp)
movl 0xc8(%rcx), %eax
cltd
idivl 0x8(%rcx)
movl %edx, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movq -0xc0(%rbp), %rdi
movq -0x38(%rbp), %rax
movq %rax, -0xc8(%rbp)
addq $0x38, %rdi
callq 0x4120
movq %rax, %rcx
movq -0xc8(%rbp), %rax
cmpq %rcx, %rax
jae 0x3e2d
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1860
movsd (%rax), %xmm0
movsd %xmm0, -0xe8(%rbp)
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1860
movsd -0xe8(%rbp), %xmm0
movq -0xc0(%rbp), %rdi
subsd (%rax), %xmm0
movsd %xmm0, -0xe0(%rbp)
addq $0x90, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x43f0
movq %rax, %rdi
movq -0x38(%rbp), %rsi
callq 0x1860
movsd -0xe0(%rbp), %xmm0
movsd %xmm0, (%rax)
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1860
movsd (%rax), %xmm0
movsd %xmm0, -0xd8(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1860
movsd -0xd8(%rbp), %xmm0
movq -0xc0(%rbp), %rdi
subsd (%rax), %xmm0
movsd %xmm0, -0xd0(%rbp)
addq $0x78, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x43f0
movq %rax, %rdi
movq -0x38(%rbp), %rsi
callq 0x1860
movsd -0xd0(%rbp), %xmm0
movsd %xmm0, (%rax)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x3d1f
movq -0xc0(%rbp), %rdi
addq $0x90, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x43f0
movq %rax, %rsi
leaq -0x58(%rbp), %rdi
callq 0x4160
movq -0xc0(%rbp), %rdi
addq $0x78, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x43f0
movq %rax, %rsi
leaq -0x70(%rbp), %rdi
callq 0x4160
jmp 0x3e72
leaq -0x58(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x2470
movsd %xmm0, -0xf0(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x19e0
leaq -0x58(%rbp), %rdi
callq 0x19e0
movsd -0xf0(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3ef2
jp 0x3ef2
movq 0x611b(%rip), %rdi      # 0x9fd8
leaq 0x2265(%rip), %rsi      # 0x6129
callq 0x10b0
movsd 0x21c7(%rip), %xmm0     # 0x6098
movsd %xmm0, -0x40(%rbp)
jmp 0x3ef2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x19e0
jmp 0x4045
movq -0xc0(%rbp), %rdi
movsd -0x40(%rbp), %xmm1
movsd 0x2192(%rip), %xmm0     # 0x6098
divsd %xmm1, %xmm0
movsd %xmm0, -0x100(%rbp)
addq $0xa8, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x1860
movsd -0x100(%rbp), %xmm0
movq -0xc0(%rbp), %rdi
movsd %xmm0, (%rax)
addq $0x90, %rdi
movq %rdi, -0xf8(%rbp)
movslq -0x2c(%rbp), %rsi
callq 0x43f0
movq %rax, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x4160
movq -0xf8(%rbp), %rdi
movslq -0x2c(%rbp), %rsi
callq 0x43f0
movq %rax, %rsi
leaq -0xb8(%rbp), %rdi
callq 0x4160
jmp 0x3f7c
leaq -0xa0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x2470
movsd %xmm0, -0x108(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x19e0
leaq -0xa0(%rbp), %rdi
callq 0x19e0
movsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4011
jp 0x4011
movq 0x5fff(%rip), %rdi      # 0x9fd8
leaq 0x2166(%rip), %rsi      # 0x6146
callq 0x10b0
movsd 0x20ab(%rip), %xmm0     # 0x6098
movsd %xmm0, -0x88(%rbp)
jmp 0x4011
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x19e0
jmp 0x4045
movq -0xc0(%rbp), %rax
movsd -0x40(%rbp), %xmm0
divsd -0x88(%rbp), %xmm0
movsd %xmm0, 0xc0(%rax)
movl 0xc8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc8(%rax)
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x1140
nop