#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*/ arr_cpy(int *, int *, int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ merge(int *, int, int *, int, int *);
int /*<<< orphan*/ printf(char *);
void merge_sort(int arr[], int n) {
  int mid = n / 2;
  int n1 = n - mid;
  int *arr1;
  int *arr2;
  arr1 = (int *)malloc(mid * sizeof(int));
  if (arr1 == NULL) {
    printf("Cannot allocate memory!!\n");
    exit(0);
  }
  arr2 = (int *)malloc(n1 * sizeof(int));
  if (arr2 == NULL) {
    printf("Cannot allocate memory!!\n");
    exit(0);
  }
  if (n > 1) {
    arr_cpy(arr1, arr, 0, mid);
    arr_cpy(arr2, arr, mid, n);
    merge_sort(arr1, mid);
    merge_sort(arr2, n1);
    merge(arr1, mid, arr2, n1, arr);
  }
}


x86 assembly (clang, -O0):
<merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b7 <merge_sort+0x57>
lea    0xe55(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e8 <merge_sort+0x88>
lea    0xe24(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
cmpl   $0x1,-0xc(%rbp)
jle    1246 <merge_sort+0xe6>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%ecx
xor    %edx,%edx
call   1040 <arr_cpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%edx
mov    -0xc(%rbp),%ecx
call   1040 <arr_cpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1070 <merge_sort@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <merge_sort@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    -0x8(%rbp),%r8
call   1050 <merge@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
mov    %esi,%ebx
shr    $0x1f,%ebx
add    %esi,%ebx
sar    %ebx
lea    0x0(,%rbx,4),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1214 <merge_sort+0xb4>
mov    %rax,%r15
mov    %ebp,%r12d
sub    %ebx,%r12d
lea    0x0(,%r12,4),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1214 <merge_sort+0xb4>
cmp    $0x1,%ebp
jle    1205 <merge_sort+0xa5>
mov    %rax,%r13
mov    %r15,%rdi
mov    %r14,%rsi
xor    %edx,%edx
mov    %ebx,%ecx
call   1040 <arr_cpy@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1040 <arr_cpy@plt>
mov    %r15,%rdi
mov    %ebx,%esi
call   1070 <merge_sort@plt>
mov    %r13,%rdi
mov    %r12d,%esi
call   1070 <merge_sort@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r13,%rdx
mov    %r12d,%ecx
mov    %r14,%r8
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <merge@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde5(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Cannot allocate memory!!\0A\00"
define void @merge_sort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
store i32* %0, i32** %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = sdiv i32 %9, 2
store i32 %10, i32* %5
%11 = load i32, i32* %4
%12 = load i32, i32* %5
%13 = sub nsw i32 %11, %12
store i32 %13, i32* %6
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = mul i64 %15, 4
%17 = trunc i64 %16 to i32
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i32*
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %2
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = mul i64 %27, 4
%29 = trunc i64 %28 to i32
%30 = call i64 @malloc(i32 %29)
%31 = inttoptr i64 %30 to i32*
store i32* %31, i32** %8
%32 = load i32*, i32** %8
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %25
%38 = load i32, i32* %4
%39 = icmp sgt i32 %38, 1
br i1 %39, label %40, label %60
40: ; preds = %37
%41 = load i32*, i32** %7
%42 = load i32*, i32** %3
%43 = load i32, i32* %5
%44 = call i32 @arr_cpy(i32* %41, i32* %42, i32 0, i32 %43)
%45 = load i32*, i32** %8
%46 = load i32*, i32** %3
%47 = load i32, i32* %5
%48 = load i32, i32* %4
%49 = call i32 @arr_cpy(i32* %45, i32* %46, i32 %47, i32 %48)
%50 = load i32*, i32** %7
%51 = load i32, i32* %5
%52 = load i32*, i32** %8
%53 = load i32, i32* %6
%54 = load i32*, i32** %7
%55 = load i32, i32* %5
%56 = load i32*, i32** %8
%57 = load i32, i32* %6
%58 = load i32*, i32** %3
%59 = call i32 @merge(i32* %54, i32 %55, i32* %56, i32 %57, i32* %58)
br label %60
60: ; preds = %40, %37
ret void
}

LLVM IR (-O2):
@str.1 = [25 x i8] c"Cannot allocate memory!!\00"
define void @merge_sort(i32* %0, i32 %1) {
%3 = sdiv i32 %1, 2
%4 = sub nsw i32 %1, %3
%5 = shl i32 %3, 2
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i32*
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %2
%13 = shl i32 %4, 2
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i32*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 0)
unreachable
20: ; preds = %12
%21 = icmp sgt i32 %1, 1
br i1 %21, label %23, label %22
22: ; preds = %20, %23
ret void
23: ; preds = %20
%24 = tail call i32 @arr_cpy(i32* %7, i32* %0, i32 0, i32 %3)
%25 = tail call i32 @arr_cpy(i32* %15, i32* %0, i32 %3, i32 %1)
tail call void @merge_sort(i32* %7, i32 %3)
tail call void @merge_sort(i32* %15, i32 %4)
%26 = tail call i32 @merge(i32* %7, i32 %3, i32* %15, i32 %4, i32* %0)
br label %22
}