// File: /workspace/github_cpp_makefile_star2_O1/melop[P]admixem/./newran02/newran.cpp
// Function: VariBinomial::iNext_small(int, double)
int VariBinomial::iNext_small(int n, Real p)
{
   // Efficient when sqrt(n) * p is small
   // Assume p <= 1/2
   // same as small but start at mode
   // generate a Binomial variable with parameters n and p

   Real q = 1 - p; Real r = p / q; Real u = U.Next();
   int k_start = (int)( (n * r) / (r + 1) );
   Real t1 = exp( ln_gamma(n+1) - ln_gamma(k_start+1) - ln_gamma(n-k_start+1)
      + k_start * log(p) + (n-k_start) * log(q) );
   if (t1 >= u) return k_start;
   Real t2 = t1; Real s = t1;
   int k1 = k_start; int k2 = k_start;
   for (;;)
   {
      t1 *= r * (Real)(n - k1) / (Real)(k1 + 1); ++k1; s += t1;
      if (s >= u) return k1;
      if (k2)
      {
         --k2; t2 *= (Real)(k2 + 1) / (Real)(n - k2) / r; s += t2;
         if (s >= u) return k2;
      }
      else if (k1 == n) return 0;  // can happen only if we have round-off error
   }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x48, %rsp
movsd %xmm0, 0x10(%rsp)
movl %esi, %ebx
movsd 0x26104(%rip), %xmm1    # 0x35018
subsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rsp)
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
callq 0xbaa8
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
movsd 0x18(%rsp), %xmm3
mulsd %xmm3, %xmm1
movsd 0x260d2(%rip), %xmm2    # 0x35018
addsd %xmm3, %xmm2
divsd %xmm2, %xmm1
movapd %xmm1, 0x20(%rsp)
cvttsd2si %xmm1, %r14d
movsd %xmm0, 0x38(%rsp)
leal 0x1(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
callq 0xca78
movsd %xmm0, 0x8(%rsp)
leal 0x1(%r14), %ebp
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x30(%rsp)
callq 0xca78
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movl %ebx, %r15d
subl %r14d, %r15d
leal 0x1(%r15), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
callq 0xca78
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
cvttpd2dq 0x20(%rsp), %xmm0
cvtdq2pd %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x7730
mulsd 0x20(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x40(%rsp), %xmm0
callq 0x7730
mulsd 0x8(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
callq 0x7748
movsd 0x38(%rsp), %xmm5
ucomisd %xmm5, %xmm0
jae 0xf0c4
movsd 0x18(%rsp), %xmm4
movsd 0x8(%rsp), %xmm6
mulsd %xmm4, %xmm6
divsd 0x30(%rsp), %xmm6
mulsd %xmm0, %xmm6
movapd %xmm0, %xmm1
addsd %xmm6, %xmm1
ucomisd %xmm5, %xmm1
jae 0xf0c9
movl %r14d, %ecx
notl %ecx
addl %ebx, %ecx
testl %r14d, %r14d
je 0xf08c
xorps %xmm2, %xmm2
cvtsi2sd %r14d, %xmm2
decl %r14d
movl %ebx, %eax
subl %r14d, %eax
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
divsd %xmm3, %xmm2
divsd %xmm4, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
ucomisd %xmm5, %xmm1
movl %r14d, %eax
jb 0xf098
jmp 0xf0cb
xorl %r14d, %r14d
movl $0x0, %eax
testl %ecx, %ecx
je 0xf0cb
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
incl %ebp
xorps %xmm3, %xmm3
cvtsi2sd %ebp, %xmm3
mulsd %xmm4, %xmm2
divsd %xmm3, %xmm2
mulsd %xmm2, %xmm6
addsd %xmm6, %xmm1
decl %ecx
ucomisd %xmm5, %xmm1
movl %ebp, %eax
jb 0xf055
jmp 0xf0cb
movl %r14d, %eax
jmp 0xf0cb
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq