// File: 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
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movsd %xmm1, -0x18(%rbp)
movsd %xmm2, -0x20(%rbp)
movsd %xmm3, -0x28(%rbp)
movsd %xmm4, -0x30(%rbp)
movsd %xmm5, -0x38(%rbp)
movsd %xmm6, -0x40(%rbp)
callq 0x855e0
movq %rax, %rdi
callq 0x113d0
movq %rax, -0x48(%rbp)
callq 0x855e0
movq %rax, %rdi
callq 0x4f340
movq %rax, -0x50(%rbp)
movsd -0x28(%rbp), %xmm0
movq -0x48(%rbp), %rax
movsd 0x72abb(%rip), %xmm1    # 0xc1010
subsd (%rax), %xmm1
divsd %xmm1, %xmm0
movsd 0x73fab(%rip), %xmm1    # 0xc2510
subsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x84(%rbp)
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
cmpl $0xa, -0x84(%rbp)
jle 0x4e59f
jmp 0x4e75f
leaq -0x80(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x12a60
movsd (%rax), %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
subsd -0x18(%rbp), %xmm0
movsd -0x80(%rbp), %xmm2
subsd -0x18(%rbp), %xmm2
movsd -0x20(%rbp), %xmm1
mulsd -0x20(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x12420
movsd %xmm0, -0x60(%rbp)
movsd -0x60(%rbp), %xmm0
subsd -0x38(%rbp), %xmm0
divsd -0x30(%rbp), %xmm0
subsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x68(%rbp), %xmm0
jbe 0x4e61f
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x68(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0x80(%rbp), %xmm1
movq -0x48(%rbp), %rax
movsd 0x729d3(%rip), %xmm0    # 0xc1010
subsd (%rax), %xmm0
mulsd %xmm0, %xmm1
divsd -0x28(%rbp), %xmm1
movsd 0x729be(%rip), %xmm0    # 0xc1010
subsd %xmm1, %xmm0
callq 0xa490
movaps %xmm0, %xmm1
movsd -0xa0(%rbp), %xmm0
movq -0x50(%rbp), %rax
divsd (%rax), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x80(%rbp), %xmm0
subsd -0x18(%rbp), %xmm0
movsd -0x60(%rbp), %xmm1
mulsd -0x30(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x50(%rbp), %rax
movsd 0x72975(%rip), %xmm1    # 0xc1010
divsd (%rax), %xmm1
movsd -0x28(%rbp), %xmm2
movq -0x48(%rbp), %rax
movsd 0x72960(%rip), %xmm3    # 0xc1010
subsd (%rax), %xmm3
divsd %xmm3, %xmm2
subsd -0x80(%rbp), %xmm2
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x78(%rbp), %xmm1
movaps 0x7298a(%rip), %xmm0    # 0xc1060
pand %xmm0, %xmm1
movsd 0x72926(%rip), %xmm0    # 0xc1008
ucomisd %xmm1, %xmm0
jbe 0x4e70b
leaq -0x78(%rbp), %rdi
callq 0x1f2e0
cvtsi2sd %eax, %xmm0
movsd 0x7290b(%rip), %xmm1    # 0xc1008
mulsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
jmp 0x4e718
movsd -0x78(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x80(%rbp), %xmm0
movsd -0x70(%rbp), %xmm1
divsd -0x78(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x70(%rbp), %xmm0
movaps 0x72917(%rip), %xmm1    # 0xc1060
pand %xmm1, %xmm0
movsd 0x72f7b(%rip), %xmm1    # 0xc16d0
ucomisd %xmm1, %xmm0
ja 0x4e582
xorps %xmm0, %xmm0
movsd %xmm0, -0x98(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x12a20
movsd (%rax), %xmm0
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax,%rax)