// File: /workspace/github_c_makefile_star2_O2/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, %r14
movq %r8, 0x40(%rsp)
movsd %xmm1, 0x58(%rsp)
movsd %xmm0, 0x50(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, 0x38(%rsp)
movq 0x10c0(%rsp), %rbp
movl %ecx, 0x4(%rsp)
movslq %ecx, %r15
leaq 0x4(,%r15,4), %rbx
movq %rbx, %rdi
callq 0x8490
testq %rax, %rax
je 0x10603
movq %r14, 0x30(%rsp)
movq %rax, 0x20(%rsp)
leaq 0x1(%r15), %r14
shlq $0x3, %r14
movq %r14, %rdi
callq 0x8490
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x10643
movq %r14, %rdi
callq 0x8490
testq %rax, %rax
je 0x10681
movq %rax, 0x18(%rsp)
movq %r14, %rdi
callq 0x8490
testq %rax, %rax
je 0x1073f
movq %rax, %rbx
andq $0x0, (%rax)
movl 0x4(%rsp), %eax
imull %eax, %eax
leaq 0x8(,%rax,8), %r14
movq %r14, %rdi
callq 0x8490
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x1077d
pushq $0x2
popq %rax
leaq (,%r15,8), %rbp
cmpq %r15, %rax
jg 0x106bf
movq -0x8(%rbx,%rax,8), %rcx
addq %rbp, %rcx
movq %rcx, (%rbx,%rax,8)
incq %rax
jmp 0x105e9
leaq 0xd49fa(%rip), %rsi     # 0xe5004
leaq 0xd5bc9(%rip), %rdx     # 0xe61da
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2e014
leaq 0xd4a08(%rip), %rsi     # 0xe5034
leaq 0xd5bac(%rip), %rdx     # 0xe61df
movq %rbp, %rdi
movl $0x448, %ecx            # imm = 0x448
movq %r14, %r8
jmp 0x10b03
leaq 0xd49ba(%rip), %rsi     # 0xe5004
leaq 0xd6859(%rip), %rdx     # 0xe6eaa
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2e014
leaq 0xd49c7(%rip), %rsi     # 0xe5034
leaq 0xd5b6b(%rip), %rdx     # 0xe61df
movq %rbp, %rdi
movl $0x449, %ecx            # imm = 0x449
jmp 0x10b00
leaq 0xd497c(%rip), %rsi     # 0xe5004
leaq 0xd5b5d(%rip), %rdx     # 0xe61ec
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2e014
leaq 0xd4989(%rip), %rsi     # 0xe5034
leaq 0xd5b2d(%rip), %rdx     # 0xe61df
movq %rbp, %rdi
movl $0x44a, %ecx            # imm = 0x44A
jmp 0x10b00
movq %rbp, %rdi
callq 0x8490
testq %rax, %rax
je 0x107bb
movq %rax, %r15
movq %rbp, %rdi
callq 0x8490
testq %rax, %rax
je 0x10a3e
movq %rax, 0x8(%rsp)
movq %rbp, %rdi
callq 0x8490
testq %rax, %rax
je 0x10ac3
movq %rax, %r14
xorl %eax, %eax
movl 0x4(%rsp), %esi
testl %esi, %esi
movl $0x0, %ebp
cmovgl %esi, %ebp
leal 0x1(%rbp), %ecx
movq %rcx, 0x48(%rsp)
movsd 0xd560b(%rip), %xmm0    # 0xe5d28
movq 0x8(%rsp), %rcx
cmpq %rax, %rbp
je 0x107fd
movsd (%r12,%rax,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rcx,%rax,8)
incq %rax
jmp 0x10722
leaq 0xd48be(%rip), %rsi     # 0xe5004
leaq 0xd5aa7(%rip), %rdx     # 0xe61f4
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2e014
leaq 0xd48cb(%rip), %rsi     # 0xe5034
leaq 0xd5a6f(%rip), %rdx     # 0xe61df
movq %rbp, %rdi
movl $0x44b, %ecx            # imm = 0x44B
jmp 0x10b00
leaq 0xd4880(%rip), %rsi     # 0xe5004
leaq 0xd5a6e(%rip), %rdx     # 0xe61f9
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2e014
leaq 0xd488d(%rip), %rsi     # 0xe5034
leaq 0xd5a31(%rip), %rdx     # 0xe61df
movq %rbp, %rdi
movl $0x44d, %ecx            # imm = 0x44D
jmp 0x10b00
leaq 0xd4842(%rip), %rsi     # 0xe5004
leaq 0xd5a38(%rip), %rdx     # 0xe6201
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2e014
leaq 0xd4850(%rip), %rsi     # 0xe5034
leaq 0xd59f4(%rip), %rdx     # 0xe61df
movq 0x10c0(%rsp), %rdi
movl $0x452, %ecx            # imm = 0x452
jmp 0x10b00
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
movq 0x28(%rsp), %r12
cmpl $0x15, 0x14(%rsp)
je 0x10b1f
movq %r13, %rdi
movq 0x40(%rsp), %rdx
movq %r15, %rcx
movq 0x10c0(%rsp), %r8
callq *0x38(%rsp)
cmpl $0x1, %eax
je 0x10bbb
movq 0x30(%rsp), %rax
incl (%rax)
xorpd %xmm0, %xmm0
xorl %eax, %eax
movl 0x4(%rsp), %esi
movq 0x8(%rsp), %rdx
movapd 0xd49ee(%rip), %xmm2    # 0xe5240
cmpq %rax, %rbp
je 0x1086a
movsd (%r15,%rax,8), %xmm1
andpd %xmm2, %xmm1
addsd %xmm1, %xmm0
incq %rax
jmp 0x10852
movsd 0x58(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x109ef
pushq $0x1
popq %r12
cmpq 0x48(%rsp), %r12
je 0x10934
xorpd %xmm0, %xmm0
ucomisd -0x8(%r15,%r12,8), %xmm0
movsd -0x8(%rdx,%r12,8), %xmm1
jbe 0x108ac
xorpd 0xd498b(%rip), %xmm1    # 0xe5230
movlpd %xmm1, -0x8(%rdx,%r12,8)
movsd -0x8(%r13,%r12,8), %xmm0
movapd %xmm1, 0x60(%rsp)
addsd %xmm1, %xmm0
movsd %xmm0, -0x8(%r13,%r12,8)
movq %r13, %rdi
movq 0x40(%rsp), %rdx
movq %r14, %rcx
movq 0x10c0(%rsp), %r8
callq *0x38(%rsp)
cmpl $0x1, %eax
je 0x10a80
xorl %eax, %eax
movq 0x8(%rsp), %rdx
movapd 0x60(%rsp), %xmm1
cmpq %rax, %rbp
je 0x10916
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
jmp 0x108f1
movsd -0x8(%r13,%r12,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x8(%r13,%r12,8)
incq %r12
movl 0x4(%rsp), %esi
jmp 0x1087e
movq 0x30(%rsp), %rax
addl %esi, (%rax)
xorl %eax, %eax
movq 0x28(%rsp), %rcx
cmpq %rax, %rbp
je 0x10960
movsd (%r15,%rax,8), %xmm0
xorpd 0xd48db(%rip), %xmm0    # 0xe5230
movlpd %xmm0, 0x8(%rcx,%rax,8)
incq %rax
jmp 0x10942
movq %rbx, %rdi
movq 0x20(%rsp), %r12
movq %r12, %rdx
leaq 0x78(%rsp), %rcx
movq 0x18(%rsp), %r8
callq 0x102b1
testl %eax, %eax
jne 0x10c01
movq %rbx, %rdi
movl 0x4(%rsp), %esi
movq %r12, %rdx
movq 0x28(%rsp), %r12
movq %r12, %rcx
callq 0xfba9
xorpd %xmm0, %xmm0
xorl %eax, %eax
movq 0x8(%rsp), %rdx
movapd 0xd4894(%rip), %xmm3    # 0xe5240
cmpq %rax, %rbp
je 0x109d7
movsd 0x8(%r12,%rax,8), %xmm1
movapd %xmm1, %xmm2
andpd %xmm3, %xmm2
addsd %xmm2, %xmm0
addsd (%r13,%rax,8), %xmm1
movsd %xmm1, (%r13,%rax,8)
incq %rax
jmp 0x109ac
incl 0x14(%rsp)
movsd 0x50(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movl 0x4(%rsp), %esi
jb 0x10809
movq %rdx, %r13
movq %r12, %rdi
callq 0x8430
movq 0x18(%rsp), %rdi
callq 0x8430
movq 0x20(%rsp), %rdi
callq 0x8430
movq 0x8(%rbx), %rdi
callq 0x8430
movq %rbx, %rdi
callq 0x8430
movq %r15, %rdi
callq 0x8430
movq %r13, %rdi
callq 0x8430
movq %r14, %rdi
callq 0x8430
xorl %eax, %eax
jmp 0x10b0d
leaq 0xd45bf(%rip), %rsi     # 0xe5004
leaq 0xd57b8(%rip), %rdx     # 0xe6204
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2e014
leaq 0xd45cd(%rip), %rsi     # 0xe5034
leaq 0xd5771(%rip), %rdx     # 0xe61df
movq 0x10c0(%rsp), %rdi
movl $0x453, %ecx            # imm = 0x453
jmp 0x10b00
leaq 0xd4969(%rip), %rsi     # 0xe53f0
leaq 0xd57ab(%rip), %rdx     # 0xe6239
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq 0x10c0(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0xd4582(%rip), %rsi     # 0xe5034
leaq 0xd5726(%rip), %rdx     # 0xe61df
movq %r14, %rdi
movl $0x47b, %ecx            # imm = 0x47B
jmp 0x10b00
leaq 0xd453a(%rip), %rsi     # 0xe5004
leaq 0xd5a78(%rip), %rdx     # 0xe6549
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2e014
leaq 0xd4548(%rip), %rsi     # 0xe5034
leaq 0xd56ec(%rip), %rdx     # 0xe61df
movq 0x10c0(%rsp), %rdi
movl $0x454, %ecx            # imm = 0x454
movq %rbx, %r8
xorl %eax, %eax
callq 0x2e014
pushq $0x1
popq %rax
addq $0x1088, %rsp           # imm = 0x1088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x8430
movq 0x18(%rsp), %rdi
callq 0x8430
movq 0x20(%rsp), %rdi
callq 0x8430
movq 0x8(%rbx), %rdi
callq 0x8430
movq %rbx, %rdi
callq 0x8430
movq %r15, %rdi
callq 0x8430
movq 0x8(%rsp), %rdi
callq 0x8430
movq %r14, %rdi
callq 0x8430
leaq 0xd56fe(%rip), %rsi     # 0xe626b
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2e014
leaq 0xd49ff(%rip), %rsi     # 0xe5585
leaq 0x880(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2e014
leaq 0xd4492(%rip), %rsi     # 0xe5034
leaq 0xd5636(%rip), %rdx     # 0xe61df
movq 0x10c0(%rsp), %rdi
movl $0x4a3, %ecx            # imm = 0x4A3
jmp 0x1063b
leaq 0xd482e(%rip), %rsi     # 0xe53f0
leaq 0xd5640(%rip), %rdx     # 0xe6209
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq 0x10c0(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0xd4447(%rip), %rsi     # 0xe5034
leaq 0xd55eb(%rip), %rdx     # 0xe61df
movq %r14, %rdi
movl $0x45f, %ecx            # imm = 0x45F
jmp 0x10b00
leaq 0xd55a5(%rip), %rsi     # 0xe61ad
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2e014
leaq 0xd4461(%rip), %rsi     # 0xe5082
leaq 0xd532f(%rip), %rdx     # 0xe5f57
leaq 0x880(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0xd43f0(%rip), %rsi     # 0xe5034
leaq 0xd5594(%rip), %rdx     # 0xe61df
movq 0x10c0(%rsp), %rdi
movl $0x487, %ecx            # imm = 0x487
jmp 0x1063b
nopl (%rax)