// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movsd %xmm0, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movsd 0x5cc4d(%rip), %xmm0    # 0x76010
subsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x20(%rbp), %xmm0
divsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0x19e20
movsd %xmm0, -0x38(%rbp)
cvtsi2sdl -0x14(%rbp), %xmm0
mulsd -0x30(%rbp), %xmm0
movsd 0x5cc18(%rip), %xmm1    # 0x76010
addsd -0x30(%rbp), %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x3c(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm0
callq 0x14be0
movsd %xmm0, -0x90(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm0
callq 0x14be0
movaps %xmm0, %xmm1
movsd -0x90(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movl -0x14(%rbp), %eax
subl -0x3c(%rbp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm0
callq 0x14be0
movaps %xmm0, %xmm1
movsd -0x88(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
cvtsi2sdl -0x3c(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x20(%rbp), %xmm0
callq 0x8940
movsd -0x80(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x78(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x14(%rbp), %eax
subl -0x3c(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x28(%rbp), %xmm0
callq 0x8940
movsd -0x70(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x68(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x8958
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
ucomisd -0x38(%rbp), %xmm0
jb 0x194e8
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x195d2
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x60(%rbp)
movsd -0x30(%rbp), %xmm0
movl -0x14(%rbp), %eax
subl -0x5c(%rbp), %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
movl -0x5c(%rbp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
mulsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movsd -0x48(%rbp), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
ucomisd -0x38(%rbp), %xmm0
jb 0x1955f
movl -0x5c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x195d2
cmpl $0x0, -0x60(%rbp)
je 0x195ba
movl -0x60(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm0
movl -0x14(%rbp), %eax
subl -0x60(%rbp), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
divsd -0x30(%rbp), %xmm0
mulsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
ucomisd -0x38(%rbp), %xmm0
jb 0x195b8
movl -0x60(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x195d2
jmp 0x195cd
movl -0x5c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x195cb
movl $0x0, -0x4(%rbp)
jmp 0x195d2
jmp 0x195cd
jmp 0x19508
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nop