#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__ INT_MAX;
double exp(double);
double rand();
int poisson(double lambda) {
  int k = 0;
  double L = exp(-lambda), p = 1;
  do {
    ++k;
    p *= rand() / (double)INT_MAX;
  } while (p > L);
  return --k;
}


x86 assembly (clang, -O0):
<poisson>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
call   1030 <exp@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  0xe9e(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    0x2e6a(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
ucomisd -0x18(%rbp),%xmm0
ja     1167 <poisson+0x47>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poisson>:
push   %r14
push   %rbx
sub    $0x18,%rsp
xorps  0xed2(%rip),%xmm0
call   1030 <exp@plt>
movsd  %xmm0,0x10(%rsp)
movsd  0xecf(%rip),%xmm0
mov    $0xffffffff,%ebx
mov    0x2e9b(%rip),%r14
nopl   (%rax)
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   1040 <rand@plt>
xorps  %xmm1,%xmm1
cvtsi2sdq (%r14),%xmm1
divsd  %xmm1,%xmm0
movsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
add    $0x1,%ebx
ucomisd 0x10(%rsp),%xmm0
ja     1150 <poisson+0x30>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@INT_MAX = global i64 0
define i32 @poisson(double %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca double
%5 = alloca double
store double %0, double* %2
store i32 0, i32* %3
%6 = load double, double* %2
%7 = fneg double %6
%8 = call double @exp(double %7)
store double %8, double* %4
store double 1.000000e+00, double* %5
br label %9
9: ; preds = %18, %1
%10 = load i32, i32* %3
%11 = add nsw i32 %10, 1
store i32 %11, i32* %3
%12 = call double (...) @rand()
%13 = load i64, i64* @INT_MAX
%14 = sitofp i64 %13 to double
%15 = fdiv double %12, %14
%16 = load double, double* %5
%17 = fmul double %16, %15
store double %17, double* %5
br label %18
18: ; preds = %9
%19 = load double, double* %5
%20 = load double, double* %4
%21 = fcmp ogt double %19, %20
br i1 %21, label %9, label %22
22: ; preds = %18
%23 = load i32, i32* %3
%24 = add nsw i32 %23, -1
store i32 %24, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@INT_MAX = global i64 0
define i32 @poisson(double %0) {
%2 = fneg double %0
%3 = tail call double @exp(double %2)
br label %4
4: ; preds = %4, %1
%5 = phi i32 [ 0, %1 ], [ %7, %4 ]
%6 = phi double [ 1.000000e+00, %1 ], [ %12, %4 ]
%7 = add nuw nsw i32 %5, 1
%8 = tail call double (...) @rand()
%9 = load i64, i64* @INT_MAX
%10 = sitofp i64 %9 to double
%11 = fdiv double %8, %10
%12 = fmul double %6, %11
%13 = fcmp ogt double %12, %3
br i1 %13, label %4, label %14
14: ; preds = %4
ret i32 %5
}