#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 INT_MAX;
double exp(double);
scalar_t__ rand();
int RandPoisson(double mean) {
  double limit = exp(-mean), product = ((double)rand() / INT_MAX);
  int count = 0;
  for (; product > limit; count++)
    product *= ((double)rand() / INT_MAX);
  return count;
}


x86 assembly (clang, -O0):
<RandPoisson>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
call   1030 <exp@plt>
movsd  %xmm0,-0x10(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e82(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movsd  -0x18(%rbp),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    11b5 <RandPoisson+0x95>
mov    $0x0,%al
call   1040 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e4f(%rip),%rax
divsd  (%rax),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1176 <RandPoisson+0x56>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RandPoisson>:
push   %r14
push   %rbx
sub    $0x18,%rsp
xorps  0xed2(%rip),%xmm0
call   1030 <exp@plt>
movsd  %xmm0,0x8(%rsp)
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <rand@plt>
movsd  0x8(%rsp),%xmm0
cvtsi2sd %rax,%xmm1
mov    0x2e94(%rip),%r14
divsd  (%r14),%xmm1
ucomisd %xmm0,%xmm1
jbe    11a3 <RandPoisson+0x83>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  %xmm1,0x10(%rsp)
xor    %eax,%eax
call   1040 <rand@plt>
movsd  0x10(%rsp),%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%r14),%xmm0
mulsd  %xmm0,%xmm1
movsd  0x8(%rsp),%xmm0
add    $0x1,%ebx
ucomisd %xmm0,%xmm1
ja     1170 <RandPoisson+0x50>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

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