#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*/ combine(int *, int, int *, int, int *);
int /*<<< orphan*/ free(int *);
int *malloc(int);
int *mergesort(int data[], int sz) {
  int *r = malloc(sizeof(int) * sz);
  int mid = sz / 2;
  int *sub1, *sub2;
  if (sz == 1) {
    r[0] = data[0];
    return r;
  }
  sub1 = mergesort(data, mid);
  sub2 = mergesort(data + mid, sz - mid);
  combine(sub1, mid, sub2, sz - mid, r);
  free(sub1);
  free(sub2);
  return r;
}


x86 assembly (clang, -O0):
<mergesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    1193 <mergesort+0x53>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f5 <mergesort+0xb5>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1060 <mergesort@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x24(%rbp),%esi
call   1060 <mergesort@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x14(%rbp),%ecx
sub    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%r8
call   1040 <combine@plt>
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    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergesort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r15
lea    0x0(,%rbx,4),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
cmp    $0x1,%ebx
jne    1169 <mergesort+0x29>
mov    (%r15),%eax
mov    %eax,(%r14)
jmp    11b4 <mergesort+0x74>
mov    %ebx,%ebp
shr    $0x1f,%ebp
add    %ebx,%ebp
sar    %ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <mergesort@plt>
mov    %rax,%r12
movslq %ebp,%rbp
lea    (%r15,%rbp,4),%rdi
sub    %ebp,%ebx
mov    %ebx,%esi
call   1060 <mergesort@plt>
mov    %rax,%r15
mov    %r12,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
mov    %ebx,%ecx
mov    %r14,%r8
call   1040 <combine@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
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*
store i32* %0, i32** %4
store i32 %1, i32* %5
%10 = load i32, i32* %5
%11 = sext i32 %10 to i64
%12 = mul i64 4, %11
%13 = trunc i64 %12 to i32
%14 = call i32* @malloc(i32 %13)
store i32* %14, i32** %6
%15 = load i32, i32* %5
%16 = sdiv i32 %15, 2
store i32 %16, i32* %7
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %26
19: ; preds = %2
%20 = load i32*, i32** %4
%21 = getelementptr inbounds i32, i32* %20, i64 0
%22 = load i32, i32* %21
%23 = load i32*, i32** %6
%24 = getelementptr inbounds i32, i32* %23, i64 0
store i32 %22, i32* %24
%25 = load i32*, i32** %6
store i32* %25, i32** %3
br label %51
26: ; preds = %2
%27 = load i32*, i32** %4
%28 = load i32, i32* %7
%29 = call i32* @mergesort(i32* %27, i32 %28)
store i32* %29, i32** %8
%30 = load i32*, i32** %4
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = load i32, i32* %5
%35 = load i32, i32* %7
%36 = sub nsw i32 %34, %35
%37 = call i32* @mergesort(i32* %33, i32 %36)
store i32* %37, i32** %9
%38 = load i32*, i32** %8
%39 = load i32, i32* %7
%40 = load i32*, i32** %9
%41 = load i32, i32* %5
%42 = load i32, i32* %7
%43 = sub nsw i32 %41, %42
%44 = load i32*, i32** %6
%45 = call i32 @combine(i32* %38, i32 %39, i32* %40, i32 %43, i32* %44)
%46 = load i32*, i32** %8
%47 = call i32 @free(i32* %46)
%48 = load i32*, i32** %9
%49 = call i32 @free(i32* %48)
%50 = load i32*, i32** %6
store i32* %50, i32** %3
br label %51
51: ; preds = %26, %19
%52 = load i32*, i32** %3
ret i32* %52
}

LLVM IR (-O2):
define i32* @mergesort(i32* %0, i32 %1) {
%3 = shl i32 %1, 2
%4 = tail call i32* @malloc(i32 %3)
%5 = sdiv i32 %1, 2
%6 = icmp eq i32 %1, 1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* %0
store i32 %8, i32* %4
br label %9
9: ; preds = %7, %10
ret i32* %4
10: ; preds = %2
%11 = tail call i32* @mergesort(i32* %0, i32 %5)
%12 = sext i32 %5 to i64
%13 = getelementptr inbounds i32, i32* %0, i64 %12
%14 = sub nsw i32 %1, %5
%15 = tail call i32* @mergesort(i32* %13, i32 %14)
%16 = tail call i32 @combine(i32* %11, i32 %5, i32* %15, i32 %14, i32* %4)
%17 = tail call i32 @free(i32* %11)
%18 = tail call i32 @free(i32* %15)
br label %9
}