float random_float_unit_gaussian(void) {
  float r, u, v, fac;
  static _Bool set = (0 != 0);
  static float dset;
  if (set) {
    set = (0 != 0);
    return dset;
  }
  do {
    u = 2.0f * random_float_unit_uniform() - 1.0f;
    v = 2.0f * random_float_unit_uniform() - 1.0f;
    r = u * u + v * v;
  } while (r >= 1.0);
  fac = (float)sqrt(-2.0 * log(r) / r);
  dset = v * fac;
  return u * fac;
}


x86 assembly (clang, -O0):
<random_float_unit_gaussian>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
testb  $0x1,0x2efd(%rip)
je     115e <random_float_unit_gaussian+0x2e>
movb   $0x0,0x2ef0(%rip)
movss  0x2eec(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    1238 <random_float_unit_gaussian+0x108>
jmp    1163 <random_float_unit_gaussian+0x33>
mov    $0x0,%al
call   1030 <random_float_unit_uniform@plt>
cvtsi2ss %eax,%xmm0
addss  %xmm0,%xmm0
movss  0xe86(%rip),%xmm1
addss  %xmm1,%xmm0
movss  %xmm0,-0xc(%rbp)
mov    $0x0,%al
call   1030 <random_float_unit_uniform@plt>
cvtsi2ss %eax,%xmm0
addss  %xmm0,%xmm0
movss  0xe66(%rip),%xmm1
addss  %xmm1,%xmm0
movss  %xmm0,-0x10(%rbp)
movss  -0xc(%rbp),%xmm0
movss  -0xc(%rbp),%xmm2
movss  -0x10(%rbp),%xmm1
mulss  -0x10(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x8(%rbp)
movss  -0x8(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe33(%rip),%xmm1
ucomisd %xmm1,%xmm0
jae    1163 <random_float_unit_gaussian+0x33>
movss  -0x8(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
call   1040 <log@plt>
movaps %xmm0,%xmm1
movsd  0xe18(%rip),%xmm0
mulsd  %xmm1,%xmm0
movss  -0x8(%rbp),%xmm1
cvtss2sd %xmm1,%xmm1
divsd  %xmm1,%xmm0
call   1050 <sqrt@plt>
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x14(%rbp)
movss  -0x10(%rbp),%xmm0
mulss  -0x14(%rbp),%xmm0
movss  %xmm0,0x2e17(%rip)
movss  -0xc(%rbp),%xmm0
mulss  -0x14(%rbp),%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_float_unit_gaussian>:
sub    $0x18,%rsp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1030 <random_float_unit_uniform@plt>
xorps  %xmm1,%xmm1
cvtsi2ss %eax,%xmm1
addss  %xmm1,%xmm1
movss  0xea6(%rip),%xmm0
addss  %xmm0,%xmm1
movss  %xmm1,0xc(%rsp)
xor    %eax,%eax
call   1030 <random_float_unit_uniform@plt>
movss  0xc(%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2ss %eax,%xmm1
addss  %xmm1,%xmm1
addss  0xe7c(%rip),%xmm1
mulss  %xmm1,%xmm1
mulss  %xmm0,%xmm0
addss  %xmm1,%xmm0
ucomiss 0xe6d(%rip),%xmm0
jae    1140 <random_float_unit_gaussian+0x10>
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,0x10(%rsp)
call   1040 <log@plt>
mulsd  0xe58(%rip),%xmm0
divsd  0x10(%rsp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     11c5 <random_float_unit_gaussian+0x95>
sqrtsd %xmm0,%xmm0
jmp    11ca <random_float_unit_gaussian+0x9a>
call   1050 <sqrt@plt>
movss  0xc(%rsp),%xmm1
cvtsd2ss %xmm0,%xmm0
mulss  %xmm0,%xmm1
movaps %xmm1,%xmm0
add    $0x18,%rsp
ret

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

LLVM IR (-O2):
define float @random_float_unit_gaussian() {
br label %1
1: ; preds = %0, %1
%2 = tail call i32 (...) @random_float_unit_uniform()
%3 = sitofp i32 %2 to float
%4 = tail call float @llvm.fmuladd.f32(float %3, float 2.000000e+00, float -1.000000e+00)
%5 = tail call i32 (...) @random_float_unit_uniform()
%6 = sitofp i32 %5 to float
%7 = tail call float @llvm.fmuladd.f32(float %6, float 2.000000e+00, float -1.000000e+00)
%8 = fmul float %7, %7
%9 = tail call float @llvm.fmuladd.f32(float %4, float %4, float %8)
%10 = fcmp ult float %9, 1.000000e+00
br i1 %10, label %11, label %1
11: ; preds = %1
%12 = fpext float %9 to double
%13 = tail call double @log(double %12)
%14 = fmul double %13, -2.000000e+00
%15 = fdiv double %14, %12
%16 = tail call double @sqrt(double %15)
%17 = fptrunc double %16 to float
%18 = fmul float %4, %17
ret float %18
}