#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*/ merge(int *, int, int, int);
int /*<<< orphan*/ printf(char *);
void merge_sort(int *a, int left, int right) {
  int mid;
  if (!a || left < 0 || right < 0) {
    printf("\n ERROR: Invalid inputs to merge sort");
    return;
  }
  if (left >= right) {
    return;
  }
  mid = left + (right - left) / 2;
  merge_sort(a, left, mid);
  merge_sort(a, mid + 1, right);
  merge(a, left, mid, right);
}


x86 assembly (clang, -O0):
<merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1161 <merge_sort+0x31>
cmpl   $0x0,-0xc(%rbp)
jl     1161 <merge_sort+0x31>
cmpl   $0x0,-0x10(%rbp)
jge    1172 <merge_sort+0x42>
lea    0xe98(%rip),%rdi
call   1030 <printf@plt>
jmp    11d4 <merge_sort+0xa4>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jl     1183 <merge_sort+0x53>
jmp    11d4 <merge_sort+0xa4>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1050 <merge_sort@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%edx
call   1050 <merge_sort@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%ecx
call   1040 <merge@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdi,%rdi
je     1192 <merge_sort+0x62>
mov    %edx,%r14d
mov    %esi,%ebx
mov    %edx,%eax
or     %esi,%eax
js     1192 <merge_sort+0x62>
mov    %r14d,%eax
sub    %ebx,%eax
jle    11a6 <merge_sort+0x76>
mov    %rdi,%r12
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
lea    (%rbx,%rbp,1),%r15d
mov    %ebx,%esi
mov    %r15d,%edx
call   1050 <merge_sort@plt>
lea    (%rbx,%rbp,1),%esi
add    $0x1,%esi
mov    %r12,%rdi
mov    %r14d,%edx
call   1050 <merge_sort@plt>
mov    %r12,%rdi
mov    %ebx,%esi
mov    %r15d,%edx
mov    %r14d,%ecx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <merge@plt>
lea    0xe67(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"\0A ERROR: Invalid inputs to merge sort\00"
define void @merge_sort(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %16
10: ; preds = %3
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13, %10, %3
%17 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %42
18: ; preds = %13
%19 = load i32, i32* %5
%20 = load i32, i32* %6
%21 = icmp sge i32 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
br label %42
23: ; preds = %18
%24 = load i32, i32* %5
%25 = load i32, i32* %6
%26 = load i32, i32* %5
%27 = sub nsw i32 %25, %26
%28 = sdiv i32 %27, 2
%29 = add nsw i32 %24, %28
store i32 %29, i32* %7
%30 = load i32*, i32** %4
%31 = load i32, i32* %5
%32 = load i32, i32* %7
%33 = load i32*, i32** %4
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
%36 = load i32, i32* %6
%37 = load i32*, i32** %4
%38 = load i32, i32* %5
%39 = load i32, i32* %7
%40 = load i32, i32* %6
%41 = call i32 @merge(i32* %37, i32 %38, i32 %39, i32 %40)
br label %42
42: ; preds = %23, %22, %16
ret void
}

LLVM IR (-O2):
@.str = [38 x i8] c"\0A ERROR: Invalid inputs to merge sort\00"
define void @merge_sort(i32* %0, i32 %1, i32 %2) {
%4 = icmp eq i32* %0, null
%5 = or i32 %2, %1
%6 = icmp slt i32 %5, 0
%7 = or i1 %4, %6
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %12
10: ; preds = %3
%11 = icmp sgt i32 %2, %1
br i1 %11, label %13, label %12
12: ; preds = %8, %10, %13
ret void
13: ; preds = %10
%14 = sub nsw i32 %2, %1
%15 = sdiv i32 %14, 2
%16 = add nsw i32 %15, %1
tail call void @merge_sort(i32* %0, i32 %1, i32 %16)
%17 = add nsw i32 %16, 1
tail call void @merge_sort(i32* %0, i32 %17, i32 %2)
%18 = tail call i32 @merge(i32* %0, i32 %1, i32 %16, i32 %2)
br label %12
}