// File: /workspace/github_cpp_makefile_star2_O1/wrighteagle2d[P]wrighteaglebase/Debug/../src/InterceptModel.cpp
// Function: InterceptModel::CalcInterPoint(double, double, double, double, double, double, double)
double InterceptModel::CalcInterPoint(double x_init, double x0, double y0,
                                      double vb, double vp, double ka,
                                      double cd) {
  static const double MINERROR = 0.01;

  const double &alpha = ServerParam::instance().ballDecay();
  const double &ln_alpha = ServerParam::instance().logBallDecay();

  const double max_x = vb / (1.0 - alpha) - 0.1;

  double s, p, f, dfdx, x;
  int iteration_cycle = 0;

  x = x_init;
  do {
    iteration_cycle += 1;
    if (iteration_cycle > 10) {
      break;
    }
    x = Min(x, max_x);
    s = Sqrt((x - x0) * (x - x0) + y0 * y0);
    p = (s - ka) / vp - cd;
    if (p < 0.0)
      p = 0.0;
    f = p - log(1.0 - x * (1.0 - alpha) / vb) / ln_alpha;
    dfdx = (x - x0) / (s * vp) + (1.0 / ln_alpha) / (vb / (1.0 - alpha) - x);
    dfdx = fabs(dfdx) < FLOAT_EPS ? (Sign(dfdx) * FLOAT_EPS) : dfdx;
    x = x - f / dfdx;
  } while (fabs(f) > MINERROR);

  return MinMax(0.0, x, max_x);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x60, %rsp
movsd %xmm6, -0x60(%rbp)
movsd %xmm5, -0x58(%rbp)
movsd %xmm4, -0x48(%rbp)
movsd %xmm3, -0x38(%rbp)
movsd %xmm2, -0x30(%rbp)
movsd %xmm1, -0x50(%rbp)
movsd %xmm0, -0x28(%rbp)
callq 0x4a75c
movq %rax, %rbx
callq 0x4a75c
movsd -0x28(%rbp), %xmm2
movsd 0x3e260(%rip), %xmm0    # 0x6b058
subsd 0x30e0(%rbx), %xmm0
movsd -0x38(%rbp), %xmm1
divsd %xmm0, %xmm1
addsd 0x3f7c7(%rip), %xmm1    # 0x6c5d8
movsd %xmm1, -0x40(%rbp)
movq %rax, %r14
movsd -0x30(%rbp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movl $0xb, %r15d
xorpd %xmm3, %xmm3
leaq 0x3f788(%rip), %r12     # 0x6c5c0
decl %r15d
je 0x2cf82
movsd -0x40(%rbp), %xmm0
minsd %xmm2, %xmm0
movsd %xmm0, -0x28(%rbp)
subsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
mulsd %xmm0, %xmm0
addsd -0x30(%rbp), %xmm0
ucomisd %xmm3, %xmm0
jb 0x2ce6e
sqrtsd %xmm0, %xmm0
jmp 0x2ce73
callq 0x93c0
movsd %xmm0, -0x68(%rbp)
subsd -0x58(%rbp), %xmm0
divsd -0x48(%rbp), %xmm0
subsd -0x60(%rbp), %xmm0
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movapd %xmm1, -0x80(%rbp)
movsd 0x3e1bc(%rip), %xmm0    # 0x6b058
movapd %xmm0, %xmm1
subsd 0x30e0(%rbx), %xmm1
mulsd -0x28(%rbp), %xmm1
divsd -0x38(%rbp), %xmm1
subsd %xmm1, %xmm0
callq 0x93d0
movsd 0x3468(%r14), %xmm1
divsd %xmm1, %xmm0
movsd -0x68(%rbp), %xmm2
mulsd -0x48(%rbp), %xmm2
movsd -0x70(%rbp), %xmm4
divsd %xmm2, %xmm4
movsd 0x3e175(%rip), %xmm3    # 0x6b058
movapd %xmm3, %xmm2
divsd %xmm1, %xmm2
movapd %xmm3, %xmm1
subsd 0x30e0(%rbx), %xmm1
movsd -0x38(%rbp), %xmm3
divsd %xmm1, %xmm3
movsd -0x28(%rbp), %xmm6
subsd %xmm6, %xmm3
divsd %xmm3, %xmm2
addsd %xmm4, %xmm2
xorl %eax, %eax
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm2
movapd -0x80(%rbp), %xmm5
subsd %xmm0, %xmm5
setae %al
movapd %xmm2, %xmm0
movapd 0x3e0dd(%rip), %xmm4    # 0x6b010
andpd %xmm4, %xmm0
movsd (%r12,%rax,8), %xmm1
cmpltsd 0x3e0fa(%rip), %xmm0    # 0x6b040
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
movapd %xmm5, %xmm2
movapd %xmm5, %xmm1
divsd %xmm0, %xmm1
movapd %xmm6, %xmm0
subsd %xmm1, %xmm0
movapd %xmm0, %xmm1
andpd %xmm4, %xmm2
ucomisd 0x3e7ca(%rip), %xmm2    # 0x6b740
movapd %xmm0, %xmm2
ja 0x2ce38
jmp 0x2cf86
movapd %xmm2, %xmm1
xorpd %xmm0, %xmm0
maxsd %xmm0, %xmm1
movsd -0x40(%rbp), %xmm0
minsd %xmm1, %xmm0
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq