#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*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(int *, int *, int);
int *merge(int, int, int *, int *);
int *mergeSort(const int n, int *v) {
  if (1 == n) {
    return v;
  }
  int *returnValue;
  int n1 = n / 2;
  int n2 = n - n1;
  int *leftHalf = (int *)malloc(sizeof(int) * n1);
  int *rightHalf = (int *)malloc(sizeof(int) * n2);
  memcpy(leftHalf, v, sizeof(int) * n1);
  memcpy(rightHalf, v + n1, sizeof(int) * n2);
  returnValue =
      merge(n1, n2, mergeSort(n1, leftHalf), mergeSort(n2, rightHalf));
  free(leftHalf);
  free(rightHalf);
  return returnValue;
}


x86 assembly (clang, -O0):
<mergeSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x1,%eax
cmp    -0xc(%rbp),%eax
jne    117a <mergeSort+0x2a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1248 <mergeSort+0xf8>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
movslq -0x28(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
movslq -0x28(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1050 <mergeSort@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1050 <mergeSort@plt>
mov    -0x48(%rbp),%edi
mov    -0x44(%rbp),%esi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
call   1060 <merge@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergeSort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
cmp    $0x1,%edi
jne    116b <mergeSort+0x1b>
mov    %r13,%rax
jmp    1202 <mergeSort+0xb2>
mov    %edi,%ebx
mov    %edi,%eax
shr    $0x1f,%eax
add    %edi,%eax
sar    %eax
sub    %eax,%ebx
movslq %eax,%r15
lea    0x0(,%r15,4),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0x0(,%rbx,4),%edi
mov    %edi,0x4(%rsp)
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %r14,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
lea    0x0(,%r15,4),%rsi
add    %r13,%rsi
mov    %r12,%rdi
mov    0x4(%rsp),%edx
call   1040 <memcpy@plt>
mov    %r15d,%edi
mov    %r14,%rsi
call   1050 <mergeSort@plt>
mov    %rax,%rbp
mov    %ebx,%edi
mov    %r12,%rsi
call   1050 <mergeSort@plt>
mov    %r15d,%edi
mov    %ebx,%esi
mov    %rbp,%rdx
mov    %rax,%rcx
call   1060 <merge@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @mergeSort(i32 %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
store i32 %0, i32* %4
store i32* %1, i32** %5
%11 = load i32, i32* %4
%12 = icmp eq i32 1, %11
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i32*, i32** %5
store i32* %14, i32** %3
br label %64
15: ; preds = %2
%16 = load i32, i32* %4
%17 = sdiv i32 %16, 2
store i32 %17, i32* %7
%18 = load i32, i32* %4
%19 = load i32, i32* %7
%20 = sub nsw i32 %18, %19
store i32 %20, i32* %8
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = mul i64 4, %22
%24 = trunc i64 %23 to i32
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i32*
store i32* %26, i32** %9
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = mul i64 4, %28
%30 = trunc i64 %29 to i32
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i32*
store i32* %32, i32** %10
%33 = load i32*, i32** %9
%34 = load i32*, i32** %5
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = mul i64 4, %36
%38 = trunc i64 %37 to i32
%39 = call i32 @memcpy(i32* %33, i32* %34, i32 %38)
%40 = load i32*, i32** %10
%41 = load i32*, i32** %5
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = mul i64 4, %46
%48 = trunc i64 %47 to i32
%49 = call i32 @memcpy(i32* %40, i32* %44, i32 %48)
%50 = load i32, i32* %7
%51 = load i32, i32* %8
%52 = load i32, i32* %7
%53 = load i32*, i32** %9
%54 = call i32* @mergeSort(i32 %52, i32* %53)
%55 = load i32, i32* %8
%56 = load i32*, i32** %10
%57 = call i32* @mergeSort(i32 %55, i32* %56)
%58 = call i32* @merge(i32 %50, i32 %51, i32* %54, i32* %57)
store i32* %58, i32** %6
%59 = load i32*, i32** %9
%60 = call i32 @free(i32* %59)
%61 = load i32*, i32** %10
%62 = call i32 @free(i32* %61)
%63 = load i32*, i32** %6
store i32* %63, i32** %3
br label %64
64: ; preds = %15, %13
%65 = load i32*, i32** %3
ret i32* %65
}

LLVM IR (-O2):
define i32* @mergeSort(i32 %0, i32* %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %4, label %6
4: ; preds = %2, %6
%5 = phi i32* [ %21, %6 ], [ %1, %2 ]
ret i32* %5
6: ; preds = %2
%7 = sdiv i32 %0, 2
%8 = sub nsw i32 %0, %7
%9 = sext i32 %7 to i64
%10 = shl i32 %7, 2
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i32*
%13 = shl i32 %8, 2
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i32*
%16 = tail call i32 @memcpy(i32* %12, i32* %1, i32 %10)
%17 = getelementptr inbounds i32, i32* %1, i64 %9
%18 = tail call i32 @memcpy(i32* %15, i32* %17, i32 %13)
%19 = tail call i32* @mergeSort(i32 %7, i32* %12)
%20 = tail call i32* @mergeSort(i32 %8, i32* %15)
%21 = tail call i32* @merge(i32 %7, i32 %8, i32* %19, i32* %20)
%22 = tail call i32 @free(i32* %12)
%23 = tail call i32 @free(i32* %15)
br label %4
}