#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__ malloc(int);
int /*<<< orphan*/ merge(int *, int, int, int, int *);
void merge_sort(int *arr, int start, int end) {
  int mid = (start + end) / 2;
  int *result;
  result = (int *)malloc(sizeof(int) * (end - start + 1));
  if (start < end) {
    merge_sort(arr, start, mid);
    merge_sort(arr, mid + 1, end);
    merge(arr, start, mid, end, result);
  }
}


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)
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11b0 <merge_sort+0x80>
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
mov    -0x20(%rbp),%r8
call   1030 <merge@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %esi,%r13d
mov    %rdi,%r15
mov    %edx,%eax
sub    %esi,%eax
lea    0x4(,%rax,4),%edi
call   1040 <malloc@plt>
cmp    %r13d,%r14d
jle    119e <merge_sort+0x6e>
mov    %rax,%r12
lea    (%r14,%r13,1),%eax
mov    %eax,%ebx
shr    $0x1f,%ebx
add    %eax,%ebx
sar    %ebx
mov    %r15,%rdi
mov    %r13d,%esi
mov    %ebx,%edx
call   1050 <merge_sort@plt>
lea    0x1(%rbx),%esi
mov    %r15,%rdi
mov    %r14d,%edx
call   1050 <merge_sort@plt>
mov    %r15,%rdi
mov    %r13d,%esi
mov    %ebx,%edx
mov    %r14d,%ecx
mov    %r12,%r8
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <merge@plt>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @merge_sort(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %5
%10 = load i32, i32* %6
%11 = add nsw i32 %9, %10
%12 = sdiv i32 %11, 2
store i32 %12, i32* %7
%13 = load i32, i32* %6
%14 = load i32, i32* %5
%15 = sub nsw i32 %13, %14
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = mul i64 4, %17
%19 = trunc i64 %18 to i32
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i32*
store i32* %21, i32** %8
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %39
25: ; preds = %3
%26 = load i32*, i32** %4
%27 = load i32, i32* %5
%28 = load i32, i32* %7
%29 = load i32*, i32** %4
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
%32 = load i32, i32* %6
%33 = load i32*, i32** %4
%34 = load i32, i32* %5
%35 = load i32, i32* %7
%36 = load i32, i32* %6
%37 = load i32*, i32** %8
%38 = call i32 @merge(i32* %33, i32 %34, i32 %35, i32 %36, i32* %37)
br label %39
39: ; preds = %25, %3
ret void
}

LLVM IR (-O2):
define void @merge_sort(i32* %0, i32 %1, i32 %2) {
%4 = add nsw i32 %2, %1
%5 = sdiv i32 %4, 2
%6 = sub nsw i32 %2, %1
%7 = shl i32 %6, 2
%8 = add i32 %7, 4
%9 = tail call i64 @malloc(i32 %8)
%10 = icmp sgt i32 %2, %1
br i1 %10, label %12, label %11
11: ; preds = %3, %12
ret void
12: ; preds = %3
%13 = inttoptr i64 %9 to i32*
tail call void @merge_sort(i32* %0, i32 %1, i32 %5)
%14 = add nsw i32 %5, 1
tail call void @merge_sort(i32* %0, i32 %14, i32 %2)
%15 = tail call i32 @merge(i32* %0, i32 %1, i32 %5, i32 %2, i32* %13)
br label %11
}