#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*/ CopyArray(void *, void *, int, int,
                             void (*)(void *, void *, int, int));
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ Merge(void *, void *, void *, int,
                         int (*)(void *, void *, int, int),
                         void (*)(void *, void *, int, int));
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(void *);
void *malloc(int);
int /*<<< orphan*/ perror(char *);
void Mergesort(void *array, int length, int Size(void),
               int Compare(void *, void *, int, int),
               void Assign(void *, void *, int, int)) {
  void *bottom_half;
  void *top_half;
  if (length == 1)
    return;
  bottom_half = malloc(length / 2 * Size());
  top_half = malloc((length / 2 + length % 2) * Size());
  if (!bottom_half || !top_half) {
    perror("Could not allocate memory");
    exit(EXIT_FAILURE);
  }
  CopyArray(bottom_half, array, length / 2, 0, Assign);
  CopyArray(top_half, array, (length / 2 + length % 2), length / 2, Assign);
  Mergesort(bottom_half, length / 2, Size, Compare, Assign);
  Mergesort(top_half, (length / 2 + length % 2), Size, Compare, Assign);
  Merge(array, bottom_half, top_half, length, Compare, Assign);
  free(bottom_half);
  free(top_half);
}


x86 assembly (clang, -O0):
<Mergesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    119a <Mergesort+0x2a>
jmp    12fe <Mergesort+0x18e>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x44(%rbp)
call   *-0x18(%rbp)
mov    -0x44(%rbp),%edi
imul   %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x40(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x40(%rbp),%eax
add    %edx,%eax
mov    %eax,-0x3c(%rbp)
call   *-0x18(%rbp)
mov    -0x3c(%rbp),%edi
imul   %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1203 <Mergesort+0x93>
cmpq   $0x0,-0x38(%rbp)
jne    121d <Mergesort+0xad>
lea    0xdf6(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
mov    -0x28(%rbp),%r8
xor    %ecx,%ecx
call   1090 <CopyArray@plt>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4c(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x4c(%rbp),%eax
add    %edx,%eax
mov    %eax,-0x48(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x48(%rbp),%edx
mov    %eax,%ecx
mov    -0x28(%rbp),%r8
call   1090 <CopyArray@plt>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1040 <Mergesort@plt>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
add    %edx,%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1040 <Mergesort@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0xc(%rbp),%ecx
mov    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9
call   1050 <Merge@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Mergesort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x1,%esi
jne    1192 <Mergesort+0x22>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,0x8(%rsp)
mov    %rcx,0x18(%rsp)
mov    %r8,0x20(%rsp)
mov    %esi,%ebx
shr    $0x1f,%ebx
add    %esi,%ebx
mov    %ebx,%r14d
sar    %r14d
call   *%rdx
imul   %r14d,%eax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
and    $0xfffffffe,%ebx
mov    %ebp,0x4(%rsp)
sub    %ebx,%ebp
add    %r14d,%ebp
mov    %r15,0x10(%rsp)
call   *%r15
imul   %ebp,%eax
mov    %eax,%edi
call   1060 <malloc@plt>
test   %r12,%r12
je     1292 <Mergesort+0x122>
mov    %rax,%r15
test   %rax,%rax
je     1292 <Mergesort+0x122>
mov    %r12,%rax
mov    %rax,%rdi
mov    0x8(%rsp),%r13
mov    %r13,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    0x20(%rsp),%rbx
mov    %rbx,%r8
call   1090 <CopyArray@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
mov    %rbx,%r8
call   1090 <CopyArray@plt>
mov    %r12,%rdi
mov    %r14d,%esi
mov    0x10(%rsp),%r13
mov    %r13,%rdx
mov    0x18(%rsp),%r14
mov    %r14,%rcx
mov    %rbx,%r8
call   1040 <Mergesort@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r13,%rdx
mov    %r14,%rcx
mov    %rbx,%r8
call   1040 <Mergesort@plt>
mov    0x8(%rsp),%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    0x4(%rsp),%ecx
mov    %r14,%r8
mov    %rbx,%r9
call   1050 <Merge@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xd67(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Could not allocate memory\00"
@EXIT_FAILURE = global i32 0
define void @Mergesort(i8* %0, i32 %1, i32 ()* %2, i32 (i8*, i8*, i32, i32)* %3, void (i8*, i8*, i32, i32)* %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32 ()*
%9 = alloca i32 (i8*, i8*, i32, i32)*
%10 = alloca void (i8*, i8*, i32, i32)*
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 ()* %2, i32 ()** %8
store i32 (i8*, i8*, i32, i32)* %3, i32 (i8*, i8*, i32, i32)** %9
store void (i8*, i8*, i32, i32)* %4, void (i8*, i8*, i32, i32)** %10
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %16
15: ; preds = %5
br label %85
16: ; preds = %5
%17 = load i32, i32* %7
%18 = sdiv i32 %17, 2
%19 = load i32 ()*, i32 ()** %8
%20 = call i32 %19()
%21 = mul nsw i32 %18, %20
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %11
%23 = load i32, i32* %7
%24 = sdiv i32 %23, 2
%25 = load i32, i32* %7
%26 = srem i32 %25, 2
%27 = add nsw i32 %24, %26
%28 = load i32 ()*, i32 ()** %8
%29 = call i32 %28()
%30 = mul nsw i32 %27, %29
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %12
%32 = load i8*, i8** %11
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %37
34: ; preds = %16
%35 = load i8*, i8** %12
%36 = icmp ne i8* %35, null
br i1 %36, label %41, label %37
37: ; preds = %34, %16
%38 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %34
%42 = load i8*, i8** %11
%43 = load i8*, i8** %6
%44 = load i32, i32* %7
%45 = sdiv i32 %44, 2
%46 = load void (i8*, i8*, i32, i32)*, void (i8*, i8*, i32, i32)** %10
%47 = call i32 @CopyArray(i8* %42, i8* %43, i32 %45, i32 0, void (i8*, i8*, i32, i32)* %46)
%48 = load i8*, i8** %12
%49 = load i8*, i8** %6
%50 = load i32, i32* %7
%51 = sdiv i32 %50, 2
%52 = load i32, i32* %7
%53 = srem i32 %52, 2
%54 = add nsw i32 %51, %53
%55 = load i32, i32* %7
%56 = sdiv i32 %55, 2
%57 = load void (i8*, i8*, i32, i32)*, void (i8*, i8*, i32, i32)** %10
%58 = call i32 @CopyArray(i8* %48, i8* %49, i32 %54, i32 %56, void (i8*, i8*, i32, i32)* %57)
%59 = load i8*, i8** %11
%60 = load i32, i32* %7
%61 = sdiv i32 %60, 2
%62 = load i32 ()*, i32 ()** %8
%63 = load i32 (i8*, i8*, i32, i32)*, i32 (i8*, i8*, i32, i32)** %9
%64 = load void (i8*, i8*, i32, i32)*, void (i8*, i8*, i32, i32)** %10
%65 = load i8*, i8** %12
%66 = load i32, i32* %7
%67 = sdiv i32 %66, 2
%68 = load i32, i32* %7
%69 = srem i32 %68, 2
%70 = add nsw i32 %67, %69
%71 = load i32 ()*, i32 ()** %8
%72 = load i32 (i8*, i8*, i32, i32)*, i32 (i8*, i8*, i32, i32)** %9
%73 = load void (i8*, i8*, i32, i32)*, void (i8*, i8*, i32, i32)** %10
%74 = load i8*, i8** %6
%75 = load i8*, i8** %11
%76 = load i8*, i8** %12
%77 = load i32, i32* %7
%78 = load i32 (i8*, i8*, i32, i32)*, i32 (i8*, i8*, i32, i32)** %9
%79 = load void (i8*, i8*, i32, i32)*, void (i8*, i8*, i32, i32)** %10
%80 = call i32 @Merge(i8* %74, i8* %75, i8* %76, i32 %77, i32 (i8*, i8*, i32, i32)* %78, void (i8*, i8*, i32, i32)* %79)
%81 = load i8*, i8** %11
%82 = call i32 @free(i8* %81)
%83 = load i8*, i8** %12
%84 = call i32 @free(i8* %83)
br label %85
85: ; preds = %41, %15
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"Could not allocate memory\00"
@EXIT_FAILURE = global i32 0
define void @Mergesort(i8* %0, i32 %1, i32 ()* %2, i32 (i8*, i8*, i32, i32)* %3, void (i8*, i8*, i32, i32)* %4) {
%6 = icmp eq i32 %1, 1
br i1 %6, label %24, label %7
7: ; preds = %5
%8 = sdiv i32 %1, 2
%9 = tail call i32 %2()
%10 = mul nsw i32 %9, %8
%11 = tail call i8* @malloc(i32 %10)
%12 = srem i32 %1, 2
%13 = add nsw i32 %8, %12
%14 = tail call i32 %2()
%15 = mul nsw i32 %14, %13
%16 = tail call i8* @malloc(i32 %15)
%17 = icmp ne i8* %11, null
%18 = icmp ne i8* %16, null
%19 = and i1 %17, %18
br i1 %19, label %25, label %20
20: ; preds = %7
%21 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %5, %25
ret void
25: ; preds = %7
%26 = tail call i32 @CopyArray(i8* %11, i8* %0, i32 %8, i32 0, void (i8*, i8*, i32, i32)* %4)
%27 = tail call i32 @CopyArray(i8* %16, i8* %0, i32 %13, i32 %8, void (i8*, i8*, i32, i32)* %4)
tail call void @Mergesort(i8* %11, i32 %8, i32 ()* %2, i32 (i8*, i8*, i32, i32)* %3, void (i8*, i8*, i32, i32)* %4)
tail call void @Mergesort(i8* %16, i32 %13, i32 ()* %2, i32 (i8*, i8*, i32, i32)* %3, void (i8*, i8*, i32, i32)* %4)
%28 = tail call i32 @Merge(i8* %0, i8* %11, i8* %16, i32 %1, i32 (i8*, i8*, i32, i32)* %3, void (i8*, i8*, i32, i32)* %4)
%29 = tail call i32 @free(i8* %11)
%30 = tail call i32 @free(i8* %16)
br label %24
}