#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*/ memcpy(int *, int *, int);
int /*<<< orphan*/ merge(int *, int, int *, int, int *);
void mergesort(int *a, int n) {
  if (n <= 1)
    return;
  int na = n / 2;
  int *b = a + na, nb = n - na;
  mergesort(a, na);
  mergesort(b, nb);
  int result[n];
  merge(a, na, b, nb, result);
  memcpy(a, result, n * sizeof(int));
}


x86 assembly (clang, -O0):
<mergesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jg     114e <mergesort+0x1e>
jmp    11e8 <mergesort+0xb8>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %ecx,%eax
sar    %eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
lea    (%rax,%rcx,4),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1050 <mergesort@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <mergesort@plt>
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%r8
sub    %rcx,%r8
mov    %r8,-0x38(%rbp)
mov    %r8,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1040 <merge@plt>
mov    -0x38(%rbp),%rsi
mov    -0x8(%rbp),%rdi
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergesort>:
cmp    $0x2,%esi
jl     11c8 <mergesort+0x98>
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    %esi,%r15d
shr    %r15d
lea    (%rdi,%r15,4),%r12
mov    %r12,-0x30(%rbp)
mov    %esi,%r13d
sub    %r15d,%r13d
mov    %r15d,%esi
call   1050 <mergesort@plt>
mov    %r12,%rdi
mov    %r13d,%esi
call   1050 <mergesort@plt>
mov    %rsp,-0x38(%rbp)
mov    %ebx,%eax
mov    %rsp,%r12
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %r14,%rdi
mov    %r15d,%esi
mov    -0x30(%rbp),%rdx
mov    %r13d,%ecx
mov    %r12,%r8
call   1040 <merge@plt>
shl    $0x2,%ebx
mov    %r14,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <memcpy@plt>
mov    -0x38(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @mergesort(i32* %0, i32 %1) {
%3 = icmp slt i32 %1, 2
br i1 %3, label %15, label %4
4: ; preds = %2
%5 = lshr i32 %1, 1
%6 = zext i32 %5 to i64
%7 = getelementptr inbounds i32, i32* %0, i64 %6
%8 = sub nsw i32 %1, %5
tail call void @mergesort(i32* %0, i32 %5)
tail call void @mergesort(i32* %7, i32 %8)
%9 = zext i32 %1 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i32, i64 %9
%12 = call i32 @merge(i32* %0, i32 %5, i32* %7, i32 %8, i32* %11)
%13 = shl i32 %1, 2
%14 = call i32 @memcpy(i32* %0, i32* %11, i32 %13)
br label %15
15: ; preds = %2, %4
ret void
}