// File: js850[P]lbfgs_cpp/lbfgs.cpp
// Function: LBFGS_ns::LBFGS::LBFGS(double (*)(double*, double*, unsigned long), double const*, unsigned long, int)
LBFGS::LBFGS(
    double (*func)(double *, double *, size_t), 
    double const * x0, 
    size_t N, 
    int M
    //double tol,
    //double maxstep,
    //double max_f_rise,
    //double H0,
    //int maxiter
    )
  :
    func_f_grad_(func),
    M_(M),
    tol_(1e-4),
    maxstep_(0.2),
    max_f_rise_(1e-4),
    maxiter_(1000),
    iprint_(-1),
    iter_number_(0),
    nfev_(0),
    H0_(0.1),
    k_(0)
{
  // set the precision of the printing
  cout << std::setprecision(12);

  // allocate arrays
  x_ = std::vector<double>(N);
  g_ = std::vector<double>(N);

  y_ = std::vector<vector<double> >(M_, vector<double>(N));
  s_ = std::vector<vector<double> >(M_, vector<double>(N));
  rho_ = std::vector<double>(M_);
  step_ = std::vector<double>(N);

  for (size_t j2 = 0; j2 < N; ++j2){
    x_[j2] = x0[j2];
  }
  compute_func_gradient(x_, f_, g_);
  rms_ = vecnorm(g_) / sqrt(N);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x160(%rbp)
movq -0x10(%rbp), %rax
movq %rax, (%rdi)
movl -0x24(%rbp), %eax
movl %eax, 0x8(%rdi)
movabsq $0x3f1a36e2eb1c432d, %rax # imm = 0x3F1A36E2EB1C432D
movq %rax, 0x10(%rdi)
movabsq $0x3fc999999999999a, %rcx # imm = 0x3FC999999999999A
movq %rcx, 0x18(%rdi)
movq %rax, 0x20(%rdi)
movl $0x3e8, 0x28(%rdi)      # imm = 0x3E8
movl $0xffffffff, 0x2c(%rdi) # imm = 0xFFFFFFFF
movl $0x0, 0x30(%rdi)
movl $0x0, 0x34(%rdi)
addq $0x38, %rdi
movq %rdi, -0x188(%rbp)
callq 0x4250
movq -0x160(%rbp), %rdi
addq $0x58, %rdi
movq %rdi, -0x180(%rbp)
callq 0x4250
movq -0x160(%rbp), %rdi
addq $0x78, %rdi
movq %rdi, -0x178(%rbp)
callq 0x4270
movq -0x160(%rbp), %rdi
addq $0x90, %rdi
movq %rdi, -0x170(%rbp)
callq 0x4270
movq -0x160(%rbp), %rdi
addq $0xa8, %rdi
movq %rdi, -0x168(%rbp)
callq 0x4250
movq -0x160(%rbp), %rdi
movabsq $0x3fb999999999999a, %rax # imm = 0x3FB999999999999A
movq %rax, 0xc0(%rdi)
movl $0x0, 0xc8(%rdi)
addq $0xd0, %rdi
movq %rdi, -0x158(%rbp)
callq 0x4250
movl $0xc, %edi
callq 0x4290
movl %eax, -0x14c(%rbp)
jmp 0x26fc
movl -0x14c(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %esi
movq 0x78c9(%rip), %rdi      # 0x9fd8
callq 0x1100
jmp 0x2716
movq -0x20(%rbp), %rax
movq %rax, -0x198(%rbp)
leaq -0x51(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x17b0
movq -0x198(%rbp), %rsi
movq -0x190(%rbp), %rdx
leaq -0x50(%rbp), %rdi
callq 0x17d0
jmp 0x274a
movq -0x160(%rbp), %rdi
addq $0x38, %rdi
leaq -0x50(%rbp), %rsi
movq %rsi, -0x1b0(%rbp)
callq 0x42b0
movq -0x1b0(%rbp), %rdi
callq 0x19e0
leaq -0x51(%rbp), %rdi
callq 0x1840
movq -0x20(%rbp), %rax
movq %rax, -0x1a8(%rbp)
leaq -0x71(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x17b0
movq -0x1a8(%rbp), %rsi
movq -0x1a0(%rbp), %rdx
leaq -0x70(%rbp), %rdi
callq 0x17d0
jmp 0x27ae
movq -0x160(%rbp), %rdi
addq $0x58, %rdi
leaq -0x70(%rbp), %rsi
movq %rsi, -0x1d0(%rbp)
callq 0x42b0
movq -0x1d0(%rbp), %rdi
callq 0x19e0
leaq -0x71(%rbp), %rdi
callq 0x1840
movq -0x160(%rbp), %rax
movslq 0x8(%rax), %rax
movq %rax, -0x1c8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x1c0(%rbp)
leaq -0xa9(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0x17b0
movq -0x1c0(%rbp), %rsi
movq -0x1b8(%rbp), %rdx
leaq -0xa8(%rbp), %rdi
callq 0x17d0
jmp 0x282a
leaq -0xaa(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x42e0
movq -0x1c8(%rbp), %rsi
movq -0x1d8(%rbp), %rcx
leaq -0x90(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0x4300
jmp 0x2860
movq -0x160(%rbp), %rdi
addq $0x90, %rdi
leaq -0x90(%rbp), %rsi
movq %rsi, -0x1f8(%rbp)
callq 0x4380
movq -0x1f8(%rbp), %rdi
callq 0x1a40
leaq -0xaa(%rbp), %rdi
callq 0x1c90
leaq -0xa8(%rbp), %rdi
callq 0x19e0
leaq -0xa9(%rbp), %rdi
callq 0x1840
movq -0x160(%rbp), %rax
movslq 0x8(%rax), %rax
movq %rax, -0x1f0(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x1e8(%rbp)
leaq -0xe1(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x17b0
movq -0x1e8(%rbp), %rsi
movq -0x1e0(%rbp), %rdx
leaq -0xe0(%rbp), %rdi
callq 0x17d0
jmp 0x28fd
leaq -0xe2(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x42e0
movq -0x1f0(%rbp), %rsi
movq -0x200(%rbp), %rcx
leaq -0xc8(%rbp), %rdi
leaq -0xe0(%rbp), %rdx
callq 0x4300
jmp 0x2933
movq -0x160(%rbp), %rdi
addq $0x78, %rdi
leaq -0xc8(%rbp), %rsi
movq %rsi, -0x218(%rbp)
callq 0x4380
movq -0x218(%rbp), %rdi
callq 0x1a40
leaq -0xe2(%rbp), %rdi
callq 0x1c90
leaq -0xe0(%rbp), %rdi
callq 0x19e0
leaq -0xe1(%rbp), %rdi
callq 0x1840
movq -0x160(%rbp), %rax
movslq 0x8(%rax), %rax
movq %rax, -0x210(%rbp)
leaq -0x101(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0x17b0
movq -0x210(%rbp), %rsi
movq -0x208(%rbp), %rdx
leaq -0x100(%rbp), %rdi
callq 0x17d0
jmp 0x29c2
movq -0x160(%rbp), %rdi
addq $0xa8, %rdi
leaq -0x100(%rbp), %rsi
movq %rsi, -0x230(%rbp)
callq 0x42b0
movq -0x230(%rbp), %rdi
callq 0x19e0
leaq -0x101(%rbp), %rdi
callq 0x1840
movq -0x20(%rbp), %rax
movq %rax, -0x228(%rbp)
leaq -0x121(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0x17b0
movq -0x228(%rbp), %rsi
movq -0x220(%rbp), %rdx
leaq -0x120(%rbp), %rdi
callq 0x17d0
jmp 0x2a35
movq -0x160(%rbp), %rdi
addq $0xd0, %rdi
leaq -0x120(%rbp), %rsi
callq 0x42b0
leaq -0x120(%rbp), %rdi
callq 0x19e0
leaq -0x121(%rbp), %rdi
callq 0x1840
movq $0x0, -0x130(%rbp)
movq -0x130(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x2bd7
movq -0x160(%rbp), %rdi
movq -0x18(%rbp), %rax
movq -0x130(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x238(%rbp)
addq $0x38, %rdi
movq -0x130(%rbp), %rsi
callq 0x1860
movsd -0x238(%rbp), %xmm0
movsd %xmm0, (%rax)
movq -0x130(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x130(%rbp)
jmp 0x2a72
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x2c89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x51(%rbp), %rdi
callq 0x1840
jmp 0x2c89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x71(%rbp), %rdi
callq 0x1840
jmp 0x2c89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x2b49
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xaa(%rbp), %rdi
callq 0x1c90
leaq -0xa8(%rbp), %rdi
callq 0x19e0
leaq -0xa9(%rbp), %rdi
callq 0x1840
jmp 0x2c89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x2b8c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xe2(%rbp), %rdi
callq 0x1c90
leaq -0xe0(%rbp), %rdi
callq 0x19e0
leaq -0xe1(%rbp), %rdi
callq 0x1840
jmp 0x2c89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x101(%rbp), %rdi
callq 0x1840
jmp 0x2c89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x121(%rbp), %rdi
callq 0x1840
jmp 0x2c89
movq -0x160(%rbp), %rdi
movq %rdi, %rsi
addq $0x38, %rsi
movq %rdi, %rdx
addq $0x50, %rdx
movq %rdi, %rcx
addq $0x58, %rcx
callq 0x2ce0
jmp 0x2bfa
movq -0x160(%rbp), %rsi
addq $0x58, %rsi
leaq -0x148(%rbp), %rdi
callq 0x4160
jmp 0x2c13
leaq -0x148(%rbp), %rdi
callq 0x2550
movsd %xmm0, -0x240(%rbp)
jmp 0x2c29
movq -0x20(%rbp), %rdi
callq 0x43b0
movsd %xmm0, -0x248(%rbp)
jmp 0x2c3c
movq -0x160(%rbp), %rax
movsd -0x248(%rbp), %xmm1
movsd -0x240(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rax)
leaq -0x148(%rbp), %rdi
callq 0x19e0
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x19e0
movq -0x158(%rbp), %rdi
callq 0x19e0
movq -0x168(%rbp), %rdi
callq 0x19e0
movq -0x170(%rbp), %rdi
callq 0x1a40
movq -0x178(%rbp), %rdi
callq 0x1a40
movq -0x180(%rbp), %rdi
callq 0x19e0
movq -0x188(%rbp), %rdi
callq 0x19e0
movq -0x30(%rbp), %rdi
callq 0x1140
nopw (%rax,%rax)