#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__ uint64_t;
struct timespec {
  long long tv_sec;
  scalar_t__ tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ M_PI;
int atoi(char *);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
double fabs(scalar_t__);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, double);
int pow(int, int);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  double volatile s = 0.;
  int N = 1000000;
  if (argc > 1) {
    N = atoi(argv[1]);
  }
  int Nrun = 10;
  if (argc > 2) {
    Nrun = atoi(argv[2]);
  }
  printf("--> C.  N=%d \n", N);
  uint64_t tmin = 1000000000LL;
  struct timespec start, end;
  uint64_t diff;
  for (int i = 1; i <= Nrun; i++) {
    s = 0.;
    clock_gettime(CLOCK_MONOTONIC, &start);
    for (int k = 1; k <= N; k++) {
      s += pow(-1., k + 1.) / (2. * k - 1);
    }
    clock_gettime(CLOCK_MONOTONIC, &end);
    diff = 1000000000LL * (end.tv_sec - start.tv_sec) + end.tv_nsec -
           start.tv_nsec;
    if (diff < tmin)
      tmin = diff;
  }
  s = 4 * s;
  double err = fabs(s - M_PI);
  if (err > 1e-4) {
    fprintf(stderr, "C pisum: large error magnitude %f", err);
    return EXIT_FAILURE;
  }
  printf("%.3e seconds \n", (double)tmin / 1000000000LL);
  return EXIT_SUCCESS;
}


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)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0xf4240,-0x1c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    119f <main+0x3f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x1c(%rbp)
movl   $0xa,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11c0 <main+0x60>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x20(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm0
lea    0xe5c(%rip),%rdi
call   1040 <printf@plt>
movq   $0x3b9aca00,-0x28(%rbp)
3b
movl   $0x1,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     12c7 <main+0x167>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1030 <clock_gettime@plt>
movl   $0x1,-0x58(%rbp)
mov    -0x58(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     1271 <main+0x111>
cvtsi2sdl -0x58(%rbp),%xmm0
movsd  0xdfa(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%esi
mov    $0xffffffff,%edi
call   1070 <pow@plt>
cvtsi2sd %eax,%xmm1
cvtsi2sdl -0x58(%rbp),%xmm0
addsd  %xmm0,%xmm0
movsd  0xdcb(%rip),%xmm2
addsd  %xmm2,%xmm0
divsd  %xmm0,%xmm1
movsd  -0x18(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    120d <main+0xad>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    -0x48(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x48(%rbp),%rax
sub    -0x38(%rbp),%rax
imul   $0x3b9aca00,%rax,%rax
add    -0x40(%rbp),%rax
sub    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    12b4 <main+0x154>
mov    -0x50(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    12b9 <main+0x159>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11e0 <main+0x80>
movsd  -0x18(%rbp),%xmm1
movsd  0xd34(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
mov    0x2cff(%rip),%rax
cvtsi2sdq (%rax),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rdi
call   1050 <fabs@plt>
movsd  %xmm0,-0x60(%rbp)
movsd  -0x60(%rbp),%xmm0
movsd  0xcf2(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1343 <main+0x1e3>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
movsd  -0x60(%rbp),%xmm0
lea    0xd0a(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2c8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136d <main+0x20d>
cvtsi2sdq -0x28(%rbp),%xmm0
movsd  0xcbf(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xcfd(%rip),%rdi
call   1040 <printf@plt>
mov    0x2c58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movq   $0x0,(%rsp)
mov    $0xf4240,%r12d
cmp    $0x2,%edi
jl     119b <main+0x3b>
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%r12d
cmp    $0x2,%ebp
jne    1349 <main+0x1e9>
cvtsi2sd %r12d,%xmm0
lea    0xe81(%rip),%rdi
call   1040 <printf@plt>
movl   $0xa,0xc(%rsp)
mov    %r12d,%ebx
neg    %ebx
mov    $0x3b9aca00,%ebp
mov    $0x1,%r15d
mov    0x2e15(%rip),%r14
jmp    1214 <main+0xb4>
nopl   (%rax)
mov    (%r14),%edi
lea    0x18(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    0x18(%rsp),%rax
sub    0x28(%rsp),%rax
imul   $0x3b9aca00,%rax,%rax
add    0x20(%rsp),%rax
sub    0x30(%rsp),%rax
cmp    %rbp,%rax
cmovl  %rax,%rbp
lea    0x1(%r15),%eax
cmp    0xc(%rsp),%r15d
mov    %eax,%r15d
mov    %r13d,%r12d
je     12ad <main+0x14d>
movq   $0x0,(%rsp)
mov    (%r14),%edi
lea    0x28(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    %r12d,%r13d
test   %r12d,%r12d
movsd  0xdd1(%rip),%xmm1
jle    11d0 <main+0x70>
mov    $0x1,%r12d
nop
xorps  %xmm0,%xmm0
cvtsi2sd %r12d,%xmm0
movsd  %xmm0,0x10(%rsp)
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%esi
mov    $0xffffffff,%edi
call   1070 <pow@plt>
movsd  0xda8(%rip),%xmm2
movsd  0xd98(%rip),%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  0x10(%rsp),%xmm3
addsd  %xmm3,%xmm3
addsd  %xmm2,%xmm3
divsd  %xmm3,%xmm0
addsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
lea    (%rbx,%r12,1),%eax
add    $0x1,%eax
mov    %r12d,%ecx
add    $0x1,%ecx
mov    %ecx,%r12d
cmp    $0x1,%eax
jne    1240 <main+0xe0>
jmp    11d0 <main+0x70>
xorps  %xmm0,%xmm0
cvtsi2sd %rbp,%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  (%rsp),%xmm0
mulsd  0xd50(%rip),%xmm0
movsd  %xmm0,(%rsp)
movsd  (%rsp),%xmm0
mov    0x2d0f(%rip),%rax
xorps  %xmm1,%xmm1
cvtsi2sdq (%rax),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rdi
call   1050 <fabs@plt>
ucomisd 0xd29(%rip),%xmm0
jbe    1317 <main+0x1b7>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
lea    0xd2e(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2cb3(%rip),%rax
jmp    1338 <main+0x1d8>
movsd  0x10(%rsp),%xmm0
divsd  0xcdb(%rip),%xmm0
lea    0xd2d(%rip),%rdi
call   1040 <printf@plt>
mov    0x2c88(%rip),%rax
mov    (%rax),%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%r14),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebx
cvtsi2sd %r12d,%xmm0
lea    0xcc8(%rip),%rdi
call   1040 <printf@plt>
mov    %ebx,0xc(%rsp)
test   %ebx,%ebx
jg     11b4 <main+0x54>
movsd  0xc87(%rip),%xmm0
jmp    12b5 <main+0x155>

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@.str = [15 x i8] c"--> C. N=%d \0A\00"
@CLOCK_MONOTONIC = global i32 0
@M_PI = global i64 0
@stderr = global i32 0
@.str.1 = [34 x i8] c"C pisum: large error magnitude %f\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [15 x i8] c"%.3e seconds \0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca %struct.timespec
%11 = alloca %struct.timespec
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
%15 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store volatile double 0.000000e+00, double* %6
store i32 1000000, i32* %7
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 1
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %7
br label %23
23: ; preds = %18, %2
store i32 10, i32* %8
%24 = load i32, i32* %4
%25 = icmp sgt i32 %24, 2
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %8
br label %31
31: ; preds = %26, %23
%32 = load i32, i32* %7
%33 = sitofp i32 %32 to double
%34 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), double %33)
store i64 1000000000, i64* %9
store i32 1, i32* %13
br label %35
35: ; preds = %83, %31
%36 = load i32, i32* %13
%37 = load i32, i32* %8
%38 = icmp sle i32 %36, %37
br i1 %38, label %39, label %86
39: ; preds = %35
store volatile double 0.000000e+00, double* %6
%40 = load i32, i32* @CLOCK_MONOTONIC
%41 = call i32 @clock_gettime(i32 %40, %struct.timespec* %10)
store i32 1, i32* %14
br label %42
42: ; preds = %59, %39
%43 = load i32, i32* %14
%44 = load i32, i32* %7
%45 = icmp sle i32 %43, %44
br i1 %45, label %46, label %62
46: ; preds = %42
%47 = load i32, i32* %14
%48 = sitofp i32 %47 to double
%49 = fadd double %48, 1.000000e+00
%50 = fptosi double %49 to i32
%51 = call i32 @pow(i32 -1, i32 %50)
%52 = sitofp i32 %51 to double
%53 = load i32, i32* %14
%54 = sitofp i32 %53 to double
%55 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %54, double -1.000000e+00)
%56 = fdiv double %52, %55
%57 = load volatile double, double* %6
%58 = fadd double %57, %56
store volatile double %58, double* %6
br label %59
59: ; preds = %46
%60 = load i32, i32* %14
%61 = add nsw i32 %60, 1
store i32 %61, i32* %14
br label %42
62: ; preds = %42
%63 = load i32, i32* @CLOCK_MONOTONIC
%64 = call i32 @clock_gettime(i32 %63, %struct.timespec* %11)
%65 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
%66 = load i64, i64* %65
%67 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 0
%68 = load i64, i64* %67
%69 = sub nsw i64 %66, %68
%70 = mul nsw i64 1000000000, %69
%71 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 1
%72 = load i64, i64* %71
%73 = add nsw i64 %70, %72
%74 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 1
%75 = load i64, i64* %74
%76 = sub nsw i64 %73, %75
store i64 %76, i64* %12
%77 = load i64, i64* %12
%78 = load i64, i64* %9
%79 = icmp slt i64 %77, %78
br i1 %79, label %80, label %82
80: ; preds = %62
%81 = load i64, i64* %12
store i64 %81, i64* %9
br label %82
82: ; preds = %80, %62
br label %83
83: ; preds = %82
%84 = load i32, i32* %13
%85 = add nsw i32 %84, 1
store i32 %85, i32* %13
br label %35
86: ; preds = %35
%87 = load volatile double, double* %6
%88 = fmul double 4.000000e+00, %87
store volatile double %88, double* %6
%89 = load volatile double, double* %6
%90 = load i64, i64* @M_PI
%91 = sitofp i64 %90 to double
%92 = fsub double %89, %91
%93 = fptosi double %92 to i64
%94 = call double @fabs(i64 %93)
store double %94, double* %15
%95 = load double, double* %15
%96 = fcmp ogt double %95, 1.000000e-04
br i1 %96, label %97, label %102
97: ; preds = %86
%98 = load i32, i32* @stderr
%99 = load double, double* %15
%100 = call i32 @fprintf(i32 %98, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), double %99)
%101 = load i32, i32* @EXIT_FAILURE
store i32 %101, i32* %3
br label %108
102: ; preds = %86
%103 = load i64, i64* %9
%104 = sitofp i64 %103 to double
%105 = fdiv double %104, 1.000000e+09
%106 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), double %105)
%107 = load i32, i32* @EXIT_SUCCESS
store i32 %107, i32* %3
br label %108
108: ; preds = %102, %97
%109 = load i32, i32* %3
ret i32 %109
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@.str = [15 x i8] c"--> C. N=%d \0A\00"
@CLOCK_MONOTONIC = global i32 0
@M_PI = global i64 0
@stderr = global i32 0
@.str.1 = [34 x i8] c"C pisum: large error magnitude %f\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [15 x i8] c"%.3e seconds \0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca double
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = bitcast double* %3 to i8*
store volatile double 0.000000e+00, double* %3
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = icmp eq i32 %0, 2
br i1 %12, label %13, label %19
13: ; preds = %8, %2
%14 = phi i32 [ 1000000, %2 ], [ %11, %8 ]
%15 = sitofp i32 %14 to double
%16 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), double %15)
%17 = bitcast %struct.timespec* %4 to i8*
%18 = bitcast %struct.timespec* %5 to i8*
br label %28
19: ; preds = %8
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = tail call i32 @atoi(i8* %21)
%23 = sitofp i32 %11 to double
%24 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), double %23)
%25 = bitcast %struct.timespec* %4 to i8*
%26 = bitcast %struct.timespec* %5 to i8*
%27 = icmp slt i32 %22, 1
br i1 %27, label %38, label %28
28: ; preds = %13, %19
%29 = phi i32 [ 10, %13 ], [ %22, %19 ]
%30 = phi i32 [ %14, %13 ], [ %11, %19 ]
%31 = icmp slt i32 %30, 1
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
br label %49
36: ; preds = %54
%37 = sitofp i64 %66 to double
br label %38
38: ; preds = %36, %19
%39 = phi double [ 1.000000e+09, %19 ], [ %37, %36 ]
%40 = load volatile double, double* %3
%41 = fmul double %40, 4.000000e+00
store volatile double %41, double* %3
%42 = load volatile double, double* %3
%43 = load i64, i64* @M_PI
%44 = sitofp i64 %43 to double
%45 = fsub double %42, %44
%46 = fptosi double %45 to i64
%47 = call double @fabs(i64 %46)
%48 = fcmp ogt double %47, 1.000000e-04
br i1 %48, label %82, label %85
49: ; preds = %28, %54
%50 = phi i32 [ 1, %28 ], [ %67, %54 ]
%51 = phi i64 [ 1000000000, %28 ], [ %66, %54 ]
store volatile double 0.000000e+00, double* %3
%52 = load i32, i32* @CLOCK_MONOTONIC
%53 = call i32 @clock_gettime(i32 %52, %struct.timespec* %4)
br i1 %31, label %54, label %69
54: ; preds = %69, %49
%55 = load i32, i32* @CLOCK_MONOTONIC
%56 = call i32 @clock_gettime(i32 %55, %struct.timespec* %5)
%57 = load i64, i64* %32
%58 = load i64, i64* %33
%59 = sub nsw i64 %57, %58
%60 = mul nsw i64 %59, 1000000000
%61 = load i64, i64* %34
%62 = add nsw i64 %60, %61
%63 = load i64, i64* %35
%64 = sub i64 %62, %63
%65 = icmp slt i64 %64, %51
%66 = select i1 %65, i64 %64, i64 %51
%67 = add nuw i32 %50, 1
%68 = icmp eq i32 %50, %29
br i1 %68, label %36, label %49
69: ; preds = %49, %69
%70 = phi i32 [ %80, %69 ], [ 1, %49 ]
%71 = sitofp i32 %70 to double
%72 = fadd double %71, 1.000000e+00
%73 = fptosi double %72 to i32
%74 = call i32 @pow(i32 -1, i32 %73)
%75 = sitofp i32 %74 to double
%76 = call double @llvm.fmuladd.f64(double %71, double 2.000000e+00, double -1.000000e+00)
%77 = fdiv double %75, %76
%78 = load volatile double, double* %3
%79 = fadd double %78, %77
store volatile double %79, double* %3
%80 = add nuw i32 %70, 1
%81 = icmp eq i32 %70, %30
br i1 %81, label %54, label %69
82: ; preds = %38
%83 = load i32, i32* @stderr
%84 = call i32 @fprintf(i32 %83, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), double %47)
br label %88
85: ; preds = %38
%86 = fdiv double %39, 1.000000e+09
%87 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), double %86)
br label %88
88: ; preds = %85, %82
%89 = phi i32* [ @EXIT_FAILURE, %82 ], [ @EXIT_SUCCESS, %85 ]
%90 = bitcast %struct.timespec* %4 to i8*
%91 = bitcast %struct.timespec* %5 to i8*
%92 = load i32, i32* %89
ret i32 %92
}