#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);
double rand_double();
double sqrt(int);
double rand_gasdev() {
  static int iset = 0;
  static float gset;
  double fac, rsq, v1, v2;
  if (iset == 0) {
    do {
      v1 = 2.0 * rand_double() - 1.0;
      v2 = 2.0 * rand_double() - 1.0;
      rsq = v1 * v1 + v2 * v2;
    } while ((rsq >= 1.0) || (rsq == 0.0));
    fac = sqrt(-2 * log(rsq) / rsq);
    gset = v1 * fac;
    iset = 1;
    return v2 * fac;
  } else {
    iset = 0;
    return gset;
  }
}


x86 assembly (clang, -O0):
<rand_gasdev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
cmpl   $0x0,0x2efd(%rip)
jne    123c <rand_gasdev+0x10c>
jmp    114a <rand_gasdev+0x1a>
mov    $0x0,%al
call   1030 <rand_double@plt>
addsd  %xmm0,%xmm0
movsd  0xea3(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    $0x0,%al
call   1030 <rand_double@plt>
addsd  %xmm0,%xmm0
movsd  0xe87(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
mov    $0x1,%al
movsd  0xe56(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x29(%rbp)
jae    11d6 <rand_gasdev+0xa6>
movsd  -0x18(%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    114a <rand_gasdev+0x1a>
movsd  -0x18(%rbp),%xmm0
call   1040 <log@plt>
imul   $0xfffffffe,%eax,%eax
cvtsi2sd %eax,%xmm0
divsd  -0x18(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <sqrt@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x20(%rbp),%xmm0
mulsd  -0x10(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,0x2e22(%rip)
movl   $0x1,0x2e14(%rip)
movsd  -0x28(%rbp),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1257 <rand_gasdev+0x127>
movl   $0x0,0x2df6(%rip)
movss  0x2df2(%rip),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_gasdev>:
sub    $0x18,%rsp
cmpb   $0x0,0x2f01(%rip)
je     1160 <rand_gasdev+0x30>
movb   $0x0,0x2ef8(%rip)
movss  0x2ef4(%rip),%xmm0
cvtss2sd %xmm0,%xmm1
jmp    1213 <rand_gasdev+0xe3>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1030 <rand_double@plt>
movapd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe89(%rip),%xmm0
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
xor    %eax,%eax
call   1030 <rand_double@plt>
movapd %xmm0,%xmm1
addsd  %xmm0,%xmm1
addsd  0xe69(%rip),%xmm1
movapd %xmm1,%xmm2
mulsd  %xmm1,%xmm2
movsd  (%rsp),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm2,%xmm0
ucomisd 0xe54(%rip),%xmm0
jae    1160 <rand_gasdev+0x30>
ucomisd 0xe52(%rip),%xmm0
jne    11c2 <rand_gasdev+0x92>
jnp    1160 <rand_gasdev+0x30>
movsd  %xmm1,0x10(%rsp)
movsd  %xmm0,0x8(%rsp)
call   1040 <log@plt>
add    %eax,%eax
neg    %eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
divsd  0x8(%rsp),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <sqrt@plt>
movsd  (%rsp),%xmm1
mulsd  %xmm0,%xmm1
cvtsd2ss %xmm1,%xmm1
movss  %xmm1,0x2e3e(%rip)
movsd  0x10(%rsp),%xmm1
movb   $0x1,0x2e2d(%rip)
mulsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x18,%rsp
ret

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

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