#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 */
typedef int int8_t;
typedef int /*<<< orphan*/ int32_t;

/* Variables and functions */
double log(double);
double rand_uniform(int /*<<< orphan*/ *);
double sqrt(double);
double rand_normal(int32_t *state, int8_t *empty, double *gset) {
  if (*empty) {
    double v1, v2, rsq;
    double fac;
    do {
      v1 = 2.0 * rand_uniform(state) - 1.0;
      v2 = 2.0 * rand_uniform(state) - 1.0;
      rsq = v1 * v1 + v2 * v2;
    } while ((rsq >= 1) || (rsq == 0));
    fac = sqrt(-2.0 * log(rsq) / rsq);
    *gset = v1 * fac;
    *empty = 0;
    return v2 * fac;
  } else {
    *empty = 1;
    return *gset;
  }
}


x86 assembly (clang, -O0):
<rand_normal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
je     1249 <rand_normal+0x119>
jmp    1156 <rand_normal+0x26>
mov    -0x10(%rbp),%rdi
call   1040 <rand_uniform@plt>
addsd  %xmm0,%xmm0
movsd  0xe95(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <rand_uniform@plt>
addsd  %xmm0,%xmm0
movsd  0xe77(%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,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
mov    $0x1,%al
movsd  0xe46(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x41(%rbp)
jae    11e6 <rand_normal+0xb6>
movsd  -0x38(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
sete   %al
setnp  %cl
and    %cl,%al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1156 <rand_normal+0x26>
movsd  -0x38(%rbp),%xmm0
call   1030 <log@plt>
movaps %xmm0,%xmm1
movsd  0xe0a(%rip),%xmm0
mulsd  %xmm1,%xmm0
divsd  -0x38(%rbp),%xmm0
call   1050 <sqrt@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x28(%rbp),%xmm0
mulsd  -0x40(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movsd  -0x30(%rbp),%xmm0
mulsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1260 <rand_normal+0x130>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x20(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_normal>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
cmpl   $0x0,(%rsi)
je     11e5 <rand_normal+0xb5>
mov    %rdi,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <rand_uniform@plt>
movapd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe98(%rip),%xmm0
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
mov    %rbx,%rdi
call   1040 <rand_uniform@plt>
addsd  %xmm0,%xmm0
addsd  0xe7a(%rip),%xmm0
movapd %xmm0,%xmm1
mulsd  %xmm0,%xmm1
movsd  0x8(%rsp),%xmm2
mulsd  %xmm2,%xmm2
addsd  %xmm1,%xmm2
ucomisd 0xe64(%rip),%xmm2
jae    1150 <rand_normal+0x20>
ucomisd 0xe6a(%rip),%xmm2
jne    11b2 <rand_normal+0x82>
jnp    1150 <rand_normal+0x20>
movsd  %xmm0,0x18(%rsp)
movapd %xmm2,%xmm0
movsd  %xmm2,0x10(%rsp)
call   1030 <log@plt>
mulsd  0xe41(%rip),%xmm0
divsd  0x10(%rsp),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     11f3 <rand_normal+0xc3>
sqrtsd %xmm0,%xmm0
jmp    11f8 <rand_normal+0xc8>
movl   $0x1,(%r15)
movsd  (%r14),%xmm1
jmp    1218 <rand_normal+0xe8>
call   1050 <sqrt@plt>
movsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm1,(%r14)
movl   $0x0,(%r15)
movsd  0x18(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define double @rand_normal(i32* %0, i32* nocapture %1, double* nocapture %2) {
%4 = load i32, i32* %1
%5 = icmp eq i32 %4, 0
br i1 %5, label %23, label %6
6: ; preds = %3, %6
%7 = tail call double @rand_uniform(i32* %0)
%8 = tail call double @llvm.fmuladd.f64(double %7, double 2.000000e+00, double -1.000000e+00)
%9 = tail call double @rand_uniform(i32* %0)
%10 = tail call double @llvm.fmuladd.f64(double %9, double 2.000000e+00, double -1.000000e+00)
%11 = fmul double %10, %10
%12 = tail call double @llvm.fmuladd.f64(double %8, double %8, double %11)
%13 = fcmp oge double %12, 1.000000e+00
%14 = fcmp oeq double %12, 0.000000e+00
%15 = or i1 %13, %14
br i1 %15, label %6, label %16
16: ; preds = %6
%17 = tail call double @log(double %12)
%18 = fmul double %17, -2.000000e+00
%19 = fdiv double %18, %12
%20 = tail call double @sqrt(double %19)
%21 = fmul double %8, %20
store double %21, double* %2
store i32 0, i32* %1
%22 = fmul double %10, %20
br label %25
23: ; preds = %3
store i32 1, i32* %1
%24 = load double, double* %2
br label %25
25: ; preds = %23, %16
%26 = phi double [ %22, %16 ], [ %24, %23 ]
ret double %26
}