#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 scalar_t__ clock_t;

/* Variables and functions */
double CLOCKS_PER_SEC;
long atol(char *);
scalar_t__ clock();
int /*<<< orphan*/ printf(char *, double, ...);
int main(int argc, char **argv) {
  long i, num_steps = 1000000000;
  double step, x, sum, pi, taken;
  clock_t start, stop;
  if (argc > 1) {
    num_steps = atol(argv[1]);
  }
  printf("Calculating PI using:\n"
         "  %ld slices\n"
         "  1 process\n",
         num_steps);
  start = clock();
  sum = 0.0;
  step = 1.0 / num_steps;
  for (i = 0; i < num_steps; i++) {
    x = (i + 0.5) * step;
    sum += 4.0 / (1.0 + x * x);
  }
  pi = sum * step;
  stop = clock();
  taken = ((double)(stop - start)) / CLOCKS_PER_SEC;
  printf("Obtained value for PI: %.16g\n"
         "Time taken:            %.16g seconds\n",
         pi, taken);
  return 0;
}


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)
movq   $0x3b9aca00,-0x20(%rbp)
3b
cmpl   $0x1,-0x8(%rbp)
jle    1169 <main+0x39>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atol@plt>
mov    %rax,-0x20(%rbp)
cvtsi2sdq -0x20(%rbp),%xmm0
lea    0xea2(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x50(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
cvtsi2sdq -0x20(%rbp),%xmm1
movsd  0xe62(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    121a <main+0xea>
cvtsi2sdq -0x18(%rbp),%xmm0
movsd  0xe45(%rip),%xmm1
addsd  %xmm1,%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
mulsd  %xmm0,%xmm1
movsd  0xe11(%rip),%xmm0
addsd  %xmm0,%xmm1
movsd  0xe0d(%rip),%xmm0
divsd  %xmm1,%xmm0
addsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11af <main+0x7f>
movsd  -0x38(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
sub    -0x50(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2da0(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm1
lea    0xde6(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x3b9aca00,%ebx
cmp    $0x2,%edi
jl     114a <main+0x1a>
mov    0x8(%rsi),%rdi
call   1050 <atol@plt>
mov    %rax,%rbx
cvtsi2sd %rbx,%xmm0
movsd  %xmm0,(%rsp)
lea    0xebd(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,%r14
movsd  0xe8c(%rip),%xmm1
movapd %xmm1,%xmm6
divsd  (%rsp),%xmm6
test   %rbx,%rbx
jle    11a2 <main+0x72>
cmp    $0x1,%rbx
jne    11ab <main+0x7b>
xorpd  %xmm0,%xmm0
movsd  0xe74(%rip),%xmm1
test   $0x1,%bl
jne    123a <main+0x10a>
jmp    125a <main+0x12a>
xorpd  %xmm0,%xmm0
jmp    125a <main+0x12a>
mov    %rbx,%rax
and    $0xfffffffffffffffe,%rax
xorpd  %xmm0,%xmm0
xor    %ecx,%ecx
movsd  0xe48(%rip),%xmm2
movsd  0xe48(%rip),%xmm3
nopl   0x0(%rax,%rax,1)
xorps  %xmm4,%xmm4
cvtsi2sd %rcx,%xmm4
addsd  %xmm2,%xmm4
mulsd  %xmm6,%xmm4
mulsd  %xmm4,%xmm4
addsd  %xmm1,%xmm4
movapd %xmm3,%xmm5
divsd  %xmm4,%xmm5
addsd  %xmm0,%xmm5
lea    0x1(%rcx),%rdx
xorps  %xmm4,%xmm4
cvtsi2sd %rdx,%xmm4
addsd  %xmm2,%xmm4
mulsd  %xmm6,%xmm4
mulsd  %xmm4,%xmm4
addsd  %xmm1,%xmm4
movapd %xmm3,%xmm0
divsd  %xmm4,%xmm0
addsd  %xmm5,%xmm0
add    $0x2,%rcx
cmp    %rax,%rcx
jne    11d0 <main+0xa0>
xorps  %xmm1,%xmm1
cvtsi2sd %rcx,%xmm1
addsd  0xdd3(%rip),%xmm1
test   $0x1,%bl
je     125a <main+0x12a>
mulsd  %xmm6,%xmm1
mulsd  %xmm1,%xmm1
addsd  0xdb6(%rip),%xmm1
movsd  0xdbe(%rip),%xmm2
divsd  %xmm1,%xmm2
addsd  %xmm2,%xmm0
mulsd  %xmm0,%xmm6
movsd  %xmm6,(%rsp)
xor    %eax,%eax
call   1030 <clock@plt>
sub    %r14,%rax
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
mov    0x2d6c(%rip),%rax
divsd  (%rax),%xmm1
lea    0xdc1(%rip),%rdi
movsd  (%rsp),%xmm0
mov    $0x2,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [48 x i8] c"Calculating PI using:\0A %ld slices\0A 1 process\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str.1 = [67 x i8] c"Obtained value for PI: %.16g\0ATime taken: %.16g seconds\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca i64
%14 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 1000000000, i64* %7
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i64 @atol(i8* %20)
store i64 %21, i64* %7
br label %22
22: ; preds = %17, %2
%23 = load i64, i64* %7
%24 = sitofp i64 %23 to double
%25 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), double %24)
%26 = call i64 (...) @clock()
store i64 %26, i64* %13
store double 0.000000e+00, double* %10
%27 = load i64, i64* %7
%28 = sitofp i64 %27 to double
%29 = fdiv double 1.000000e+00, %28
store double %29, double* %8
store i64 0, i64* %6
br label %30
30: ; preds = %46, %22
%31 = load i64, i64* %6
%32 = load i64, i64* %7
%33 = icmp slt i64 %31, %32
br i1 %33, label %34, label %49
34: ; preds = %30
%35 = load i64, i64* %6
%36 = sitofp i64 %35 to double
%37 = fadd double %36, 5.000000e-01
%38 = load double, double* %8
%39 = fmul double %37, %38
store double %39, double* %9
%40 = load double, double* %9
%41 = load double, double* %9
%42 = call double @llvm.fmuladd.f64(double %40, double %41, double 1.000000e+00)
%43 = fdiv double 4.000000e+00, %42
%44 = load double, double* %10
%45 = fadd double %44, %43
store double %45, double* %10
br label %46
46: ; preds = %34
%47 = load i64, i64* %6
%48 = add nsw i64 %47, 1
store i64 %48, i64* %6
br label %30
49: ; preds = %30
%50 = load double, double* %10
%51 = load double, double* %8
%52 = fmul double %50, %51
store double %52, double* %11
%53 = call i64 (...) @clock()
store i64 %53, i64* %14
%54 = load i64, i64* %14
%55 = load i64, i64* %13
%56 = sub nsw i64 %54, %55
%57 = sitofp i64 %56 to double
%58 = load double, double* @CLOCKS_PER_SEC
%59 = fdiv double %57, %58
store double %59, double* %12
%60 = load double, double* %11
%61 = load double, double* %12
%62 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), double %60, double %61)
ret i32 0
}

LLVM IR (-O2):
@.str = [48 x i8] c"Calculating PI using:\0A %ld slices\0A 1 process\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str.1 = [67 x i8] c"Obtained value for PI: %.16g\0ATime taken: %.16g seconds\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i64 @atol(i8* %6)
br label %8
8: ; preds = %4, %2
%9 = phi i64 [ %7, %4 ], [ 1000000000, %2 ]
%10 = sitofp i64 %9 to double
%11 = tail call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), double %10)
%12 = tail call i64 (...) @clock()
%13 = fdiv double 1.000000e+00, %10
%14 = icmp sgt i64 %9, 0
br i1 %14, label %15, label %53
15: ; preds = %8
%16 = and i64 %9, 1
%17 = icmp eq i64 %9, 1
br i1 %17, label %43, label %18
18: ; preds = %15
%19 = and i64 %9, -2
br label %20
20: ; preds = %20, %18
%21 = phi i64 [ 0, %18 ], [ %37, %20 ]
%22 = phi double [ 0.000000e+00, %18 ], [ %36, %20 ]
%23 = phi i64 [ 0, %18 ], [ %38, %20 ]
%24 = sitofp i64 %21 to double
%25 = fadd double %24, 5.000000e-01
%26 = fmul double %13, %25
%27 = tail call double @llvm.fmuladd.f64(double %26, double %26, double 1.000000e+00)
%28 = fdiv double 4.000000e+00, %27
%29 = fadd double %22, %28
%30 = or i64 %21, 1
%31 = sitofp i64 %30 to double
%32 = fadd double %31, 5.000000e-01
%33 = fmul double %13, %32
%34 = tail call double @llvm.fmuladd.f64(double %33, double %33, double 1.000000e+00)
%35 = fdiv double 4.000000e+00, %34
%36 = fadd double %29, %35
%37 = add nuw nsw i64 %21, 2
%38 = add i64 %23, 2
%39 = icmp eq i64 %38, %19
br i1 %39, label %40, label %20
40: ; preds = %20
%41 = sitofp i64 %37 to double
%42 = fadd double %41, 5.000000e-01
br label %43
43: ; preds = %40, %15
%44 = phi double [ undef, %15 ], [ %36, %40 ]
%45 = phi double [ 5.000000e-01, %15 ], [ %42, %40 ]
%46 = phi double [ 0.000000e+00, %15 ], [ %36, %40 ]
%47 = icmp eq i64 %16, 0
br i1 %47, label %53, label %48
48: ; preds = %43
%49 = fmul double %13, %45
%50 = tail call double @llvm.fmuladd.f64(double %49, double %49, double 1.000000e+00)
%51 = fdiv double 4.000000e+00, %50
%52 = fadd double %46, %51
br label %53
53: ; preds = %48, %43, %8
%54 = phi double [ 0.000000e+00, %8 ], [ %44, %43 ], [ %52, %48 ]
%55 = fmul double %13, %54
%56 = tail call i64 (...) @clock()
%57 = sub nsw i64 %56, %12
%58 = sitofp i64 %57 to double
%59 = load double, double* @CLOCKS_PER_SEC
%60 = fdiv double %58, %59
%61 = tail call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), double %55, double %60)
ret i32 0
}