// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movapd %xmm0, %xmm1
cvtsi2sd %edi, %xmm3
cmpl $0x30d41, %edi          # imm = 0x30D41
jl 0x353c1
movsd 0x60f5(%rip), %xmm0     # 0x3b428
mulsd %xmm3, %xmm0
mulsd %xmm1, %xmm0
movsd 0x112d(%rip), %xmm4     # 0x36470
addsd %xmm4, %xmm0
mulsd 0x60b9(%rip), %xmm3     # 0x3b408
mulsd %xmm0, %xmm0
divsd %xmm3, %xmm0
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
movsd 0x6191(%rip), %xmm2     # 0x3b4f8
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
addsd 0x1115(%rip), %xmm1     # 0x36490
divsd %xmm3, %xmm1
movapd %xmm4, %xmm2
subsd %xmm1, %xmm2
xorpd %xmm1, %xmm1
ucomisd %xmm2, %xmm1
jae 0x3565b
xorpd 0x1113(%rip), %xmm0     # 0x364b0
movsd %xmm2, (%rsp)
callq 0x7760
movsd (%rsp), %xmm2
mulsd %xmm0, %xmm2
movsd 0x10b8(%rip), %xmm1     # 0x36470
minsd %xmm2, %xmm1
jmp 0x3565b
movl %edi, %ebx
movsd 0x10a5(%rip), %xmm0     # 0x36470
movapd %xmm1, 0x30(%rsp)
subsd %xmm1, %xmm0
movapd %xmm0, %xmm1
mulsd %xmm3, %xmm1
cvttsd2si %xmm1, %ebp
cvttpd2dq %xmm1, %xmm1
cvtdq2pd %xmm1, %xmm1
movsd %xmm3, 0x28(%rsp)
divsd %xmm3, %xmm1
subsd %xmm1, %xmm0
xorpd %xmm2, %xmm2
xorpd %xmm1, %xmm1
movsd %xmm1, (%rsp)
ucomisd %xmm0, %xmm2
adcl $-0x1, %ebp
xorl %ecx, %ecx
cmpl $0xbb9, %edi            # imm = 0xBB9
setge %cl
xorl $0x3, %ecx
movl %ebp, %eax
cltd
idivl %ecx
movl %eax, %r14d
leal 0x1(%r14), %r15d
callq 0x3566e
movsd %xmm0, 0x10(%rsp)
movl %r15d, %edi
callq 0x3566e
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movl %ebx, %edi
subl %r15d, %edi
callq 0x3566e
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movl %r14d, %r12d
notl %r12d
addl %ebx, %r12d
movsd %xmm1, 0x10(%rsp)
movl %r15d, %r13d
cmpl %ebp, %r13d
jg 0x3553a
cvtsi2sd %r13d, %xmm0
divsd 0x28(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
movapd %xmm0, 0x40(%rsp)
leal -0x1(%r13), %eax
cvtsi2sd %eax, %xmm2
movsd %xmm2, 0x18(%rsp)
movsd %xmm1, 0x8(%rsp)
callq 0x7370
mulsd 0x18(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x18(%rsp)
movapd 0x40(%rsp), %xmm0
xorpd 0xfde(%rip), %xmm0      # 0x364b0
callq 0x7830
mulsd 0x18(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
callq 0x7760
movsd %xmm0, 0x20(%rsp)
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
incl %r13d
cvtsi2sd %r13d, %xmm1
movsd 0x18(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x7370
movsd (%rsp), %xmm1
mulsd 0x5fd0(%rip), %xmm1     # 0x3b4f0
ucomisd 0x20(%rsp), %xmm1
jae 0x3553a
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
decl %r12d
jmp 0x3546e
cvtsi2sd %r15d, %xmm0
subl %r14d, %ebx
cvtsi2sd %ebx, %xmm1
divsd %xmm1, %xmm0
callq 0x7370
movsd 0x10(%rsp), %xmm2
addsd %xmm0, %xmm2
movapd 0x30(%rsp), %xmm0
testl %r14d, %r14d
movsd 0x28(%rsp), %xmm1
jle 0x3562c
cvtsi2sd %r14d, %xmm3
movsd %xmm3, 0x18(%rsp)
divsd %xmm1, %xmm3
addsd %xmm0, %xmm3
movapd %xmm3, 0x40(%rsp)
decl %r14d
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0x8(%rsp)
movapd %xmm3, %xmm0
movsd %xmm2, 0x10(%rsp)
callq 0x7370
mulsd 0x8(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x20(%rsp)
movapd 0x40(%rsp), %xmm0
xorpd 0xee2(%rip), %xmm0      # 0x364b0
callq 0x7830
mulsd 0x20(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
callq 0x7760
movsd %xmm0, 0x8(%rsp)
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
incl %ebx
cvtsi2sd %ebx, %xmm1
movsd 0x18(%rsp), %xmm0
divsd %xmm1, %xmm0
callq 0x7370
movsd (%rsp), %xmm1
mulsd 0x5ed6(%rip), %xmm1     # 0x3b4f0
ucomisd 0x8(%rsp), %xmm1
jb 0x3554f
movapd 0x30(%rsp), %xmm0
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
xorpd 0xe6e(%rip), %xmm0      # 0x364b0
callq 0x7830
mulsd 0x28(%rsp), %xmm0
callq 0x7760
movapd %xmm0, %xmm1
addsd (%rsp), %xmm1
movapd %xmm1, %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq