#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;
int atoi(char *);
scalar_t__ clock();
int /*<<< orphan*/ create_random_file(char *, int);
double left_riemann(double *, int);
double *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read_data_from_file(char *, int, double *);
double right_riemann(double *, int);
double trapezoidal_riemann(double *, int);
int main(int argc, char *argv[]) {
  printf("\n");
  if (argc < 2) {
    printf("Worng number of arguments.\nYou may execute with problem "
           "size.\n\n./riemann problem_size\n\nYou can also execute with data "
           "file\n\n./riemann problem_size filename\n\n");
    return -1;
  }
  int problem_size = atoi(argv[1]);
  char *filename = "data.dat";
  if (argc == 2)
    create_random_file(filename, problem_size);
  else
    filename = argv[2];
  double *data = malloc(sizeof(double) * problem_size * 2);
  read_data_from_file(filename, problem_size, data);
  double result;
  clock_t time = clock();
  result = left_riemann(data, problem_size);
  printf("Left Riemann: %f\n", result);
  result = right_riemann(data, problem_size);
  printf("Right Riemann: %f\n", result);
  result = trapezoidal_riemann(data, problem_size);
  printf("Trapezoidal Riemann: %f\n", result);
  time = clock() - time;
  printf("\nTime spent: %f seconds\n\n", ((double)time / CLOCKS_PER_SEC));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xeef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
jge    11d8 <main+0x48>
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <main+0x15d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
lea    0xeaf(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    120e <main+0x7e>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10a0 <create_random_file@plt>
jmp    121a <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
shl    $0x1,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1080 <read_data_from_file@plt>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <left_riemann@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
lea    0xe3e(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <right_riemann@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
lea    0xe2c(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10b0 <trapezoidal_riemann@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
lea    0xe1b(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <clock@plt>
sub    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
cvtsi2sdq -0x38(%rbp),%xmm0
mov    0x2d14(%rip),%rax
divsd  (%rax),%xmm0
lea    0xe06(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0xa,%edi
call   1030 <putchar@plt>
cmp    $0x1,%ebp
jg     11e3 <main+0x33>
lea    0xe8e(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%r15d
jmp    12ab <main+0xfb>
mov    0x8(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r12d
cmp    $0x2,%ebp
jne    1208 <main+0x58>
lea    0xe05(%rip),%r14
mov    %r14,%rdi
mov    %r12d,%esi
call   10c0 <create_random_file@plt>
jmp    120c <main+0x5c>
mov    0x10(%rbx),%r14
mov    %r12d,%edi
shl    $0x4,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r12d,%esi
mov    %rax,%rdx
call   10a0 <read_data_from_file@plt>
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <clock@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %r12d,%esi
call   1060 <left_riemann@plt>
lea    0xdc2(%rip),%rdi
mov    $0x1,%al
call   1070 <printf@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1080 <right_riemann@plt>
lea    0xdbb(%rip),%rdi
mov    $0x1,%al
call   1070 <printf@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   10d0 <trapezoidal_riemann@plt>
lea    0xdb5(%rip),%rdi
mov    $0x1,%al
call   1070 <printf@plt>
xor    %eax,%eax
call   1050 <clock@plt>
sub    %r14,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mov    0x2d4f(%rip),%rax
divsd  (%rax),%xmm0
lea    0xda3(%rip),%rdi
mov    $0x1,%al
call   1070 <printf@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
@.str.1 = [158 x i8] c"Worng number of arguments.\0AYou may execute with problem size.\0A\0A./riemann problem_size\0A\0AYou can also execute with data file\0A\0A./riemann problem_size filename\0A\0A\00"
@.str.2 = [9 x i8] c"data.dat\00"
@.str.3 = [18 x i8] c"Left Riemann: %f\0A\00"
@.str.4 = [19 x i8] c"Right Riemann: %f\0A\00"
@.str.5 = [25 x i8] c"Trapezoidal Riemann: %f\0A\00"
@.str.6 = [26 x i8] c"\0ATime spent: %f seconds\0A\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca double*
%9 = alloca double
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([158 x i8], [158 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %66
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %6
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8** %7
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 2
br i1 %22, label %23, label %27
23: ; preds = %16
%24 = load i8*, i8** %7
%25 = load i32, i32* %6
%26 = call i32 @create_random_file(i8* %24, i32 %25)
br label %31
27: ; preds = %16
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
store i8* %30, i8** %7
br label %31
31: ; preds = %27, %23
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = mul i64 8, %33
%35 = mul i64 %34, 2
%36 = trunc i64 %35 to i32
%37 = call double* @malloc(i32 %36)
store double* %37, double** %8
%38 = load i8*, i8** %7
%39 = load i32, i32* %6
%40 = load double*, double** %8
%41 = call i32 @read_data_from_file(i8* %38, i32 %39, double* %40)
%42 = call i64 (...) @clock()
store i64 %42, i64* %10
%43 = load double*, double** %8
%44 = load i32, i32* %6
%45 = call double @left_riemann(double* %43, i32 %44)
store double %45, double* %9
%46 = load double, double* %9
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), double %46)
%48 = load double*, double** %8
%49 = load i32, i32* %6
%50 = call double @right_riemann(double* %48, i32 %49)
store double %50, double* %9
%51 = load double, double* %9
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), double %51)
%53 = load double*, double** %8
%54 = load i32, i32* %6
%55 = call double @trapezoidal_riemann(double* %53, i32 %54)
store double %55, double* %9
%56 = load double, double* %9
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), double %56)
%58 = call i64 (...) @clock()
%59 = load i64, i64* %10
%60 = sub nsw i64 %58, %59
store i64 %60, i64* %10
%61 = load i64, i64* %10
%62 = sitofp i64 %61 to double
%63 = load double, double* @CLOCKS_PER_SEC
%64 = fdiv double %62, %63
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), double %64)
store i32 0, i32* %3
br label %66
66: ; preds = %31, %14
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str.2 = [9 x i8] c"data.dat\00"
@.str.3 = [18 x i8] c"Left Riemann: %f\0A\00"
@.str.4 = [19 x i8] c"Right Riemann: %f\0A\00"
@.str.5 = [25 x i8] c"Trapezoidal Riemann: %f\0A\00"
@.str.6 = [26 x i8] c"\0ATime spent: %f seconds\0A\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@str = [157 x i8] c"Worng number of arguments.\0AYou may execute with problem size.\0A\0A./riemann problem_size\0A\0AYou can also execute with data file\0A\0A./riemann problem_size filename\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @putchar(i32 10)
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([157 x i8], [157 x i8]* @str, i64 0, i64 0))
br label %35
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = icmp eq i32 %0, 2
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @create_random_file(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %10)
br label %17
14: ; preds = %7
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
br label %17
17: ; preds = %14, %12
%18 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), %12 ], [ %16, %14 ]
%19 = shl i32 %10, 4
%20 = tail call double* @malloc(i32 %19)
%21 = tail call i32 @read_data_from_file(i8* %18, i32 %10, double* %20)
%22 = tail call i64 (...) @clock()
%23 = tail call double @left_riemann(double* %20, i32 %10)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), double %23)
%25 = tail call double @right_riemann(double* %20, i32 %10)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), double %25)
%27 = tail call double @trapezoidal_riemann(double* %20, i32 %10)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), double %27)
%29 = tail call i64 (...) @clock()
%30 = sub nsw i64 %29, %22
%31 = sitofp i64 %30 to double
%32 = load double, double* @CLOCKS_PER_SEC
%33 = fdiv double %31, %32
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), double %33)
br label %35
35: ; preds = %17, %5
%36 = phi i32 [ -1, %5 ], [ 0, %17 ]
ret i32 %36
}