#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 */
scalar_t__ calloc(size_t, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ main_merge_sort(int *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, ...);
int /*<<< orphan*/ stderr;
int main() {
  size_t i, n;
  int *a;
  printf("Enter the total number of elements: ");
  scanf("%u", &n);
  a = (int *)calloc(n, sizeof(int));
  if (a == NULL) {
    fprintf(stderr,
            "MemoryException: Can't allocate "
            "%u"
            " byte of memory.",
            n * sizeof(int));
    exit(1);
  }
  printf("Enter the elements which to be sort:\n");
  for (i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  main_merge_sort(a, 0, n - 1);
  printf("After merge sorting, elements are:\n");
  for (i = 0; i < n; ++i) {
    printf("%d ", a[i]);
  }
  printf("\n");
  free(a);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe91(%rip),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1080 <scanf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e3 <main+0x73>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
shl    $0x2,%rdx
lea    0xe54(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1235 <main+0xc5>
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1080 <scanf@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11f9 <main+0x89>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
xor    %esi,%esi
call   1070 <main_merge_sort@plt>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1296 <main+0x126>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    125e <main+0xee>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe61(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe78(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   10a0 <scanf@plt>
mov    (%rsp),%rdi
mov    $0x4,%esi
call   1070 <calloc@plt>
test   %rax,%rax
je     1288 <main+0xf8>
mov    %rax,%r14
lea    0xe8a(%rip),%rdi
call   1050 <puts@plt>
cmpq   $0x0,(%rsp)
je     1214 <main+0x84>
lea    0xe70(%rip),%r15
mov    %r14,%r12
xor    %ebx,%ebx
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   10a0 <scanf@plt>
add    $0x1,%rbx
mov    (%rsp),%rdx
add    $0x4,%r12
cmp    %rdx,%rbx
jb     11f0 <main+0x60>
add    $0xffffffffffffffff,%rdx
jmp    121b <main+0x8b>
mov    $0xffffffffffffffff,%rdx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <main_merge_sort@plt>
lea    0xe5b(%rip),%rdi
call   1050 <puts@plt>
cmpq   $0x0,(%rsp)
je     1268 <main+0xd8>
lea    0xe1f(%rip),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r14,%rbx,4),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    (%rsp),%rbx
jb     1250 <main+0xc0>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%rdx
shl    $0x2,%rdx
lea    0xd88(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"Enter the total number of elements: \00"
@.str.1 = [3 x i8] c"%u\00"
@stderr = global i32 0
@.str.2 = [51 x i8] c"MemoryException: Can't allocate %u byte of memory.\00"
@.str.3 = [38 x i8] c"Enter the elements which to be sort:\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [36 x i8] c"After merge sorting, elements are:\0A\00"
@.str.6 = [4 x i8] c"%d \00"
@.str.7 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32*
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64* %3)
%7 = load i64, i64* %3
%8 = call i64 @calloc(i64 %7, i32 4)
%9 = inttoptr i64 %8 to i32*
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %18
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = load i64, i64* %3
%15 = mul i64 %14, 4
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i64 %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
store i64 0, i64* %2
br label %20
20: ; preds = %29, %18
%21 = load i64, i64* %2
%22 = load i64, i64* %3
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %32
24: ; preds = %20
%25 = load i32*, i32** %4
%26 = load i64, i64* %2
%27 = getelementptr inbounds i32, i32* %25, i64 %26
%28 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %27)
br label %29
29: ; preds = %24
%30 = load i64, i64* %2
%31 = add i64 %30, 1
store i64 %31, i64* %2
br label %20
32: ; preds = %20
%33 = load i32*, i32** %4
%34 = load i64, i64* %3
%35 = sub i64 %34, 1
%36 = call i32 @main_merge_sort(i32* %33, i32 0, i64 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
store i64 0, i64* %2
br label %38
38: ; preds = %48, %32
%39 = load i64, i64* %2
%40 = load i64, i64* %3
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %51
42: ; preds = %38
%43 = load i32*, i32** %4
%44 = load i64, i64* %2
%45 = getelementptr inbounds i32, i32* %43, i64 %44
%46 = load i32, i32* %45
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %42
%49 = load i64, i64* %2
%50 = add i64 %49, 1
store i64 %50, i64* %2
br label %38
51: ; preds = %38
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%53 = load i32*, i32** %4
%54 = call i32 @free(i32* %53)
ret i32 0
}

LLVM IR (-O2):
@.str = [37 x i8] c"Enter the total number of elements: \00"
@.str.1 = [3 x i8] c"%u\00"
@stderr = global i32 0
@.str.2 = [51 x i8] c"MemoryException: Can't allocate %u byte of memory.\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.6 = [4 x i8] c"%d \00"
@str = [37 x i8] c"Enter the elements which to be sort:\00"
@str.8 = [35 x i8] c"After merge sorting, elements are:\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64* %1)
%5 = load i64, i64* %1
%6 = call i64 @calloc(i64 %5, i32 4)
%7 = inttoptr i64 %6 to i32*
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = load i64, i64* %1
%12 = shl i64 %11, 2
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i64 %12)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%17 = load i64, i64* %1
%18 = icmp eq i64 %17, 0
br i1 %18, label %28, label %19
19: ; preds = %15, %19
%20 = phi i64 [ %23, %19 ], [ 0, %15 ]
%21 = getelementptr inbounds i32, i32* %7, i64 %20
%22 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %21)
%23 = add nuw i64 %20, 1
%24 = load i64, i64* %1
%25 = icmp ult i64 %23, %24
br i1 %25, label %19, label %26
26: ; preds = %19
%27 = add i64 %24, -1
br label %28
28: ; preds = %26, %15
%29 = phi i64 [ -1, %15 ], [ %27, %26 ]
%30 = call i32 @main_merge_sort(i32* %7, i32 0, i64 %29)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.8, i64 0, i64 0))
%32 = load i64, i64* %1
%33 = icmp eq i64 %32, 0
br i1 %33, label %42, label %34
34: ; preds = %28, %34
%35 = phi i64 [ %39, %34 ], [ 0, %28 ]
%36 = getelementptr inbounds i32, i32* %7, i64 %35
%37 = load i32, i32* %36
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %37)
%39 = add nuw i64 %35, 1
%40 = load i64, i64* %1
%41 = icmp ult i64 %39, %40
br i1 %41, label %34, label %42
42: ; preds = %34, %28
%43 = call i32 @putchar(i32 10)
%44 = call i32 @free(i32* %7)
ret i32 0
}