#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 */
int is_sorted(int *, int);
int magic_index(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ mergesort(int *, int /*<<< orphan*/, int);
int populate_array(int *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("Incorrect number of command line arguments\n");
    printf("Correct usage: ./a.out <input file>\n");
    return 0;
  }
  int array[1000];
  int magic_number;
  int length = populate_array(array, argv[1]);
  if (length == -1) {
    printf("Fail to open the file.\n");
  }
  mergesort(array, 0, length - 1);
  if (is_sorted(array, length) == 1) {
    printf("The array is sorted:\n");
    int i;
    for (i = 0; i < length; i++) {
      printf("%d ", array[i]);
    }
    printf("\n");
    magic_number = magic_index(array, 0, length);
    printf("Magic number: %d\n", magic_number);
  } else {
    printf("Fail to sort the array.\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xfc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     119b <main+0x4b>
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b2 <main+0x162>
lea    -0xfb0(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1050 <populate_array@plt>
mov    %eax,-0xfb8(%rbp)
cmpl   $0xffffffff,-0xfb8(%rbp)
jne    11d0 <main+0x80>
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xfb0(%rbp),%rdi
mov    -0xfb8(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1070 <mergesort@plt>
lea    -0xfb0(%rbp),%rdi
mov    -0xfb8(%rbp),%esi
call   1030 <is_sorted@plt>
cmp    $0x1,%eax
jne    129d <main+0x14d>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0xfbc(%rbp)
mov    -0xfbc(%rbp),%eax
cmp    -0xfb8(%rbp),%eax
jge    125c <main+0x10c>
movslq -0xfbc(%rbp),%rax
mov    -0xfb0(%rbp,%rax,4),%esi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xfbc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xfbc(%rbp)
jmp    121a <main+0xca>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xfb0(%rbp),%rdi
mov    -0xfb8(%rbp),%edx
xor    %esi,%esi
call   1060 <magic_index@plt>
mov    %eax,-0xfb4(%rbp)
mov    -0xfb4(%rbp),%esi
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ab <main+0x15b>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xfc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xfa8,%rsp
cmp    $0x2,%edi
jne    122f <main+0xbf>
mov    0x8(%rsi),%rsi
mov    %rsp,%rdi
call   1070 <populate_array@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    11a7 <main+0x37>
lea    0xea1(%rip),%rdi
call   1040 <puts@plt>
lea    -0x1(%r14),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <mergesort@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <is_sorted@plt>
cmp    $0x1,%eax
jne    1244 <main+0xd4>
lea    0xe5b(%rip),%rdi
call   1040 <puts@plt>
test   %r14d,%r14d
jle    1206 <main+0x96>
mov    %r14d,%r12d
lea    0xe19(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    (%rsp,%rbx,4),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11f0 <main+0x80>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1080 <magic_index@plt>
lea    0xde0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1250 <main+0xe0>
lea    0xe24(%rip),%rdi
call   1040 <puts@plt>
lea    0xe43(%rip),%rdi
jmp    124b <main+0xdb>
lea    0xdcb(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0xfa8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [44 x i8] c"Incorrect number of command line arguments\0A\00"
@.str.1 = [37 x i8] c"Correct usage: ./a.out <input file>\0A\00"
@.str.2 = [24 x i8] c"Fail to open the file.\0A\00"
@.str.3 = [22 x i8] c"The array is sorted:\0A\00"
@.str.4 = [4 x i8] c"%d \00"
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [18 x i8] c"Magic number: %d\0A\00"
@.str.7 = [25 x i8] c"Fail to sort the array.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1000 x 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
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %59
15: ; preds = %2
%16 = getelementptr inbounds [1000 x i32], [1000 x i32]* %6, i64 0, i64 0
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @populate_array(i32* %16, i8* %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %15
%26 = getelementptr inbounds [1000 x i32], [1000 x i32]* %6, i64 0, i64 0
%27 = load i32, i32* %8
%28 = sub nsw i32 %27, 1
%29 = call i32 @mergesort(i32* %26, i32 0, i32 %28)
%30 = getelementptr inbounds [1000 x i32], [1000 x i32]* %6, i64 0, i64 0
%31 = load i32, i32* %8
%32 = call i32 @is_sorted(i32* %30, i32 %31)
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %56
34: ; preds = %25
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %9
br label %36
36: ; preds = %46, %34
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %49
40: ; preds = %36
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [1000 x i32], [1000 x i32]* %6, i64 0, i64 %42
%44 = load i32, i32* %43
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %40
%47 = load i32, i32* %9
%48 = add nsw i32 %47, 1
store i32 %48, i32* %9
br label %36
49: ; preds = %36
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%51 = getelementptr inbounds [1000 x i32], [1000 x i32]* %6, i64 0, i64 0
%52 = load i32, i32* %8
%53 = call i32 @magic_index(i32* %51, i32 0, i32 %52)
store i32 %53, i32* %7
%54 = load i32, i32* %7
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %54)
br label %58
56: ; preds = %25
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
br label %58
58: ; preds = %56, %49
store i32 0, i32* %3
br label %59
59: ; preds = %58, %12
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str.4 = [4 x i8] c"%d \00"
@.str.6 = [18 x i8] c"Magic number: %d\0A\00"
@str = [24 x i8] c"Fail to sort the array.\00"
@str.8 = [21 x i8] c"The array is sorted:\00"
@str.9 = [23 x i8] c"Fail to open the file.\00"
@str.10 = [43 x i8] c"Incorrect number of command line arguments\00"
@str.11 = [36 x i8] c"Correct usage: ./a.out <input file>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1000 x i32]
%4 = icmp eq i32 %0, 2
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.10, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.11, i64 0, i64 0))
br label %41
8: ; preds = %2
%9 = bitcast [1000 x i32]* %3 to i8*
%10 = getelementptr inbounds [1000 x i32], [1000 x i32]* %3, i64 0, i64 0
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = call i32 @populate_array(i32* %10, i8* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.9, i64 0, i64 0))
br label %17
17: ; preds = %15, %8
%18 = add nsw i32 %13, -1
%19 = call i32 @mergesort(i32* %10, i32 0, i32 %18)
%20 = call i32 @is_sorted(i32* %10, i32 %13)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %38
22: ; preds = %17
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.8, i64 0, i64 0))
%24 = icmp sgt i32 %13, 0
br i1 %24, label %25, label %34
25: ; preds = %22
%26 = zext i32 %13 to i64
br label %27
27: ; preds = %25, %27
%28 = phi i64 [ 0, %25 ], [ %32, %27 ]
%29 = getelementptr inbounds [1000 x i32], [1000 x i32]* %3, i64 0, i64 %28
%30 = load i32, i32* %29
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = add nuw nsw i64 %28, 1
%33 = icmp eq i64 %32, %26
br i1 %33, label %34, label %27
34: ; preds = %27, %22
%35 = call i32 @putchar(i32 10)
%36 = call i32 @magic_index(i32* %10, i32 0, i32 %13)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %36)
br label %40
38: ; preds = %17
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
br label %41
41: ; preds = %40, %5
ret i32 0
}