#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 fabs(double);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main(int argc, char *argv[]) {
  double pi25dt = 3.141592653589793238462643;
  double pi, h, sum, x, f, a;
  int n, i, ierr;
  printf("Enter the number of intervals: \n");
  scanf("%d", &n);
  h = 1.0 / (double)n;
  sum = 0.0;
  for (i = 1; i <= n; i++) {
    x = h * ((double)i - 0.5);
    sum += 4.0 / (1.0 + x * x);
  }
  pi = h * sum;
  printf("pi is approximately %.16f, Error is %.16f\n", pi, fabs(pi - pi25dt));
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  0xeca(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
lea    0xee6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xef9(%rip),%rdi
lea    -0x4c(%rbp),%rsi
call   1040 <scanf@plt>
cvtsi2sdl -0x4c(%rbp),%xmm1
movsd  0xe92(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jg     11ef <main+0xcf>
movsd  -0x28(%rbp),%xmm0
cvtsi2sdl -0x50(%rbp),%xmm1
movsd  0xe74(%rip),%xmm2
subsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
mulsd  %xmm0,%xmm1
movsd  0xe39(%rip),%xmm0
addsd  %xmm0,%xmm1
movsd  0xe3d(%rip),%xmm0
divsd  %xmm1,%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    1186 <main+0x66>
movsd  -0x28(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
subsd  -0x18(%rbp),%xmm1
movaps 0xe0c(%rip),%xmm2
pand   %xmm2,%xmm1
lea    0xe35(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xf36(%rip),%rdi
call   1030 <puts@plt>
lea    0xefc(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x4(%rsp),%eax
cvtsi2sd %eax,%xmm1
movsd  0xea2(%rip),%xmm2
movapd %xmm2,%xmm0
divsd  %xmm1,%xmm0
test   %eax,%eax
jle    1188 <main+0x58>
cmp    $0x1,%eax
jne    1191 <main+0x61>
movsd  0xe91(%rip),%xmm2
xorpd  %xmm1,%xmm1
test   $0x1,%al
jne    1213 <main+0xe3>
jmp    1233 <main+0x103>
xorpd  %xmm1,%xmm1
jmp    1233 <main+0x103>
mov    %eax,%ecx
and    $0xfffffffe,%ecx
mov    $0x1,%edx
xorpd  %xmm1,%xmm1
movsd  0xe69(%rip),%xmm3
movsd  0xe69(%rip),%xmm4
nop
xorps  %xmm5,%xmm5
cvtsi2sd %edx,%xmm5
addsd  %xmm3,%xmm5
mulsd  %xmm0,%xmm5
mulsd  %xmm5,%xmm5
addsd  %xmm2,%xmm5
movapd %xmm4,%xmm6
divsd  %xmm5,%xmm6
addsd  %xmm1,%xmm6
lea    0x1(%rdx),%esi
xorps  %xmm5,%xmm5
cvtsi2sd %esi,%xmm5
addsd  %xmm3,%xmm5
mulsd  %xmm0,%xmm5
mulsd  %xmm5,%xmm5
addsd  %xmm2,%xmm5
movapd %xmm4,%xmm1
divsd  %xmm5,%xmm1
addsd  %xmm6,%xmm1
add    $0x2,%edx
cmp    %ecx,%esi
jne    11b0 <main+0x80>
xorps  %xmm2,%xmm2
cvtsi2sd %edx,%xmm2
addsd  0xe01(%rip),%xmm2
test   $0x1,%al
je     1233 <main+0x103>
mulsd  %xmm0,%xmm2
mulsd  %xmm2,%xmm2
addsd  0xddd(%rip),%xmm2
movsd  0xded(%rip),%xmm3
divsd  %xmm2,%xmm3
addsd  %xmm3,%xmm1
mulsd  %xmm1,%xmm0
movsd  0xde1(%rip),%xmm1
addsd  %xmm0,%xmm1
andpd  0xde5(%rip),%xmm1
lea    0xdf1(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [33 x i8] c"Enter the number of intervals: \0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [43 x i8] c"pi is approximately %.16f, Error is %.16f\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store double 0x400921FB54442D18, double* %6
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %13)
%18 = load i32, i32* %13
%19 = sitofp i32 %18 to double
%20 = fdiv double 1.000000e+00, %19
store double %20, double* %8
store double 0.000000e+00, double* %9
store i32 1, i32* %14
br label %21
21: ; preds = %37, %2
%22 = load i32, i32* %14
%23 = load i32, i32* %13
%24 = icmp sle i32 %22, %23
br i1 %24, label %25, label %40
25: ; preds = %21
%26 = load double, double* %8
%27 = load i32, i32* %14
%28 = sitofp i32 %27 to double
%29 = fsub double %28, 5.000000e-01
%30 = fmul double %26, %29
store double %30, double* %10
%31 = load double, double* %10
%32 = load double, double* %10
%33 = call double @llvm.fmuladd.f64(double %31, double %32, double 1.000000e+00)
%34 = fdiv double 4.000000e+00, %33
%35 = load double, double* %9
%36 = fadd double %35, %34
store double %36, double* %9
br label %37
37: ; preds = %25
%38 = load i32, i32* %14
%39 = add nsw i32 %38, 1
store i32 %39, i32* %14
br label %21
40: ; preds = %21
%41 = load double, double* %8
%42 = load double, double* %9
%43 = fmul double %41, %42
store double %43, double* %7
%44 = load double, double* %7
%45 = load double, double* %7
%46 = load double, double* %6
%47 = fsub double %45, %46
%48 = call double @llvm.fabs.f64(double %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), double %44, double %48)
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [43 x i8] c"pi is approximately %.16f, Error is %.16f\0A\00"
@str = [32 x i8] c"Enter the number of intervals: \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%7 = load i32, i32* %3
%8 = sitofp i32 %7 to double
%9 = fdiv double 1.000000e+00, %8
%10 = icmp slt i32 %7, 1
br i1 %10, label %49, label %11
11: ; preds = %2
%12 = and i32 %7, 1
%13 = icmp eq i32 %7, 1
br i1 %13, label %39, label %14
14: ; preds = %11
%15 = and i32 %7, -2
br label %16
16: ; preds = %16, %14
%17 = phi double [ 0.000000e+00, %14 ], [ %32, %16 ]
%18 = phi i32 [ 1, %14 ], [ %33, %16 ]
%19 = phi i32 [ 0, %14 ], [ %34, %16 ]
%20 = sitofp i32 %18 to double
%21 = fadd double %20, -5.000000e-01
%22 = fmul double %9, %21
%23 = call double @llvm.fmuladd.f64(double %22, double %22, double 1.000000e+00)
%24 = fdiv double 4.000000e+00, %23
%25 = fadd double %17, %24
%26 = add nuw i32 %18, 1
%27 = sitofp i32 %26 to double
%28 = fadd double %27, -5.000000e-01
%29 = fmul double %9, %28
%30 = call double @llvm.fmuladd.f64(double %29, double %29, double 1.000000e+00)
%31 = fdiv double 4.000000e+00, %30
%32 = fadd double %25, %31
%33 = add nuw i32 %18, 2
%34 = add i32 %19, 2
%35 = icmp eq i32 %34, %15
br i1 %35, label %36, label %16
36: ; preds = %16
%37 = sitofp i32 %33 to double
%38 = fadd double %37, -5.000000e-01
br label %39
39: ; preds = %36, %11
%40 = phi double [ undef, %11 ], [ %32, %36 ]
%41 = phi double [ 0.000000e+00, %11 ], [ %32, %36 ]
%42 = phi double [ 5.000000e-01, %11 ], [ %38, %36 ]
%43 = icmp eq i32 %12, 0
br i1 %43, label %49, label %44
44: ; preds = %39
%45 = fmul double %9, %42
%46 = call double @llvm.fmuladd.f64(double %45, double %45, double 1.000000e+00)
%47 = fdiv double 4.000000e+00, %46
%48 = fadd double %41, %47
br label %49
49: ; preds = %44, %39, %2
%50 = phi double [ 0.000000e+00, %2 ], [ %40, %39 ], [ %48, %44 ]
%51 = fmul double %9, %50
%52 = fadd double %51, 0xC00921FB54442D18
%53 = call double @llvm.fabs.f64(double %52)
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), double %51, double %53)
ret i32 0
}