#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 *mergesort(int *a, int n) {
  int odd = n % 2;
  if (n == 1)
    return a;
  return merge(mergesort(a, n / 2), n / 2, mergesort(a + n / 2, n / 2 + odd),
               n / 2 + odd);
}


x86 assembly (clang, -O0):
<mergesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    1154 <mergesort+0x34>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d1 <mergesort+0xb1>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
call   1040 <mergesort@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
add    -0x18(%rbp),%esi
call   1040 <mergesort@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x20(%rbp),%rdx
mov    %eax,%ecx
add    -0x18(%rbp),%ecx
call   1030 <merge@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergesort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
cmp    $0x1,%esi
jne    113d <mergesort+0x1d>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %esi,%ebx
mov    %esi,%ebp
shr    $0x1f,%ebp
add    %esi,%ebp
mov    %ebp,%eax
and    $0xfffffffe,%eax
sub    %eax,%ebx
sar    %ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <mergesort@plt>
mov    %rax,%r15
movslq %ebp,%rbp
lea    (%r14,%rbp,4),%rdi
add    %ebp,%ebx
mov    %ebx,%esi
call   1040 <mergesort@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
mov    %ebx,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <merge@plt>

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

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