// File: millanek[P]Dsuite/KolmogorovSmirnovDist.cpp
// Function: KSPlusbarUpper(int, double)
static double KSPlusbarUpper (int n, double x)
{
   /* Compute the probability of the KS+ distribution in the upper tail
      using Smirnov's stable formula */
   const double EPSILON = 1.0E-12;
   double q;
   double Sum = 0.0;
   double term;
   double t;
   double LogCom;
   double LOGJMAX;
   int j;
   int jdiv;
   int jmax = (int) (n * (1.0 - x));

   if (n > 200000)
      return KSPlusbarAsymp (n, x);

   /* Avoid log(0) for j = jmax and q ~ 1.0 */
   if ((1.0 - x - (double) jmax / n) <= 0.0)
      jmax--;

   if (n > 3000)
      jdiv = 2;
   else
      jdiv = 3;

   j = jmax / jdiv + 1;
   LogCom = getLogFactorial (n) - getLogFactorial (j) -
            getLogFactorial (n - j);
   LOGJMAX = LogCom;

   while (j <= jmax) {
      q = (double) j / n + x;
      term = LogCom + (j - 1) * log (q) + (n - j) * log1p (-q);
      t = exp (term);
      Sum += t;
      LogCom += log ((double) (n - j) / (j + 1));
      if (t <= Sum * EPSILON)
         break;
      j++;
   }

   j = jmax / jdiv;
   LogCom = LOGJMAX + log ((double) (j + 1) / (n - j));

   while (j > 0) {
      q = (double) j / n + x;
      term = LogCom + (j - 1) * log (q) + (n - j) * log1p (-q);
      t = exp (term);
      Sum += t;
      LogCom += log ((double) j / (n - j + 1));
      if (t <= Sum * EPSILON)
         break;
      j--;
   }

   Sum *= x;
   /* add the term j = 0 */
   Sum += exp (n * log1p (-x));
   return Sum;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movl %edi, -0xc(%rbp)
movsd %xmm0, -0x18(%rbp)
movsd 0x686d(%rip), %xmm0     # 0x8d7d8
movsd %xmm0, -0x20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
cvtsi2sdl -0xc(%rbp), %xmm0
movsd 0x1523(%rip), %xmm1     # 0x884a8
subsd -0x18(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x5c(%rbp)
cmpl $0x30d40, -0xc(%rbp)    # imm = 0x30D40
jle 0x86fb5
movl -0xc(%rbp), %edi
movsd -0x18(%rbp), %xmm0
callq 0x87950
movsd %xmm0, -0x8(%rbp)
jmp 0x87355
movsd 0x14eb(%rip), %xmm1     # 0x884a8
subsd -0x18(%rbp), %xmm1
cvtsi2sdl -0x5c(%rbp), %xmm0
cvtsi2sdl -0xc(%rbp), %xmm2
divsd %xmm2, %xmm0
subsd %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jb 0x86fe6
movl -0x5c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x5c(%rbp)
cmpl $0xbb8, -0xc(%rbp)      # imm = 0xBB8
jle 0x86ff8
movl $0x2, -0x58(%rbp)
jmp 0x86fff
movl $0x3, -0x58(%rbp)
movl -0x5c(%rbp), %eax
cltd
idivl -0x58(%rbp)
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movl -0xc(%rbp), %edi
callq 0x87370
movsd %xmm0, -0x70(%rbp)
movl -0x54(%rbp), %edi
callq 0x87370
movaps %xmm0, %xmm1
movsd -0x70(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movl -0xc(%rbp), %edi
subl -0x54(%rbp), %edi
callq 0x87370
movaps %xmm0, %xmm1
movsd -0x68(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jg 0x8717c
cvtsi2sdl -0x54(%rbp), %xmm0
cvtsi2sdl -0xc(%rbp), %xmm1
divsd %xmm1, %xmm0
addsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movl -0x54(%rbp), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x28(%rbp), %xmm0
callq 0x8490
movsd -0x90(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x88(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movl -0xc(%rbp), %eax
subl -0x54(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x28(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
callq 0x8b90
movsd -0x80(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x78(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x38(%rbp), %xmm0
callq 0x8a70
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movl -0xc(%rbp), %eax
subl -0x54(%rbp), %eax
cvtsi2sd %eax, %xmm0
movl -0x54(%rbp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
callq 0x8490
addsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x40(%rbp), %xmm1
movsd 0x6677(%rip), %xmm0     # 0x8d7d8
mulsd -0x30(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x8716e
jmp 0x8717c
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x87058
movl -0x5c(%rbp), %eax
cltd
idivl -0x58(%rbp)
movl %eax, -0x54(%rbp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm0
movl -0xc(%rbp), %eax
subl -0x54(%rbp), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
callq 0x8490
movaps %xmm0, %xmm1
movsd -0x98(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
cmpl $0x0, -0x54(%rbp)
jle 0x872f0
cvtsi2sdl -0x54(%rbp), %xmm0
cvtsi2sdl -0xc(%rbp), %xmm1
divsd %xmm1, %xmm0
addsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movl -0x54(%rbp), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0x28(%rbp), %xmm0
callq 0x8490
movsd -0xb8(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0xb0(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movl -0xc(%rbp), %eax
subl -0x54(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0x28(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
callq 0x8b90
movsd -0xa8(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0xa0(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x38(%rbp), %xmm0
callq 0x8a70
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
cvtsi2sdl -0x54(%rbp), %xmm0
movl -0xc(%rbp), %eax
subl -0x54(%rbp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
callq 0x8490
addsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x40(%rbp), %xmm1
movsd 0x6503(%rip), %xmm0     # 0x8d7d8
mulsd -0x30(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x872e2
jmp 0x872f0
movl -0x54(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x871c4
movsd -0x18(%rbp), %xmm0
mulsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
cvtsi2sdl -0xc(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0x18(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
callq 0x8b90
movaps %xmm0, %xmm1
movsd -0xc0(%rbp), %xmm0
mulsd %xmm1, %xmm0
callq 0x8a70
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)