#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 randd();
double sqrt(int);
double gaussrand() {
  static double V1, V2, S;
  static int phase = 0;
  double X;
  if (phase == 0) {
    do {
      V1 = 2 * randd() - 1;
      V2 = 2 * randd() - 1;
      S = V1 * V1 + V2 * V2;
    } while (S >= 1 || !(S < 0 || S > 0));
    X = V1 * sqrt(-2 * log(S) / S);
  } else {
    X = V2 * sqrt(-2 * log(S) / S);
  }
  phase = !phase;
  return X;
}


x86 assembly (clang, -O0):
<gaussrand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
cmpl   $0x0,0x2f19(%rip)
jne    1254 <gaussrand+0x124>
jmp    114a <gaussrand+0x1a>
mov    $0x0,%al
call   1030 <randd@plt>
shl    $0x1,%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x2edd(%rip)
mov    $0x0,%al
call   1030 <randd@plt>
shl    $0x1,%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x2ecc(%rip)
movsd  0x2ebc(%rip),%xmm0
movsd  0x2eb4(%rip),%xmm2
movsd  0x2eb4(%rip),%xmm1
mulsd  0x2eac(%rip),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2ea4(%rip)
movsd  0x2e9c(%rip),%xmm0
mov    $0x1,%al
movsd  0xe42(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x9(%rbp)
jae    11fe <gaussrand+0xce>
mov    $0x1,%al
xorps  %xmm0,%xmm0
ucomisd 0x2e78(%rip),%xmm0
mov    %al,-0xa(%rbp)
ja     11f6 <gaussrand+0xc6>
movsd  0x2e67(%rip),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
seta   %al
mov    %al,-0xa(%rbp)
mov    -0xa(%rbp),%al
xor    $0xff,%al
mov    %al,-0x9(%rbp)
mov    -0x9(%rbp),%al
test   $0x1,%al
jne    114a <gaussrand+0x1a>
movsd  0x2e2f(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0x2e32(%rip),%xmm0
call   1040 <log@plt>
imul   $0xfffffffe,%eax,%eax
cvtsi2sd %eax,%xmm0
divsd  0x2e1b(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x18(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    129a <gaussrand+0x16a>
movsd  0x2dec(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0x2de7(%rip),%xmm0
call   1040 <log@plt>
imul   $0xfffffffe,%eax,%eax
cvtsi2sd %eax,%xmm0
divsd  0x2dd0(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
cmpl   $0x0,0x2db7(%rip)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,0x2da7(%rip)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gaussrand>:
push   %rax
cmpl   $0x0,0x2f20(%rip)
je     1150 <gaussrand+0x20>
movsd  0x2f06(%rip),%xmm2
movsd  0x2f06(%rip),%xmm0
jmp    11ba <gaussrand+0x8a>
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <randd@plt>
add    %eax,%eax
add    $0xffffffff,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x2ed5(%rip)
xor    %eax,%eax
call   1030 <randd@plt>
add    %eax,%eax
add    $0xffffffff,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
movsd  %xmm1,0x2ec2(%rip)
movsd  0x2eb2(%rip),%xmm2
mulsd  %xmm1,%xmm1
movapd %xmm2,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2eaa(%rip)
ucomisd 0xe52(%rip),%xmm0
jae    1150 <gaussrand+0x20>
ucomisd 0xe50(%rip),%xmm0
jbe    1150 <gaussrand+0x20>
movsd  %xmm2,(%rsp)
call   1040 <log@plt>
add    %eax,%eax
neg    %eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
divsd  0x2e79(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <sqrt@plt>
mulsd  (%rsp),%xmm0
xor    %eax,%eax
cmpl   $0x0,0x2e6a(%rip)
sete   %al
mov    %eax,0x2e61(%rip)
pop    %rax
ret

LLVM IR (-O0):
@gaussrand.V1 = internal global double 0.000000e+00
@gaussrand.V2 = internal global double 0.000000e+00
@gaussrand.S = internal global double 0.000000e+00
@gaussrand.phase = internal global i32 0
define double @gaussrand() {
%1 = alloca double
%2 = load i32, i32* @gaussrand.phase
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %45
4: ; preds = %0
br label %5
5: ; preds = %32, %4
%6 = call i32 (...) @randd()
%7 = mul nsw i32 2, %6
%8 = sub nsw i32 %7, 1
%9 = sitofp i32 %8 to double
store double %9, double* @gaussrand.V1
%10 = call i32 (...) @randd()
%11 = mul nsw i32 2, %10
%12 = sub nsw i32 %11, 1
%13 = sitofp i32 %12 to double
store double %13, double* @gaussrand.V2
%14 = load double, double* @gaussrand.V1
%15 = load double, double* @gaussrand.V1
%16 = load double, double* @gaussrand.V2
%17 = load double, double* @gaussrand.V2
%18 = fmul double %16, %17
%19 = call double @llvm.fmuladd.f64(double %14, double %15, double %18)
store double %19, double* @gaussrand.S
br label %20
20: ; preds = %5
%21 = load double, double* @gaussrand.S
%22 = fcmp oge double %21, 1.000000e+00
br i1 %22, label %32, label %23
23: ; preds = %20
%24 = load double, double* @gaussrand.S
%25 = fcmp olt double %24, 0.000000e+00
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load double, double* @gaussrand.S
%28 = fcmp ogt double %27, 0.000000e+00
br label %29
29: ; preds = %26, %23
%30 = phi i1 [ true, %23 ], [ %28, %26 ]
%31 = xor i1 %30, true
br label %32
32: ; preds = %29, %20
%33 = phi i1 [ true, %20 ], [ %31, %29 ]
br i1 %33, label %5, label %34
34: ; preds = %32
%35 = load double, double* @gaussrand.V1
%36 = load double, double* @gaussrand.S
%37 = call i32 @log(double %36)
%38 = mul nsw i32 -2, %37
%39 = sitofp i32 %38 to double
%40 = load double, double* @gaussrand.S
%41 = fdiv double %39, %40
%42 = fptosi double %41 to i32
%43 = call double @sqrt(i32 %42)
%44 = fmul double %35, %43
store double %44, double* %1
br label %56
45: ; preds = %0
%46 = load double, double* @gaussrand.V2
%47 = load double, double* @gaussrand.S
%48 = call i32 @log(double %47)
%49 = mul nsw i32 -2, %48
%50 = sitofp i32 %49 to double
%51 = load double, double* @gaussrand.S
%52 = fdiv double %50, %51
%53 = fptosi double %52 to i32
%54 = call double @sqrt(i32 %53)
%55 = fmul double %46, %54
store double %55, double* %1
br label %56
56: ; preds = %45, %34
%57 = load i32, i32* @gaussrand.phase
%58 = icmp ne i32 %57, 0
%59 = xor i1 %58, true
%60 = zext i1 %59 to i32
store i32 %60, i32* @gaussrand.phase
%61 = load double, double* %1
ret double %61
}

LLVM IR (-O2):
@gaussrand.V1 = internal unnamed_addr global double 0.000000e+00
@gaussrand.V2 = internal unnamed_addr global double 0.000000e+00
@gaussrand.S = internal unnamed_addr global double 0.000000e+00
@gaussrand.phase = internal unnamed_addr global i32 0
define double @gaussrand() {
%1 = load i32, i32* @gaussrand.phase
%2 = icmp eq i32 %1, 0
br i1 %2, label %3, label %18
3: ; preds = %0, %3
%4 = tail call i32 (...) @randd()
%5 = shl nsw i32 %4, 1
%6 = add nsw i32 %5, -1
%7 = sitofp i32 %6 to double
store double %7, double* @gaussrand.V1
%8 = tail call i32 (...) @randd()
%9 = shl nsw i32 %8, 1
%10 = add nsw i32 %9, -1
%11 = sitofp i32 %10 to double
store double %11, double* @gaussrand.V2
%12 = load double, double* @gaussrand.V1
%13 = fmul double %11, %11
%14 = tail call double @llvm.fmuladd.f64(double %12, double %12, double %13)
store double %14, double* @gaussrand.S
%15 = fcmp oge double %14, 1.000000e+00
%16 = fcmp ule double %14, 0.000000e+00
%17 = or i1 %15, %16
br i1 %17, label %3, label %21
18: ; preds = %0
%19 = load double, double* @gaussrand.V2
%20 = load double, double* @gaussrand.S
br label %21
21: ; preds = %3, %18
%22 = phi double [ %20, %18 ], [ %14, %3 ]
%23 = phi double [ %19, %18 ], [ %12, %3 ]
%24 = tail call i32 @log(double %22)
%25 = mul nsw i32 %24, -2
%26 = sitofp i32 %25 to double
%27 = load double, double* @gaussrand.S
%28 = fdiv double %26, %27
%29 = fptosi double %28 to i32
%30 = tail call double @sqrt(i32 %29)
%31 = fmul double %23, %30
%32 = load i32, i32* @gaussrand.phase
%33 = icmp eq i32 %32, 0
%34 = zext i1 %33 to i32
store i32 %34, i32* @gaussrand.phase
ret double %31
}