#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 */
double log(double);
double sqrt(double);
double uniformrandom();
double gaussdouble(double mean, double variance) {
  static int iset = 0;
  static double gset;
  double fac, r, v1, v2;
  if (iset == 0) {
    do {
      v1 = 2.0 * uniformrandom() - 1.0;
      v2 = 2.0 * uniformrandom() - 1.0;
      r = v1 * v1 + v2 * v2;
    } while (r >= 1.0);
    fac = sqrt(-2.0 * log(r) / r);
    gset = v1 * fac;
    iset = 1;
    return (sqrt(variance) * v2 * fac + mean);
  } else {
    iset = 0;
    return (sqrt(variance) * gset + mean);
  }
}


x86 assembly (clang, -O0):
<gaussdouble>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
cmpl   $0x0,0x2ef7(%rip)
jne    1233 <gaussdouble+0x103>
jmp    1154 <gaussdouble+0x24>
mov    $0x0,%al
call   1030 <uniformrandom@plt>
addsd  %xmm0,%xmm0
movsd  0xe99(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    $0x0,%al
call   1030 <uniformrandom@plt>
addsd  %xmm0,%xmm0
movsd  0xe7d(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
movsd  -0x38(%rbp),%xmm1
mulsd  -0x38(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xe4e(%rip),%xmm1
ucomisd %xmm1,%xmm0
jae    1154 <gaussdouble+0x24>
movsd  -0x28(%rbp),%xmm0
call   1040 <log@plt>
movaps %xmm0,%xmm1
movsd  0xe37(%rip),%xmm0
mulsd  %xmm1,%xmm0
divsd  -0x28(%rbp),%xmm0
call   1050 <sqrt@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x30(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,0x2e4a(%rip)
movl   $0x1,0x2e38(%rip)
movsd  -0x18(%rbp),%xmm0
call   1050 <sqrt@plt>
mulsd  -0x38(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
movsd  -0x10(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1261 <gaussdouble+0x131>
movl   $0x0,0x2e03(%rip)
movsd  -0x18(%rbp),%xmm0
call   1050 <sqrt@plt>
movsd  0x2df9(%rip),%xmm2
movsd  -0x10(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gaussdouble>:
sub    $0x28,%rsp
movapd %xmm0,%xmm2
cmpb   $0x0,0x2f01(%rip)
je     1162 <gaussdouble+0x32>
movb   $0x0,0x2ef8(%rip)
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
jb     11f8 <gaussdouble+0xc8>
xorps  %xmm0,%xmm0
sqrtsd %xmm1,%xmm0
jmp    120d <gaussdouble+0xdd>
movsd  %xmm1,0x10(%rsp)
movsd  %xmm2,0x8(%rsp)
xchg   %ax,%ax
xor    %eax,%eax
call   1030 <uniformrandom@plt>
movapd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe79(%rip),%xmm0
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
xor    %eax,%eax
call   1030 <uniformrandom@plt>
movsd  (%rsp),%xmm3
addsd  %xmm0,%xmm0
addsd  0xe58(%rip),%xmm0
movapd %xmm0,%xmm1
mulsd  %xmm0,%xmm1
mulsd  %xmm3,%xmm3
addsd  %xmm1,%xmm3
ucomisd 0xe48(%rip),%xmm3
jae    1170 <gaussdouble+0x40>
movsd  %xmm0,0x20(%rsp)
movapd %xmm3,%xmm0
movsd  %xmm3,0x18(%rsp)
call   1040 <log@plt>
mulsd  0xe31(%rip),%xmm0
divsd  0x18(%rsp),%xmm0
xorpd  %xmm2,%xmm2
ucomisd %xmm2,%xmm0
jb     121e <gaussdouble+0xee>
xorps  %xmm3,%xmm3
sqrtsd %xmm0,%xmm3
jmp    122b <gaussdouble+0xfb>
movapd %xmm1,%xmm0
movsd  %xmm2,0x8(%rsp)
call   1050 <sqrt@plt>
movsd  0x8(%rsp),%xmm2
mulsd  0x2e33(%rip),%xmm0
addsd  %xmm2,%xmm0
add    $0x28,%rsp
ret
call   1050 <sqrt@plt>
xorpd  %xmm2,%xmm2
movapd %xmm0,%xmm3
movsd  0x10(%rsp),%xmm0
movsd  (%rsp),%xmm1
mulsd  %xmm3,%xmm1
movsd  %xmm1,0x2e06(%rip)
movb   $0x1,0x2df7(%rip)
ucomisd %xmm2,%xmm0
jb     1255 <gaussdouble+0x125>
sqrtsd %xmm0,%xmm0
jmp    1264 <gaussdouble+0x134>
movsd  %xmm3,(%rsp)
call   1050 <sqrt@plt>
movsd  (%rsp),%xmm3
movsd  0x8(%rsp),%xmm1
movsd  0x20(%rsp),%xmm2
mulsd  %xmm0,%xmm2
mulsd  %xmm3,%xmm2
addsd  %xmm1,%xmm2
movapd %xmm2,%xmm0
add    $0x28,%rsp
ret

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

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