#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
double cdfBinomial(long, double, long);
long idfBinomial(long n, double p, double u) {
  long x = (long)(n * p);
  if (cdfBinomial(n, p, x) <= u)
    while (cdfBinomial(n, p, x) <= u)
      x++;
  else if (cdfBinomial(n, p, 0) <= u)
    while (cdfBinomial(n, p, x - 1) > u)
      x--;
  else
    x = 0;
  return (x);
}


x86 assembly (clang, -O0):
<idfBinomial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
cvtsi2sdq -0x8(%rbp),%xmm0
mulsd  -0x10(%rbp),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
movsd  -0x10(%rbp),%xmm0
mov    -0x20(%rbp),%rsi
call   1030 <cdfBinomial@plt>
movaps %xmm0,%xmm1
movsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     119d <idfBinomial+0x8d>
jmp    1163 <idfBinomial+0x53>
mov    -0x8(%rbp),%rdi
movsd  -0x10(%rbp),%xmm0
mov    -0x20(%rbp),%rsi
call   1030 <cdfBinomial@plt>
movaps %xmm0,%xmm1
movsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     1198 <idfBinomial+0x88>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1163 <idfBinomial+0x53>
jmp    120a <idfBinomial+0xfa>
mov    -0x8(%rbp),%rdi
movsd  -0x10(%rbp),%xmm0
xor    %eax,%eax
mov    %eax,%esi
call   1030 <cdfBinomial@plt>
movaps %xmm0,%xmm1
movsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     11fd <idfBinomial+0xed>
jmp    11c6 <idfBinomial+0xb6>
mov    -0x8(%rbp),%rdi
movsd  -0x10(%rbp),%xmm0
mov    -0x20(%rbp),%rsi
sub    $0x1,%rsi
call   1030 <cdfBinomial@plt>
ucomisd -0x18(%rbp),%xmm0
jbe    11f8 <idfBinomial+0xe8>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    11c6 <idfBinomial+0xb6>
jmp    1205 <idfBinomial+0xf5>
movq   $0x0,-0x20(%rbp)
jmp    120a <idfBinomial+0xfa>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<idfBinomial>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
cvtsi2sd %rdi,%xmm2
movsd  %xmm1,(%rsp)
mulsd  %xmm0,%xmm2
cvttsd2si %xmm2,%rbx
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rsi
call   1030 <cdfBinomial@plt>
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
jae    1190 <idfBinomial+0x80>
xor    %r15d,%r15d
mov    %r14,%rdi
movsd  0x8(%rsp),%xmm0
xor    %esi,%esi
call   1030 <cdfBinomial@plt>
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
jb     11b7 <idfBinomial+0xa7>
add    $0x1,%rbx
mov    %rbx,%r15
nopl   (%rax)
lea    -0x2(%r15),%rsi
mov    %r14,%rdi
movsd  0x8(%rsp),%xmm0
call   1030 <cdfBinomial@plt>
add    $0xffffffffffffffff,%r15
ucomisd (%rsp),%xmm0
ja     1170 <idfBinomial+0x60>
jmp    11b7 <idfBinomial+0xa7>
nop
mov    %r14,%rdi
movsd  0x8(%rsp),%xmm0
mov    %rbx,%rsi
call   1030 <cdfBinomial@plt>
add    $0x1,%rbx
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
jae    1190 <idfBinomial+0x80>
add    $0xffffffffffffffff,%rbx
mov    %rbx,%r15
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @idfBinomial(i64 %0, double %1, double %2) {
%4 = alloca i64
%5 = alloca double
%6 = alloca double
%7 = alloca i64
store i64 %0, i64* %4
store double %1, double* %5
store double %2, double* %6
%8 = load i64, i64* %4
%9 = sitofp i64 %8 to double
%10 = load double, double* %5
%11 = fmul double %9, %10
%12 = fptosi double %11 to i64
store i64 %12, i64* %7
%13 = load i64, i64* %4
%14 = load double, double* %5
%15 = load i64, i64* %7
%16 = call double @cdfBinomial(i64 %13, double %14, i64 %15)
%17 = load double, double* %6
%18 = fcmp ole double %16, %17
br i1 %18, label %19, label %31
19: ; preds = %3
br label %20
20: ; preds = %27, %19
%21 = load i64, i64* %4
%22 = load double, double* %5
%23 = load i64, i64* %7
%24 = call double @cdfBinomial(i64 %21, double %22, i64 %23)
%25 = load double, double* %6
%26 = fcmp ole double %24, %25
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i64, i64* %7
%29 = add nsw i64 %28, 1
store i64 %29, i64* %7
br label %20
30: ; preds = %20
br label %52
31: ; preds = %3
%32 = load i64, i64* %4
%33 = load double, double* %5
%34 = call double @cdfBinomial(i64 %32, double %33, i64 0)
%35 = load double, double* %6
%36 = fcmp ole double %34, %35
br i1 %36, label %37, label %50
37: ; preds = %31
br label %38
38: ; preds = %46, %37
%39 = load i64, i64* %4
%40 = load double, double* %5
%41 = load i64, i64* %7
%42 = sub nsw i64 %41, 1
%43 = call double @cdfBinomial(i64 %39, double %40, i64 %42)
%44 = load double, double* %6
%45 = fcmp ogt double %43, %44
br i1 %45, label %46, label %49
46: ; preds = %38
%47 = load i64, i64* %7
%48 = add nsw i64 %47, -1
store i64 %48, i64* %7
br label %38
49: ; preds = %38
br label %51
50: ; preds = %31
store i64 0, i64* %7
br label %51
51: ; preds = %50, %49
br label %52
52: ; preds = %51, %30
%53 = load i64, i64* %7
ret i64 %53
}

LLVM IR (-O2):
define i64 @idfBinomial(i64 %0, double %1, double %2) {
%4 = sitofp i64 %0 to double
%5 = fmul double %4, %1
%6 = fptosi double %5 to i64
%7 = tail call double @cdfBinomial(i64 %0, double %1, i64 %6)
%8 = fcmp ugt double %7, %2
br i1 %8, label %14, label %9
9: ; preds = %3, %9
%10 = phi i64 [ %13, %9 ], [ %6, %3 ]
%11 = tail call double @cdfBinomial(i64 %0, double %1, i64 %10)
%12 = fcmp ugt double %11, %2
%13 = add nsw i64 %10, 1
br i1 %12, label %22, label %9
14: ; preds = %3
%15 = tail call double @cdfBinomial(i64 %0, double %1, i64 0)
%16 = fcmp ugt double %15, %2
br i1 %16, label %22, label %17
17: ; preds = %14, %17
%18 = phi i64 [ %19, %17 ], [ %6, %14 ]
%19 = add nsw i64 %18, -1
%20 = tail call double @cdfBinomial(i64 %0, double %1, i64 %19)
%21 = fcmp ogt double %20, %2
br i1 %21, label %17, label %22
22: ; preds = %9, %17, %14
%23 = phi i64 [ 0, %14 ], [ %18, %17 ], [ %10, %9 ]
ret i64 %23
}