#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 */
scalar_t__ rand_exp();
double rand_gaussian();
double sqrt(double);
int rand_poisson(double lambda) {
  int v;
  if (lambda > 10000) {
    v = (int)(lambda + rand_gaussian() * sqrt(lambda) + 0.5);
  } else {
    v = 0;
    for (;;) {
      lambda -= rand_exp();
      if (lambda <= 0)
        break;
      v += 1;
    }
  }
  return v;
}


x86 assembly (clang, -O0):
<rand_poisson>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xeb6(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11a1 <rand_poisson+0x71>
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    $0x0,%al
call   1040 <rand_gaussian@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1050 <sqrt@plt>
movsd  -0x20(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x18(%rbp),%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  0xe77(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0xc(%rbp)
jmp    11e8 <rand_poisson+0xb8>
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1030 <rand_exp@plt>
cvtsi2sd %rax,%xmm1
movsd  -0x8(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jb     11d5 <rand_poisson+0xa5>
jmp    11e3 <rand_poisson+0xb3>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a8 <rand_poisson+0x78>
jmp    11e8 <rand_poisson+0xb8>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_poisson>:
push   %rbx
sub    $0x10,%rsp
ucomisd 0xec3(%rip),%xmm0
jbe    1167 <rand_poisson+0x37>
xor    %eax,%eax
movsd  %xmm0,(%rsp)
call   1040 <rand_gaussian@plt>
movsd  (%rsp),%xmm1
movapd %xmm0,%xmm2
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
jb     119c <rand_poisson+0x6c>
xorps  %xmm0,%xmm0
sqrtsd %xmm1,%xmm0
jmp    11b6 <rand_poisson+0x86>
mov    $0xffffffff,%ebx
nopl   0x0(%rax)
movsd  %xmm0,(%rsp)
xor    %eax,%eax
call   1030 <rand_exp@plt>
movsd  (%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
subsd  %xmm1,%xmm0
add    $0x1,%ebx
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jb     1170 <rand_poisson+0x40>
jmp    11ca <rand_poisson+0x9a>
movapd %xmm1,%xmm0
movsd  %xmm2,0x8(%rsp)
call   1050 <sqrt@plt>
movsd  0x8(%rsp),%xmm2
movsd  (%rsp),%xmm1
mulsd  %xmm0,%xmm2
addsd  %xmm1,%xmm2
addsd  0xe42(%rip),%xmm2
cvttsd2si %xmm2,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @rand_poisson(double %0) {
%2 = alloca double
%3 = alloca i32
store double %0, double* %2
%4 = load double, double* %2
%5 = fcmp ogt double %4, 1.000000e+04
br i1 %5, label %6, label %14
6: ; preds = %1
%7 = load double, double* %2
%8 = call double (...) @rand_gaussian()
%9 = load double, double* %2
%10 = call double @sqrt(double %9)
%11 = call double @llvm.fmuladd.f64(double %8, double %10, double %7)
%12 = fadd double %11, 5.000000e-01
%13 = fptosi double %12 to i32
store i32 %13, i32* %3
br label %27
14: ; preds = %1
store i32 0, i32* %3
br label %15
15: ; preds = %23, %14
%16 = call i64 (...) @rand_exp()
%17 = sitofp i64 %16 to double
%18 = load double, double* %2
%19 = fsub double %18, %17
store double %19, double* %2
%20 = load double, double* %2
%21 = fcmp ole double %20, 0.000000e+00
br i1 %21, label %22, label %23
22: ; preds = %15
br label %26
23: ; preds = %15
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %15
26: ; preds = %22
br label %27
27: ; preds = %26, %6
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
define i32 @rand_poisson(double %0) {
%2 = fcmp ogt double %0, 1.000000e+04
br i1 %2, label %3, label %9
3: ; preds = %1
%4 = tail call double (...) @rand_gaussian()
%5 = tail call double @sqrt(double %0)
%6 = tail call double @llvm.fmuladd.f64(double %4, double %5, double %0)
%7 = fadd double %6, 5.000000e-01
%8 = fptosi double %7 to i32
br label %17
9: ; preds = %1, %9
%10 = phi double [ %14, %9 ], [ %0, %1 ]
%11 = phi i32 [ %16, %9 ], [ 0, %1 ]
%12 = tail call i64 (...) @rand_exp()
%13 = sitofp i64 %12 to double
%14 = fsub double %10, %13
%15 = fcmp ugt double %14, 0.000000e+00
%16 = add nuw nsw i32 %11, 1
br i1 %15, label %9, label %17
17: ; preds = %9, %3
%18 = phi i32 [ %8, %3 ], [ %11, %9 ]
ret i32 %18
}