#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);
void mergesort(int *array, int length) {
  int left_len = length / 2;
  int right_len = length - length / 2;
  int left[left_len];
  int right[right_len];
  int i, j, k;
  if (length == 1)
    return;
  i = j = k = 0;
  while (i < left_len)
    left[i++] = array[k++];
  while (j < right_len)
    right[j++] = array[k++];
  mergesort(left, left_len);
  mergesort(right, right_len);
  merge(array, left, left_len, right, right_len);
}


x86 assembly (clang, -O0):
<mergesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %ecx,%eax
sar    %eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%edx
shr    $0x1f,%edx
mov    %eax,%ecx
add    %edx,%ecx
sar    %ecx
sub    %ecx,%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)
jne    11ae <mergesort+0x8e>
movl   $0x1,-0x40(%rbp)
jmp    1275 <mergesort+0x155>
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11fe <mergesort+0xde>
mov    -0x50(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    -0x3c(%rbp),%edx
mov    %edx,%esi
add    $0x1,%esi
mov    %esi,-0x3c(%rbp)
movslq %edx,%rdx
mov    (%rcx,%rdx,4),%edx
mov    -0x34(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    11c3 <mergesort+0xa3>
jmp    1203 <mergesort+0xe3>
mov    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    123e <mergesort+0x11e>
mov    -0x48(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    -0x3c(%rbp),%edx
mov    %edx,%esi
add    $0x1,%esi
mov    %esi,-0x3c(%rbp)
movslq %edx,%rdx
mov    (%rcx,%rdx,4),%edx
mov    -0x38(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x38(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1203 <mergesort+0xe3>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1040 <mergesort@plt>
mov    -0x48(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mergesort@plt>
mov    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rcx
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%r8d
call   1030 <merge@plt>
movl   $0x0,-0x40(%rbp)
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
push   %rax
mov    %esi,%ebx
shr    $0x1f,%ebx
add    %esi,%ebx
sar    %ebx
mov    %esi,%r15d
sub    %ebx,%r15d
lea    0xf(,%rbx,4),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r12
sub    %rax,%r12
mov    %r12,%rsp
lea    0xf(,%r15,4),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
cmp    $0x1,%esi
je     11f6 <mergesort+0xc6>
mov    %esi,%r13d
mov    %rdi,%rcx
mov    %rdi,-0x30(%rbp)
jle    11a7 <mergesort+0x77>
lea    -0x1(%rbx),%eax
lea    0x4(,%rax,4),%rdx
mov    %r12,%rdi
mov    %rcx,%rsi
call   1030 <memcpy@plt>
mov    %ebx,%eax
test   %r15d,%r15d
jg     11ae <mergesort+0x7e>
jmp    11cd <mergesort+0x9d>
xor    %eax,%eax
test   %r15d,%r15d
jle    11cd <mergesort+0x9d>
mov    -0x30(%rbp),%rcx
lea    (%rcx,%rax,4),%rsi
mov    %ebx,%eax
not    %eax
add    %r13d,%eax
lea    0x4(,%rax,4),%rdx
mov    %r14,%rdi
call   1030 <memcpy@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   1050 <mergesort@plt>
mov    %r14,%rdi
mov    %r15d,%esi
call   1050 <mergesort@plt>
mov    -0x30(%rbp),%rdi
mov    %r12,%rsi
mov    %ebx,%edx
mov    %r14,%rcx
mov    %r15d,%r8d
call   1040 <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
%13 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%14 = load i32, i32* %4
%15 = sdiv i32 %14, 2
store i32 %15, i32* %5
%16 = load i32, i32* %4
%17 = load i32, i32* %4
%18 = sdiv i32 %17, 2
%19 = sub nsw i32 %16, %18
store i32 %19, i32* %6
%20 = load i32, i32* %5
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %7
%23 = alloca i32, i64 %21
store i64 %21, i64* %8
%24 = load i32, i32* %6
%25 = zext i32 %24 to i64
%26 = alloca i32, i64 %25
store i64 %25, i64* %9
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %30
29: ; preds = %2
store i32 1, i32* %13
br label %69
30: ; preds = %2
store i32 0, i32* %12
store i32 0, i32* %11
store i32 0, i32* %10
br label %31
31: ; preds = %35, %30
%32 = load i32, i32* %10
%33 = load i32, i32* %5
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %46
35: ; preds = %31
%36 = load i32*, i32** %3
%37 = load i32, i32* %12
%38 = add nsw i32 %37, 1
store i32 %38, i32* %12
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i32, i32* %36, i64 %39
%41 = load i32, i32* %40
%42 = load i32, i32* %10
%43 = add nsw i32 %42, 1
store i32 %43, i32* %10
%44 = sext i32 %42 to i64
%45 = getelementptr inbounds i32, i32* %23, i64 %44
store i32 %41, i32* %45
br label %31
46: ; preds = %31
br label %47
47: ; preds = %51, %46
%48 = load i32, i32* %11
%49 = load i32, i32* %6
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %62
51: ; preds = %47
%52 = load i32*, i32** %3
%53 = load i32, i32* %12
%54 = add nsw i32 %53, 1
store i32 %54, i32* %12
%55 = sext i32 %53 to i64
%56 = getelementptr inbounds i32, i32* %52, i64 %55
%57 = load i32, i32* %56
%58 = load i32, i32* %11
%59 = add nsw i32 %58, 1
store i32 %59, i32* %11
%60 = sext i32 %58 to i64
%61 = getelementptr inbounds i32, i32* %26, i64 %60
store i32 %57, i32* %61
br label %47
62: ; preds = %47
%63 = load i32, i32* %5
%64 = load i32, i32* %6
%65 = load i32*, i32** %3
%66 = load i32, i32* %5
%67 = load i32, i32* %6
%68 = call i32 @merge(i32* %65, i32* %23, i32 %66, i32* %26, i32 %67)
store i32 0, i32* %13
br label %69
69: ; preds = %62, %29
%70 = load i8*, i8** %7
%71 = load i32, i32* %13
switch i32 %71, label %73 [
i32 0, label %72
i32 1, label %72
]
72: ; preds = %69, %69
ret void
73: ; preds = %69
unreachable
}

LLVM IR (-O2):
define void @mergesort(i32* %0, i32 %1) {
%3 = bitcast i32* %0 to i8*
%4 = sdiv i32 %1, 2
%5 = sub nsw i32 %1, %4
%6 = zext i32 %4 to i64
%7 = alloca i32, i64 %6
%8 = bitcast i32* %7 to i8*
%9 = zext i32 %5 to i64
%10 = alloca i32, i64 %9
%11 = bitcast i32* %10 to i8*
%12 = icmp eq i32 %1, 1
br i1 %12, label %35, label %13
13: ; preds = %2
%14 = icmp sgt i32 %1, 1
br i1 %14, label %15, label %22
15: ; preds = %13
%16 = add nsw i32 %4, -1
%17 = zext i32 %16 to i64
%18 = shl nuw nsw i64 %17, 2
%19 = add nuw nsw i64 %18, 4
%20 = add nuw nsw i64 %17, 1
%21 = and i64 %20, 4294967295
br label %22
22: ; preds = %15, %13
%23 = phi i64 [ 0, %13 ], [ %21, %15 ]
%24 = icmp sgt i32 %5, 0
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = getelementptr i32, i32* %0, i64 %23
%27 = bitcast i32* %26 to i8*
%28 = xor i32 %4, -1
%29 = add i32 %28, %1
%30 = zext i32 %29 to i64
%31 = shl nuw nsw i64 %30, 2
%32 = add nuw nsw i64 %31, 4
br label %33
33: ; preds = %25, %22
%34 = call i32 @merge(i32* %0, i32* %7, i32 %4, i32* %10, i32 %5)
br label %35
35: ; preds = %2, %33
ret void
}