#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 */
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
scalar_t__ f(double);
double fabs(double);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, double, ...);
int main(int argc, char *argv[]) {
  int done = 0, n, i;
  double PI25DT = 3.141592653589793238462643;
  double mypi, pi, h, sum, x;
  double startwtime = 0.0, endwtime = 0.0;
  struct timeval tv;
  n = 100000000;
  if (gettimeofday(&tv, NULL) == 0) {
    startwtime = tv.tv_sec * 1000000 + tv.tv_usec;
  } else {
    startwtime = 0;
  }
  h = 1.0 / (double)n;
  sum = 0.0;
  for (i = 1; i <= n; i += 1) {
    x = h * ((double)i - 0.5);
    sum += f(x);
  }
  mypi = h * sum;
  printf("pi is approximately %.16f, Error is %.16f\n Number of intervals is "
         "%5d\n",
         mypi, fabs(mypi - PI25DT), n);
  if (gettimeofday(&tv, NULL) == 0) {
    endwtime = tv.tv_sec * 1000000 + tv.tv_usec;
  } else {
    endwtime = 0;
  }
  printf("wall clock time = %.0f usecs \n", (endwtime - startwtime));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movsd  0xeab(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x58(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x60(%rbp)
movl   $0x5f5e100,-0x18(%rbp)
lea    -0x68(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
jne    11a0 <main+0x70>
imul   $0xf4240,-0x68(%rbp),%eax
add    -0x64(%rbp),%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x58(%rbp)
jmp    11a8 <main+0x78>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x58(%rbp)
cvtsi2sdl -0x18(%rbp),%xmm1
movsd  0xe53(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     121f <main+0xef>
movsd  -0x40(%rbp),%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm1
movsd  0xe25(%rip),%xmm2
subsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
call   1030 <f@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11cd <main+0x9d>
movsd  -0x40(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
subsd  -0x28(%rbp),%xmm1
movaps 0xddc(%rip),%xmm2
pand   %xmm2,%xmm1
mov    -0x18(%rbp),%esi
lea    0xdde(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
lea    -0x68(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
jne    1288 <main+0x158>
imul   $0xf4240,-0x68(%rbp),%eax
add    -0x64(%rbp),%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x60(%rbp)
jmp    1290 <main+0x160>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x60(%rbp),%xmm0
subsd  -0x58(%rbp),%xmm0
lea    0xdd6(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
xorpd  %xmm0,%xmm0
xorpd  %xmm1,%xmm1
test   %rax,%rax
jne    115e <main+0x2e>
imul   $0xf4240,(%rsp),%eax
add    0x4(%rsp),%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0xe94(%rip),%xmm1
mov    $0x5f5e100,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  %xmm1,0x10(%rsp)
movsd  %xmm0,0x18(%rsp)
movapd %xmm1,%xmm0
addsd  0xe70(%rip),%xmm0
mulsd  0xe70(%rip),%xmm0
call   1030 <f@plt>
movsd  0x18(%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
addsd  %xmm1,%xmm0
movsd  0x10(%rsp),%xmm1
addsd  0xe3b(%rip),%xmm1
add    $0xffffffff,%ebx
jne    1180 <main+0x50>
mulsd  0xe3e(%rip),%xmm0
movsd  0xe3e(%rip),%xmm1
addsd  %xmm0,%xmm1
andpd  0xe3a(%rip),%xmm1
lea    0xe43(%rip),%rdi
mov    $0x5f5e100,%esi
mov    $0x2,%al
call   1040 <printf@plt>
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
xorpd  %xmm0,%xmm0
test   %rax,%rax
jne    121e <main+0xee>
imul   $0xf4240,(%rsp),%eax
add    0x4(%rsp),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
subsd  0x8(%rsp),%xmm0
lea    0xe4c(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [71 x i8] c"pi is approximately %.16f, Error is %.16f\0A Number of intervals is %5d\0A\00"
@.str.1 = [31 x i8] c"wall clock time = %.0f usecs \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca double
%17 = alloca %struct.timeval
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store double 0x400921FB54442D18, double* %9
store double 0.000000e+00, double* %15
store double 0.000000e+00, double* %16
store i32 100000000, i32* %7
%18 = call i64 @gettimeofday(%struct.timeval* %17, i32* null)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %2
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %17, i32 0, i32 0
%22 = load i32, i32* %21
%23 = mul nsw i32 %22, 1000000
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %17, i32 0, i32 1
%25 = load i32, i32* %24
%26 = add nsw i32 %23, %25
%27 = sitofp i32 %26 to double
store double %27, double* %15
br label %29
28: ; preds = %2
store double 0.000000e+00, double* %15
br label %29
29: ; preds = %28, %20
%30 = load i32, i32* %7
%31 = sitofp i32 %30 to double
%32 = fdiv double 1.000000e+00, %31
store double %32, double* %12
store double 0.000000e+00, double* %13
store i32 1, i32* %8
br label %33
33: ; preds = %48, %29
%34 = load i32, i32* %8
%35 = load i32, i32* %7
%36 = icmp sle i32 %34, %35
br i1 %36, label %37, label %51
37: ; preds = %33
%38 = load double, double* %12
%39 = load i32, i32* %8
%40 = sitofp i32 %39 to double
%41 = fsub double %40, 5.000000e-01
%42 = fmul double %38, %41
store double %42, double* %14
%43 = load double, double* %14
%44 = call i64 @f(double %43)
%45 = sitofp i64 %44 to double
%46 = load double, double* %13
%47 = fadd double %46, %45
store double %47, double* %13
br label %48
48: ; preds = %37
%49 = load i32, i32* %8
%50 = add nsw i32 %49, 1
store i32 %50, i32* %8
br label %33
51: ; preds = %33
%52 = load double, double* %12
%53 = load double, double* %13
%54 = fmul double %52, %53
store double %54, double* %10
%55 = load double, double* %10
%56 = load double, double* %10
%57 = load double, double* %9
%58 = fsub double %56, %57
%59 = call double @llvm.fabs.f64(double %58)
%60 = load i32, i32* %7
%61 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0), double %55, double %59, i32 %60)
%62 = call i64 @gettimeofday(%struct.timeval* %17, i32* null)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %72
64: ; preds = %51
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %17, i32 0, i32 0
%66 = load i32, i32* %65
%67 = mul nsw i32 %66, 1000000
%68 = getelementptr inbounds %struct.timeval, %struct.timeval* %17, i32 0, i32 1
%69 = load i32, i32* %68
%70 = add nsw i32 %67, %69
%71 = sitofp i32 %70 to double
store double %71, double* %16
br label %73
72: ; preds = %51
store double 0.000000e+00, double* %16
br label %73
73: ; preds = %72, %64
%74 = load double, double* %16
%75 = load double, double* %15
%76 = fsub double %74, %75
%77 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), double %76)
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [71 x i8] c"pi is approximately %.16f, Error is %.16f\0A Number of intervals is %5d\0A\00"
@.str.1 = [31 x i8] c"wall clock time = %.0f usecs \0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %2
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = mul nsw i32 %9, 1000000
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%12 = load i32, i32* %11
%13 = add nsw i32 %10, %12
%14 = sitofp i32 %13 to double
br label %15
15: ; preds = %2, %7
%16 = phi double [ %14, %7 ], [ 0.000000e+00, %2 ]
br label %17
17: ; preds = %15, %17
%18 = phi i32 [ 1, %15 ], [ %26, %17 ]
%19 = phi double [ 0.000000e+00, %15 ], [ %25, %17 ]
%20 = sitofp i32 %18 to double
%21 = fadd double %20, -5.000000e-01
%22 = fmul double %21, 1.000000e-08
%23 = tail call i64 @f(double %22)
%24 = sitofp i64 %23 to double
%25 = fadd double %19, %24
%26 = add nuw nsw i32 %18, 1
%27 = icmp eq i32 %26, 100000001
br i1 %27, label %28, label %17
28: ; preds = %17
%29 = fmul double %25, 1.000000e-08
%30 = fadd double %29, 0xC00921FB54442D18
%31 = tail call double @llvm.fabs.f64(double %30)
%32 = tail call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0), double %29, double %31, i32 100000000)
%33 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %43
35: ; preds = %28
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%37 = load i32, i32* %36
%38 = mul nsw i32 %37, 1000000
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%40 = load i32, i32* %39
%41 = add nsw i32 %38, %40
%42 = sitofp i32 %41 to double
br label %43
43: ; preds = %28, %35
%44 = phi double [ %42, %35 ], [ 0.000000e+00, %28 ]
%45 = fsub double %44, %16
%46 = tail call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), double %45)
ret i32 0
}