#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 timespec {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
double atan(double);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ elapsed_time_hr(struct timespec, struct timespec, char *);
double fabs(double);
double piapprox(int, unsigned int);
int /*<<< orphan*/ printf(char *, double, double);
int /*<<< orphan*/ sleep(int);
int function_calls(unsigned int N) {
  if (N < 1000000) {
    N = 1000000;
  }
  double pi, exactpi;
  int i;
  struct timespec start, end;
  pi = 0.0;
#pragma omp parallel private(i) firstprivate(pi)
  for (i = 1; i <= 1000; i++) {
    pi = pi + 1.0 / (1.0 + (((double)i) - 0.5) / ((double)1000) *
                               (((double)i) - 0.5) / ((double)1000));
  }
  clock_gettime(CLOCK_MONOTONIC, &start);
#pragma omp parallel private(i) firstprivate(pi)
  for (i = N; i >= 1; i--) {
    pi = pi + 1.0 / (1.0 + (((double)i) - 0.5) / ((double)N) *
                               (((double)i) - 0.5) / ((double)N));
  }
  clock_gettime(CLOCK_MONOTONIC, &end);
  elapsed_time_hr(start, end, "No Function Calls");
  pi = pi * 4.0 / ((double)N);
  exactpi = 4.0 * atan(1.0);
  sleep(5);
  pi = 0.0;
#pragma omp parallel private(i) firstprivate(pi)
  for (i = 1; i <= 1000; i++) {
    pi = pi + piapprox(i, 1000);
  }
  clock_gettime(CLOCK_MONOTONIC, &start);
#pragma omp parallel private(i) firstprivate(pi)
  for (i = N; i >= 1; i--) {
    pi = pi + piapprox(i, N);
  }
  clock_gettime(CLOCK_MONOTONIC, &end);
  elapsed_time_hr(start, end, "Function Calls");
  pi = pi * 4.0 / ((double)N);
  exactpi = 4.0 * atan(1.0);
  printf("pi = %f, %% error = %f\n", pi,
         fabs(100.0 * (pi - exactpi) / exactpi));
  return 0;
}


x86 assembly (clang, -O0):
<function_calls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0xf4240,-0x4(%rbp)
jae    117f <function_calls+0x1f>
movl   $0xf4240,-0x4(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x3e8,-0x1c(%rbp)
jg     120d <function_calls+0xad>
movsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm1
movsd  0xe7b(%rip),%xmm2
subsd  %xmm2,%xmm1
movsd  0xe67(%rip),%xmm2
divsd  %xmm2,%xmm1
cvtsi2sdl -0x1c(%rbp),%xmm2
movsd  0xe5e(%rip),%xmm3
subsd  %xmm3,%xmm2
mulsd  %xmm2,%xmm1
movsd  0xe46(%rip),%xmm2
divsd  %xmm2,%xmm1
movsd  0xe22(%rip),%xmm2
addsd  %xmm1,%xmm2
movsd  0xe16(%rip),%xmm1
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118e <function_calls+0x2e>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jl     128b <function_calls+0x12b>
movsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm3
movsd  0xdd7(%rip),%xmm1
addsd  %xmm1,%xmm3
mov    -0x4(%rbp),%eax
cvtsi2sd %rax,%xmm2
movaps %xmm3,%xmm1
divsd  %xmm2,%xmm1
mulsd  %xmm3,%xmm1
divsd  %xmm2,%xmm1
movsd  0xda4(%rip),%xmm2
addsd  %xmm1,%xmm2
movsd  0xd98(%rip),%xmm1
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1225 <function_calls+0xc5>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x20(%rbp),%edi
mov    -0x28(%rbp),%esi
lea    0xd96(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
movsd  -0x10(%rbp),%xmm0
movsd  0xd44(%rip),%xmm1
mulsd  %xmm1,%xmm0
mov    -0x4(%rbp),%eax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  0xd2f(%rip),%xmm0
call   1070 <atan@plt>
movaps %xmm0,%xmm1
movsd  0xd17(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    $0x5,%edi
call   1080 <sleep@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x3e8,-0x1c(%rbp)
jg     134e <function_calls+0x1ee>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x3e8,%esi
call   1060 <piapprox@plt>
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    130b <function_calls+0x1ab>
mov    0x2c93(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jl     13a4 <function_calls+0x244>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1060 <piapprox@plt>
movaps %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1366 <function_calls+0x206>
mov    0x2c3d(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x20(%rbp),%edi
mov    -0x28(%rbp),%esi
lea    0xc80(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
movsd  -0x10(%rbp),%xmm0
movsd  0xc2b(%rip),%xmm1
mulsd  %xmm1,%xmm0
mov    -0x4(%rbp),%eax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  0xc16(%rip),%xmm0
call   1070 <atan@plt>
movaps %xmm0,%xmm1
movsd  0xbfe(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
subsd  -0x18(%rbp),%xmm2
movsd  0xbee(%rip),%xmm1
mulsd  %xmm2,%xmm1
divsd  -0x18(%rbp),%xmm1
movaps 0xbfe(%rip),%xmm2
pand   %xmm2,%xmm1
lea    0xc15(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<function_calls>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
cmp    $0xf4241,%edi
mov    $0xf4240,%ebx
cmovae %edi,%ebx
mov    0x2e6b(%rip),%r14
mov    (%r14),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    0x10(%rsp),%edi
mov    0x8(%rsp),%esi
lea    0xe9a(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
movsd  0xe4d(%rip),%xmm0
call   1070 <atan@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
xorpd  %xmm0,%xmm0
mov    $0x1,%ebp
nopl   0x0(%rax,%rax,1)
movsd  %xmm0,(%rsp)
mov    %ebp,%edi
mov    $0x3e8,%esi
call   1060 <piapprox@plt>
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  (%rsp),%xmm0
add    $0x1,%ebp
cmp    $0x3e9,%ebp
jne    11d0 <function_calls+0x70>
mov    (%r14),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
test   %ebx,%ebx
jle    1241 <function_calls+0xe1>
mov    %ebx,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %ebx,%esi
call   1060 <piapprox@plt>
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
lea    -0x1(%rbp),%eax
cmp    $0x1,%ebp
mov    %eax,%ebp
ja     1220 <function_calls+0xc0>
mov    %ebx,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  %xmm0,0x18(%rsp)
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    0x10(%rsp),%edi
mov    0x8(%rsp),%esi
lea    0xdd6(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
movsd  (%rsp),%xmm0
mulsd  0xd89(%rip),%xmm0
divsd  0x18(%rsp),%xmm0
movsd  %xmm0,(%rsp)
movsd  0xd6e(%rip),%xmm0
call   1070 <atan@plt>
movsd  0xd71(%rip),%xmm1
movsd  (%rsp),%xmm0
addsd  %xmm0,%xmm1
mulsd  0xd68(%rip),%xmm1
divsd  0xd68(%rip),%xmm1
andpd  0xd70(%rip),%xmm1
lea    0xd8b(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@.str = [18 x i8] c"No Function Calls\00"
@.str.1 = [15 x i8] c"Function Calls\00"
@.str.2 = [24 x i8] c"pi = %f, %% error = %f\0A\00"
define i32 @function_calls(i32 %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca double
%5 = alloca i32
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
store i32 %0, i32* %2
%8 = load i32, i32* %2
%9 = icmp ult i32 %8, 1000000
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 1000000, i32* %2
br label %11
11: ; preds = %10, %1
store double 0.000000e+00, double* %3
store i32 1, i32* %5
br label %12
12: ; preds = %29, %11
%13 = load i32, i32* %5
%14 = icmp sle i32 %13, 1000
br i1 %14, label %15, label %32
15: ; preds = %12
%16 = load double, double* %3
%17 = load i32, i32* %5
%18 = sitofp i32 %17 to double
%19 = fsub double %18, 5.000000e-01
%20 = fdiv double %19, 1.000000e+03
%21 = load i32, i32* %5
%22 = sitofp i32 %21 to double
%23 = fsub double %22, 5.000000e-01
%24 = fmul double %20, %23
%25 = fdiv double %24, 1.000000e+03
%26 = fadd double 1.000000e+00, %25
%27 = fdiv double 1.000000e+00, %26
%28 = fadd double %16, %27
store double %28, double* %3
br label %29
29: ; preds = %15
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
br label %12
32: ; preds = %12
%33 = load i32, i32* @CLOCK_MONOTONIC
%34 = call i32 @clock_gettime(i32 %33, %struct.timespec* %6)
%35 = load i32, i32* %2
store i32 %35, i32* %5
br label %36
36: ; preds = %57, %32
%37 = load i32, i32* %5
%38 = icmp sge i32 %37, 1
br i1 %38, label %39, label %60
39: ; preds = %36
%40 = load double, double* %3
%41 = load i32, i32* %5
%42 = sitofp i32 %41 to double
%43 = fsub double %42, 5.000000e-01
%44 = load i32, i32* %2
%45 = uitofp i32 %44 to double
%46 = fdiv double %43, %45
%47 = load i32, i32* %5
%48 = sitofp i32 %47 to double
%49 = fsub double %48, 5.000000e-01
%50 = fmul double %46, %49
%51 = load i32, i32* %2
%52 = uitofp i32 %51 to double
%53 = fdiv double %50, %52
%54 = fadd double 1.000000e+00, %53
%55 = fdiv double 1.000000e+00, %54
%56 = fadd double %40, %55
store double %56, double* %3
br label %57
57: ; preds = %39
%58 = load i32, i32* %5
%59 = add nsw i32 %58, -1
store i32 %59, i32* %5
br label %36
60: ; preds = %36
%61 = load i32, i32* @CLOCK_MONOTONIC
%62 = call i32 @clock_gettime(i32 %61, %struct.timespec* %7)
%63 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%64 = load i32, i32* %63
%65 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call i32 @elapsed_time_hr(i32 %64, i32 %66, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%68 = load double, double* %3
%69 = fmul double %68, 4.000000e+00
%70 = load i32, i32* %2
%71 = uitofp i32 %70 to double
%72 = fdiv double %69, %71
store double %72, double* %3
%73 = call double @atan(double 1.000000e+00)
%74 = fmul double 4.000000e+00, %73
store double %74, double* %4
%75 = call i32 @sleep(i32 5)
store double 0.000000e+00, double* %3
store i32 1, i32* %5
br label %76
76: ; preds = %84, %60
%77 = load i32, i32* %5
%78 = icmp sle i32 %77, 1000
br i1 %78, label %79, label %87
79: ; preds = %76
%80 = load double, double* %3
%81 = load i32, i32* %5
%82 = call double @piapprox(i32 %81, i32 1000)
%83 = fadd double %80, %82
store double %83, double* %3
br label %84
84: ; preds = %79
%85 = load i32, i32* %5
%86 = add nsw i32 %85, 1
store i32 %86, i32* %5
br label %76
87: ; preds = %76
%88 = load i32, i32* @CLOCK_MONOTONIC
%89 = call i32 @clock_gettime(i32 %88, %struct.timespec* %6)
%90 = load i32, i32* %2
store i32 %90, i32* %5
br label %91
91: ; preds = %100, %87
%92 = load i32, i32* %5
%93 = icmp sge i32 %92, 1
br i1 %93, label %94, label %103
94: ; preds = %91
%95 = load double, double* %3
%96 = load i32, i32* %5
%97 = load i32, i32* %2
%98 = call double @piapprox(i32 %96, i32 %97)
%99 = fadd double %95, %98
store double %99, double* %3
br label %100
100: ; preds = %94
%101 = load i32, i32* %5
%102 = add nsw i32 %101, -1
store i32 %102, i32* %5
br label %91
103: ; preds = %91
%104 = load i32, i32* @CLOCK_MONOTONIC
%105 = call i32 @clock_gettime(i32 %104, %struct.timespec* %7)
%106 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%107 = load i32, i32* %106
%108 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%109 = load i32, i32* %108
%110 = call i32 @elapsed_time_hr(i32 %107, i32 %109, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%111 = load double, double* %3
%112 = fmul double %111, 4.000000e+00
%113 = load i32, i32* %2
%114 = uitofp i32 %113 to double
%115 = fdiv double %112, %114
store double %115, double* %3
%116 = call double @atan(double 1.000000e+00)
%117 = fmul double 4.000000e+00, %116
store double %117, double* %4
%118 = load double, double* %3
%119 = load double, double* %3
%120 = load double, double* %4
%121 = fsub double %119, %120
%122 = fmul double 1.000000e+02, %121
%123 = load double, double* %4
%124 = fdiv double %122, %123
%125 = call double @llvm.fabs.f64(double %124)
%126 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), double %118, double %125)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@.str = [18 x i8] c"No Function Calls\00"
@.str.1 = [15 x i8] c"Function Calls\00"
@.str.2 = [24 x i8] c"pi = %f, %% error = %f\0A\00"
define i32 @function_calls(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = icmp ugt i32 %0, 1000000
%5 = bitcast %struct.timespec* %2 to i8*
%6 = bitcast %struct.timespec* %3 to i8*
%7 = select i1 %4, i32 %0, i32 1000000
%8 = load i32, i32* @CLOCK_MONOTONIC
%9 = call i32 @clock_gettime(i32 %8, %struct.timespec* %2)
%10 = load i32, i32* @CLOCK_MONOTONIC
%11 = call i32 @clock_gettime(i32 %10, %struct.timespec* %3)
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @elapsed_time_hr(i32 %13, i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%17 = call double @atan(double 1.000000e+00)
%18 = call i32 @sleep(i32 5)
br label %19
19: ; preds = %1, %19
%20 = phi i32 [ 1, %1 ], [ %24, %19 ]
%21 = phi double [ 0.000000e+00, %1 ], [ %23, %19 ]
%22 = call double @piapprox(i32 %20, i32 1000)
%23 = fadd double %21, %22
%24 = add nuw nsw i32 %20, 1
%25 = icmp eq i32 %24, 1001
br i1 %25, label %26, label %19
26: ; preds = %19
%27 = load i32, i32* @CLOCK_MONOTONIC
%28 = call i32 @clock_gettime(i32 %27, %struct.timespec* %2)
%29 = icmp sgt i32 %7, 0
br i1 %29, label %30, label %37
30: ; preds = %26, %30
%31 = phi i32 [ %35, %30 ], [ %7, %26 ]
%32 = phi double [ %34, %30 ], [ %23, %26 ]
%33 = call double @piapprox(i32 %31, i32 %7)
%34 = fadd double %32, %33
%35 = add nsw i32 %31, -1
%36 = icmp ugt i32 %31, 1
br i1 %36, label %30, label %37
37: ; preds = %30, %26
%38 = phi double [ %23, %26 ], [ %34, %30 ]
%39 = uitofp i32 %7 to double
%40 = load i32, i32* @CLOCK_MONOTONIC
%41 = call i32 @clock_gettime(i32 %40, %struct.timespec* %3)
%42 = load i32, i32* %12
%43 = load i32, i32* %14
%44 = call i32 @elapsed_time_hr(i32 %42, i32 %43, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%45 = fmul double %38, 4.000000e+00
%46 = fdiv double %45, %39
%47 = call double @atan(double 1.000000e+00)
%48 = fadd double %46, 0xC00921FB54442D18
%49 = fmul double %48, 1.000000e+02
%50 = fdiv double %49, 0x400921FB54442D18
%51 = call double @llvm.fabs.f64(double %50)
%52 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), double %46, double %51)
ret i32 0
}