// File: /workspace/github_cpp_makefile_star2_O2/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, -0x68(%rbp)
movsd %xmm5, -0x60(%rbp)
movsd %xmm4, -0x40(%rbp)
movsd %xmm3, -0x30(%rbp)
movsd %xmm2, -0x28(%rbp)
movsd %xmm1, -0x58(%rbp)
movsd %xmm0, -0x50(%rbp)
callq 0x3db2c
movq %rax, %rbx
callq 0x3db2c
movsd -0x50(%rbp), %xmm3
movsd 0x30e0(%rbx), %xmm1
movsd 0x30ee6(%rip), %xmm0    # 0x57048
subsd %xmm1, %xmm0
movsd -0x30(%rbp), %xmm2
divsd %xmm0, %xmm2
addsd 0x32421(%rip), %xmm2    # 0x58598
movsd %xmm2, -0x38(%rbp)
movq %rax, %r14
movsd -0x28(%rbp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
pushq $0xb
popq %r15
leaq 0x323d8(%rip), %r12     # 0x58570
decl %r15d
je 0x262dc
movsd -0x38(%rbp), %xmm0
minsd %xmm3, %xmm0
movapd %xmm0, %xmm2
movapd %xmm0, %xmm3
movsd %xmm0, -0x78(%rbp)
subsd -0x58(%rbp), %xmm2
movsd %xmm2, -0x80(%rbp)
movapd %xmm2, %xmm0
mulsd %xmm2, %xmm0
addsd -0x28(%rbp), %xmm0
sqrtsd %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
subsd -0x60(%rbp), %xmm0
divsd -0x40(%rbp), %xmm0
subsd -0x68(%rbp), %xmm0
xorpd %xmm2, %xmm2
maxsd %xmm0, %xmm2
movapd %xmm2, -0x50(%rbp)
movsd 0x30e4d(%rip), %xmm0    # 0x57048
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
mulsd %xmm3, %xmm2
divsd -0x30(%rbp), %xmm2
subsd %xmm2, %xmm0
callq 0x93f0
movsd 0x3468(%r14), %xmm1
divsd %xmm1, %xmm0
movsd -0x70(%rbp), %xmm2
mulsd -0x40(%rbp), %xmm2
movsd -0x80(%rbp), %xmm5
divsd %xmm2, %xmm5
movsd 0x30e0b(%rip), %xmm3    # 0x57048
movapd %xmm3, %xmm2
divsd %xmm1, %xmm2
movsd 0x30e0(%rbx), %xmm1
subsd %xmm1, %xmm3
movsd -0x30(%rbp), %xmm4
divsd %xmm3, %xmm4
movsd -0x78(%rbp), %xmm6
subsd %xmm6, %xmm4
divsd %xmm4, %xmm2
addsd %xmm5, %xmm2
xorl %eax, %eax
ucomisd 0x3206b(%rip), %xmm2    # 0x582e0
movapd -0x50(%rbp), %xmm5
subsd %xmm0, %xmm5
setae %al
movapd %xmm2, %xmm0
movapd 0x30d83(%rip), %xmm4    # 0x57010
andpd %xmm4, %xmm0
movsd (%r12,%rax,8), %xmm3
cmpltsd 0x30da0(%rip), %xmm0    # 0x57040
andpd %xmm0, %xmm3
andnpd %xmm2, %xmm0
orpd %xmm3, %xmm0
movapd %xmm5, %xmm3
movapd %xmm5, %xmm2
divsd %xmm0, %xmm2
movapd %xmm6, %xmm0
subsd %xmm2, %xmm0
movapd %xmm0, %xmm2
andpd %xmm4, %xmm3
ucomisd 0x31448(%rip), %xmm3    # 0x57718
movapd %xmm0, %xmm3
ja 0x26198
jmp 0x262e0
movapd %xmm3, %xmm2
xorpd %xmm0, %xmm0
maxsd %xmm0, %xmm2
movsd -0x38(%rbp), %xmm0
minsd %xmm2, %xmm0
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq