// File: /workspace/github_c_makefile_star2_O3/PoulinV[P]AxiCLASS/build/../tools/evolver_ndf15.c
// Function: fzero_Newton
int fzero_Newton(int (*func)(double *x,
                             int x_size,
                             void *param,
                             double *F,
                             ErrorMsg error_message),
                 double *x_inout,
                 double *dxdF,
                 int x_size,
                 double tolx,
                 double tolF,
                 void *param,
                 int *fevals,
                 ErrorMsg error_message){
  /**Given an initial guess x[1..n] for a root in n dimensions,
     take ntrial Newton-Raphson steps to improve the root.
     Stop if the root converges in either summed absolute
     variable increments tolx or summed absolute function values tolf.*/
  int k,i,j,*indx, ntrial=20;
  double errx,errf,d,*F0,*Fdel,**Fjac,*p, *lu_work;
  int has_converged = _FALSE_;
  int funcreturn;
  double toljac = 1e-3;
  double *delx;

  /** All arrays are indexed as [0, n-1] with the exception of p, indx,
      lu_work and Fjac, since they are passed to ludcmp and lubksb. */
  class_alloc(indx, sizeof(int)*(x_size+1), error_message);
  class_alloc(p, sizeof(double)*(x_size+1), error_message);
  class_alloc(lu_work, sizeof(double)*(x_size+1), error_message);
  class_alloc(Fjac, sizeof(double *)*(x_size+1), error_message);
  Fjac[0] = NULL;
  class_alloc(Fjac[1], sizeof(double)*(x_size*x_size+1), error_message);
  for (i=2; i<=x_size; i++){
    Fjac[i] = Fjac[i-1] + x_size;
  }

  class_alloc(F0, sizeof(double)*x_size, error_message);
  class_alloc(delx, sizeof(double)*x_size, error_message);
  class_alloc(Fdel, sizeof(double)*x_size, error_message);

  for (i=1; i<=x_size; i++){
    delx[i-1] = toljac*dxdF[i-1];
  }

  for (k=1;k<=ntrial;k++) {
    /** Compute F(x): */
    /**printf("x = [%f, %f], delx = [%e, %e]\n",
       x_inout[0],x_inout[1],delx[0],delx[1]);*/
    class_call(func(x_inout, x_size, param, F0, error_message),
               error_message, error_message);
    /**    printf("F0 = [%f, %f]\n",F0[0],F0[1]);*/
    *fevals = *fevals + 1;
    errf=0.0; //fvec and Jacobian matrix in fjac.
    for (i=1; i<=x_size; i++)
      errf += fabs(F0[i-1]); //Check function convergence.
    if (errf <= tolF){
      has_converged = _TRUE_;
      break;
    }

    /**
    if (k==1){
      for (i=1; i<=x_size; i++){
        delx[i-1] *= F0[i-1];
      }
    }
    */

    /** Compute the jacobian of F: */
    for (i=1; i<=x_size; i++){
      if (F0[i-1]<0.0)
        delx[i-1] *= -1;
      x_inout[i-1] += delx[i-1];

      /**      printf("x = [%f, %f], delx = [%e, %e]\n",
               x_inout[0],x_inout[1],delx[0],delx[1]);*/
      class_call(func(x_inout, x_size, param, Fdel, error_message),
                 error_message, error_message);
      /**      printf("F = [%f, %f]\n",Fdel[0],Fdel[1]);*/
      for (j=1; j<=x_size; j++)
        Fjac[j][i] = (Fdel[j-1]-F0[j-1])/delx[i-1];
      x_inout[i-1] -= delx[i-1];
    }
    *fevals = *fevals + x_size;

    for (i=1; i<=x_size; i++)
      p[i] = -F0[i-1]; //Right-hand side of linear equations.
    funcreturn = ludcmp(Fjac, x_size, indx, &d, lu_work); //Solve linear equations using LU decomposition.
    class_test(funcreturn == _FAILURE_,error_message,
               "Failure in ludcmp. Possibly singular matrix!");
    funcreturn = lubksb(Fjac, x_size, indx, p);
    class_test(funcreturn == _FAILURE_,error_message,
               "Failure in lubksb. Possibly singular matrix!");
    errx=0.0; //Check root convergence.
    for (i=1; i<=x_size; i++) { //Update solution.
      errx += fabs(p[i]);
      x_inout[i-1] += p[i];
    }
    if (errx <= tolx){
      has_converged = _TRUE_;
      break;
    }
  }

  free(p);
  free(lu_work);
  free(indx);
  free(Fjac[1]);
  free(Fjac);
  free(F0);
  free(delx);
  free(Fdel);

  if (has_converged == _TRUE_){
    return _SUCCESS_;
  }
  else{
    class_stop(error_message, "Newton's method failed to converge. Try improving initial guess on the parameters, decrease the tolerance requirements to Newtons method or increase the precision of the input function.\n");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1088, %rsp           # imm = 0x1088
movq %r9, 0x20(%rsp)
movq %r8, 0x48(%rsp)
movsd %xmm1, 0x38(%rsp)
movsd %xmm0, 0x70(%rsp)
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, 0x40(%rsp)
movq 0x10c0(%rsp), %r15
movq %rcx, 0x28(%rsp)
leal 0x1(%rcx), %r13d
movslq %r13d, %rbx
leaq (,%rbx,4), %r14
movq %r14, %rdi
callq 0x8460
testq %rax, %rax
je 0x125a5
movq %rax, 0x10(%rsp)
shlq $0x3, %rbx
movq %rbx, %rdi
callq 0x8460
testq %rax, %rax
je 0x125e6
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
callq 0x8460
testq %rax, %rax
je 0x12623
movq %rax, %r14
movq %rbx, %rdi
callq 0x8460
testq %rax, %rax
je 0x1265d
movq %rax, %r15
movq %r14, 0x18(%rsp)
movq $0x0, (%rax)
movq 0x28(%rsp), %r14
movl %r14d, %eax
imull %eax, %eax
leaq 0x8(,%rax,8), %rbx
movq %rbx, %rdi
callq 0x8460
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x1269c
cmpl $0x2, %r14d
jl 0x12250
movl %r14d, %eax
movl %r13d, %ecx
shlq $0x3, %rax
movq 0x8(%r15), %rdx
addq %rax, %rdx
movl $0x2, %esi
movq %rdx, (%r15,%rsi,8)
incq %rsi
addq %rax, %rdx
cmpq %rsi, %rcx
jne 0x12241
movslq %r14d, %r14
shlq $0x3, %r14
movq %r14, %rdi
callq 0x8460
testq %rax, %rax
je 0x126fa
movq %rax, %rbx
movq %r14, %rdi
callq 0x8460
testq %rax, %rax
je 0x1273d
movq %rax, (%rsp)
movq %r14, 0x50(%rsp)
movq %r14, %rdi
callq 0x8460
testq %rax, %rax
je 0x12780
movq %rax, %r14
movq 0x28(%rsp), %rsi
testl %esi, %esi
movq (%rsp), %rdx
jle 0x122cd
movl %r13d, %eax
decq %rax
xorl %ecx, %ecx
movsd 0xdca92(%rip), %xmm0    # 0xeed48
movsd (%r12,%rcx,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x122b6
movl %r13d, %eax
movq %rax, 0x68(%rsp)
leaq -0x1(%rax), %r12
movl $0x1, %eax
movl %eax, 0x34(%rsp)
movq %rbp, %rdi
movq 0x48(%rsp), %rdx
movq %rbx, %rcx
movq %rsi, %r13
movq 0x10c0(%rsp), %r8
callq *0x40(%rsp)
cmpl $0x1, %eax
je 0x1285c
movq 0x20(%rsp), %rcx
movl (%rcx), %eax
incl %eax
movl %eax, (%rcx)
testl %r13d, %r13d
movq %r13, %rsi
jle 0x12446
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movq (%rsp), %rdx
movapd 0xdbf12(%rip), %xmm2    # 0xee240
movsd (%rbx,%rcx,8), %xmm1
andpd %xmm2, %xmm1
addsd %xmm1, %xmm0
incq %rcx
cmpq %rcx, %r12
jne 0x1232e
movsd 0x38(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x1280b
testl %esi, %esi
jle 0x1245e
movl $0x1, %r13d
movapd 0xdbec7(%rip), %xmm1    # 0xee230
xorpd %xmm0, %xmm0
ucomisd -0x8(%rbx,%r13,8), %xmm0
movsd -0x8(%rdx,%r13,8), %xmm2
jbe 0x12388
xorpd %xmm1, %xmm2
movlpd %xmm2, -0x8(%rdx,%r13,8)
movsd -0x8(%rbp,%r13,8), %xmm0
movapd %xmm2, 0x50(%rsp)
addsd %xmm2, %xmm0
movsd %xmm0, -0x8(%rbp,%r13,8)
movq %rbp, %rdi
movq 0x48(%rsp), %rdx
movq %r14, %rcx
movq 0x10c0(%rsp), %r8
callq *0x40(%rsp)
cmpl $0x1, %eax
je 0x127c5
xorl %eax, %eax
movapd 0x50(%rsp), %xmm1
movsd (%r14,%rax,8), %xmm0
subsd (%rbx,%rax,8), %xmm0
divsd %xmm1, %xmm0
movq 0x8(%r15,%rax,8), %rcx
movsd %xmm0, (%rcx,%r13,8)
incq %rax
cmpq %rax, %r12
jne 0x123c8
movsd -0x8(%rbp,%r13,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp,%r13,8)
incq %r13
cmpq 0x68(%rsp), %r13
movq 0x28(%rsp), %rsi
movq (%rsp), %rdx
movapd 0xdbe1b(%rip), %xmm1    # 0xee230
jne 0x12369
movq 0x20(%rsp), %rax
addl %esi, (%rax)
testl %esi, %esi
movq 0x8(%rsp), %rcx
jle 0x12467
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm0
xorpd %xmm1, %xmm0
movlpd %xmm0, 0x8(%rcx,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x1242d
jmp 0x12467
movsd 0x38(%rsp), %xmm0
ucomisd 0xdbe04(%rip), %xmm0    # 0xee258
movq (%rsp), %rdx
jae 0x1280b
addl %esi, %eax
movq 0x20(%rsp), %rcx
movl %eax, (%rcx)
movq %r15, %rdi
movq 0x10(%rsp), %rdx
leaq 0x78(%rsp), %rcx
movq %rsi, %r13
movq 0x18(%rsp), %r8
callq 0x11eef
testl %eax, %eax
jne 0x128a2
movq %r15, %rdi
movl %r13d, %esi
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
callq 0x1179a
testl %r13d, %r13d
movq %r13, %rsi
xorpd %xmm0, %xmm0
jle 0x124e5
xorl %eax, %eax
movq 0x8(%rsp), %rcx
movq (%rsp), %rdx
movapd 0xdbd83(%rip), %xmm3    # 0xee240
movsd 0x8(%rcx,%rax,8), %xmm1
movapd %xmm1, %xmm2
andpd %xmm3, %xmm2
addsd (%rbp,%rax,8), %xmm1
addsd %xmm2, %xmm0
movsd %xmm1, (%rbp,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x124bd
jmp 0x124e9
movq (%rsp), %rdx
movsd 0x70(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movl 0x34(%rsp), %eax
jae 0x1280b
incl %eax
cmpl $0x15, %eax
jne 0x122de
movq 0x8(%rsp), %rdi
callq 0x8410
movq 0x18(%rsp), %rdi
callq 0x8410
movq 0x10(%rsp), %rdi
callq 0x8410
movq 0x8(%r15), %rdi
callq 0x8410
movq %r15, %rdi
callq 0x8410
movq %rbx, %rdi
callq 0x8410
movq (%rsp), %rdi
callq 0x8410
movq %r14, %rdi
callq 0x8410
leaq 0xdcd34(%rip), %rsi     # 0xef28b
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x30af0
leaq 0xdc015(%rip), %rsi     # 0xee585
leaq 0x880(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x30af0
leaq 0xdbaa8(%rip), %rsi     # 0xee034
leaq 0xdcc6c(%rip), %rdx     # 0xef1ff
movq 0x10c0(%rsp), %rdi
movl $0x4a3, %ecx            # imm = 0x4A3
jmp 0x126d9
leaq 0xdba58(%rip), %rsi     # 0xee004
leaq 0xdcc47(%rip), %rdx     # 0xef1fa
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x30af0
leaq 0xdba65(%rip), %rsi     # 0xee034
leaq 0xdcc29(%rip), %rdx     # 0xef1ff
movq %r15, %rdi
movl $0x448, %ecx            # imm = 0x448
movq %rbx, %r8
jmp 0x126dc
leaq 0xdba17(%rip), %rsi     # 0xee004
leaq 0xdd8f6(%rip), %rdx     # 0xefeea
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x30af0
leaq 0xdba25(%rip), %rsi     # 0xee034
leaq 0xdcbe9(%rip), %rdx     # 0xef1ff
movq %r15, %rdi
movl $0x449, %ecx            # imm = 0x449
jmp 0x126d9
leaq 0xdb9da(%rip), %rsi     # 0xee004
leaq 0xdcbdb(%rip), %rdx     # 0xef20c
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x30af0
leaq 0xdb9e8(%rip), %rsi     # 0xee034
leaq 0xdcbac(%rip), %rdx     # 0xef1ff
movq %r15, %rdi
movl $0x44a, %ecx            # imm = 0x44A
jmp 0x126d9
leaq 0xdb9a0(%rip), %rsi     # 0xee004
leaq 0xdcba9(%rip), %rdx     # 0xef214
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x30af0
leaq 0xdb9ae(%rip), %rsi     # 0xee034
leaq 0xdcb72(%rip), %rdx     # 0xef1ff
movq 0x10c0(%rsp), %rdi
movl $0x44b, %ecx            # imm = 0x44B
jmp 0x126d9
leaq 0xdb961(%rip), %rsi     # 0xee004
leaq 0xdcb6f(%rip), %rdx     # 0xef219
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x30af0
leaq 0xdb96f(%rip), %rsi     # 0xee034
leaq 0xdcb33(%rip), %rdx     # 0xef1ff
movq 0x10c0(%rsp), %rdi
movl $0x44d, %ecx            # imm = 0x44D
movq %r14, %r8
xorl %eax, %eax
callq 0x30af0
movl $0x1, %eax
addq $0x1088, %rsp           # imm = 0x1088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xdb903(%rip), %rsi     # 0xee004
leaq 0xdcb19(%rip), %rdx     # 0xef221
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x30af0
leaq 0xdb910(%rip), %rsi     # 0xee034
leaq 0xdcad4(%rip), %rdx     # 0xef1ff
movq 0x10c0(%rsp), %rdi
movl $0x452, %ecx            # imm = 0x452
jmp 0x125de
leaq 0xdb8c0(%rip), %rsi     # 0xee004
leaq 0xdcad9(%rip), %rdx     # 0xef224
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x30af0
leaq 0xdb8cd(%rip), %rsi     # 0xee034
leaq 0xdca91(%rip), %rdx     # 0xef1ff
movq 0x10c0(%rsp), %rdi
movl $0x453, %ecx            # imm = 0x453
jmp 0x125de
leaq 0xdb87d(%rip), %rsi     # 0xee004
leaq 0xdcddb(%rip), %rdx     # 0xef569
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq 0x50(%rsp), %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0xdb888(%rip), %rsi     # 0xee034
leaq 0xdca4c(%rip), %rdx     # 0xef1ff
movq 0x10c0(%rsp), %rdi
movl $0x454, %ecx            # imm = 0x454
jmp 0x125de
leaq 0xdbc24(%rip), %rsi     # 0xee3f0
leaq 0xdca86(%rip), %rdx     # 0xef259
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq 0x10c0(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0xdb83d(%rip), %rsi     # 0xee034
leaq 0xdca01(%rip), %rdx     # 0xef1ff
movq %r14, %rdi
movl $0x47b, %ecx            # imm = 0x47B
jmp 0x125de
movq %rdx, %r12
movq 0x8(%rsp), %rdi
callq 0x8410
movq 0x18(%rsp), %rdi
callq 0x8410
movq 0x10(%rsp), %rdi
callq 0x8410
movq 0x8(%r15), %rdi
callq 0x8410
movq %r15, %rdi
callq 0x8410
movq %rbx, %rdi
callq 0x8410
movq %r12, %rdi
callq 0x8410
movq %r14, %rdi
callq 0x8410
xorl %eax, %eax
jmp 0x126e8
leaq 0xdbb8d(%rip), %rsi     # 0xee3f0
leaq 0xdc9bf(%rip), %rdx     # 0xef229
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq 0x10c0(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0xdb7a6(%rip), %rsi     # 0xee034
leaq 0xdc96a(%rip), %rdx     # 0xef1ff
movq %r14, %rdi
movl $0x45f, %ecx            # imm = 0x45F
jmp 0x125de
leaq 0xdc924(%rip), %rsi     # 0xef1cd
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x30af0
leaq 0xdb7c0(%rip), %rsi     # 0xee082
leaq 0xdc6ae(%rip), %rdx     # 0xeef77
leaq 0x880(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0xdb74f(%rip), %rsi     # 0xee034
leaq 0xdc913(%rip), %rdx     # 0xef1ff
movq 0x10c0(%rsp), %rdi
movl $0x487, %ecx            # imm = 0x487
jmp 0x126d9
nop