#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 *merge(int *, int *, int *, int, int);
int *merge_sort(int *res, int *arr, int size) {
  if (size <= 1)
    return arr;
  int r_half = size / 2;
  int l_half = r_half + size % 2;
  return merge(res, merge_sort(res, arr, l_half),
               merge_sort(res + l_half, arr + l_half, r_half), l_half, r_half);
}


x86 assembly (clang, -O0):
<merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jg     114a <merge_sort+0x2a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ce <merge_sort+0xae>
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x3c(%rbp),%eax
add    %edx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <merge_sort@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%edx
call   1040 <merge_sort@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%r8d
call   1030 <merge@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
cmp    $0x1,%edx
jg     113c <merge_sort+0x1c>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %edx,%ebx
mov    %rdi,%r14
mov    %edx,%ebp
shr    %ebp
and    $0x1,%ebx
add    %ebp,%ebx
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <merge_sort@plt>
mov    %rax,%r12
lea    (%r14,%rbx,4),%rdi
lea    (%r15,%rbx,4),%rsi
mov    %ebp,%edx
call   1040 <merge_sort@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
mov    %ebx,%ecx
mov    %ebp,%r8d
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <merge@plt>

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

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