#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 */
size_t atoi(char *);
int /*<<< orphan*/ bogoSort(int *, size_t, int *);
int /*<<< orphan*/ bubbleSort(int *, size_t, int *);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ mergeSort(int *, size_t, int *);
int *newArray(size_t);
int /*<<< orphan*/ printArray(int *, size_t);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
int main(int argc, char **argv) {
  size_t size;
  int *trackers = (int *)calloc(2, sizeof(int));
  if (argc != 3) {
    printf(
        "Usage: sortit [size to sort] [algorithm(bogo, bubble, or merge)]\n");
    return 1;
  }
  size = atoi(argv[1]);
  if (size < 1) {
    printf(
        "Usage: sortit [size to sort] [algorithm(bogo, bubble, or merge)]\n");
    return 1;
  }
  int *array = newArray(size);
  printf("Initial array:\n");
  printArray(array, size);
  char *selection = argv[2];
  if (strcmp(selection, "bogo") == 0)
    bogoSort(array, size, trackers);
  else if (strcmp(selection, "bubble") == 0)
    bubbleSort(array, size, trackers);
  else if (strcmp(selection, "merge") == 0)
    mergeSort(array, size, trackers);
  else {
    printf(
        "Usage: sortit [size to sort] [algorithm(bogo, bubble, or merge)]\n");
    return 1;
  }
  printf("Sorted array:\n");
  printArray(array, size);
  printf("Comparisons: %d, Array Accesses: %d\n", trackers[0], trackers[1]);
  free(trackers);
  free(array);
  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)
mov    $0x2,%edi
mov    $0x4,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11ed <main+0x4d>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135a <main+0x1ba>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x1,-0x18(%rbp)
jae    1223 <main+0x83>
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135a <main+0x1ba>
mov    -0x18(%rbp),%rdi
call   10a0 <newArray@plt>
mov    %rax,-0x28(%rbp)
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10b0 <printArray@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1287 <main+0xe7>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <bogoSort@plt>
jmp    130b <main+0x16b>
mov    -0x30(%rbp),%rdi
lea    0xdc5(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12b7 <main+0x117>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <bubbleSort@plt>
jmp    1306 <main+0x166>
mov    -0x30(%rbp),%rdi
lea    0xd9c(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12e7 <main+0x147>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1090 <mergeSort@plt>
jmp    1301 <main+0x161>
lea    0xd12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135a <main+0x1ba>
jmp    1306 <main+0x166>
jmp    130b <main+0x16b>
lea    0xd52(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10b0 <printArray@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
lea    0xd39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@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   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x2,%edi
mov    $0x4,%esi
call   1080 <calloc@plt>
cmp    $0x3,%ebp
jne    1247 <main+0x97>
mov    %rax,%r15
mov    0x8(%rbx),%rdi
call   10d0 <atoi@plt>
test   %rax,%rax
je     1247 <main+0x97>
mov    %rax,%r12
mov    %rax,%rdi
call   10b0 <newArray@plt>
mov    %rax,%r14
lea    0xe40(%rip),%rdi
call   1050 <puts@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   10c0 <printArray@plt>
mov    0x10(%rbx),%rbx
lea    0xdee(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     125a <main+0xaa>
lea    0xddf(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     126a <main+0xba>
lea    0xdd2(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     127a <main+0xca>
lea    0xe06(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%ebx
jmp    12c6 <main+0x116>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1040 <bogoSort@plt>
jmp    1288 <main+0xd8>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1070 <bubbleSort@plt>
jmp    1288 <main+0xd8>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   10a0 <mergeSort@plt>
lea    0xdb7(%rip),%rdi
call   1050 <puts@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   10c0 <printArray@plt>
mov    (%r15),%esi
mov    0x4(%r15),%edx
lea    0xd65(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [66 x i8] c"Usage: sortit [size to sort] [algorithm(bogo, bubble, or merge)]\0A\00"
@.str.1 = [16 x i8] c"Initial array:\0A\00"
@.str.2 = [5 x i8] c"bogo\00"
@.str.3 = [7 x i8] c"bubble\00"
@.str.4 = [6 x i8] c"merge\00"
@.str.5 = [15 x i8] c"Sorted array:\0A\00"
@.str.6 = [37 x i8] c"Comparisons: %d, Array Accesses: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i64 @calloc(i32 2, i32 4)
%11 = inttoptr i64 %10 to i32*
store i32* %11, i32** %7
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %81
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i64 @atoi(i8* %19)
store i64 %20, i64* %6
%21 = load i64, i64* %6
%22 = icmp ult i64 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %81
25: ; preds = %16
%26 = load i64, i64* %6
%27 = call i32* @newArray(i64 %26)
store i32* %27, i32** %8
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32*, i32** %8
%30 = load i64, i64* %6
%31 = call i32 @printArray(i32* %29, i64 %30)
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = call i64 @strcmp(i8* %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %25
%39 = load i32*, i32** %8
%40 = load i64, i64* %6
%41 = load i32*, i32** %7
%42 = call i32 @bogoSort(i32* %39, i64 %40, i32* %41)
br label %65
43: ; preds = %25
%44 = load i8*, i8** %9
%45 = call i64 @strcmp(i8* %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %43
%48 = load i32*, i32** %8
%49 = load i64, i64* %6
%50 = load i32*, i32** %7
%51 = call i32 @bubbleSort(i32* %48, i64 %49, i32* %50)
br label %64
52: ; preds = %43
%53 = load i8*, i8** %9
%54 = call i64 @strcmp(i8* %53, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %52
%57 = load i32*, i32** %8
%58 = load i64, i64* %6
%59 = load i32*, i32** %7
%60 = call i32 @mergeSort(i32* %57, i64 %58, i32* %59)
br label %63
61: ; preds = %52
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %81
63: ; preds = %56
br label %64
64: ; preds = %63, %47
br label %65
65: ; preds = %64, %38
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%67 = load i32*, i32** %8
%68 = load i64, i64* %6
%69 = call i32 @printArray(i32* %67, i64 %68)
%70 = load i32*, i32** %7
%71 = getelementptr inbounds i32, i32* %70, i64 0
%72 = load i32, i32* %71
%73 = load i32*, i32** %7
%74 = getelementptr inbounds i32, i32* %73, i64 1
%75 = load i32, i32* %74
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %72, i32 %75)
%77 = load i32*, i32** %7
%78 = call i32 @free(i32* %77)
%79 = load i32*, i32** %8
%80 = call i32 @free(i32* %79)
store i32 0, i32* %3
br label %81
81: ; preds = %65, %61, %23, %14
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
@.str.2 = [5 x i8] c"bogo\00"
@.str.3 = [7 x i8] c"bubble\00"
@.str.4 = [6 x i8] c"merge\00"
@.str.6 = [37 x i8] c"Comparisons: %d, Array Accesses: %d\0A\00"
@str = [15 x i8] c"Initial array:\00"
@str.8 = [14 x i8] c"Sorted array:\00"
@str.10 = [65 x i8] c"Usage: sortit [size to sort] [algorithm(bogo, bubble, or merge)]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i64 @calloc(i32 2, i32 4)
%4 = inttoptr i64 %3 to i32*
%5 = icmp eq i32 %0, 3
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @str.10, i64 0, i64 0))
br label %46
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @atoi(i8* %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @str.10, i64 0, i64 0))
br label %46
15: ; preds = %8
%16 = tail call i32* @newArray(i64 %11)
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 @printArray(i32* %16, i64 %11)
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = tail call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = tail call i32 @bogoSort(i32* %16, i64 %11, i32* %4)
br label %37
25: ; preds = %15
%26 = tail call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = tail call i32 @bubbleSort(i32* %16, i64 %11, i32* %4)
br label %37
30: ; preds = %25
%31 = tail call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = tail call i32 @mergeSort(i32* %16, i64 %11, i32* %4)
br label %37
35: ; preds = %30
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @str.10, i64 0, i64 0))
br label %46
37: ; preds = %28, %33, %23
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
%39 = tail call i32 @printArray(i32* %16, i64 %11)
%40 = load i32, i32* %4
%41 = getelementptr inbounds i32, i32* %4, i64 1
%42 = load i32, i32* %41
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %40, i32 %42)
%44 = tail call i32 @free(i32* %4)
%45 = tail call i32 @free(i32* %16)
br label %46
46: ; preds = %35, %37, %13, %6
%47 = phi i32 [ 1, %6 ], [ 1, %13 ], [ 0, %37 ], [ 1, %35 ]
ret i32 %47
}