#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 *, long long *);
int *mergeSort(const int n, int *v, long long *count) {
  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, count),
                      mergeSort(n2, rightHalf, count), count);
  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    %rdx,-0x20(%rbp)
mov    $0x1,%eax
cmp    -0xc(%rbp),%eax
jne    117e <mergeSort+0x2e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1258 <mergeSort+0x108>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
movslq -0x30(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
movslq -0x30(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <mergeSort@plt>
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <mergeSort@plt>
mov    -0x50(%rbp),%edi
mov    -0x4c(%rbp),%esi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
mov    -0x20(%rbp),%r8
call   1060 <merge@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%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
sub    $0x18,%rsp
mov    %rsi,%r14
cmp    $0x1,%edi
jne    116e <mergeSort+0x1e>
mov    %r14,%rax
jmp    121f <mergeSort+0xcf>
mov    %edi,%ebx
mov    %edi,%eax
shr    $0x1f,%eax
add    %edi,%eax
sar    %eax
sub    %eax,%ebx
cltq
mov    %rax,0x8(%rsp)
lea    0x0(,%rax,4),%ebp
mov    %ebp,%edi
mov    %rdx,0x10(%rsp)
call   1070 <malloc@plt>
mov    %rax,%r12
lea    0x0(,%rbx,4),%r15d
mov    %r15d,%edi
call   1070 <malloc@plt>
mov    %rax,(%rsp)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
mov    0x8(%rsp),%rbp
lea    (%r14,%rbp,4),%rsi
mov    (%rsp),%r13
mov    %r13,%rdi
mov    %r15d,%edx
call   1040 <memcpy@plt>
mov    %ebp,%edi
mov    %rbp,%r15
mov    %r12,%rsi
mov    0x10(%rsp),%rbp
mov    %rbp,%rdx
call   1050 <mergeSort@plt>
mov    %rax,%r14
mov    %ebx,%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1050 <mergeSort@plt>
mov    %r15d,%edi
mov    %ebx,%esi
mov    %r14,%rdx
mov    %rax,%rcx
mov    %rbp,%r8
call   1060 <merge@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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