#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*/ mergesort(int *, int);
int /*<<< orphan*/ printf(char *, ...);
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;
  }
  fclose(fh);
  printf("%d elements read\n", length);
  display(array, length);
  mergesort(array, length);
  printf("done sorting\n");
  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   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c7 <main+0x157>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1050 <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   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c7 <main+0x157>
jmp    1218 <main+0xa8>
mov    -0x28(%rbp),%rdi
lea    0xe26(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1060 <fscanf@plt>
mov    0x2db5(%rip),%rcx
cmp    (%rcx),%rax
je     1274 <main+0x104>
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   1070 <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)
jmp    1218 <main+0xa8>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%esi
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <display@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1080 <mergesort@plt>
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@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   1060 <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   1070 <fscanf@plt>
mov    0x2e0d(%rip),%r13
cmp    0x0(%r13),%rax
jne    1213 <main+0x93>
xor    %ebx,%ebx
jmp    1259 <main+0xd9>
mov    (%rbx),%rsi
lea    0xe11(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebp
jmp    12a1 <main+0x121>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
jmp    12a1 <main+0x121>
mov    $0x4,%ebp
xor    %r12d,%r12d
xor    %ebx,%ebx
nopl   (%rax)
mov    %r12,%r15
add    $0x1,%r12
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <realloc@plt>
mov    %rax,%rbx
mov    0x4(%rsp),%eax
mov    %eax,(%rbx,%r15,4)
mov    %r14,%rdi
lea    0xdef(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1070 <fscanf@plt>
add    $0x4,%ebp
cmp    0x0(%r13),%rax
jne    1220 <main+0xa0>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xdd0(%rip),%rdi
xor    %ebp,%ebp
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1050 <display@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1090 <mergesort@plt>
lea    0xdb9(%rip),%rdi
call   1030 <puts@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"
@.str.6 = [14 x i8] c"done sorting\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 %67
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 %67
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 %52
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
br label %31
52: ; preds = %31
%53 = load i32*, i32** %8
%54 = call i32 @fclose(i32* %53)
%55 = load i32, i32* %7
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %55)
%57 = load i32*, i32** %6
%58 = load i32, i32* %7
%59 = call i32 @display(i32* %57, i32 %58)
%60 = load i32*, i32** %6
%61 = load i32, i32* %7
%62 = call i32 @mergesort(i32* %60, i32 %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%64 = load i32*, i32** %6
%65 = load i32, i32* %7
%66 = call i32 @display(i32* %64, i32 %65)
store i32 0, i32* %3
br label %67
67: ; preds = %52, %28, %12
%68 = load i32, i32* %3
ret i32 %68
}

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 = [13 x i8] c"done sorting\00"
@str.7 = [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 %47
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 %38, label %22
20: ; preds = %9
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.7, i64 0, i64 0))
br label %47
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 i64 @fscanf(i32* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%33 = load i64, i64* @EOF
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %22
35: ; preds = %22
%36 = trunc i64 %25 to i32
%37 = inttoptr i64 %28 to i32*
br label %38
38: ; preds = %35, %16
%39 = phi i32* [ null, %16 ], [ %37, %35 ]
%40 = phi i32 [ 0, %16 ], [ %36, %35 ]
%41 = call i32 @fclose(i32* %14)
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %40)
%43 = call i32 @display(i32* %39, i32 %40)
%44 = call i32 @mergesort(i32* %39, i32 %40)
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%46 = call i32 @display(i32* %39, i32 %40)
br label %47
47: ; preds = %38, %20, %6
%48 = phi i32 [ 1, %6 ], [ 0, %20 ], [ 0, %38 ]
ret i32 %48
}