#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 gasdev(long *);
double sqrt(double);
double chisquare(int n, long *seed) {
  int i, j;
  double rand, gauss;
  for (j = 0, rand = 0; j < n; j++) {
    gauss = gasdev(seed);
    rand += gauss * gauss;
  }
  rand = (rand / n - 1) / sqrt(2.0 / n);
  return rand;
}


x86 assembly (clang, -O0):
<chisquare>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1182 <chisquare+0x62>
mov    -0x10(%rbp),%rdi
call   1040 <gasdev@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113e <chisquare+0x1e>
movsd  -0x20(%rbp),%xmm0
cvtsi2sdl -0x4(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe70(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
cvtsi2sdl -0x4(%rbp),%xmm1
movsd  0xe52(%rip),%xmm0
divsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chisquare>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
xorpd  %xmm2,%xmm2
test   %edi,%edi
jle    1161 <chisquare+0x41>
mov    %rsi,%rbx
xorpd  %xmm2,%xmm2
mov    %r14d,%ebp
nopl   (%rax)
movsd  %xmm2,0x8(%rsp)
mov    %rbx,%rdi
call   1040 <gasdev@plt>
movsd  0x8(%rsp),%xmm2
mulsd  %xmm0,%xmm0
addsd  %xmm0,%xmm2
add    $0xffffffff,%ebp
jne    1140 <chisquare+0x20>
cvtsi2sd %r14d,%xmm1
divsd  %xmm1,%xmm2
addsd  0xe8e(%rip),%xmm2
movsd  0xe8e(%rip),%xmm0
divsd  %xmm1,%xmm0
ucomisd 0xe8a(%rip),%xmm0
jb     118e <chisquare+0x6e>
sqrtsd %xmm0,%xmm0
jmp    119f <chisquare+0x7f>
movsd  %xmm2,0x8(%rsp)
call   1030 <sqrt@plt>
movsd  0x8(%rsp),%xmm2
divsd  %xmm0,%xmm2
movapd %xmm2,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define double @chisquare(i32 %0, i64* %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i32
%7 = alloca double
%8 = alloca double
store i32 %0, i32* %3
store i64* %1, i64** %4
store i32 0, i32* %6
store double 0.000000e+00, double* %7
br label %9
9: ; preds = %20, %2
%10 = load i32, i32* %6
%11 = load i32, i32* %3
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %23
13: ; preds = %9
%14 = load i64*, i64** %4
%15 = call double @gasdev(i64* %14)
store double %15, double* %8
%16 = load double, double* %8
%17 = load double, double* %8
%18 = load double, double* %7
%19 = call double @llvm.fmuladd.f64(double %16, double %17, double %18)
store double %19, double* %7
br label %20
20: ; preds = %13
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
store i32 %22, i32* %6
br label %9
23: ; preds = %9
%24 = load double, double* %7
%25 = load i32, i32* %3
%26 = sitofp i32 %25 to double
%27 = fdiv double %24, %26
%28 = fsub double %27, 1.000000e+00
%29 = load i32, i32* %3
%30 = sitofp i32 %29 to double
%31 = fdiv double 2.000000e+00, %30
%32 = call double @sqrt(double %31)
%33 = fdiv double %28, %32
store double %33, double* %7
%34 = load double, double* %7
ret double %34
}

LLVM IR (-O2):
define double @chisquare(i32 %0, i64* %1) {
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %11
4: ; preds = %2, %4
%5 = phi i32 [ %9, %4 ], [ 0, %2 ]
%6 = phi double [ %8, %4 ], [ 0.000000e+00, %2 ]
%7 = tail call double @gasdev(i64* %1)
%8 = tail call double @llvm.fmuladd.f64(double %7, double %7, double %6)
%9 = add nuw nsw i32 %5, 1
%10 = icmp eq i32 %9, %0
br i1 %10, label %11, label %4
11: ; preds = %4, %2
%12 = phi double [ 0.000000e+00, %2 ], [ %8, %4 ]
%13 = sitofp i32 %0 to double
%14 = fdiv double %12, %13
%15 = fadd double %14, -1.000000e+00
%16 = fdiv double 2.000000e+00, %13
%17 = tail call double @sqrt(double %16)
%18 = fdiv double %15, %17
ret double %18
}