#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 */
long long *merge(long long *, long long *, int, int);
long long *merge_sort(long long *arr, int size) {
  if (size > 1) {
    int middle = size / 2, i;
    long long *left, *right;
    left = arr;
    right = arr + middle;
    left = merge_sort(left, middle);
    right = merge_sort(right, size - middle);
    return merge(left, right, middle, size - middle);
  } else {
    return arr;
  }
}


x86 assembly (clang, -O0):
<merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jle    11a4 <merge_sort+0x84>
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1040 <merge_sort@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
sub    -0x18(%rbp),%esi
call   1040 <merge_sort@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
sub    -0x18(%rbp),%ecx
call   1030 <merge@plt>
mov    %rax,-0x8(%rbp)
jmp    11ac <merge_sort+0x8c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort>:
cmp    $0x1,%esi
jle    116c <merge_sort+0x4c>
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %esi,%r14d
shr    %r14d
lea    (%rdi,%r14,8),%r15
mov    %r14d,%esi
call   1040 <merge_sort@plt>
mov    %rax,%r12
sub    %r14d,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
call   1040 <merge_sort@plt>
mov    %r12,%rdi
mov    %rax,%rsi
mov    %r14d,%edx
mov    %ebx,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <merge@plt>
mov    %rdi,%rax
ret

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

LLVM IR (-O2):
define i64* @merge_sort(i64* %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %6, label %4
4: ; preds = %2, %6
%5 = phi i64* [ %13, %6 ], [ %0, %2 ]
ret i64* %5
6: ; preds = %2
%7 = lshr i32 %1, 1
%8 = zext i32 %7 to i64
%9 = getelementptr inbounds i64, i64* %0, i64 %8
%10 = tail call i64* @merge_sort(i64* %0, i32 %7)
%11 = sub nsw i32 %1, %7
%12 = tail call i64* @merge_sort(i64* %9, i32 %11)
%13 = tail call i64* @merge(i64* %10, i64* %12, i32 %7, i32 %11)
br label %4
}