double gasdev(idum)
int *idum;
{
  static int iset = 0;
  static double gset;
  double fac, r, v1, v2;
  if (iset == 0) {
    do {
      v1 = 2.0 * ran1(idum) - 1.0;
      v2 = 2.0 * ran1(idum) - 1.0;
      r = v1 * v1 + v2 * v2;
    } while (r >= 1.0);
    fac = sqrt(-2.0 * log(r) / r);
    gset = v1 * fac;
    iset = 1;
    return v2 * fac;
  } else {
    iset = 0;
    return gset;
  }
}


x86 assembly (clang, -O0):
<gasdev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpl   $0x0,0x2efd(%rip)
jne    1226 <gasdev+0xf6>
jmp    114e <gasdev+0x1e>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1030 <ran1@plt>
cvtsi2sd %eax,%xmm0
addsd  %xmm0,%xmm0
movsd  0xe97(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1030 <ran1@plt>
cvtsi2sd %eax,%xmm0
addsd  %xmm0,%xmm0
movsd  0xe73(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm2
movsd  -0x30(%rbp),%xmm1
mulsd  -0x30(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  0xe44(%rip),%xmm1
ucomisd %xmm1,%xmm0
jae    114e <gasdev+0x1e>
movsd  -0x20(%rbp),%xmm0
call   1040 <log@plt>
movaps %xmm0,%xmm1
movsd  0xe2d(%rip),%xmm0
mulsd  %xmm1,%xmm0
divsd  -0x20(%rbp),%xmm0
call   1050 <sqrt@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x28(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,0x2e40(%rip)
movl   $0x1,0x2e2e(%rip)
movsd  -0x30(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    123d <gasdev+0x10d>
movl   $0x0,0x2e10(%rip)
movsd  0x2e10(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gasdev>:
push   %rbx
sub    $0x20,%rsp
cmpb   $0x0,0x2f04(%rip)
je     1152 <gasdev+0x22>
movb   $0x0,0x2efb(%rip)
movsd  0x2efb(%rip),%xmm1
jmp    121b <gasdev+0xeb>
mov    %rdi,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <ran1@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
addsd  %xmm1,%xmm1
movsd  0xe83(%rip),%xmm0
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <ran1@plt>
movsd  0x8(%rsp),%xmm0
xorps  %xmm2,%xmm2
cvtsi2sd %eax,%xmm2
addsd  %xmm2,%xmm2
addsd  0xe56(%rip),%xmm2
movapd %xmm2,%xmm3
mulsd  %xmm2,%xmm3
mulsd  %xmm0,%xmm0
addsd  %xmm3,%xmm0
ucomisd 0xe46(%rip),%xmm0
jae    1160 <gasdev+0x30>
movsd  %xmm2,0x18(%rsp)
movsd  %xmm0,0x10(%rsp)
call   1040 <log@plt>
mulsd  0xe33(%rip),%xmm0
divsd  0x10(%rsp),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     11f3 <gasdev+0xc3>
sqrtsd %xmm0,%xmm0
jmp    11f8 <gasdev+0xc8>
call   1050 <sqrt@plt>
movsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm1,0x2e3e(%rip)
movb   $0x1,0x2e2f(%rip)
movsd  0x18(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x20,%rsp
pop    %rbx
ret

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

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