#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 calculatePi(double, double, double);
double nextA(double, double);
double nextB(double, double);
double nextP(double);
double nextT(double, double, double, double);
int sqrt(int);
double gaussLeg(int iterations) {
  double a[iterations], b[iterations], t[iterations], p[iterations];
  int i;
  a[0] = 1;
  b[0] = 1 / sqrt(2);
  t[0] = 0.25;
  p[0] = 1;
  for (i = 1; i <= iterations; i++) {
    int prev = i - 1;
    a[i] = nextA(a[prev], b[prev]);
    b[i] = nextB(a[prev], b[prev]);
    t[i] = nextT(t[prev], p[prev], a[prev], a[i]);
    p[i] = nextP(p[prev]);
  }
  return calculatePi(a[iterations], b[iterations], t[iterations]);
}


x86 assembly (clang, -O0):
<gaussLeg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x10(%rbp)
lea    0xf(,%rcx,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x58(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x18(%rbp)
mov    -0x4(%rbp),%ecx
lea    0xf(,%rcx,8),%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x20(%rbp)
mov    -0x4(%rbp),%ecx
lea    0xf(,%rcx,8),%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x50(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x28(%rbp)
mov    -0x4(%rbp),%ecx
lea    0xf(,%rcx,8),%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x40(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x30(%rbp)
movsd  0xe04(%rip),%xmm0
movsd  %xmm0,(%rax)
mov    $0x2,%edi
call   1070 <sqrt@plt>
mov    -0x50(%rbp),%rcx
mov    %eax,%esi
mov    $0x1,%eax
cltd
idiv   %esi
mov    -0x48(%rbp),%rdx
mov    %eax,%esi
mov    -0x40(%rbp),%rax
cvtsi2sd %esi,%xmm0
movsd  %xmm0,(%rdx)
movsd  0xdd6(%rip),%xmm0
movsd  %xmm0,(%rcx)
movsd  0xdc2(%rip),%xmm0
movsd  %xmm0,(%rax)
movl   $0x1,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x4(%rbp),%eax
jg     1321 <gaussLeg+0x1c1>
mov    -0x58(%rbp),%rcx
mov    -0x48(%rbp),%rax
mov    -0x34(%rbp),%edx
sub    $0x1,%edx
mov    %edx,-0x38(%rbp)
movslq -0x38(%rbp),%rdx
movsd  (%rcx,%rdx,8),%xmm0
movslq -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
call   1080 <nextA@plt>
mov    -0x48(%rbp),%rax
mov    -0x58(%rbp),%rcx
movslq -0x34(%rbp),%rdx
movsd  %xmm0,(%rcx,%rdx,8)
movslq -0x38(%rbp),%rdx
movsd  (%rcx,%rdx,8),%xmm0
movslq -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
call   1040 <nextB@plt>
mov    -0x48(%rbp),%rsi
mov    -0x58(%rbp),%rax
mov    -0x50(%rbp),%rdx
mov    -0x40(%rbp),%rcx
movslq -0x34(%rbp),%rdi
movsd  %xmm0,(%rsi,%rdi,8)
movslq -0x38(%rbp),%rsi
movsd  (%rdx,%rsi,8),%xmm0
movslq -0x38(%rbp),%rdx
movsd  (%rcx,%rdx,8),%xmm1
movslq -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm3
call   1050 <nextT@plt>
mov    -0x50(%rbp),%rcx
mov    -0x40(%rbp),%rax
movslq -0x34(%rbp),%rdx
movsd  %xmm0,(%rcx,%rdx,8)
movslq -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
call   1030 <nextP@plt>
mov    -0x40(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1249 <gaussLeg+0xe9>
mov    -0x50(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    -0x58(%rbp),%rdx
movslq -0x4(%rbp),%rsi
movsd  (%rdx,%rsi,8),%xmm0
movslq -0x4(%rbp),%rdx
movsd  (%rcx,%rdx,8),%xmm1
movslq -0x4(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
call   1060 <calculatePi@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gaussLeg>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edi,%ebx
mov    %edi,%eax
mov    %rsp,%r15
lea    0xf(,%rax,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %rsp,%r12
sub    %rax,%r12
mov    %r12,%rsp
mov    %rsp,%r13
sub    %rax,%r13
mov    %r13,%rsp
movabs $0x3ff0000000000000,%rax
mov    %rax,(%r15)
mov    $0x2,%edi
call   1070 <sqrt@plt>
lea    0x1(%rax),%ecx
xor    %edx,%edx
cmp    $0x3,%ecx
cmovb  %eax,%edx
cvtsi2sd %edx,%xmm1
movsd  %xmm1,(%r12)
movabs $0x3fd0000000000000,%rax
mov    %rax,0x0(%r13)
mov    %rbx,-0x38(%rbp)
test   %ebx,%ebx
jle    128e <gaussLeg+0x12e>
mov    -0x38(%rbp),%rax
lea    0x1(%rax),%ebx
mov    $0x1,%r14d
movsd  0xe09(%rip),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  0xe04(%rip),%xmm0
movaps %xmm0,%xmm2
nopw   0x0(%rax,%rax,1)
movsd  %xmm2,-0x48(%rbp)
movsd  %xmm0,-0x50(%rbp)
movaps %xmm2,%xmm0
movsd  %xmm1,-0x30(%rbp)
call   1080 <nextA@plt>
movsd  %xmm0,-0x58(%rbp)
movsd  %xmm0,(%r15,%r14,8)
movsd  -0x48(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
call   1040 <nextB@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  %xmm0,(%r12,%r14,8)
movsd  -0x40(%rbp),%xmm0
movsd  -0x50(%rbp),%xmm1
movsd  -0x48(%rbp),%xmm2
movsd  -0x58(%rbp),%xmm3
call   1050 <nextT@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  %xmm0,0x0(%r13,%r14,8)
movsd  -0x50(%rbp),%xmm0
call   1030 <nextP@plt>
movsd  -0x30(%rbp),%xmm1
add    $0x1,%r14
movsd  -0x58(%rbp),%xmm2
cmp    %r14,%rbx
jne    1210 <gaussLeg+0xb0>
movslq -0x38(%rbp),%rax
movsd  (%r15,%rax,8),%xmm0
movsd  (%r12,%rax,8),%xmm1
movsd  0x0(%r13,%rax,8),%xmm2
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <calculatePi@plt>

LLVM IR (-O0):
define double @gaussLeg(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %2
%10 = load i32, i32* %2
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %3
%13 = alloca double, i64 %11
store i64 %11, i64* %4
%14 = load i32, i32* %2
%15 = zext i32 %14 to i64
%16 = alloca double, i64 %15
store i64 %15, i64* %5
%17 = load i32, i32* %2
%18 = zext i32 %17 to i64
%19 = alloca double, i64 %18
store i64 %18, i64* %6
%20 = load i32, i32* %2
%21 = zext i32 %20 to i64
%22 = alloca double, i64 %21
store i64 %21, i64* %7
%23 = getelementptr inbounds double, double* %13, i64 0
store double 1.000000e+00, double* %23
%24 = call i32 @sqrt(i32 2)
%25 = sdiv i32 1, %24
%26 = sitofp i32 %25 to double
%27 = getelementptr inbounds double, double* %16, i64 0
store double %26, double* %27
%28 = getelementptr inbounds double, double* %19, i64 0
store double 2.500000e-01, double* %28
%29 = getelementptr inbounds double, double* %22, i64 0
store double 1.000000e+00, double* %29
store i32 1, i32* %8
br label %30
30: ; preds = %89, %1
%31 = load i32, i32* %8
%32 = load i32, i32* %2
%33 = icmp sle i32 %31, %32
br i1 %33, label %34, label %92
34: ; preds = %30
%35 = load i32, i32* %8
%36 = sub nsw i32 %35, 1
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds double, double* %13, i64 %38
%40 = load double, double* %39
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds double, double* %16, i64 %42
%44 = load double, double* %43
%45 = call double @nextA(double %40, double %44)
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds double, double* %13, i64 %47
store double %45, double* %48
%49 = load i32, i32* %9
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds double, double* %13, i64 %50
%52 = load double, double* %51
%53 = load i32, i32* %9
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds double, double* %16, i64 %54
%56 = load double, double* %55
%57 = call double @nextB(double %52, double %56)
%58 = load i32, i32* %8
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds double, double* %16, i64 %59
store double %57, double* %60
%61 = load i32, i32* %9
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds double, double* %19, i64 %62
%64 = load double, double* %63
%65 = load i32, i32* %9
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds double, double* %22, i64 %66
%68 = load double, double* %67
%69 = load i32, i32* %9
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds double, double* %13, i64 %70
%72 = load double, double* %71
%73 = load i32, i32* %8
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds double, double* %13, i64 %74
%76 = load double, double* %75
%77 = call double @nextT(double %64, double %68, double %72, double %76)
%78 = load i32, i32* %8
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds double, double* %19, i64 %79
store double %77, double* %80
%81 = load i32, i32* %9
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds double, double* %22, i64 %82
%84 = load double, double* %83
%85 = call double @nextP(double %84)
%86 = load i32, i32* %8
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds double, double* %22, i64 %87
store double %85, double* %88
br label %89
89: ; preds = %34
%90 = load i32, i32* %8
%91 = add nsw i32 %90, 1
store i32 %91, i32* %8
br label %30
92: ; preds = %30
%93 = load i32, i32* %2
%94 = sext i32 %93 to i64
%95 = getelementptr inbounds double, double* %13, i64 %94
%96 = load double, double* %95
%97 = load i32, i32* %2
%98 = sext i32 %97 to i64
%99 = getelementptr inbounds double, double* %16, i64 %98
%100 = load double, double* %99
%101 = load i32, i32* %2
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds double, double* %19, i64 %102
%104 = load double, double* %103
%105 = call double @calculatePi(double %96, double %100, double %104)
%106 = load i8*, i8** %3
ret double %105
}

LLVM IR (-O2):
define double @gaussLeg(i32 %0) {
%2 = zext i32 %0 to i64
%3 = alloca double, i64 %2
%4 = alloca double, i64 %2
%5 = alloca double, i64 %2
store double 1.000000e+00, double* %3
%6 = tail call i32 @sqrt(i32 2)
%7 = add i32 %6, 1
%8 = icmp ult i32 %7, 3
%9 = select i1 %8, i32 %6, i32 0
%10 = sitofp i32 %9 to double
store double %10, double* %4
store double 2.500000e-01, double* %5
%11 = icmp slt i32 %0, 1
br i1 %11, label %30, label %12
12: ; preds = %1
%13 = add nuw i32 %0, 1
%14 = zext i32 %13 to i64
br label %15
15: ; preds = %12, %15
%16 = phi double [ 1.000000e+00, %12 ], [ %27, %15 ]
%17 = phi double [ 2.500000e-01, %12 ], [ %25, %15 ]
%18 = phi double [ %10, %12 ], [ %23, %15 ]
%19 = phi double [ 1.000000e+00, %12 ], [ %21, %15 ]
%20 = phi i64 [ 1, %12 ], [ %28, %15 ]
%21 = tail call double @nextA(double %19, double %18)
%22 = getelementptr inbounds double, double* %3, i64 %20
store double %21, double* %22
%23 = tail call double @nextB(double %19, double %18)
%24 = getelementptr inbounds double, double* %4, i64 %20
store double %23, double* %24
%25 = tail call double @nextT(double %17, double %16, double %19, double %21)
%26 = getelementptr inbounds double, double* %5, i64 %20
store double %25, double* %26
%27 = tail call double @nextP(double %16)
%28 = add nuw nsw i64 %20, 1
%29 = icmp eq i64 %28, %14
br i1 %29, label %30, label %15
30: ; preds = %15, %1
%31 = sext i32 %0 to i64
%32 = getelementptr inbounds double, double* %3, i64 %31
%33 = load double, double* %32
%34 = getelementptr inbounds double, double* %4, i64 %31
%35 = load double, double* %34
%36 = getelementptr inbounds double, double* %5, i64 %31
%37 = load double, double* %36
%38 = tail call double @calculatePi(double %33, double %35, double %37)
ret double %38
}