#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);
int rnd(unsigned long *);
double sqrt(int);
double nrandom(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    1247 <nrandom+0x117>
movl   $0x1,0x2eed(%rip)
mov    -0x10(%rbp),%rdi
call   1030 <rnd@plt>
shl    $0x1,%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x2eda(%rip)
mov    -0x10(%rbp),%rdi
call   1030 <rnd@plt>
shl    $0x1,%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x2ec7(%rip)
movsd  0x2eb7(%rip),%xmm0
movsd  0x2eaf(%rip),%xmm2
movsd  0x2eaf(%rip),%xmm1
mulsd  0x2ea7(%rip),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2e9f(%rip)
movsd  0x2e97(%rip),%xmm0
mov    $0x1,%al
movsd  0xe35(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x11(%rbp)
ja     11f2 <nrandom+0xc2>
movsd  0x2e78(%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  0x2e53(%rip),%xmm0
call   1040 <log@plt>
imul   $0xfffffffe,%eax,%eax
cvtsi2sd %eax,%xmm0
divsd  0x2e3c(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <sqrt@plt>
movsd  %xmm0,0x2e2b(%rip)
movsd  0x2e13(%rip),%xmm0
mulsd  0x2e1b(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1266 <nrandom+0x136>
movl   $0x0,0x2def(%rip)
movsd  0x2df7(%rip),%xmm0
mulsd  0x2df7(%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    11fa <nrandom+0xca>
mov    %rdi,%rbx
movb   $0x1,0x2ee1(%rip)
nop
mov    %rbx,%rdi
call   1030 <rnd@plt>
add    %eax,%eax
add    $0xffffffff,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x2ecc(%rip)
mov    %rbx,%rdi
call   1030 <rnd@plt>
add    %eax,%eax
add    $0xffffffff,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
movsd  %xmm1,0x2eb8(%rip)
movsd  0x2ea8(%rip),%xmm0
mulsd  %xmm1,%xmm1
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2ea4(%rip)
ucomisd 0xe44(%rip),%xmm0
ja     1160 <nrandom+0x30>
ucomisd 0xe42(%rip),%xmm0
jne    11ca <nrandom+0x9a>
jnp    1160 <nrandom+0x30>
call   1040 <log@plt>
add    %eax,%eax
neg    %eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
divsd  0x2e76(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <sqrt@plt>
movsd  %xmm0,0x2e65(%rip)
lea    0x2e4e(%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 %44
6: ; preds = %1
store i32 1, i32* @nrandom.sw
br label %7
7: ; preds = %30, %6
%8 = load i64*, i64** %3
%9 = call i32 @rnd(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 @rnd(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 i32 @log(double %33)
%35 = mul nsw i32 -2, %34
%36 = sitofp i32 %35 to double
%37 = load double, double* @nrandom.s
%38 = fdiv double %36, %37
%39 = fptosi double %38 to i32
%40 = call double @sqrt(i32 %39)
store double %40, double* @nrandom.s
%41 = load double, double* @nrandom.r1
%42 = load double, double* @nrandom.s
%43 = fmul double %41, %42
store double %43, double* %2
br label %48
44: ; preds = %1
store i32 0, i32* @nrandom.sw
%45 = load double, double* @nrandom.r2
%46 = load double, double* @nrandom.s
%47 = fmul double %45, %46
store double %47, double* %2
br label %48
48: ; preds = %44, %32
%49 = load double, double* %2
ret double %49
}

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 %27, label %3
3: ; preds = %1
store i1 true, i1* @nrandom.sw
br label %4
4: ; preds = %4, %3
%5 = tail call i32 @rnd(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 @rnd(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 i32 @log(double %15)
%21 = mul nsw i32 %20, -2
%22 = sitofp i32 %21 to double
%23 = load double, double* @nrandom.s
%24 = fdiv double %22, %23
%25 = fptosi double %24 to i32
%26 = tail call double @sqrt(i32 %25)
store double %26, double* @nrandom.s
br label %29
27: ; preds = %1
store i1 false, i1* @nrandom.sw
%28 = load double, double* @nrandom.r2
br label %29
29: ; preds = %27, %19
%30 = phi double* [ @nrandom.s, %27 ], [ @nrandom.r1, %19 ]
%31 = phi double [ %28, %27 ], [ %26, %19 ]
%32 = load double, double* %30
%33 = fmul double %31, %32
ret double %33
}