#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 *calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ merge(int *, int *, int *, int, int);
int /*<<< orphan*/ printf(char *);
void mergesort(int key[], int n) {
  int j, k, m, *w;
  for (m = 1; m < n; m *= 2)
    ;
  if (m != n) {
    printf("ERROR: Size of the array is not a power of 2 - bye!\n");
    exit(1);
  }
  w = calloc(n, sizeof(int));
  for (k = 1; k < n; k *= 2) {
    for (j = 0; j < n - k; j += 2 * k)
      merge(key + j, key + j + k, w + j, k, k);
    for (j = 0; j < n; ++j)
      key[j] = w[j];
  }
  free(w);
}


x86 assembly (clang, -O0):
<mergesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1185 <mergesort+0x35>
jmp    1177 <mergesort+0x27>
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1166 <mergesort+0x16>
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     11a7 <mergesort+0x57>
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1282 <mergesort+0x132>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    -0x14(%rbp),%ecx
cmp    %ecx,%eax
jge    1238 <mergesort+0xe8>
mov    -0x8(%rbp),%rdi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
mov    -0x14(%rbp),%ecx
mov    -0x14(%rbp),%r8d
call   1060 <merge@plt>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11d2 <mergesort+0x82>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    126f <mergesort+0x11f>
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    123f <mergesort+0xef>
jmp    1274 <mergesort+0x124>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11bf <mergesort+0x6f>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergesort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebp
mov    %rdi,0x18(%rsp)
mov    $0x1,%eax
nopw   0x0(%rax,%rax,1)
lea    (%rax,%rax,1),%ecx
cmp    %ebp,%eax
mov    %ecx,%eax
jl     1180 <mergesort+0x20>
jne    1266 <mergesort+0x106>
mov    %ebp,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,0x8(%rsp)
cmp    $0x2,%ebp
jl     124e <mergesort+0xee>
mov    %ebp,%eax
shl    $0x2,%rax
mov    %rax,0x20(%rsp)
mov    $0x1,%ebx
mov    %ebp,0x14(%rsp)
jmp    11de <mergesort+0x7e>
nop
mov    0x18(%rsp),%rdi
mov    0x8(%rsp),%rsi
mov    0x20(%rsp),%rdx
call   1060 <memcpy@plt>
add    %ebx,%ebx
mov    0x14(%rsp),%ebp
cmp    %ebp,%ebx
jge    124e <mergesort+0xee>
sub    %ebx,%ebp
jle    11c0 <mergesort+0x60>
movslq %ebx,%rdx
lea    (%rbx,%rbx,1),%ecx
movslq %ecx,%rcx
mov    %ebp,%eax
mov    %rax,0x28(%rsp)
shl    $0x2,%rdx
mov    %rdx,0x30(%rsp)
mov    %rcx,%r15
lea    0x0(,%rcx,4),%r12
mov    0x8(%rsp),%rbp
mov    0x18(%rsp),%r14
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x30(%rsp),%rax
lea    (%r14,%rax,1),%rsi
mov    %r14,%rdi
mov    %rbp,%rdx
mov    %ebx,%ecx
mov    %ebx,%r8d
call   1070 <merge@plt>
add    %r15,%r13
add    %r12,%r14
add    %r12,%rbp
cmp    0x28(%rsp),%r13
jl     1220 <mergesort+0xc0>
jmp    11c0 <mergesort+0x60>
mov    0x8(%rsp),%rdi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xd93(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [53 x i8] c"ERROR: Size of the array is not a power of 2 - bye!\0A\00"
define void @mergesort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 1, i32* %7
br label %9
9: ; preds = %14, %2
%10 = load i32, i32* %7
%11 = load i32, i32* %4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %9
br label %14
14: ; preds = %13
%15 = load i32, i32* %7
%16 = mul nsw i32 %15, 2
store i32 %16, i32* %7
br label %9
17: ; preds = %9
%18 = load i32, i32* %7
%19 = load i32, i32* %4
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = load i32, i32* %4
%26 = call i32* @calloc(i32 %25, i32 4)
store i32* %26, i32** %8
store i32 1, i32* %6
br label %27
27: ; preds = %81, %24
%28 = load i32, i32* %6
%29 = load i32, i32* %4
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %84
31: ; preds = %27
store i32 0, i32* %5
br label %32
32: ; preds = %57, %31
%33 = load i32, i32* %5
%34 = load i32, i32* %4
%35 = load i32, i32* %6
%36 = sub nsw i32 %34, %35
%37 = icmp slt i32 %33, %36
br i1 %37, label %38, label %62
38: ; preds = %32
%39 = load i32*, i32** %3
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32*, i32** %3
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
%50 = load i32*, i32** %8
%51 = load i32, i32* %5
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
%54 = load i32, i32* %6
%55 = load i32, i32* %6
%56 = call i32 @merge(i32* %42, i32* %49, i32* %53, i32 %54, i32 %55)
br label %57
57: ; preds = %38
%58 = load i32, i32* %6
%59 = mul nsw i32 2, %58
%60 = load i32, i32* %5
%61 = add nsw i32 %60, %59
store i32 %61, i32* %5
br label %32
62: ; preds = %32
store i32 0, i32* %5
br label %63
63: ; preds = %77, %62
%64 = load i32, i32* %5
%65 = load i32, i32* %4
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %80
67: ; preds = %63
%68 = load i32*, i32** %8
%69 = load i32, i32* %5
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i32, i32* %68, i64 %70
%72 = load i32, i32* %71
%73 = load i32*, i32** %3
%74 = load i32, i32* %5
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i32, i32* %73, i64 %75
store i32 %72, i32* %76
br label %77
77: ; preds = %67
%78 = load i32, i32* %5
%79 = add nsw i32 %78, 1
store i32 %79, i32* %5
br label %63
80: ; preds = %63
br label %81
81: ; preds = %80
%82 = load i32, i32* %6
%83 = mul nsw i32 %82, 2
store i32 %83, i32* %6
br label %27
84: ; preds = %27
%85 = load i32*, i32** %8
%86 = call i32 @free(i32* %85)
ret void
}

LLVM IR (-O2):
@str = [52 x i8] c"ERROR: Size of the array is not a power of 2 - bye!\00"
define void @mergesort(i32* %0, i32 %1) {
%3 = bitcast i32* %0 to i8*
br label %4
4: ; preds = %4, %2
%5 = phi i32 [ 1, %2 ], [ %7, %4 ]
%6 = icmp slt i32 %5, %1
%7 = shl nsw i32 %5, 1
br i1 %6, label %4, label %8
8: ; preds = %4
%9 = icmp eq i32 %5, %1
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %8
%14 = tail call i32* @calloc(i32 %1, i32 4)
%15 = bitcast i32* %14 to i8*
%16 = icmp sgt i32 %1, 1
br i1 %16, label %17, label %40
17: ; preds = %13
%18 = zext i32 %1 to i64
%19 = shl nuw nsw i64 %18, 2
br label %20
20: ; preds = %17, %37
%21 = phi i32 [ 1, %17 ], [ %38, %37 ]
%22 = sub nsw i32 %1, %21
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %37
24: ; preds = %20
%25 = sext i32 %21 to i64
%26 = shl i32 %21, 1
%27 = sext i32 %26 to i64
%28 = zext i32 %22 to i64
br label %29
29: ; preds = %24, %29
%30 = phi i64 [ 0, %24 ], [ %35, %29 ]
%31 = getelementptr inbounds i32, i32* %0, i64 %30
%32 = getelementptr inbounds i32, i32* %31, i64 %25
%33 = getelementptr inbounds i32, i32* %14, i64 %30
%34 = tail call i32 @merge(i32* %31, i32* %32, i32* %33, i32 %21, i32 %21)
%35 = add i64 %30, %27
%36 = icmp slt i64 %35, %28
br i1 %36, label %29, label %37
37: ; preds = %29, %20
%38 = shl nsw i32 %21, 1
%39 = icmp slt i32 %38, %1
br i1 %39, label %20, label %40
40: ; preds = %37, %13
%41 = tail call i32 @free(i32* %14)
ret void
}