#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 drand2();
double exp(double);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, double);
int /*<<< orphan*/ stderr;
double ranpoiss1(double xm) {
  double x, t, tc, s, y;
  int k;
  t = exp(-xm);
  tc = 1.0 - t;
  if (tc == 0.0)
    return 1;
  y = t;
  s = tc * (drand2());
  k = 1;
  y *= xm / (double)k;
  for (;;) {
    if (s < y)
      return (double)k;
    s -= y;
    ++k;
    y *= xm / (double)k;
    if (k >= 100) {
      fprintf(stderr, "(ranpoiss1) bug? xm: %12.6f\n", xm);
      return k;
    }
  }
}


x86 assembly (clang, -O0):
<ranpoiss1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
call   1050 <exp@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  0xe95(%rip),%xmm0
subsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    119f <ranpoiss1+0x6f>
jp     119f <ranpoiss1+0x6f>
movsd  0xe6b(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1279 <ranpoiss1+0x149>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    $0x0,%al
call   1030 <drand2@plt>
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x1,-0x3c(%rbp)
movsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x3c(%rbp),%xmm1
divsd  %xmm1,%xmm0
mulsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    120d <ranpoiss1+0xdd>
cvtsi2sdl -0x3c(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1279 <ranpoiss1+0x149>
movsd  -0x38(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
movsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x3c(%rbp),%xmm1
divsd  %xmm1,%xmm0
mulsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
cmpl   $0x64,-0x3c(%rbp)
jl     1274 <ranpoiss1+0x144>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
movsd  -0x10(%rbp),%xmm0
lea    0xda8(%rip),%rsi
call   1040 <fprintf@plt>
cvtsi2sdl -0x3c(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1279 <ranpoiss1+0x149>
jmp    11ea <ranpoiss1+0xba>
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ranpoiss1>:
sub    $0x38,%rsp
movapd %xmm0,%xmm1
movapd 0xec0(%rip),%xmm0
movapd %xmm1,(%rsp)
xorpd  %xmm1,%xmm0
call   1050 <exp@plt>
movsd  0xeba(%rip),%xmm5
movapd %xmm5,%xmm1
subsd  %xmm0,%xmm1
xorpd  %xmm2,%xmm2
ucomisd %xmm2,%xmm1
jne    1173 <ranpoiss1+0x43>
jp     1173 <ranpoiss1+0x43>
movapd %xmm5,%xmm0
add    $0x38,%rsp
ret
xor    %eax,%eax
movsd  %xmm1,0x18(%rsp)
movapd %xmm0,0x20(%rsp)
call   1030 <drand2@plt>
movapd 0x20(%rsp),%xmm4
movsd  0x18(%rsp),%xmm3
mulsd  %xmm0,%xmm3
movapd (%rsp),%xmm0
mulsd  %xmm0,%xmm4
mov    $0x3,%eax
ucomisd %xmm3,%xmm4
ja     11f5 <ranpoiss1+0xc5>
nopw   0x0(%rax,%rax,1)
lea    -0x1(%rax),%ecx
xorps  %xmm5,%xmm5
cvtsi2sd %ecx,%xmm5
cmp    $0x65,%eax
je     1204 <ranpoiss1+0xd4>
subsd  %xmm4,%xmm3
movapd %xmm0,%xmm2
divsd  %xmm5,%xmm2
mulsd  %xmm4,%xmm2
ucomisd %xmm3,%xmm2
ja     116a <ranpoiss1+0x3a>
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
subsd  %xmm2,%xmm3
movapd %xmm0,%xmm4
divsd  %xmm1,%xmm4
mulsd  %xmm2,%xmm4
add    $0x2,%eax
ucomisd %xmm3,%xmm4
jbe    11b0 <ranpoiss1+0x80>
add    $0xfffffffe,%eax
xorps  %xmm5,%xmm5
cvtsi2sd %eax,%xmm5
jmp    116a <ranpoiss1+0x3a>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
movsd  %xmm5,(%rsp)
call   1040 <fprintf@plt>
movsd  (%rsp),%xmm5
jmp    116a <ranpoiss1+0x3a>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"(ranpoiss1) bug? xm: %12.6f\0A\00"
define double @ranpoiss1(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca i32
store double %0, double* %3
%10 = load double, double* %3
%11 = fneg double %10
%12 = call double @exp(double %11)
store double %12, double* %5
%13 = load double, double* %5
%14 = fsub double 1.000000e+00, %13
store double %14, double* %6
%15 = load double, double* %6
%16 = fcmp oeq double %15, 0.000000e+00
br i1 %16, label %17, label %18
17: ; preds = %1
store double 1.000000e+00, double* %2
br label %57
18: ; preds = %1
%19 = load double, double* %5
store double %19, double* %8
%20 = load double, double* %6
%21 = call double (...) @drand2()
%22 = fmul double %20, %21
store double %22, double* %7
store i32 1, i32* %9
%23 = load double, double* %3
%24 = load i32, i32* %9
%25 = sitofp i32 %24 to double
%26 = fdiv double %23, %25
%27 = load double, double* %8
%28 = fmul double %27, %26
store double %28, double* %8
br label %29
29: ; preds = %56, %18
%30 = load double, double* %7
%31 = load double, double* %8
%32 = fcmp olt double %30, %31
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* %9
%35 = sitofp i32 %34 to double
store double %35, double* %2
br label %57
36: ; preds = %29
%37 = load double, double* %8
%38 = load double, double* %7
%39 = fsub double %38, %37
store double %39, double* %7
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
%42 = load double, double* %3
%43 = load i32, i32* %9
%44 = sitofp i32 %43 to double
%45 = fdiv double %42, %44
%46 = load double, double* %8
%47 = fmul double %46, %45
store double %47, double* %8
%48 = load i32, i32* %9
%49 = icmp sge i32 %48, 100
br i1 %49, label %50, label %56
50: ; preds = %36
%51 = load i32, i32* @stderr
%52 = load double, double* %3
%53 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), double %52)
%54 = load i32, i32* %9
%55 = sitofp i32 %54 to double
store double %55, double* %2
br label %57
56: ; preds = %36
br label %29
57: ; preds = %50, %33, %17
%58 = load double, double* %2
ret double %58
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"(ranpoiss1) bug? xm: %12.6f\0A\00"
define double @ranpoiss1(double %0) {
%2 = fneg double %0
%3 = tail call double @exp(double %2)
%4 = fsub double 1.000000e+00, %3
%5 = fcmp oeq double %4, 0.000000e+00
br i1 %5, label %36, label %6
6: ; preds = %1
%7 = tail call double (...) @drand2()
%8 = fmul double %4, %7
%9 = fmul double %3, %0
br label %10
10: ; preds = %27, %6
%11 = phi double [ %8, %6 ], [ %28, %27 ]
%12 = phi double [ %9, %6 ], [ %32, %27 ]
%13 = phi i32 [ 1, %6 ], [ %29, %27 ]
%14 = fcmp olt double %11, %12
br i1 %14, label %15, label %18
15: ; preds = %25, %10
%16 = phi i32 [ %13, %10 ], [ %20, %25 ]
%17 = sitofp i32 %16 to double
br label %36
18: ; preds = %10
%19 = fsub double %11, %12
%20 = add nuw nsw i32 %13, 1
%21 = sitofp i32 %20 to double
%22 = fdiv double %0, %21
%23 = fmul double %12, %22
%24 = icmp eq i32 %20, 100
br i1 %24, label %33, label %25
25: ; preds = %18
%26 = fcmp olt double %19, %23
br i1 %26, label %15, label %27
27: ; preds = %25
%28 = fsub double %19, %23
%29 = add nuw nsw i32 %13, 2
%30 = sitofp i32 %29 to double
%31 = fdiv double %0, %30
%32 = fmul double %23, %31
br label %10
33: ; preds = %18
%34 = load i32, i32* @stderr
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), double %0)
br label %36
36: ; preds = %1, %33, %15
%37 = phi double [ %17, %15 ], [ %21, %33 ], [ 1.000000e+00, %1 ]
ret double %37
}