// File: /workspace/github_c_makefile_star2_O1/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 $0x1078, %rsp           # imm = 0x1078
movq %r9, %r14
movq %r8, 0x48(%rsp)
movsd %xmm1, 0x60(%rsp)
movsd %xmm0, 0x58(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %rbp
movq %rdi, %r15
movq 0x10b0(%rsp), %r13
movq %rcx, 0x28(%rsp)
movslq %ecx, %r12
leaq 0x4(,%r12,4), %rbx
movq %rbx, %rdi
callq 0x8460
testq %rax, %rax
je 0x1244e
movq %r14, 0x38(%rsp)
movq %rax, 0x10(%rsp)
leaq 0x1(%r12), %r14
shlq $0x3, %r14
movq %r14, %rdi
callq 0x8460
testq %rax, %rax
je 0x1248b
movq %rax, (%rsp)
movq %r14, %rdi
callq 0x8460
testq %rax, %rax
je 0x124cb
movq %rax, 0x18(%rsp)
movq %r14, %rdi
callq 0x8460
testq %rax, %rax
je 0x1250b
movq %rax, %rbx
movq %r15, 0x40(%rsp)
movq $0x0, (%rax)
movq 0x28(%rsp), %r15
movl %r15d, %eax
imull %eax, %eax
leaq 0x8(,%rax,8), %r14
movq %r14, %rdi
callq 0x8460
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x1254b
cmpl $0x2, %r15d
jl 0x12120
movl %r15d, %eax
leal 0x1(%r15), %ecx
shlq $0x3, %rax
movq 0x8(%rbx), %rdx
addq %rax, %rdx
movl $0x2, %esi
movq %rdx, (%rbx,%rsi,8)
incq %rsi
addq %rax, %rdx
cmpq %rsi, %rcx
jne 0x12111
shlq $0x3, %r12
movq %r12, %rdi
callq 0x8460
testq %rax, %rax
je 0x1258b
movq %rax, %r15
movq %r12, %rdi
callq 0x8460
testq %rax, %rax
je 0x125cb
movq %rax, 0x8(%rsp)
movq %r12, %rdi
callq 0x8460
testq %rax, %rax
je 0x12648
movq %rax, %r14
movq 0x28(%rsp), %r12
testl %r12d, %r12d
movq 0x8(%rsp), %rdx
movq 0x20(%rsp), %rdi
jle 0x12199
movl %r12d, %eax
xorl %ecx, %ecx
movsd 0xdfbc5(%rip), %xmm0    # 0xf1d48
movsd (%rdi,%rcx,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x12183
leal 0x1(%r12), %eax
movq %rax, 0x50(%rsp)
leaq -0x1(%rax), %r13
movl $0x1, %eax
movl %eax, 0x34(%rsp)
movq %rbp, %rdi
movl %r12d, %esi
movq 0x48(%rsp), %rdx
movq %r15, %rcx
movq 0x10b0(%rsp), %r8
callq *0x40(%rsp)
cmpl $0x1, %eax
je 0x126ec
movq 0x38(%rsp), %rax
incl (%rax)
testl %r12d, %r12d
movq %r12, %rsi
xorpd %xmm0, %xmm0
jle 0x1220c
xorl %eax, %eax
movq (%rsp), %rdi
movapd 0xdf04c(%rip), %xmm2    # 0xf1240
movsd (%r15,%rax,8), %xmm1
andpd %xmm2, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq %rax, %r13
jne 0x121f4
jmp 0x12210
movq (%rsp), %rdi
movsd 0x60(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x126a4
testl %esi, %esi
movq 0x8(%rsp), %rax
movapd 0xdf001(%rip), %xmm1    # 0xf1230
jle 0x122f6
movl $0x1, %r12d
xorpd %xmm0, %xmm0
ucomisd -0x8(%r15,%r12,8), %xmm0
jbe 0x1225a
movsd -0x8(%rax,%r12,8), %xmm0
xorpd %xmm1, %xmm0
movlpd %xmm0, -0x8(%rax,%r12,8)
movsd -0x8(%rax,%r12,8), %xmm1
movsd -0x8(%rbp,%r12,8), %xmm0
movsd %xmm1, 0x20(%rsp)
addsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp,%r12,8)
movq %rbp, %rdi
movq 0x48(%rsp), %rdx
movq %r14, %rcx
movq 0x10b0(%rsp), %r8
callq *0x40(%rsp)
cmpl $0x1, %eax
je 0x12608
xorl %eax, %eax
movsd 0x20(%rsp), %xmm1
movsd (%r14,%rax,8), %xmm0
subsd (%r15,%rax,8), %xmm0
divsd %xmm1, %xmm0
movq 0x8(%rbx,%rax,8), %rcx
movsd %xmm0, (%rcx,%r12,8)
incq %rax
cmpq %rax, %r13
jne 0x122a1
movsd -0x8(%rbp,%r12,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp,%r12,8)
incq %r12
cmpq 0x50(%rsp), %r12
movq 0x28(%rsp), %rsi
movq 0x8(%rsp), %rax
movapd 0xdef40(%rip), %xmm1    # 0xf1230
jne 0x1223b
movq 0x38(%rsp), %rax
addl %esi, (%rax)
testl %esi, %esi
movq (%rsp), %rcx
jle 0x1231f
xorl %eax, %eax
movsd (%r15,%rax,8), %xmm0
xorpd %xmm1, %xmm0
movlpd %xmm0, 0x8(%rcx,%rax,8)
incq %rax
cmpq %rax, %r13
jne 0x12307
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
leaq 0x68(%rsp), %rcx
movq %rsi, %r12
movq 0x18(%rsp), %r8
callq 0x11dba
testl %eax, %eax
jne 0x1272f
movq %rbx, %rdi
movl %r12d, %esi
movq 0x10(%rsp), %rdx
movq (%rsp), %rcx
callq 0x1164d
xorpd %xmm0, %xmm0
testl %r12d, %r12d
jle 0x12394
xorl %eax, %eax
movq (%rsp), %rdi
movapd 0xdeed4(%rip), %xmm3    # 0xf1240
movsd 0x8(%rdi,%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, %r13
jne 0x1236c
jmp 0x12398
movq (%rsp), %rdi
movsd 0x58(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movl 0x34(%rsp), %eax
jae 0x126a4
incl %eax
cmpl $0x15, %eax
jne 0x121ac
movq (%rsp), %rdi
callq 0x8410
movq 0x18(%rsp), %rdi
callq 0x8410
movq 0x10(%rsp), %rdi
callq 0x8410
movq 0x8(%rbx), %rdi
callq 0x8410
movq %rbx, %rdi
callq 0x8410
movq %r15, %rdi
callq 0x8410
movq 0x8(%rsp), %rdi
callq 0x8410
movq %r14, %rdi
callq 0x8410
leaq 0xdfe85(%rip), %rsi     # 0xf228b
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x30c2c
leaq 0xdf169(%rip), %rsi     # 0xf1585
leaq 0x870(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x30c2c
leaq 0xdebfc(%rip), %rsi     # 0xf1034
leaq 0xdfdc0(%rip), %rdx     # 0xf21ff
movq 0x10b0(%rsp), %rdi
movl $0x4a3, %ecx            # imm = 0x4A3
jmp 0x12483
leaq 0xdebaf(%rip), %rsi     # 0xf1004
leaq 0xdfd9e(%rip), %rdx     # 0xf21fa
leaq 0x70(%rsp), %r14
movq %r14, %rdi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x30c2c
leaq 0xdebc0(%rip), %rsi     # 0xf1034
leaq 0xdfd84(%rip), %rdx     # 0xf21ff
movq %r13, %rdi
movl $0x448, %ecx            # imm = 0x448
movq %r14, %r8
jmp 0x12686
leaq 0xdeb72(%rip), %rsi     # 0xf1004
leaq 0xe0a41(%rip), %rdx     # 0xf2eda
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x30c2c
leaq 0xdeb82(%rip), %rsi     # 0xf1034
leaq 0xdfd46(%rip), %rdx     # 0xf21ff
movq 0x10b0(%rsp), %rdi
movl $0x449, %ecx            # imm = 0x449
jmp 0x12683
leaq 0xdeb32(%rip), %rsi     # 0xf1004
leaq 0xdfd33(%rip), %rdx     # 0xf220c
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x30c2c
leaq 0xdeb42(%rip), %rsi     # 0xf1034
leaq 0xdfd06(%rip), %rdx     # 0xf21ff
movq 0x10b0(%rsp), %rdi
movl $0x44a, %ecx            # imm = 0x44A
jmp 0x12683
leaq 0xdeaf2(%rip), %rsi     # 0xf1004
leaq 0xdfcfb(%rip), %rdx     # 0xf2214
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x30c2c
leaq 0xdeb02(%rip), %rsi     # 0xf1034
leaq 0xdfcc6(%rip), %rdx     # 0xf21ff
movq 0x10b0(%rsp), %rdi
movl $0x44b, %ecx            # imm = 0x44B
jmp 0x12683
leaq 0xdeab2(%rip), %rsi     # 0xf1004
leaq 0xdfcc0(%rip), %rdx     # 0xf2219
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x30c2c
leaq 0xdeac2(%rip), %rsi     # 0xf1034
leaq 0xdfc86(%rip), %rdx     # 0xf21ff
movq 0x10b0(%rsp), %rdi
movl $0x44d, %ecx            # imm = 0x44D
jmp 0x12683
leaq 0xdea72(%rip), %rsi     # 0xf1004
leaq 0xdfc88(%rip), %rdx     # 0xf2221
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x30c2c
leaq 0xdea82(%rip), %rsi     # 0xf1034
leaq 0xdfc46(%rip), %rdx     # 0xf21ff
movq 0x10b0(%rsp), %rdi
movl $0x452, %ecx            # imm = 0x452
jmp 0x12683
leaq 0xdea32(%rip), %rsi     # 0xf1004
leaq 0xdfc4b(%rip), %rdx     # 0xf2224
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x30c2c
leaq 0xdea42(%rip), %rsi     # 0xf1034
leaq 0xdfc06(%rip), %rdx     # 0xf21ff
movq 0x10b0(%rsp), %rdi
movl $0x453, %ecx            # imm = 0x453
jmp 0x12683
leaq 0xdede1(%rip), %rsi     # 0xf13f0
leaq 0xdfc43(%rip), %rdx     # 0xf2259
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq 0x10b0(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0xde9fd(%rip), %rsi     # 0xf1034
leaq 0xdfbc1(%rip), %rdx     # 0xf21ff
movq %r14, %rdi
movl $0x47b, %ecx            # imm = 0x47B
jmp 0x12683
leaq 0xde9b5(%rip), %rsi     # 0xf1004
leaq 0xdff13(%rip), %rdx     # 0xf2569
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x30c2c
leaq 0xde9c5(%rip), %rsi     # 0xf1034
leaq 0xdfb89(%rip), %rdx     # 0xf21ff
movq 0x10b0(%rsp), %rdi
movl $0x454, %ecx            # imm = 0x454
movq %rbx, %r8
xorl %eax, %eax
callq 0x30c2c
movl $0x1, %eax
addq $0x1078, %rsp           # imm = 0x1078
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x8410
movq 0x18(%rsp), %rdi
callq 0x8410
movq 0x10(%rsp), %rdi
callq 0x8410
movq 0x8(%rbx), %rdi
callq 0x8410
movq %rbx, %rdi
callq 0x8410
movq %r15, %rdi
callq 0x8410
movq 0x8(%rsp), %rdi
callq 0x8410
movq %r14, %rdi
callq 0x8410
xorl %eax, %eax
jmp 0x12692
leaq 0xdecfd(%rip), %rsi     # 0xf13f0
leaq 0xdfb2f(%rip), %rdx     # 0xf2229
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq 0x10b0(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0xde919(%rip), %rsi     # 0xf1034
leaq 0xdfadd(%rip), %rdx     # 0xf21ff
movq %r14, %rdi
movl $0x45f, %ecx            # imm = 0x45F
jmp 0x12683
leaq 0xdfa97(%rip), %rsi     # 0xf21cd
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x30c2c
leaq 0xde936(%rip), %rsi     # 0xf1082
leaq 0xdf824(%rip), %rdx     # 0xf1f77
leaq 0x870(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0xde8c5(%rip), %rsi     # 0xf1034
leaq 0xdfa89(%rip), %rdx     # 0xf21ff
movq 0x10b0(%rsp), %rdi
movl $0x487, %ecx            # imm = 0x487
jmp 0x12483