#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 */
scalar_t__ local_trap(double, double, int);
double parallel_trap(double, double, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, double *, double *, int *);
int strtol(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ trap(double, double, int, double *);
int main(int argc, char *argv[]) {
  int n;
  int thread_count = 1;
  double a;
  double b;
  double global_result = 0.0;
  printf("Enter a, b and n:\n");
  scanf("%lf %lf %d", &a, &b, &n);
  if (argc == 2) {
    thread_count = strtol(argv[1], NULL, 10);
  }
#pragma omp parallel num_threads(thread_count)
  trap(a, b, n, &global_result);
  printf("Version 1: With n = %d trapezoids, our estimate\n", n);
  printf("of the integral from %f to %f = %.14e\n", a, b, global_result);
  global_result = 0.0;
#pragma omp parallel num_threads(thread_count) reduction(+ : global_result)
  global_result += local_trap(a, b, n);
  printf("\n");
  printf("Version 2: With n = %d trapezoids, our estimate\n", n);
  printf("of the integral from %f to %f = %.14e\n", a, b, global_result);
  global_result = 0.0;
  global_result = parallel_trap(a, b, n);
  printf("\n");
  printf("Version 3: With n = %d trapezoids, our estimate\n", n);
  printf("of the integral from %f to %f = %.14e\n", a, b, global_result);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe79(%rip),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x14(%rbp),%rcx
call   1070 <scanf@plt>
cmpl   $0x2,-0x8(%rbp)
jne    11ce <main+0x6e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x18(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1080 <trap@plt>
mov    -0x14(%rbp),%esi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm2
lea    0xe44(%rip),%rdi
mov    $0x3,%al
call   1040 <printf@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
mov    -0x14(%rbp),%edi
call   1050 <local_trap@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm2
lea    0xddf(%rip),%rdi
mov    $0x3,%al
call   1040 <printf@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
mov    -0x14(%rbp),%edi
call   1030 <parallel_trap@plt>
movsd  %xmm0,-0x30(%rbp)
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm2
lea    0xd84(%rip),%rdi
mov    $0x3,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
movq   $0x0,0x10(%rsp)
lea    0xf2a(%rip),%rdi
call   1050 <puts@plt>
lea    0xe59(%rip),%rdi
lea    0x20(%rsp),%rsi
lea    0x18(%rsp),%rdx
lea    0xc(%rsp),%rcx
call   1090 <scanf@plt>
cmp    $0x2,%ebp
jne    11d0 <main+0x50>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
movsd  0x20(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
call   10a0 <trap@plt>
mov    0xc(%rsp),%esi
lea    0xe16(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
movsd  0x20(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
movsd  0x10(%rsp),%xmm2
lea    0xe27(%rip),%rbx
mov    %rbx,%rdi
mov    $0x3,%al
call   1060 <printf@plt>
movq   $0x0,0x10(%rsp)
movsd  0x20(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
mov    0xc(%rsp),%edi
call   1070 <local_trap@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
addsd  0x10(%rsp),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0xc(%rsp),%esi
lea    0xdfd(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
movsd  0x20(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
movsd  0x10(%rsp),%xmm2
mov    %rbx,%rdi
mov    $0x3,%al
call   1060 <printf@plt>
movq   $0x0,0x10(%rsp)
movsd  0x20(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
mov    0xc(%rsp),%edi
call   1040 <parallel_trap@plt>
movsd  %xmm0,0x10(%rsp)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0xc(%rsp),%esi
lea    0xdd2(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
movsd  0x20(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
movsd  0x10(%rsp),%xmm2
mov    %rbx,%rdi
mov    $0x3,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"Enter a, b and n:\0A\00"
@.str.1 = [11 x i8] c"%lf %lf %d\00"
@.str.2 = [49 x i8] c"Version 1: With n = %d trapezoids, our estimate\0A\00"
@.str.3 = [39 x i8] c"of the integral from %f to %f = %.14e\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [49 x i8] c"Version 2: With n = %d trapezoids, our estimate\0A\00"
@.str.6 = [49 x i8] c"Version 3: With n = %d trapezoids, our estimate\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 double
%9 = alloca double
%10 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %7
store double 0.000000e+00, double* %10
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), double* %8, double* %9, i32* %6)
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @strtol(i8* %18, i32* null, i32 10)
store i32 %19, i32* %7
br label %20
20: ; preds = %15, %2
%21 = load double, double* %8
%22 = load double, double* %9
%23 = load i32, i32* %6
%24 = call i32 @trap(double %21, double %22, i32 %23, double* %10)
%25 = load i32, i32* %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = load double, double* %8
%28 = load double, double* %9
%29 = load double, double* %10
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), double %27, double %28, double %29)
store double 0.000000e+00, double* %10
%31 = load double, double* %8
%32 = load double, double* %9
%33 = load i32, i32* %6
%34 = call i64 @local_trap(double %31, double %32, i32 %33)
%35 = sitofp i64 %34 to double
%36 = load double, double* %10
%37 = fadd double %36, %35
store double %37, double* %10
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%39 = load i32, i32* %6
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0), i32 %39)
%41 = load double, double* %8
%42 = load double, double* %9
%43 = load double, double* %10
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), double %41, double %42, double %43)
store double 0.000000e+00, double* %10
%45 = load double, double* %8
%46 = load double, double* %9
%47 = load i32, i32* %6
%48 = call double @parallel_trap(double %45, double %46, i32 %47)
store double %48, double* %10
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%50 = load i32, i32* %6
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.6, i64 0, i64 0), i32 %50)
%52 = load double, double* %8
%53 = load double, double* %9
%54 = load double, double* %10
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), double %52, double %53, double %54)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"%lf %lf %d\00"
@.str.2 = [49 x i8] c"Version 1: With n = %d trapezoids, our estimate\0A\00"
@.str.3 = [39 x i8] c"of the integral from %f to %f = %.14e\0A\00"
@.str.5 = [49 x i8] c"Version 2: With n = %d trapezoids, our estimate\0A\00"
@.str.6 = [49 x i8] c"Version 3: With n = %d trapezoids, our estimate\0A\00"
@str = [18 x i8] c"Enter a, b and n:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = bitcast i32* %3 to i8*
%8 = bitcast double* %4 to i8*
%9 = bitcast double* %5 to i8*
%10 = bitcast double* %6 to i8*
store double 0.000000e+00, double* %6
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), double* %4, double* %5, i32* %3)
%13 = icmp eq i32 %0, 2
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @strtol(i8* nocapture %16, i32* null, i32 10)
br label %18
18: ; preds = %14, %2
%19 = load double, double* %4
%20 = load double, double* %5
%21 = load i32, i32* %3
%22 = call i32 @trap(double %19, double %20, i32 %21, double* %6)
%23 = load i32, i32* %3
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = load double, double* %4
%26 = load double, double* %5
%27 = load double, double* %6
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), double %25, double %26, double %27)
store double 0.000000e+00, double* %6
%29 = load double, double* %4
%30 = load double, double* %5
%31 = load i32, i32* %3
%32 = call i64 @local_trap(double %29, double %30, i32 %31)
%33 = sitofp i64 %32 to double
%34 = load double, double* %6
%35 = fadd double %34, %33
store double %35, double* %6
%36 = call i32 @putchar(i32 10)
%37 = load i32, i32* %3
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0), i32 %37)
%39 = load double, double* %4
%40 = load double, double* %5
%41 = load double, double* %6
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), double %39, double %40, double %41)
store double 0.000000e+00, double* %6
%43 = load double, double* %4
%44 = load double, double* %5
%45 = load i32, i32* %3
%46 = call double @parallel_trap(double %43, double %44, i32 %45)
store double %46, double* %6
%47 = call i32 @putchar(i32 10)
%48 = load i32, i32* %3
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.6, i64 0, i64 0), i32 %48)
%50 = load double, double* %4
%51 = load double, double* %5
%52 = load double, double* %6
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), double %50, double %51, double %52)
ret i32 0
}