#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 /*<<< orphan*/ getch();
int /*<<< orphan*/ heapSort(int *, int);
int /*<<< orphan*/ mergeSort(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printArray(int *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ quickSort(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ system(char *);
int main() {
  int arr[50];
  int n, i;
  int choice;
  do {
    system("cls");
    printf("===To implement various Sorting Algorithms===\n"
           "1. MergeSort\n"
           "2. QuickSort\n"
           "3. HeapSort\n"
           "4. Exit\n"
           "Enter your choice: ");
    scanf("%d", &choice);
    if (choice < 4 && choice > 0) {
      printf("Enter Size of array: ");
      scanf("%d", &n);
      printf("Enter Array elements: ");
      for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);
    }
    switch (choice) {
    case 1:
      mergeSort(arr, 0, n - 1);
      printf("Sorted array is: ");
      printArray(arr, n);
      break;
    case 2:
      quickSort(arr, 0, n - 1);
      printf("Sorted array is: ");
      printArray(arr, n);
      break;
    case 3:
      heapSort(arr, n - 1);
      printf("Sorted array is: ");
      printArray(arr, n);
      break;
    case 4:
      printf("Thank you for using\n");
      break;
    default:
      printf("Invalid Choice please try again\n");
      break;
    }
    getch();
  } while (choice != 4);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
call   1030 <system@plt>
lea    0xe6f(%rip),%rdi
call   1050 <printf@plt>
lea    0xed3(%rip),%rdi
lea    -0xdc(%rbp),%rsi
call   1070 <scanf@plt>
cmpl   $0x4,-0xdc(%rbp)
jge    1258 <main+0xd8>
cmpl   $0x0,-0xdc(%rbp)
jle    1258 <main+0xd8>
lea    0xea9(%rip),%rdi
call   1050 <printf@plt>
lea    0xe9a(%rip),%rdi
lea    -0xd4(%rbp),%rsi
call   1070 <scanf@plt>
lea    0xea0(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0xd8(%rbp)
mov    -0xd8(%rbp),%eax
cmp    -0xd4(%rbp),%eax
jge    1253 <main+0xd3>
movslq -0xd8(%rbp),%rax
lea    -0xd0(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe4a(%rip),%rdi
call   1070 <scanf@plt>
mov    -0xd8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xd8(%rbp)
jmp    120c <main+0x8c>
jmp    1258 <main+0xd8>
mov    -0xdc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0xe8(%rbp)
sub    $0x3,%eax
ja     1347 <main+0x1c7>
mov    -0xe8(%rbp),%rax
lea    0xd7f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    -0xd0(%rbp),%rdi
mov    -0xd4(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1060 <mergeSort@plt>
lea    0xe0c(%rip),%rdi
call   1050 <printf@plt>
lea    -0xd0(%rbp),%rdi
mov    -0xd4(%rbp),%esi
call   10a0 <printArray@plt>
jmp    1353 <main+0x1d3>
lea    -0xd0(%rbp),%rdi
mov    -0xd4(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1090 <quickSort@plt>
lea    0xdd2(%rip),%rdi
call   1050 <printf@plt>
lea    -0xd0(%rbp),%rdi
mov    -0xd4(%rbp),%esi
call   10a0 <printArray@plt>
jmp    1353 <main+0x1d3>
lea    -0xd0(%rbp),%rdi
mov    -0xd4(%rbp),%esi
sub    $0x1,%esi
call   1040 <heapSort@plt>
lea    0xd9a(%rip),%rdi
call   1050 <printf@plt>
lea    -0xd0(%rbp),%rdi
mov    -0xd4(%rbp),%esi
call   10a0 <printArray@plt>
jmp    1353 <main+0x1d3>
lea    0xd89(%rip),%rdi
call   1050 <printf@plt>
jmp    1353 <main+0x1d3>
lea    0xd8d(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <getch@plt>
cmpl   $0x4,-0xdc(%rbp)
jne    1192 <main+0x12>
xor    %eax,%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xd8,%rsp
lea    0xe68(%rip),%r12
lea    0xe65(%rip),%r13
lea    0xece(%rip),%rbx
lea    0xc(%rsp),%rbp
jmp    11fe <main+0x6e>
nopl   (%rax)
mov    0x8(%rsp),%esi
add    $0xffffffff,%esi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <heapSort@plt>
lea    0xed9(%rip),%rdi
call   1060 <printf@plt>
mov    0x8(%rsp),%esi
mov    %r14,%rdi
call   10b0 <printArray@plt>
xor    %eax,%eax
call   1090 <getch@plt>
cmpl   $0x4,0xc(%rsp)
je     1301 <main+0x171>
mov    %r12,%rdi
call   1040 <system@plt>
mov    %r13,%rdi
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <scanf@plt>
mov    0xc(%rsp),%eax
lea    -0x1(%rax),%ecx
cmp    $0x2,%ecx
ja     1284 <main+0xf4>
mov    %rbp,%r14
lea    0xe58(%rip),%rdi
call   1060 <printf@plt>
mov    %rbx,%rdi
lea    0x8(%rsp),%rsi
call   1080 <scanf@plt>
lea    0xe55(%rip),%rdi
call   1060 <printf@plt>
cmpl   $0x0,0x8(%rsp)
jle    127d <main+0xed>
lea    0x10(%rsp),%rbp
xor    %r15d,%r15d
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <scanf@plt>
add    $0x1,%r15
movslq 0x8(%rsp),%rax
add    $0x4,%rbp
cmp    %rax,%r15
jl     1260 <main+0xd0>
mov    0xc(%rsp),%eax
mov    %r14,%rbp
add    $0xffffffff,%eax
cmp    $0x3,%eax
ja     12c0 <main+0x130>
lea    0xd6d(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x8(%rsp),%edx
add    $0xffffffff,%edx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <mergeSort@plt>
jmp    11d4 <main+0x44>
nopw   0x0(%rax,%rax,1)
lea    0xe13(%rip),%rdi
jmp    12f7 <main+0x167>
nopl   0x0(%rax)
mov    0x8(%rsp),%edx
add    $0xffffffff,%edx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <quickSort@plt>
jmp    11d4 <main+0x44>
nopl   0x0(%rax,%rax,1)
lea    0xdcf(%rip),%rdi
call   1030 <puts@plt>
jmp    11ec <main+0x5c>
xor    %eax,%eax
add    $0xd8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"cls\00"
@.str.1 = [112 x i8] c"===To implement various Sorting Algorithms===\0A1. MergeSort\0A2. QuickSort\0A3. HeapSort\0A4. Exit\0AEnter your choice: \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [22 x i8] c"Enter Size of array: \00"
@.str.4 = [23 x i8] c"Enter Array elements: \00"
@.str.5 = [18 x i8] c"Sorted array is: \00"
@.str.6 = [21 x i8] c"Thank you for using\0A\00"
@.str.7 = [33 x i8] c"Invalid Choice please try again\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [50 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
br label %6
6: ; preds = %67, %0
%7 = call i32 @system(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @printf(i8* getelementptr inbounds ([112 x i8], [112 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 4
br i1 %11, label %12, label %32
12: ; preds = %6
%13 = load i32, i32* %5
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%18 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %4
br label %19
19: ; preds = %28, %15
%20 = load i32, i32* %4
%21 = load i32, i32* %3
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %31
23: ; preds = %19
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 %25
%27 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %26)
br label %28
28: ; preds = %23
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
br label %19
31: ; preds = %19
br label %32
32: ; preds = %31, %12, %6
%33 = load i32, i32* %5
switch i32 %33, label %63 [
i32 1, label %34
i32 2, label %43
i32 3, label %52
i32 4, label %61
]
34: ; preds = %32
%35 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%36 = load i32, i32* %3
%37 = sub nsw i32 %36, 1
%38 = call i32 @mergeSort(i32* %35, i32 0, i32 %37)
%39 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%40 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%41 = load i32, i32* %3
%42 = call i32 @printArray(i32* %40, i32 %41)
br label %65
43: ; preds = %32
%44 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%45 = load i32, i32* %3
%46 = sub nsw i32 %45, 1
%47 = call i32 @quickSort(i32* %44, i32 0, i32 %46)
%48 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%49 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%50 = load i32, i32* %3
%51 = call i32 @printArray(i32* %49, i32 %50)
br label %65
52: ; preds = %32
%53 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%54 = load i32, i32* %3
%55 = sub nsw i32 %54, 1
%56 = call i32 @heapSort(i32* %53, i32 %55)
%57 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%58 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%59 = load i32, i32* %3
%60 = call i32 @printArray(i32* %58, i32 %59)
br label %65
61: ; preds = %32
%62 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %65
63: ; preds = %32
%64 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
br label %65
65: ; preds = %63, %61, %52, %43, %34
%66 = call i32 (...) @getch()
br label %67
67: ; preds = %65
%68 = load i32, i32* %5
%69 = icmp ne i32 %68, 4
br i1 %69, label %6, label %70
70: ; preds = %67
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"cls\00"
@.str.1 = [112 x i8] c"===To implement various Sorting Algorithms===\0A1. MergeSort\0A2. QuickSort\0A3. HeapSort\0A4. Exit\0AEnter your choice: \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [22 x i8] c"Enter Size of array: \00"
@.str.4 = [23 x i8] c"Enter Array elements: \00"
@.str.5 = [18 x i8] c"Sorted array is: \00"
@str = [20 x i8] c"Thank you for using\00"
@str.8 = [32 x i8] c"Invalid Choice please try again\00"
define i32 @main() {
%1 = alloca [50 x i32]
%2 = alloca i32
%3 = alloca i32
%4 = bitcast [50 x i32]* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [50 x i32], [50 x i32]* %1, i64 0, i64 0
br label %8
8: ; preds = %58, %0
%9 = call i32 @system(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([112 x i8], [112 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%12 = load i32, i32* %3
%13 = add i32 %12, -1
%14 = icmp ult i32 %13, 3
br i1 %14, label %15, label %31
15: ; preds = %8
%16 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%18 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%19 = load i32, i32* %2
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %15, %21
%22 = phi i64 [ %25, %21 ], [ 0, %15 ]
%23 = getelementptr inbounds [50 x i32], [50 x i32]* %1, i64 0, i64 %22
%24 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %23)
%25 = add nuw nsw i64 %22, 1
%26 = load i32, i32* %2
%27 = sext i32 %26 to i64
%28 = icmp slt i64 %25, %27
br i1 %28, label %21, label %29
29: ; preds = %21, %15
%30 = load i32, i32* %3
br label %31
31: ; preds = %29, %8
%32 = phi i32 [ %30, %29 ], [ %12, %8 ]
switch i32 %32, label %56 [
i32 1, label %33
i32 2, label %40
i32 3, label %47
i32 4, label %54
]
33: ; preds = %31
%34 = load i32, i32* %2
%35 = add nsw i32 %34, -1
%36 = call i32 @mergeSort(i32* %7, i32 0, i32 %35)
%37 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%38 = load i32, i32* %2
%39 = call i32 @printArray(i32* %7, i32 %38)
br label %58
40: ; preds = %31
%41 = load i32, i32* %2
%42 = add nsw i32 %41, -1
%43 = call i32 @quickSort(i32* %7, i32 0, i32 %42)
%44 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%45 = load i32, i32* %2
%46 = call i32 @printArray(i32* %7, i32 %45)
br label %58
47: ; preds = %31
%48 = load i32, i32* %2
%49 = add nsw i32 %48, -1
%50 = call i32 @heapSort(i32* %7, i32 %49)
%51 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%52 = load i32, i32* %2
%53 = call i32 @printArray(i32* %7, i32 %52)
br label %58
54: ; preds = %31
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %58
56: ; preds = %31
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.8, i64 0, i64 0))
br label %58
58: ; preds = %56, %54, %47, %40, %33
%59 = call i32 (...) @getch()
%60 = load i32, i32* %3
%61 = icmp eq i32 %60, 4
br i1 %61, label %62, label %8
62: ; preds = %58
ret i32 0
}