// File: js850[P]lbfgs_cpp/lbfgs.cpp
// Function: LBFGS_ns::LBFGS::backtracking_linesearch()
double LBFGS::backtracking_linesearch()
{
  vector<double> xnew(x_.size());
  vector<double> gnew(x_.size());
  double fnew;

  // if the step is pointing uphill, invert it
  if (vecdot(step_, g_) > 0.){
    cout << "warning: step direction was uphill.  inverting\n";
    for (size_t j2 = 0; j2 < step_.size(); ++j2){
      step_[j2] *= -1;
    }
  }

  double factor = 1.;
  double stepsize = vecnorm(step_);

  // make sure the step is no larger than maxstep_
  if (factor * stepsize > maxstep_){
    factor = maxstep_ / stepsize;
  }

  int nred;
  int nred_max = 10;
  for (nred = 0; nred < nred_max; ++nred){
    for (size_t j2 = 0; j2 < xnew.size(); ++j2){
      xnew[j2] = x_[j2] + factor * step_[j2];
    }
    compute_func_gradient(xnew, fnew, gnew);

    double df = fnew - f_;
    if (df < max_f_rise_){
      break;
    } else {
      factor /= 10.;
      cout 
        << "function increased: " << df 
        << " reducing step size to " << factor * stepsize 
        << " H0 " << H0_ << "\n";
    }
  }

  if (nred >= nred_max){
    // possibly raise an error here
    cout << "warning: the line search backtracked too many times\n";
  }

  x_ = xnew;
  g_ = gnew;
  f_ = fnew;
  rms_ = vecnorm(gnew) / sqrt(gnew.size());
  return stepsize * factor;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x108(%rbp)
addq $0x38, %rdi
callq 0x4120
movq %rax, -0x100(%rbp)
leaq -0x21(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x17b0
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rdx
leaq -0x20(%rbp), %rdi
callq 0x17d0
jmp 0x36d3
leaq -0x21(%rbp), %rdi
callq 0x1840
movq -0x108(%rbp), %rdi
addq $0x38, %rdi
callq 0x4120
movq %rax, -0x118(%rbp)
leaq -0x51(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x17b0
movq -0x118(%rbp), %rsi
movq -0x110(%rbp), %rdx
leaq -0x50(%rbp), %rdi
callq 0x17d0
jmp 0x371c
leaq -0x51(%rbp), %rdi
callq 0x1840
movq -0x108(%rbp), %rsi
addq $0xd0, %rsi
leaq -0x78(%rbp), %rdi
callq 0x4160
jmp 0x373e
movq -0x108(%rbp), %rsi
addq $0x58, %rsi
leaq -0x90(%rbp), %rdi
callq 0x4160
jmp 0x3757
leaq -0x78(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x2470
movsd %xmm0, -0x120(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x19e0
leaq -0x78(%rbp), %rdi
callq 0x19e0
movsd -0x120(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x388c
movq 0x6838(%rip), %rdi      # 0x9fd8
leaq 0x29bc(%rip), %rsi      # 0x6163
callq 0x10b0
jmp 0x37ae
movq $0x0, -0x98(%rbp)
movq -0x108(%rbp), %rdi
movq -0x98(%rbp), %rax
movq %rax, -0x128(%rbp)
addq $0xd0, %rdi
callq 0x4120
movq %rax, %rcx
movq -0x128(%rbp), %rax
cmpq %rcx, %rax
jae 0x388a
movq -0x108(%rbp), %rdi
addq $0xd0, %rdi
movq -0x98(%rbp), %rsi
callq 0x1860
movsd 0x2881(%rip), %xmm0     # 0x6090
mulsd (%rax), %xmm0
movsd %xmm0, (%rax)
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0x37b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x21(%rbp), %rdi
callq 0x1840
jmp 0x3ccb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x51(%rbp), %rdi
callq 0x1840
jmp 0x3cc2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x3cb9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x19e0
jmp 0x3cb9
jmp 0x388c
movq -0x108(%rbp), %rsi
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0xa0(%rbp)
addq $0xd0, %rsi
leaq -0xc0(%rbp), %rdi
callq 0x4160
jmp 0x38b9
leaq -0xc0(%rbp), %rdi
callq 0x2550
movsd %xmm0, -0x130(%rbp)
jmp 0x38cf
leaq -0xc0(%rbp), %rdi
callq 0x19e0
movsd -0x130(%rbp), %xmm0
movq -0x108(%rbp), %rax
movsd %xmm0, -0xa8(%rbp)
movsd -0xa0(%rbp), %xmm0
mulsd -0xa8(%rbp), %xmm0
ucomisd 0x18(%rax), %xmm0
jbe 0x3944
movq -0x108(%rbp), %rax
movsd 0x18(%rax), %xmm0
divsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
jmp 0x3944
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x19e0
jmp 0x3cb9
movl $0xa, -0xc8(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpl -0xc8(%rbp), %eax
jge 0x3b95
movq $0x0, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x4120
movq %rax, %rcx
movq -0x138(%rbp), %rax
cmpq %rcx, %rax
jae 0x3a43
movq -0x108(%rbp), %rdi
addq $0x38, %rdi
movq -0xd0(%rbp), %rsi
callq 0x1860
movq -0x108(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x148(%rbp)
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0x150(%rbp)
addq $0xd0, %rdi
movq -0xd0(%rbp), %rsi
callq 0x1860
movsd -0x150(%rbp), %xmm0
movsd -0x148(%rbp), %xmm1
movsd (%rax), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x140(%rbp)
movq -0xd0(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0x1860
movsd -0x140(%rbp), %xmm0
movsd %xmm0, (%rax)
movq -0xd0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x3975
movq -0x108(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x60(%rbp), %rdx
leaq -0x50(%rbp), %rcx
callq 0x2ce0
jmp 0x3a5d
movq -0x108(%rbp), %rax
movsd -0x60(%rbp), %xmm0
subsd 0x50(%rax), %xmm0
movsd %xmm0, -0xd8(%rbp)
movsd -0xd8(%rbp), %xmm1
movsd 0x20(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3a8e
jmp 0x3b95
movsd -0xa0(%rbp), %xmm0
movsd 0x2602(%rip), %xmm1     # 0x60a0
divsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
movq 0x6527(%rip), %rdi      # 0x9fd8
leaq 0x26db(%rip), %rsi      # 0x6193
callq 0x10b0
movq %rax, -0x158(%rbp)
jmp 0x3ac6
movq -0x158(%rbp), %rdi
movsd -0xd8(%rbp), %xmm0
callq 0x1160
movq %rax, -0x160(%rbp)
jmp 0x3ae3
movq -0x160(%rbp), %rdi
leaq 0x26b7(%rip), %rsi      # 0x61a8
callq 0x10b0
movq %rax, -0x168(%rbp)
jmp 0x3aff
movq -0x168(%rbp), %rdi
movsd -0xa0(%rbp), %xmm0
movsd -0xa8(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x1160
movq %rax, -0x170(%rbp)
jmp 0x3b28
movq -0x170(%rbp), %rdi
leaq 0x268a(%rip), %rsi      # 0x61c0
callq 0x10b0
movq %rax, -0x178(%rbp)
jmp 0x3b44
movq -0x178(%rbp), %rdi
movq -0x108(%rbp), %rax
movsd 0xc0(%rax), %xmm0
callq 0x1160
movq %rax, -0x180(%rbp)
jmp 0x3b68
movq -0x180(%rbp), %rdi
leaq 0x25ce(%rip), %rsi      # 0x6144
callq 0x10b0
jmp 0x3b7d
jmp 0x3b7f
jmp 0x3b81
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x3958
movl -0xc4(%rbp), %eax
cmpl -0xc8(%rbp), %eax
jl 0x3bba
movq 0x642e(%rip), %rdi      # 0x9fd8
leaq 0x2614(%rip), %rsi      # 0x61c5
callq 0x10b0
jmp 0x3bb8
jmp 0x3bba
movq -0x108(%rbp), %rdi
addq $0x38, %rdi
leaq -0x20(%rbp), %rsi
callq 0x4410
jmp 0x3bd0
movq -0x108(%rbp), %rdi
addq $0x58, %rdi
leaq -0x50(%rbp), %rsi
callq 0x4410
jmp 0x3be6
movq -0x108(%rbp), %rax
movsd -0x60(%rbp), %xmm0
movsd %xmm0, 0x50(%rax)
leaq -0xf0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4160
jmp 0x3c09
leaq -0xf0(%rbp), %rdi
callq 0x2550
movsd %xmm0, -0x188(%rbp)
jmp 0x3c1f
leaq -0x50(%rbp), %rdi
callq 0x4120
movq %rax, %rdi
callq 0x43b0
movsd %xmm0, -0x190(%rbp)
jmp 0x3c3a
movq -0x108(%rbp), %rax
movsd -0x190(%rbp), %xmm1
movsd -0x188(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rax)
leaq -0xf0(%rbp), %rdi
callq 0x19e0
movsd -0xa8(%rbp), %xmm0
mulsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0x198(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x19e0
leaq -0x20(%rbp), %rdi
callq 0x19e0
movsd -0x198(%rbp), %xmm0
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x19e0
leaq -0x50(%rbp), %rdi
callq 0x19e0
leaq -0x20(%rbp), %rdi
callq 0x19e0
movq -0x30(%rbp), %rdi
callq 0x1140
nopw %cs:(%rax,%rax)