#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ display(int *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ quicksort(int *, int /*<<< orphan*/, int);
scalar_t__ realloc(int *, int);
int main(int argc, char *argv[]) {
  int *array = NULL;
  int length = 0;
  FILE *fh;
  int data;
  if (argc != 2) {
    printf("usage: %s <filename>\n", argv[0]);
    return 1;
  }
  printf("attempting to sort file: %s\n", argv[1]);
  fh = fopen(argv[1], "r");
  if (fh == NULL) {
    printf("error opening file\n");
    return 0;
  }
  while (fscanf(fh, "%d", &data) != EOF) {
    ++length;
    array = (int *)realloc(array, length * sizeof(int));
    array[length - 1] = data;
    display(array, length);
  }
  fclose(fh);
  printf("%d elements read\n", length);
  display(array, length);
  quicksort(array, 0, length - 1);
  display(array, length);
  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)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c0 <main+0x50>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ca <main+0x15a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe4e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1213 <main+0xa3>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ca <main+0x15a>
jmp    1218 <main+0xa8>
mov    -0x28(%rbp),%rdi
lea    0xe26(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1070 <fscanf@plt>
mov    0x2db5(%rip),%rcx
cmp    (%rcx),%rax
je     1280 <main+0x110>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
call   1080 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x2c(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <display@plt>
jmp    1218 <main+0xa8>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%esi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <display@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1050 <quicksort@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <display@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11e5 <main+0x65>
mov    0x8(%rbx),%rsi
lea    0xe78(%rip),%rdi
xor    %r12d,%r12d
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x8(%rbx),%rdi
lea    0xe80(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1200 <main+0x80>
mov    %rax,%r14
lea    0xe6e(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1080 <fscanf@plt>
mov    0x2e0d(%rip),%r13
cmp    0x0(%r13),%rax
jne    1213 <main+0x93>
xor    %ebx,%ebx
jmp    1264 <main+0xe4>
mov    (%rbx),%rsi
lea    0xe11(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%ebp
jmp    12a4 <main+0x124>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
jmp    12a4 <main+0x124>
mov    $0x4,%ebp
xor    %r12d,%r12d
xor    %ebx,%ebx
nopl   (%rax)
mov    %r12,%r15
add    $0x1,%r12
mov    %rbx,%rdi
mov    %ebp,%esi
call   1090 <realloc@plt>
mov    %rax,%rbx
mov    0x4(%rsp),%eax
mov    %eax,(%rbx,%r15,4)
mov    %rbx,%rdi
mov    %r12d,%esi
call   1050 <display@plt>
mov    %r14,%rdi
lea    0xde4(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1080 <fscanf@plt>
add    $0x4,%ebp
cmp    0x0(%r13),%rax
jne    1220 <main+0xa0>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xdc5(%rip),%rdi
xor    %ebp,%ebp
mov    %r12d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1050 <display@plt>
lea    -0x1(%r12),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <quicksort@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1050 <display@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"usage: %s <filename>\0A\00"
@.str.1 = [29 x i8] c"attempting to sort file: %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [20 x i8] c"error opening file\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.5 = [18 x i8] c"%d elements read\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 i32*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %6
store i32 0, i32* %7
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %70
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %25, i32** %8
%26 = load i32*, i32** %8
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %70
30: ; preds = %17
br label %31
31: ; preds = %36, %30
%32 = load i32*, i32** %8
%33 = call i64 @fscanf(i32* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %9)
%34 = load i64, i64* @EOF
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %55
36: ; preds = %31
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
%39 = load i32*, i32** %6
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = mul i64 %41, 4
%43 = trunc i64 %42 to i32
%44 = call i64 @realloc(i32* %39, i32 %43)
%45 = inttoptr i64 %44 to i32*
store i32* %45, i32** %6
%46 = load i32, i32* %9
%47 = load i32*, i32** %6
%48 = load i32, i32* %7
%49 = sub nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i32, i32* %47, i64 %50
store i32 %46, i32* %51
%52 = load i32*, i32** %6
%53 = load i32, i32* %7
%54 = call i32 @display(i32* %52, i32 %53)
br label %31
55: ; preds = %31
%56 = load i32*, i32** %8
%57 = call i32 @fclose(i32* %56)
%58 = load i32, i32* %7
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %58)
%60 = load i32*, i32** %6
%61 = load i32, i32* %7
%62 = call i32 @display(i32* %60, i32 %61)
%63 = load i32*, i32** %6
%64 = load i32, i32* %7
%65 = sub nsw i32 %64, 1
%66 = call i32 @quicksort(i32* %63, i32 0, i32 %65)
%67 = load i32*, i32** %6
%68 = load i32, i32* %7
%69 = call i32 @display(i32* %67, i32 %68)
store i32 0, i32* %3
br label %70
70: ; preds = %55, %28, %12
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@.str = [22 x i8] c"usage: %s <filename>\0A\00"
@.str.1 = [29 x i8] c"attempting to sort file: %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.5 = [18 x i8] c"%d elements read\0A\00"
@str = [19 x i8] c"error opening file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %48
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = load i8*, i8** %10
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %20, label %16
16: ; preds = %9
%17 = call i64 @fscanf(i32* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%18 = load i64, i64* @EOF
%19 = icmp eq i64 %17, %18
br i1 %19, label %39, label %22
20: ; preds = %9
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %48
22: ; preds = %16, %22
%23 = phi i64 [ %25, %22 ], [ 0, %16 ]
%24 = phi i32* [ %29, %22 ], [ null, %16 ]
%25 = add nuw i64 %23, 1
%26 = trunc i64 %25 to i32
%27 = shl i32 %26, 2
%28 = call i64 @realloc(i32* %24, i32 %27)
%29 = inttoptr i64 %28 to i32*
%30 = load i32, i32* %3
%31 = getelementptr inbounds i32, i32* %29, i64 %23
store i32 %30, i32* %31
%32 = call i32 @display(i32* %29, i32 %26)
%33 = call i64 @fscanf(i32* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%34 = load i64, i64* @EOF
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %22
36: ; preds = %22
%37 = trunc i64 %25 to i32
%38 = inttoptr i64 %28 to i32*
br label %39
39: ; preds = %36, %16
%40 = phi i32* [ null, %16 ], [ %38, %36 ]
%41 = phi i32 [ 0, %16 ], [ %37, %36 ]
%42 = call i32 @fclose(i32* %14)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %41)
%44 = call i32 @display(i32* %40, i32 %41)
%45 = add nsw i32 %41, -1
%46 = call i32 @quicksort(i32* %40, i32 0, i32 %45)
%47 = call i32 @display(i32* %40, i32 %41)
br label %48
48: ; preds = %39, %20, %6
%49 = phi i32 [ 1, %6 ], [ 0, %20 ], [ 0, %39 ]
ret i32 %49
}