double nrandom(next)
unsigned long *next;
{
  static int sw = 0;
  static double r1, r2, s;
  if (sw == 0) {
    sw = 1;
    do {
      r1 = 2 * rnd(next) - 1;
      r2 = 2 * rnd(next) - 1;
      s = r1 * r1 + r2 * r2;
    } while (s > 1 || s == 0);
    s = sqrt(-2 * log(s) / s);
    return (r1 * s);
  } else {
    sw = 0;
    return (r2 * s);
  }
}


x86 assembly (clang, -O0):
<nrandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpl   $0x0,0x2efd(%rip)
jne    124c <nrandom+0x11c>
movl   $0x1,0x2eed(%rip)
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1030 <rnd@plt>
shl    $0x1,%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x2ed8(%rip)
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1030 <rnd@plt>
shl    $0x1,%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x2ec3(%rip)
movsd  0x2eb3(%rip),%xmm0
movsd  0x2eab(%rip),%xmm2
movsd  0x2eab(%rip),%xmm1
mulsd  0x2ea3(%rip),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2e9b(%rip)
movsd  0x2e93(%rip),%xmm0
mov    $0x1,%al
movsd  0xe31(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x11(%rbp)
ja     11f6 <nrandom+0xc6>
movsd  0x2e74(%rip),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
sete   %al
setnp  %cl
and    %cl,%al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1153 <nrandom+0x23>
movsd  0x2e4f(%rip),%xmm0
call   1040 <log@plt>
movaps %xmm0,%xmm1
movsd  0xdef(%rip),%xmm0
mulsd  %xmm1,%xmm0
divsd  0x2e33(%rip),%xmm0
call   1050 <sqrt@plt>
movsd  %xmm0,0x2e26(%rip)
movsd  0x2e0e(%rip),%xmm0
mulsd  0x2e16(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    126b <nrandom+0x13b>
movl   $0x0,0x2dea(%rip)
movsd  0x2df2(%rip),%xmm0
mulsd  0x2df2(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nrandom>:
push   %rbx
cmpb   $0x0,0x2f08(%rip)
je     1155 <nrandom+0x25>
movb   $0x0,0x2eff(%rip)
lea    0x2f10(%rip),%rax
movsd  0x2f00(%rip),%xmm0
jmp    1207 <nrandom+0xd7>
mov    %rdi,%rbx
movb   $0x1,0x2ee1(%rip)
nop
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <rnd@plt>
add    %eax,%eax
add    $0xffffffff,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x2eca(%rip)
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <rnd@plt>
add    %eax,%eax
add    $0xffffffff,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
movsd  %xmm1,0x2eb4(%rip)
movsd  0x2ea4(%rip),%xmm0
mulsd  %xmm1,%xmm1
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2ea0(%rip)
ucomisd 0xe40(%rip),%xmm0
ja     1160 <nrandom+0x30>
ucomisd 0xe46(%rip),%xmm0
jne    11ce <nrandom+0x9e>
jnp    1160 <nrandom+0x30>
call   1040 <log@plt>
mulsd  0xe2d(%rip),%xmm0
divsd  0x2e75(%rip),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     11f3 <nrandom+0xc3>
sqrtsd %xmm0,%xmm0
jmp    11f8 <nrandom+0xc8>
call   1050 <sqrt@plt>
movsd  %xmm0,0x2e58(%rip)
lea    0x2e41(%rip),%rax
mulsd  (%rax),%xmm0
pop    %rbx
ret

LLVM IR (-O0):
@nrandom.sw = internal global i32 0
@nrandom.r1 = internal global double 0.000000e+00
@nrandom.r2 = internal global double 0.000000e+00
@nrandom.s = internal global double 0.000000e+00
define double @nrandom(i64* %0) {
%2 = alloca double
%3 = alloca i64*
store i64* %0, i64** %3
%4 = load i32, i32* @nrandom.sw
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %42
6: ; preds = %1
store i32 1, i32* @nrandom.sw
br label %7
7: ; preds = %30, %6
%8 = load i64*, i64** %3
%9 = call i32 (i64*, ...) bitcast (i32 (...)* @rnd to i32 (i64*, ...)*)(i64* %8)
%10 = mul nsw i32 2, %9
%11 = sub nsw i32 %10, 1
%12 = sitofp i32 %11 to double
store double %12, double* @nrandom.r1
%13 = load i64*, i64** %3
%14 = call i32 (i64*, ...) bitcast (i32 (...)* @rnd to i32 (i64*, ...)*)(i64* %13)
%15 = mul nsw i32 2, %14
%16 = sub nsw i32 %15, 1
%17 = sitofp i32 %16 to double
store double %17, double* @nrandom.r2
%18 = load double, double* @nrandom.r1
%19 = load double, double* @nrandom.r1
%20 = load double, double* @nrandom.r2
%21 = load double, double* @nrandom.r2
%22 = fmul double %20, %21
%23 = call double @llvm.fmuladd.f64(double %18, double %19, double %22)
store double %23, double* @nrandom.s
br label %24
24: ; preds = %7
%25 = load double, double* @nrandom.s
%26 = fcmp ogt double %25, 1.000000e+00
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load double, double* @nrandom.s
%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 %7, label %32
32: ; preds = %30
%33 = load double, double* @nrandom.s
%34 = call double @log(double %33)
%35 = fmul double -2.000000e+00, %34
%36 = load double, double* @nrandom.s
%37 = fdiv double %35, %36
%38 = call double @sqrt(double %37)
store double %38, double* @nrandom.s
%39 = load double, double* @nrandom.r1
%40 = load double, double* @nrandom.s
%41 = fmul double %39, %40
store double %41, double* %2
br label %46
42: ; preds = %1
store i32 0, i32* @nrandom.sw
%43 = load double, double* @nrandom.r2
%44 = load double, double* @nrandom.s
%45 = fmul double %43, %44
store double %45, double* %2
br label %46
46: ; preds = %42, %32
%47 = load double, double* %2
ret double %47
}

LLVM IR (-O2):
@nrandom.sw = internal unnamed_addr global i1 false
@nrandom.r1 = internal unnamed_addr global double 0.000000e+00
@nrandom.r2 = internal unnamed_addr global double 0.000000e+00
@nrandom.s = internal unnamed_addr global double 0.000000e+00
define double @nrandom(i64* %0) {
%2 = load i1, i1* @nrandom.sw
br i1 %2, label %25, label %3
3: ; preds = %1
store i1 true, i1* @nrandom.sw
br label %4
4: ; preds = %4, %3
%5 = tail call i32 (i64*, ...) bitcast (i32 (...)* @rnd to i32 (i64*, ...)*)(i64* %0)
%6 = shl nsw i32 %5, 1
%7 = add nsw i32 %6, -1
%8 = sitofp i32 %7 to double
store double %8, double* @nrandom.r1
%9 = tail call i32 (i64*, ...) bitcast (i32 (...)* @rnd to i32 (i64*, ...)*)(i64* %0)
%10 = shl nsw i32 %9, 1
%11 = add nsw i32 %10, -1
%12 = sitofp i32 %11 to double
store double %12, double* @nrandom.r2
%13 = load double, double* @nrandom.r1
%14 = fmul double %12, %12
%15 = tail call double @llvm.fmuladd.f64(double %13, double %13, double %14)
store double %15, double* @nrandom.s
%16 = fcmp ogt double %15, 1.000000e+00
%17 = fcmp oeq double %15, 0.000000e+00
%18 = or i1 %16, %17
br i1 %18, label %4, label %19
19: ; preds = %4
%20 = tail call double @log(double %15)
%21 = fmul double %20, -2.000000e+00
%22 = load double, double* @nrandom.s
%23 = fdiv double %21, %22
%24 = tail call double @sqrt(double %23)
store double %24, double* @nrandom.s
br label %27
25: ; preds = %1
store i1 false, i1* @nrandom.sw
%26 = load double, double* @nrandom.r2
br label %27
27: ; preds = %25, %19
%28 = phi double* [ @nrandom.s, %25 ], [ @nrandom.r1, %19 ]
%29 = phi double [ %26, %25 ], [ %24, %19 ]
%30 = load double, double* %28
%31 = fmul double %29, %30
ret double %31
}