#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 rand01();
double sqrt(int);
double randnorm(void) {
  static double v1, v2, s;
  static int phase = 0;
  double X, u1, u2;
  if (phase == 0) {
    do {
      u1 = rand01();
      u2 = rand01();
      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):
<randnorm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
cmpl   $0x0,0x2f19(%rip)
jne    125b <randnorm+0x12b>
jmp    114a <randnorm+0x1a>
mov    $0x0,%al
call   1030 <rand01@plt>
movsd  %xmm0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <rand01@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 <randnorm+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 <randnorm+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 <randnorm+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):
<randnorm>:
push   %rax
cmpl   $0x0,0x2f18(%rip)
je     1160 <randnorm+0x30>
movsd  0x2efe(%rip),%xmm0
movsd  %xmm0,(%rsp)
movsd  0x2ef9(%rip),%xmm0
jmp    11cf <randnorm+0x9f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <rand01@plt>
movsd  %xmm0,(%rsp)
xor    %eax,%eax
call   1030 <rand01@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 <randnorm+0x30>
ucomisd 0xe45(%rip),%xmm0
jne    11cf <randnorm+0x9f>
jnp    1160 <randnorm+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):
@randnorm.v1 = internal global double 0.000000e+00
@randnorm.v2 = internal global double 0.000000e+00
@randnorm.s = internal global double 0.000000e+00
@randnorm.phase = internal global i32 0
define double @randnorm() {
%1 = alloca double
%2 = alloca double
%3 = alloca double
%4 = load i32, i32* @randnorm.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 (...) @rand01()
store double %8, double* %2
%9 = call double (...) @rand01()
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* @randnorm.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* @randnorm.v2
%14 = load double, double* @randnorm.v1
%15 = load double, double* @randnorm.v1
%16 = load double, double* @randnorm.v2
%17 = load double, double* @randnorm.v2
%18 = fmul double %16, %17
%19 = call double @llvm.fmuladd.f64(double %14, double %15, double %18)
store double %19, double* @randnorm.s
br label %20
20: ; preds = %7
%21 = load double, double* @randnorm.s
%22 = fcmp oge double %21, 1.000000e+00
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load double, double* @randnorm.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* @randnorm.v1
%30 = load double, double* @randnorm.s
%31 = call i32 @log(double %30)
%32 = mul nsw i32 -2, %31
%33 = sitofp i32 %32 to double
%34 = load double, double* @randnorm.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* @randnorm.v2
%41 = load double, double* @randnorm.s
%42 = call i32 @log(double %41)
%43 = mul nsw i32 -2, %42
%44 = sitofp i32 %43 to double
%45 = load double, double* @randnorm.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* @randnorm.phase
%52 = sub nsw i32 1, %51
store i32 %52, i32* @randnorm.phase
%53 = load double, double* %1
ret double %53
}

LLVM IR (-O2):
@randnorm.v2 = internal unnamed_addr global double 0.000000e+00
@randnorm.s = internal unnamed_addr global double 0.000000e+00
@randnorm.phase = internal unnamed_addr global i32 0
define double @randnorm() {
%1 = load i32, i32* @randnorm.phase
%2 = icmp eq i32 %1, 0
br i1 %2, label %3, label %13
3: ; preds = %0, %3
%4 = tail call double (...) @rand01()
%5 = tail call double (...) @rand01()
%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* @randnorm.v2
%8 = fmul double %7, %7
%9 = tail call double @llvm.fmuladd.f64(double %6, double %6, double %8)
store double %9, double* @randnorm.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* @randnorm.v2
%15 = load double, double* @randnorm.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* @randnorm.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* @randnorm.phase
%28 = sub nsw i32 1, %27
store i32 %28, i32* @randnorm.phase
ret double %26
}