#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 RAND_MAX;
int log(double);
scalar_t__ rand();
double sqrt(int);
double gaussrand(void) {
  static double V1, V2, S;
  static int phase;
  double X;
  if (phase == 0) {
    do {
      double U1 = (double)rand() / RAND_MAX;
      double U2 = (double)rand() / RAND_MAX;
      V1 = 2 * U1 - 1;
      V2 = 2 * U2 - 1;
      S = V1 * V1 + V2 * V2;
    } while (S >= 1 || S == 0);
    X = V1 * sqrt(-2 * log(S) / S);
  } else {
    X = V2 * sqrt(-2 * log(S) / S);
  }
  phase = 1 - phase;
  return X;
}


x86 assembly (clang, -O0):
<gaussrand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
cmpl   $0x0,0x2f19(%rip)
jne    127b <gaussrand+0x14b>
jmp    114a <gaussrand+0x1a>
mov    $0x0,%al
call   1050 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e8b(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e6f(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
addsd  %xmm0,%xmm0
movsd  0xe6d(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2ea1(%rip)
movsd  -0x18(%rbp),%xmm0
addsd  %xmm0,%xmm0
movsd  0xe50(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2e8c(%rip)
movsd  0x2e7c(%rip),%xmm0
movsd  0x2e74(%rip),%xmm2
movsd  0x2e74(%rip),%xmm1
mulsd  0x2e6c(%rip),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2e64(%rip)
movsd  0x2e5c(%rip),%xmm0
mov    $0x1,%al
movsd  0xe0a(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x19(%rbp)
jae    1225 <gaussrand+0xf5>
movsd  0x2e3d(%rip),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
sete   %al
setnp  %cl
and    %cl,%al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    114a <gaussrand+0x1a>
movsd  0x2e08(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0x2e0b(%rip),%xmm0
call   1030 <log@plt>
imul   $0xfffffffe,%eax,%eax
cvtsi2sd %eax,%xmm0
divsd  0x2df4(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1040 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12c1 <gaussrand+0x191>
movsd  0x2dc5(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0x2dc0(%rip),%xmm0
call   1030 <log@plt>
imul   $0xfffffffe,%eax,%eax
cvtsi2sd %eax,%xmm0
divsd  0x2da9(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1040 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    $0x1,%eax
sub    0x2d8c(%rip),%eax
mov    %eax,0x2d86(%rip)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gaussrand>:
push   %rbx
sub    $0x10,%rsp
cmpl   $0x0,0x2f14(%rip)
je     1159 <gaussrand+0x29>
movsd  0x2efa(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
movsd  0x2ef4(%rip),%xmm0
jmp    11ea <gaussrand+0xba>
mov    0x2e88(%rip),%rbx
xor    %eax,%eax
call   1050 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%rbx),%xmm0
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   1050 <rand@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
divsd  (%rbx),%xmm1
movsd  0x8(%rsp),%xmm2
addsd  %xmm2,%xmm2
movsd  0xe62(%rip),%xmm0
addsd  %xmm0,%xmm2
addsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x2e8e(%rip)
mulsd  %xmm1,%xmm1
movsd  %xmm2,0x8(%rsp)
movapd %xmm2,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2e78(%rip)
ucomisd 0xe30(%rip),%xmm0
jae    1160 <gaussrand+0x30>
ucomisd 0xe2e(%rip),%xmm0
jne    11ea <gaussrand+0xba>
jnp    1160 <gaussrand+0x30>
call   1030 <log@plt>
add    %eax,%eax
neg    %eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
divsd  0x2e46(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1040 <sqrt@plt>
mulsd  0x8(%rsp),%xmm0
mov    $0x1,%eax
sub    0x2e34(%rip),%eax
mov    %eax,0x2e2e(%rip)
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@gaussrand.V1 = internal global double 0.000000e+00
@gaussrand.V2 = internal global double 0.000000e+00
@gaussrand.S = internal global double 0.000000e+00
@gaussrand.phase = internal global i32 0
@RAND_MAX = global double 0.000000e+00
define double @gaussrand() {
%1 = alloca double
%2 = alloca double
%3 = alloca double
%4 = load i32, i32* @gaussrand.phase
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %45
6: ; preds = %0
br label %7
7: ; preds = %32, %6
%8 = call i64 (...) @rand()
%9 = sitofp i64 %8 to double
%10 = load double, double* @RAND_MAX
%11 = fdiv double %9, %10
store double %11, double* %2
%12 = call i64 (...) @rand()
%13 = sitofp i64 %12 to double
%14 = load double, double* @RAND_MAX
%15 = fdiv double %13, %14
store double %15, double* %3
%16 = load double, double* %2
%17 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %16, double -1.000000e+00)
store double %17, double* @gaussrand.V1
%18 = load double, double* %3
%19 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %18, double -1.000000e+00)
store double %19, double* @gaussrand.V2
%20 = load double, double* @gaussrand.V1
%21 = load double, double* @gaussrand.V1
%22 = load double, double* @gaussrand.V2
%23 = load double, double* @gaussrand.V2
%24 = fmul double %22, %23
%25 = call double @llvm.fmuladd.f64(double %20, double %21, double %24)
store double %25, double* @gaussrand.S
br label %26
26: ; preds = %7
%27 = load double, double* @gaussrand.S
%28 = fcmp oge double %27, 1.000000e+00
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load double, double* @gaussrand.S
%31 = fcmp oeq double %30, 0.000000e+00
br label %32
32: ; preds = %29, %26
%33 = phi i1 [ true, %26 ], [ %31, %29 ]
br i1 %33, label %7, label %34
34: ; preds = %32
%35 = load double, double* @gaussrand.V1
%36 = load double, double* @gaussrand.S
%37 = call i32 @log(double %36)
%38 = mul nsw i32 -2, %37
%39 = sitofp i32 %38 to double
%40 = load double, double* @gaussrand.S
%41 = fdiv double %39, %40
%42 = fptosi double %41 to i32
%43 = call double @sqrt(i32 %42)
%44 = fmul double %35, %43
store double %44, double* %1
br label %56
45: ; preds = %0
%46 = load double, double* @gaussrand.V2
%47 = load double, double* @gaussrand.S
%48 = call i32 @log(double %47)
%49 = mul nsw i32 -2, %48
%50 = sitofp i32 %49 to double
%51 = load double, double* @gaussrand.S
%52 = fdiv double %50, %51
%53 = fptosi double %52 to i32
%54 = call double @sqrt(i32 %53)
%55 = fmul double %46, %54
store double %55, double* %1
br label %56
56: ; preds = %45, %34
%57 = load i32, i32* @gaussrand.phase
%58 = sub nsw i32 1, %57
store i32 %58, i32* @gaussrand.phase
%59 = load double, double* %1
ret double %59
}

LLVM IR (-O2):
@gaussrand.V2 = internal unnamed_addr global double 0.000000e+00
@gaussrand.S = internal unnamed_addr global double 0.000000e+00
@gaussrand.phase = internal unnamed_addr global i32 0
@RAND_MAX = global double 0.000000e+00
define double @gaussrand() {
%1 = load i32, i32* @gaussrand.phase
%2 = icmp eq i32 %1, 0
br i1 %2, label %3, label %19
3: ; preds = %0, %3
%4 = tail call i64 (...) @rand()
%5 = sitofp i64 %4 to double
%6 = load double, double* @RAND_MAX
%7 = fdiv double %5, %6
%8 = tail call i64 (...) @rand()
%9 = sitofp i64 %8 to double
%10 = load double, double* @RAND_MAX
%11 = fdiv double %9, %10
%12 = tail call double @llvm.fmuladd.f64(double %7, double 2.000000e+00, double -1.000000e+00)
%13 = tail call double @llvm.fmuladd.f64(double %11, double 2.000000e+00, double -1.000000e+00)
store double %13, double* @gaussrand.V2
%14 = fmul double %13, %13
%15 = tail call double @llvm.fmuladd.f64(double %12, double %12, double %14)
store double %15, double* @gaussrand.S
%16 = fcmp oge double %15, 1.000000e+00
%17 = fcmp oeq double %15, 0.000000e+00
%18 = or i1 %16, %17
br i1 %18, label %3, label %22
19: ; preds = %0
%20 = load double, double* @gaussrand.V2
%21 = load double, double* @gaussrand.S
br label %22
22: ; preds = %3, %19
%23 = phi double [ %21, %19 ], [ %15, %3 ]
%24 = phi double [ %20, %19 ], [ %12, %3 ]
%25 = tail call i32 @log(double %23)
%26 = mul nsw i32 %25, -2
%27 = sitofp i32 %26 to double
%28 = load double, double* @gaussrand.S
%29 = fdiv double %27, %28
%30 = fptosi double %29 to i32
%31 = tail call double @sqrt(i32 %30)
%32 = fmul double %24, %31
%33 = load i32, i32* @gaussrand.phase
%34 = sub nsw i32 1, %33
store i32 %34, i32* @gaussrand.phase
ret double %32
}