#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 timeval {
  double tv_sec;
  double tv_usec;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, double);
int /*<<< orphan*/ generate_random_array(int *, int);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ quick_sort(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ system(char *);
int main() {
  printf("Quick sort algorithm\n");
  int i, j;
  int arr[10000];
  struct timeval t;
  FILE *fp;
  fp = fopen("exec_time.txt", "w");
  for (i = 100; i <= 10000; i += 100) {
    printf("\nGenerating random array of size %d \n", i);
    generate_random_array(arr, i);
    printf("\nSorting");
    gettimeofday(&t, NULL);
    double start = t.tv_sec + t.tv_usec / 1000000.0;
    quick_sort(arr, 0, i - 1);
    gettimeofday(&t, NULL);
    double end = t.tv_sec + t.tv_usec / 1000000.0;
    fprintf(fp, "%d\t%lf\n", i, (end - start));
  }
  fclose(fp);
  system("gnuplot");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x9c80,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe77(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x9c68(%rbp)
movl   $0x64,-0x8(%rbp)
cmpl   $0x2710,-0x8(%rbp)
jg     12b4 <main+0x134>
mov    -0x8(%rbp),%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x9c50(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1080 <generate_random_array@plt>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x9c60(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
movsd  -0x9c60(%rbp),%xmm0
ff
movsd  -0x9c58(%rbp),%xmm1
ff
movsd  0xddc(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x9c70(%rbp)
ff
lea    -0x9c50(%rbp),%rdi
mov    -0x8(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1090 <quick_sort@plt>
lea    -0x9c60(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
movsd  -0x9c60(%rbp),%xmm0
ff
movsd  -0x9c58(%rbp),%xmm1
ff
movsd  0xd90(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x9c78(%rbp)
ff
mov    -0x9c68(%rbp),%rdi
mov    -0x8(%rbp),%edx
movsd  -0x9c78(%rbp),%xmm0
ff
subsd  -0x9c70(%rbp),%xmm0
ff
lea    0xdbc(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%eax
add    $0x64,%eax
mov    %eax,-0x8(%rbp)
jmp    11c1 <main+0x41>
mov    -0x9c68(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xd9e(%rip),%rdi
call   1040 <system@plt>
xor    %eax,%eax
add    $0x9c80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x9c78,%rsp
lea    0xeb7(%rip),%rdi
call   1030 <puts@plt>
lea    0xe5c(%rip),%rdi
lea    0xe63(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r14
mov    $0x64,%ebx
lea    0x30(%rsp),%r12
lea    0xe72(%rip),%r13
mov    %rsp,%rbp
lea    0xe71(%rip),%r15
xchg   %ax,%ax
lea    0xe39(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   1090 <generate_random_array@plt>
mov    %r13,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
lea    -0x1(%rbx),%edx
movsd  (%rsp),%xmm0
movaps %xmm0,0x20(%rsp)
movsd  0x8(%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
mov    %r12,%rdi
xor    %esi,%esi
call   10a0 <quick_sort@plt>
mov    %rbp,%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
movapd (%rsp),%xmm0
movapd %xmm0,%xmm1
shufpd $0x1,0x10(%rsp),%xmm1
divpd  0xdae(%rip),%xmm1
unpcklpd 0x20(%rsp),%xmm0
addpd  %xmm1,%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
subsd  %xmm1,%xmm0
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1080 <fprintf@plt>
lea    0x64(%rbx),%eax
cmp    $0x26ad,%ebx
mov    %eax,%ebx
jb     11e0 <main+0x50>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xdc2(%rip),%rdi
call   1050 <system@plt>
xor    %eax,%eax
add    $0x9c78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { double, double }
@.str = [22 x i8] c"Quick sort algorithm\0A\00"
@.str.1 = [14 x i8] c"exec_time.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [38 x i8] c"\0AGenerating random array of size %d \0A\00"
@.str.4 = [9 x i8] c"\0ASorting\00"
@.str.5 = [8 x i8] c"%d\09%lf\0A\00"
@.str.6 = [8 x i8] c"gnuplot\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [10000 x i32]
%5 = alloca %struct.timeval
%6 = alloca i32*
%7 = alloca double
%8 = alloca double
store i32 0, i32* %1
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%10 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %10, i32** %6
store i32 100, i32* %2
br label %11
11: ; preds = %45, %0
%12 = load i32, i32* %2
%13 = icmp sle i32 %12, 10000
br i1 %13, label %14, label %48
14: ; preds = %11
%15 = load i32, i32* %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [10000 x i32], [10000 x i32]* %4, i64 0, i64 0
%18 = load i32, i32* %2
%19 = call i32 @generate_random_array(i32* %17, i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%21 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%23 = load double, double* %22
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%25 = load double, double* %24
%26 = fdiv double %25, 1.000000e+06
%27 = fadd double %23, %26
store double %27, double* %7
%28 = getelementptr inbounds [10000 x i32], [10000 x i32]* %4, i64 0, i64 0
%29 = load i32, i32* %2
%30 = sub nsw i32 %29, 1
%31 = call i32 @quick_sort(i32* %28, i32 0, i32 %30)
%32 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%34 = load double, double* %33
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%36 = load double, double* %35
%37 = fdiv double %36, 1.000000e+06
%38 = fadd double %34, %37
store double %38, double* %8
%39 = load i32*, i32** %6
%40 = load i32, i32* %2
%41 = load double, double* %8
%42 = load double, double* %7
%43 = fsub double %41, %42
%44 = call i32 @fprintf(i32* %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %40, double %43)
br label %45
45: ; preds = %14
%46 = load i32, i32* %2
%47 = add nsw i32 %46, 100
store i32 %47, i32* %2
br label %11
48: ; preds = %11
%49 = load i32*, i32** %6
%50 = call i32 @fclose(i32* %49)
%51 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { double, double }
@.str.1 = [14 x i8] c"exec_time.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [38 x i8] c"\0AGenerating random array of size %d \0A\00"
@.str.4 = [9 x i8] c"\0ASorting\00"
@.str.5 = [8 x i8] c"%d\09%lf\0A\00"
@.str.6 = [8 x i8] c"gnuplot\00"
@str = [21 x i8] c"Quick sort algorithm\00"
define i32 @main() {
%1 = alloca [10000 x i32]
%2 = alloca <2 x double>
%3 = bitcast <2 x double>* %2 to %struct.timeval*
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%5 = bitcast [10000 x i32]* %1 to i8*
%6 = bitcast <2 x double>* %2 to i8*
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%8 = getelementptr inbounds [10000 x i32], [10000 x i32]* %1, i64 0, i64 0
%9 = getelementptr inbounds <2 x double>, <2 x double>* %2, i64 0, i64 0
%10 = getelementptr inbounds <2 x double>, <2 x double>* %2, i64 0, i64 1
br label %11
11: ; preds = %0, %11
%12 = phi i32 [ 100, %0 ], [ %32, %11 ]
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = call i32 @generate_random_array(i32* %8, i32 %12)
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%16 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%17 = load double, double* %9
%18 = load double, double* %10
%19 = add nsw i32 %12, -1
%20 = call i32 @quick_sort(i32* %8, i32 0, i32 %19)
%21 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%22 = load <2 x double>, <2 x double>* %2
%23 = shufflevector <2 x double> %22, <2 x double> poison, <2 x i32> <i32 1, i32 undef>
%24 = insertelement <2 x double> %23, double %18, i64 1
%25 = fdiv <2 x double> %24, <double 1.000000e+06, double 1.000000e+06>
%26 = insertelement <2 x double> %22, double %17, i64 1
%27 = fadd <2 x double> %26, %25
%28 = shufflevector <2 x double> %27, <2 x double> poison, <2 x i32> <i32 1, i32 undef>
%29 = fsub <2 x double> %27, %28
%30 = extractelement <2 x double> %29, i64 0
%31 = call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %12, double %30)
%32 = add nuw nsw i32 %12, 100
%33 = icmp ult i32 %12, 9901
br i1 %33, label %11, label %34
34: ; preds = %11
%35 = call i32 @fclose(i32* %7)
%36 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}