#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*/ merge(int *, int, int *, int, int *, int);
void mergeSort(int *array, int length) {
  int sub1Length = (length % 2 == 0) ? length >> 1 : (length >> 1) + 1;
  int sub2Length = length >> 1;
  int sub1[sub1Length];
  int sub2[sub2Length];
  int i;
  if (length > 1) {
    for (i = 0; i < sub1Length; ++i) {
      sub1[i] = array[i];
    }
    for (int j = 0, i = sub1Length; i < length && j < sub2Length; ++i, ++j) {
      sub2[j] = array[i];
    }
    mergeSort(sub1, sub1Length);
    mergeSort(sub2, sub2Length);
    merge(array, length, sub1, sub1Length, sub2, sub2Length);
  }
}


x86 assembly (clang, -O0):
<mergeSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1151 <mergeSort+0x31>
mov    -0xc(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    115d <mergeSort+0x3d>
mov    -0xc(%rbp),%eax
sar    $0x1,%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sar    %eax
mov    %eax,-0x14(%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,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
cmpl   $0x1,-0xc(%rbp)
jle    128d <mergeSort+0x16d>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11f3 <mergeSort+0xd3>
mov    -0x50(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq -0x34(%rbp),%rdx
mov    (%rcx,%rdx,4),%edx
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11c3 <mergeSort+0xa3>
movl   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%ecx
xor    %eax,%eax
cmp    -0xc(%rbp),%ecx
mov    %al,-0x51(%rbp)
jge    121d <mergeSort+0xfd>
mov    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    122d <mergeSort+0x10d>
jmp    125a <mergeSort+0x13a>
mov    -0x48(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq -0x3c(%rbp),%rdx
mov    (%rcx,%rdx,4),%edx
movslq -0x38(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1200 <mergeSort+0xe0>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <mergeSort@plt>
mov    -0x48(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mergeSort@plt>
mov    -0x50(%rbp),%rdx
mov    -0x48(%rbp),%r8
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%ecx
mov    -0x14(%rbp),%r9d
call   1040 <merge@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergeSort>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,-0x30(%rbp)
mov    %esi,%r12d
and    $0x1,%r12d
mov    %esi,%ebx
sar    %ebx
lea    (%rbx,%r12,1),%ecx
lea    0xf(,%rcx,4),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r13
sub    %rax,%r13
mov    %r13,%rsp
mov    %rsp,%r15
lea    0xf(,%rbx,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
cmp    $0x2,%esi
jl     1222 <mergeSort+0xf2>
mov    %esi,%r14d
test   %ecx,%ecx
mov    %rcx,-0x38(%rbp)
jle    11ad <mergeSort+0x7d>
lea    -0x1(%rcx),%eax
lea    0x4(,%rax,4),%rdx
mov    %r13,%rdi
mov    -0x30(%rbp),%rsi
call   1030 <memcpy@plt>
mov    -0x38(%rbp),%rcx
cmp    %r14d,%ecx
jge    11f3 <mergeSort+0xc3>
movslq %ecx,%rax
mov    -0x30(%rbp),%rcx
lea    (%rcx,%rax,4),%rsi
mov    %ebx,%eax
not    %eax
add    %r14d,%eax
sub    %r12d,%eax
cmp    $0x2,%ebx
mov    $0x1,%ecx
cmovge %ebx,%ecx
add    $0xffffffff,%ecx
cmp    %eax,%ecx
cmovae %eax,%ecx
test   %eax,%eax
cmove  %eax,%ecx
lea    0x4(,%rcx,4),%rdx
mov    %r15,%rdi
call   1030 <memcpy@plt>
mov    -0x38(%rbp),%rcx
mov    %r13,%rdi
mov    %ecx,%esi
mov    %rcx,%r12
call   1040 <mergeSort@plt>
mov    %r15,%rdi
mov    %ebx,%esi
call   1040 <mergeSort@plt>
mov    -0x30(%rbp),%rdi
mov    %r14d,%esi
mov    %r13,%rdx
mov    %r12d,%ecx
mov    %r15,%r8
mov    %ebx,%r9d
call   1050 <merge@plt>
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 i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%13 = load i32, i32* %4
%14 = srem i32 %13, 2
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* %4
%18 = ashr i32 %17, 1
br label %23
19: ; preds = %2
%20 = load i32, i32* %4
%21 = ashr i32 %20, 1
%22 = add nsw i32 %21, 1
br label %23
23: ; preds = %19, %16
%24 = phi i32 [ %18, %16 ], [ %22, %19 ]
store i32 %24, i32* %5
%25 = load i32, i32* %4
%26 = ashr i32 %25, 1
store i32 %26, i32* %6
%27 = load i32, i32* %5
%28 = zext i32 %27 to i64
%29 = call i8* @llvm.stacksave()
store i8* %29, i8** %7
%30 = alloca i32, i64 %28
store i64 %28, i64* %8
%31 = load i32, i32* %6
%32 = zext i32 %31 to i64
%33 = alloca i32, i64 %32
store i64 %32, i64* %9
%34 = load i32, i32* %4
%35 = icmp sgt i32 %34, 1
br i1 %35, label %36, label %87
36: ; preds = %23
store i32 0, i32* %10
br label %37
37: ; preds = %50, %36
%38 = load i32, i32* %10
%39 = load i32, i32* %5
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %53
41: ; preds = %37
%42 = load i32*, i32** %3
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %42, i64 %44
%46 = load i32, i32* %45
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %30, i64 %48
store i32 %46, i32* %49
br label %50
50: ; preds = %41
%51 = load i32, i32* %10
%52 = add nsw i32 %51, 1
store i32 %52, i32* %10
br label %37
53: ; preds = %37
store i32 0, i32* %11
%54 = load i32, i32* %5
store i32 %54, i32* %12
br label %55
55: ; preds = %74, %53
%56 = load i32, i32* %12
%57 = load i32, i32* %4
%58 = icmp slt i32 %56, %57
br i1 %58, label %59, label %63
59: ; preds = %55
%60 = load i32, i32* %11
%61 = load i32, i32* %6
%62 = icmp slt i32 %60, %61
br label %63
63: ; preds = %59, %55
%64 = phi i1 [ false, %55 ], [ %62, %59 ]
br i1 %64, label %65, label %79
65: ; preds = %63
%66 = load i32*, i32** %3
%67 = load i32, i32* %12
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i32, i32* %66, i64 %68
%70 = load i32, i32* %69
%71 = load i32, i32* %11
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i32, i32* %33, i64 %72
store i32 %70, i32* %73
br label %74
74: ; preds = %65
%75 = load i32, i32* %12
%76 = add nsw i32 %75, 1
store i32 %76, i32* %12
%77 = load i32, i32* %11
%78 = add nsw i32 %77, 1
store i32 %78, i32* %11
br label %55
79: ; preds = %63
%80 = load i32, i32* %5
%81 = load i32, i32* %6
%82 = load i32*, i32** %3
%83 = load i32, i32* %4
%84 = load i32, i32* %5
%85 = load i32, i32* %6
%86 = call i32 @merge(i32* %82, i32 %83, i32* %30, i32 %84, i32* %33, i32 %85)
br label %87
87: ; preds = %79, %23
%88 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
define void @mergeSort(i32* %0, i32 %1) {
%3 = bitcast i32* %0 to i8*
%4 = and i32 %1, 1
%5 = ashr i32 %1, 1
%6 = add nsw i32 %5, %4
%7 = zext i32 %6 to i64
%8 = alloca i32, i64 %7
%9 = bitcast i32* %8 to i8*
%10 = zext i32 %5 to i64
%11 = alloca i32, i64 %10
%12 = bitcast i32* %11 to i8*
%13 = icmp sgt i32 %1, 1
br i1 %13, label %14, label %40
14: ; preds = %2
%15 = icmp sgt i32 %6, 0
br i1 %15, label %16, label %21
16: ; preds = %14
%17 = add nsw i32 %6, -1
%18 = zext i32 %17 to i64
%19 = shl nuw nsw i64 %18, 2
%20 = add nuw nsw i64 %19, 4
br label %21
21: ; preds = %16, %14
%22 = icmp slt i32 %6, %1
br i1 %22, label %23, label %38
23: ; preds = %21
%24 = sext i32 %6 to i64
%25 = getelementptr i32, i32* %0, i64 %24
%26 = bitcast i32* %25 to i8*
%27 = xor i32 %5, -1
%28 = add i32 %27, %1
%29 = sub i32 %28, %4
%30 = call i32 @llvm.smax.i32(i32 %5, i32 1)
%31 = add nsw i32 %30, -1
%32 = icmp eq i32 %29, 0
%33 = call i32 @llvm.umin.i32(i32 %31, i32 %29)
%34 = select i1 %32, i32 0, i32 %33
%35 = zext i32 %34 to i64
%36 = shl nuw nsw i64 %35, 2
%37 = add nuw nsw i64 %36, 4
br label %38
38: ; preds = %23, %21
%39 = call i32 @merge(i32* %0, i32 %1, i32* %8, i32 %6, i32* %11, i32 %5)
br label %40
40: ; preds = %38, %2
ret void
}