double gauss() {
  static int iset = 0;
  static double gset;
  double v1, v2, rsq, fac;
  if (iset == 1) {
    iset = 0;
    return gset;
  }
  do {
    v1 = 2.0 * (drand2()) - 1.0;
    v2 = 2.0 * (drand2()) - 1.0;
    rsq = v1 * v1 + v2 * v2;
  } while (rsq >= 1.0 || rsq == 0.0);
  fac = sqrt(-2.0 * log(rsq) / rsq);
  gset = v1 * fac;
  iset = 1;
  return v2 * fac;
}


x86 assembly (clang, -O0):
<gauss>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
cmpl   $0x1,0x2f01(%rip)
jne    1161 <gauss+0x31>
movl   $0x0,0x2ef1(%rip)
movsd  0x2ef1(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1258 <gauss+0x128>
jmp    1166 <gauss+0x36>
mov    $0x0,%al
call   1030 <drand2@plt>
cvtsi2sd %eax,%xmm0
addsd  %xmm0,%xmm0
movsd  0xe83(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <drand2@plt>
cvtsi2sd %eax,%xmm0
addsd  %xmm0,%xmm0
movsd  0xe63(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
movsd  -0x18(%rbp),%xmm1
mulsd  -0x18(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
mov    $0x1,%al
movsd  0xe32(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x29(%rbp)
jae    11fa <gauss+0xca>
movsd  -0x20(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
sete   %al
setnp  %cl
and    %cl,%al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1166 <gauss+0x36>
movsd  -0x20(%rbp),%xmm0
call   1040 <log@plt>
movaps %xmm0,%xmm1
movsd  0xdf6(%rip),%xmm0
mulsd  %xmm1,%xmm0
divsd  -0x20(%rbp),%xmm0
call   1050 <sqrt@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  %xmm0,0x2e09(%rip)
movl   $0x1,0x2df7(%rip)
movsd  -0x18(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gauss>:
sub    $0x18,%rsp
cmpb   $0x0,0x2f05(%rip)
je     1160 <gauss+0x30>
movb   $0x0,0x2efc(%rip)
movsd  0x2efc(%rip),%xmm1
jmp    121e <gauss+0xee>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <drand2@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
addsd  %xmm1,%xmm1
movsd  0xe86(%rip),%xmm0
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
xor    %eax,%eax
call   1030 <drand2@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
addsd  %xmm1,%xmm1
addsd  0xe63(%rip),%xmm1
movapd %xmm1,%xmm2
mulsd  %xmm1,%xmm2
movsd  (%rsp),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm2,%xmm0
ucomisd 0xe4e(%rip),%xmm0
jae    1160 <gauss+0x30>
ucomisd 0xe54(%rip),%xmm0
jne    11c8 <gauss+0x98>
jnp    1160 <gauss+0x30>
movsd  %xmm1,0x10(%rsp)
movsd  %xmm0,0x8(%rsp)
call   1040 <log@plt>
mulsd  0xe2f(%rip),%xmm0
divsd  0x8(%rsp),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     11f7 <gauss+0xc7>
sqrtsd %xmm0,%xmm0
jmp    11fc <gauss+0xcc>
call   1050 <sqrt@plt>
movsd  (%rsp),%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm1,0x2e3b(%rip)
movb   $0x1,0x2e2c(%rip)
movsd  0x10(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x18,%rsp
ret

LLVM IR (-O0):
@gauss.iset = internal global i32 0
@gauss.gset = internal global double 0.000000e+00
define double @gauss() {
%1 = alloca double
%2 = alloca double
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = load i32, i32* @gauss.iset
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %10
8: ; preds = %0
store i32 0, i32* @gauss.iset
%9 = load double, double* @gauss.gset
store double %9, double* %1
br label %45
10: ; preds = %0
br label %11
11: ; preds = %30, %10
%12 = call i32 (...) @drand2()
%13 = sitofp i32 %12 to double
%14 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %13, double -1.000000e+00)
store double %14, double* %2
%15 = call i32 (...) @drand2()
%16 = sitofp i32 %15 to double
%17 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %16, double -1.000000e+00)
store double %17, double* %3
%18 = load double, double* %2
%19 = load double, double* %2
%20 = load double, double* %3
%21 = load double, double* %3
%22 = fmul double %20, %21
%23 = call double @llvm.fmuladd.f64(double %18, double %19, double %22)
store double %23, double* %4
br label %24
24: ; preds = %11
%25 = load double, double* %4
%26 = fcmp oge double %25, 1.000000e+00
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load double, double* %4
%29 = fcmp oeq double %28, 0.000000e+00
br label %30
30: ; preds = %27, %24
%31 = phi i1 [ true, %24 ], [ %29, %27 ]
br i1 %31, label %11, label %32
32: ; preds = %30
%33 = load double, double* %4
%34 = call double @log(double %33)
%35 = fmul double -2.000000e+00, %34
%36 = load double, double* %4
%37 = fdiv double %35, %36
%38 = call double @sqrt(double %37)
store double %38, double* %5
%39 = load double, double* %2
%40 = load double, double* %5
%41 = fmul double %39, %40
store double %41, double* @gauss.gset
store i32 1, i32* @gauss.iset
%42 = load double, double* %3
%43 = load double, double* %5
%44 = fmul double %42, %43
store double %44, double* %1
br label %45
45: ; preds = %32, %8
%46 = load double, double* %1
ret double %46
}

LLVM IR (-O2):
@gauss.iset = internal unnamed_addr global i1 false
@gauss.gset = internal unnamed_addr global double 0.000000e+00
define double @gauss() {
%1 = load i1, i1* @gauss.iset
br i1 %1, label %2, label %4
2: ; preds = %0
store i1 false, i1* @gauss.iset
%3 = load double, double* @gauss.gset
br label %23
4: ; preds = %0, %4
%5 = tail call i32 (...) @drand2()
%6 = sitofp i32 %5 to double
%7 = tail call double @llvm.fmuladd.f64(double %6, double 2.000000e+00, double -1.000000e+00)
%8 = tail call i32 (...) @drand2()
%9 = sitofp i32 %8 to double
%10 = tail call double @llvm.fmuladd.f64(double %9, double 2.000000e+00, double -1.000000e+00)
%11 = fmul double %10, %10
%12 = tail call double @llvm.fmuladd.f64(double %7, double %7, double %11)
%13 = fcmp oge double %12, 1.000000e+00
%14 = fcmp oeq double %12, 0.000000e+00
%15 = or i1 %13, %14
br i1 %15, label %4, label %16
16: ; preds = %4
%17 = tail call double @log(double %12)
%18 = fmul double %17, -2.000000e+00
%19 = fdiv double %18, %12
%20 = tail call double @sqrt(double %19)
%21 = fmul double %7, %20
store double %21, double* @gauss.gset
store i1 true, i1* @gauss.iset
%22 = fmul double %10, %20
br label %23
23: ; preds = %16, %2
%24 = phi double [ %3, %2 ], [ %22, %16 ]
ret double %24
}