#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 binopdf_mat(int, int, double);
int invbinocdf(double y, int n, double p) {
  int x = 0;
  double cumdist = 0;
  double currpdf;
  int count;
  if (y == 1)
    x = n;
  if ((n < 0) | (p < 0) | (p > 1) | (y < 0.0) | (y > 1.0))
    x = -1;
  if (x == 0) {
    x = -1;
    while (cumdist < y) {
      x++;
      currpdf = binopdf_mat(n, x, p);
      cumdist += currpdf;
    }
  }
  return x;
}


x86 assembly (clang, -O0):
<invbinocdf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
movsd  %xmm1,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xebf(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    1157 <invbinocdf+0x47>
jp     1157 <invbinocdf+0x47>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0xc(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%eax
xorps  %xmm0,%xmm0
ucomisd -0x18(%rbp),%xmm0
seta   %cl
and    $0x1,%cl
movzbl %cl,%ecx
or     %ecx,%eax
movsd  -0x18(%rbp),%xmm0
movsd  0xe7d(%rip),%xmm1
ucomisd %xmm1,%xmm0
seta   %cl
and    $0x1,%cl
movzbl %cl,%ecx
or     %ecx,%eax
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
seta   %cl
and    $0x1,%cl
movzbl %cl,%ecx
or     %ecx,%eax
movsd  -0x8(%rbp),%xmm0
movsd  0xe4e(%rip),%xmm1
ucomisd %xmm1,%xmm0
seta   %cl
and    $0x1,%cl
movzbl %cl,%ecx
or     %ecx,%eax
cmp    $0x0,%eax
je     11d1 <invbinocdf+0xc1>
movl   $0xffffffff,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    122d <invbinocdf+0x11d>
movl   $0xffffffff,-0x1c(%rbp)
movsd  -0x28(%rbp),%xmm1
movsd  -0x8(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1228 <invbinocdf+0x118>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x1c(%rbp),%esi
movsd  -0x18(%rbp),%xmm0
call   1030 <binopdf_mat@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    11e2 <invbinocdf+0xd2>
jmp    122d <invbinocdf+0x11d>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<invbinocdf>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movapd %xmm1,%xmm3
mov    %edi,%ebx
mov    %edi,%eax
shr    $0x1f,%eax
xorpd  %xmm1,%xmm1
xor    %ecx,%ecx
ucomisd %xmm3,%xmm1
seta   %cl
movsd  0xeca(%rip),%xmm2
xor    %edx,%edx
ucomisd %xmm2,%xmm3
seta   %dl
xor    %esi,%esi
ucomisd %xmm0,%xmm1
seta   %sil
xor    %r8d,%r8d
xor    %edi,%edi
ucomisd %xmm2,%xmm0
mov    %ebx,%ebp
cmovne %r8d,%ebp
cmovp  %r8d,%ebp
seta   %dil
or     %eax,%esi
or     %ecx,%esi
or     %edi,%esi
or     %edx,%esi
mov    $0xffffffff,%eax
cmovne %eax,%ebp
test   %ebp,%ebp
jne    11d1 <invbinocdf+0xc1>
ucomisd %xmm1,%xmm0
mov    $0xffffffff,%ebp
jbe    11d1 <invbinocdf+0xc1>
xorpd  %xmm1,%xmm1
mov    $0xffffffff,%ebp
movsd  %xmm3,0x8(%rsp)
movsd  %xmm0,(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  %xmm1,0x10(%rsp)
add    $0x1,%ebp
mov    %ebx,%edi
mov    %ebp,%esi
movapd %xmm3,%xmm0
call   1030 <binopdf_mat@plt>
movsd  0x10(%rsp),%xmm1
movsd  0x8(%rsp),%xmm3
addsd  %xmm0,%xmm1
movsd  (%rsp),%xmm0
ucomisd %xmm1,%xmm0
ja     11a0 <invbinocdf+0x90>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @invbinocdf(double %0, i32 %1, double %2) {
%4 = alloca double
%5 = alloca i32
%6 = alloca double
%7 = alloca i32
%8 = alloca double
%9 = alloca double
%10 = alloca i32
store double %0, double* %4
store i32 %1, i32* %5
store double %2, double* %6
store i32 0, i32* %7
store double 0.000000e+00, double* %8
%11 = load double, double* %4
%12 = fcmp oeq double %11, 1.000000e+00
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i32, i32* %5
store i32 %14, i32* %7
br label %15
15: ; preds = %13, %3
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 0
%18 = zext i1 %17 to i32
%19 = load double, double* %6
%20 = fcmp olt double %19, 0.000000e+00
%21 = zext i1 %20 to i32
%22 = or i32 %18, %21
%23 = load double, double* %6
%24 = fcmp ogt double %23, 1.000000e+00
%25 = zext i1 %24 to i32
%26 = or i32 %22, %25
%27 = load double, double* %4
%28 = fcmp olt double %27, 0.000000e+00
%29 = zext i1 %28 to i32
%30 = or i32 %26, %29
%31 = load double, double* %4
%32 = fcmp ogt double %31, 1.000000e+00
%33 = zext i1 %32 to i32
%34 = or i32 %30, %33
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %15
store i32 -1, i32* %7
br label %37
37: ; preds = %36, %15
%38 = load i32, i32* %7
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %56
40: ; preds = %37
store i32 -1, i32* %7
br label %41
41: ; preds = %45, %40
%42 = load double, double* %8
%43 = load double, double* %4
%44 = fcmp olt double %42, %43
br i1 %44, label %45, label %55
45: ; preds = %41
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
%48 = load i32, i32* %5
%49 = load i32, i32* %7
%50 = load double, double* %6
%51 = call double @binopdf_mat(i32 %48, i32 %49, double %50)
store double %51, double* %9
%52 = load double, double* %9
%53 = load double, double* %8
%54 = fadd double %53, %52
store double %54, double* %8
br label %41
55: ; preds = %41
br label %56
56: ; preds = %55, %37
%57 = load i32, i32* %7
ret i32 %57
}

LLVM IR (-O2):
define i32 @invbinocdf(double %0, i32 %1, double %2) {
%4 = fcmp oeq double %0, 1.000000e+00
%5 = select i1 %4, i32 %1, i32 0
%6 = lshr i32 %1, 31
%7 = fcmp olt double %2, 0.000000e+00
%8 = zext i1 %7 to i32
%9 = fcmp ogt double %2, 1.000000e+00
%10 = zext i1 %9 to i32
%11 = fcmp olt double %0, 0.000000e+00
%12 = zext i1 %11 to i32
%13 = fcmp ogt double %0, 1.000000e+00
%14 = zext i1 %13 to i32
%15 = or i32 %6, %12
%16 = or i32 %15, %14
%17 = or i32 %16, %8
%18 = or i32 %17, %10
%19 = icmp eq i32 %18, 0
%20 = select i1 %19, i32 %5, i32 -1
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %3
%23 = fcmp ogt double %0, 0.000000e+00
br i1 %23, label %24, label %31
24: ; preds = %22, %24
%25 = phi i32 [ %27, %24 ], [ -1, %22 ]
%26 = phi double [ %29, %24 ], [ 0.000000e+00, %22 ]
%27 = add nsw i32 %25, 1
%28 = tail call double @binopdf_mat(i32 %1, i32 %27, double %2)
%29 = fadd double %26, %28
%30 = fcmp olt double %29, %0
br i1 %30, label %24, label %31
31: ; preds = %24, %22, %3
%32 = phi i32 [ %20, %3 ], [ -1, %22 ], [ %27, %24 ]
ret i32 %32
}