#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 */
typedef int /*<<< orphan*/ A;

/* Variables and functions */
int /*<<< orphan*/ merge(int *, int *, int *);
void mergesort(int A[]) {
  int i, mid, n;
  n = sizeof(A) / sizeof(A[0]);
  if (n < 2) {
    return;
  }
  mid = n / 2;
  int L[mid];
  int R[n - mid];
  for (i = 0; i < mid; i++) {
    L[i] = A[i];
  }
  for (i = 0; i < n - mid; i++) {
    R[i] = A[mid + 1 + i];
  }
  mergesort(L);
  mergesort(R);
  merge(L, R, A);
}


x86 assembly (clang, -O0):
<mergesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x2,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
jge    1142 <mergesort+0x22>
jmp    1242 <mergesort+0x122>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %ecx,%eax
sar    %eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    %ecx,%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11d4 <mergesort+0xb4>
mov    -0x40(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq -0xc(%rbp),%rdx
mov    (%rcx,%rdx,4),%edx
movslq -0xc(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a4 <mergesort+0x84>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    -0x10(%rbp),%ecx
cmp    %ecx,%eax
jge    1218 <mergesort+0xf8>
mov    -0x38(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%edx
add    $0x1,%edx
add    -0xc(%rbp),%edx
movslq %edx,%rdx
mov    (%rcx,%rdx,4),%edx
movslq -0xc(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11db <mergesort+0xbb>
mov    -0x40(%rbp),%rdi
call   1040 <mergesort@plt>
mov    -0x38(%rbp),%rdi
call   1040 <mergesort@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1030 <merge@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergesort>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
mov    (%rdi),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rdi),%eax
mov    %eax,(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <mergesort@plt>
mov    %rsp,%r15
mov    %r15,%rdi
call   1040 <mergesort@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <merge@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @mergesort(i32* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = load i32, i32* %0
store i32 %4, i32* %2
%5 = getelementptr inbounds i32, i32* %0, i64 2
%6 = load i32, i32* %5
store i32 %6, i32* %3
%7 = call i32 @merge(i32* %2, i32* %3, i32* %0)
ret void
}