#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 */
int log(double);
double rng_dbl();
double sqrt(int);
double gaussrand(void) {
  static double V1, V2, S;
  static int phase = 0;
  double X;
  if (phase == 0) {
    do {
      double U1 = rng_dbl();
      double U2 = rng_dbl();
      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    125b <gaussrand+0x12b>
jmp    114a <gaussrand+0x1a>
mov    $0x0,%al
call   1030 <rng_dbl@plt>
movsd  %xmm0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <rng_dbl@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
addsd  %xmm0,%xmm0
movsd  0xe8d(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2ec1(%rip)
movsd  -0x18(%rbp),%xmm0
addsd  %xmm0,%xmm0
movsd  0xe70(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2eac(%rip)
movsd  0x2e9c(%rip),%xmm0
movsd  0x2e94(%rip),%xmm2
movsd  0x2e94(%rip),%xmm1
mulsd  0x2e8c(%rip),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2e84(%rip)
movsd  0x2e7c(%rip),%xmm0
mov    $0x1,%al
movsd  0xe2a(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x19(%rbp)
jae    1205 <gaussrand+0xd5>
movsd  0x2e5d(%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  0x2e28(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0x2e2b(%rip),%xmm0
call   1040 <log@plt>
imul   $0xfffffffe,%eax,%eax
cvtsi2sd %eax,%xmm0
divsd  0x2e14(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a1 <gaussrand+0x171>
movsd  0x2de5(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0x2de0(%rip),%xmm0
call   1040 <log@plt>
imul   $0xfffffffe,%eax,%eax
cvtsi2sd %eax,%xmm0
divsd  0x2dc9(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    $0x1,%eax
sub    0x2dac(%rip),%eax
mov    %eax,0x2da6(%rip)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gaussrand>:
push   %rax
cmpl   $0x0,0x2f18(%rip)
je     1160 <gaussrand+0x30>
movsd  0x2efe(%rip),%xmm0
movsd  %xmm0,(%rsp)
movsd  0x2ef9(%rip),%xmm0
jmp    11cf <gaussrand+0x9f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <rng_dbl@plt>
movsd  %xmm0,(%rsp)
xor    %eax,%eax
call   1030 <rng_dbl@plt>
movapd %xmm0,%xmm1
movsd  (%rsp),%xmm2
addsd  %xmm2,%xmm2
movsd  0xe78(%rip),%xmm0
addsd  %xmm0,%xmm2
addsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x2ea4(%rip)
mulsd  %xmm1,%xmm1
movsd  %xmm2,(%rsp)
movapd %xmm2,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2e8f(%rip)
ucomisd 0xe47(%rip),%xmm0
jae    1160 <gaussrand+0x30>
ucomisd 0xe45(%rip),%xmm0
jne    11cf <gaussrand+0x9f>
jnp    1160 <gaussrand+0x30>
call   1040 <log@plt>
add    %eax,%eax
neg    %eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
divsd  0x2e61(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <sqrt@plt>
mulsd  (%rsp),%xmm0
mov    $0x1,%eax
sub    0x2e50(%rip),%eax
mov    %eax,0x2e4a(%rip)
pop    %rax
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
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 %39
6: ; preds = %0
br label %7
7: ; preds = %26, %6
%8 = call double (...) @rng_dbl()
store double %8, double* %2
%9 = call double (...) @rng_dbl()
store double %9, double* %3
%10 = load double, double* %2
%11 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %10, double -1.000000e+00)
store double %11, double* @gaussrand.V1
%12 = load double, double* %3
%13 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %12, double -1.000000e+00)
store double %13, double* @gaussrand.V2
%14 = load double, double* @gaussrand.V1
%15 = load double, double* @gaussrand.V1
%16 = load double, double* @gaussrand.V2
%17 = load double, double* @gaussrand.V2
%18 = fmul double %16, %17
%19 = call double @llvm.fmuladd.f64(double %14, double %15, double %18)
store double %19, double* @gaussrand.S
br label %20
20: ; preds = %7
%21 = load double, double* @gaussrand.S
%22 = fcmp oge double %21, 1.000000e+00
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load double, double* @gaussrand.S
%25 = fcmp oeq double %24, 0.000000e+00
br label %26
26: ; preds = %23, %20
%27 = phi i1 [ true, %20 ], [ %25, %23 ]
br i1 %27, label %7, label %28
28: ; preds = %26
%29 = load double, double* @gaussrand.V1
%30 = load double, double* @gaussrand.S
%31 = call i32 @log(double %30)
%32 = mul nsw i32 -2, %31
%33 = sitofp i32 %32 to double
%34 = load double, double* @gaussrand.S
%35 = fdiv double %33, %34
%36 = fptosi double %35 to i32
%37 = call double @sqrt(i32 %36)
%38 = fmul double %29, %37
store double %38, double* %1
br label %50
39: ; preds = %0
%40 = load double, double* @gaussrand.V2
%41 = load double, double* @gaussrand.S
%42 = call i32 @log(double %41)
%43 = mul nsw i32 -2, %42
%44 = sitofp i32 %43 to double
%45 = load double, double* @gaussrand.S
%46 = fdiv double %44, %45
%47 = fptosi double %46 to i32
%48 = call double @sqrt(i32 %47)
%49 = fmul double %40, %48
store double %49, double* %1
br label %50
50: ; preds = %39, %28
%51 = load i32, i32* @gaussrand.phase
%52 = sub nsw i32 1, %51
store i32 %52, i32* @gaussrand.phase
%53 = load double, double* %1
ret double %53
}

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
define double @gaussrand() {
%1 = load i32, i32* @gaussrand.phase
%2 = icmp eq i32 %1, 0
br i1 %2, label %3, label %13
3: ; preds = %0, %3
%4 = tail call double (...) @rng_dbl()
%5 = tail call double (...) @rng_dbl()
%6 = tail call double @llvm.fmuladd.f64(double %4, double 2.000000e+00, double -1.000000e+00)
%7 = tail call double @llvm.fmuladd.f64(double %5, double 2.000000e+00, double -1.000000e+00)
store double %7, double* @gaussrand.V2
%8 = fmul double %7, %7
%9 = tail call double @llvm.fmuladd.f64(double %6, double %6, double %8)
store double %9, double* @gaussrand.S
%10 = fcmp oge double %9, 1.000000e+00
%11 = fcmp oeq double %9, 0.000000e+00
%12 = or i1 %10, %11
br i1 %12, label %3, label %16
13: ; preds = %0
%14 = load double, double* @gaussrand.V2
%15 = load double, double* @gaussrand.S
br label %16
16: ; preds = %3, %13
%17 = phi double [ %15, %13 ], [ %9, %3 ]
%18 = phi double [ %14, %13 ], [ %6, %3 ]
%19 = tail call i32 @log(double %17)
%20 = mul nsw i32 %19, -2
%21 = sitofp i32 %20 to double
%22 = load double, double* @gaussrand.S
%23 = fdiv double %21, %22
%24 = fptosi double %23 to i32
%25 = tail call double @sqrt(i32 %24)
%26 = fmul double %18, %25
%27 = load i32, i32* @gaussrand.phase
%28 = sub nsw i32 1, %27
store i32 %28, i32* @gaussrand.phase
ret double %26
}