#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*/ free(void *);
void *malloc(size_t);
int /*<<< orphan*/ mergesort_rec(void *, void *, int /*<<< orphan*/, size_t,
                                 size_t, int (*)(void const *, void const *));
void mergesort(void *base, size_t nmemb, size_t size,
               int (*compare)(const void *, const void *)) {
  if (base == NULL || compare == NULL)
    return;
  void *helper = malloc(nmemb * size);
  mergesort_rec(base, helper, 0, nmemb - 1, size, compare);
  free(helper);
}


x86 assembly (clang, -O0):
<mergesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     115e <mergesort+0x2e>
cmpq   $0x0,-0x20(%rbp)
jne    1163 <mergesort+0x33>
jmp    119d <mergesort+0x6d>
mov    -0x10(%rbp),%rdi
imul   -0x18(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
xor    %edx,%edx
call   1050 <mergesort_rec@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergesort>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rdi,%rdi
je     1189 <mergesort+0x59>
mov    %rcx,%r15
test   %rcx,%rcx
je     1189 <mergesort+0x59>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rdx,%rdi
imul   %rsi,%rdi
call   1040 <malloc@plt>
mov    %rax,%r13
add    $0xffffffffffffffff,%rbx
mov    %r12,%rdi
mov    %rax,%rsi
xor    %edx,%edx
mov    %rbx,%rcx
mov    %r14,%r8
mov    %r15,%r9
call   1050 <mergesort_rec@plt>
mov    %r13,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <free@plt>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @mergesort(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32 (i8*, i8*)*
%9 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32 (i8*, i8*)* %3, i32 (i8*, i8*)** %8
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %4
%13 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%14 = icmp eq i32 (i8*, i8*)* %13, null
br i1 %14, label %15, label %16
15: ; preds = %12, %4
br label %30
16: ; preds = %12
%17 = load i64, i64* %6
%18 = load i64, i64* %7
%19 = mul i64 %17, %18
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %9
%21 = load i8*, i8** %5
%22 = load i8*, i8** %9
%23 = load i64, i64* %6
%24 = sub i64 %23, 1
%25 = load i64, i64* %7
%26 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%27 = call i32 @mergesort_rec(i8* %21, i8* %22, i32 0, i64 %24, i64 %25, i32 (i8*, i8*)* %26)
%28 = load i8*, i8** %9
%29 = call i32 @free(i8* %28)
br label %30
30: ; preds = %16, %15
ret void
}

LLVM IR (-O2):
define void @mergesort(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = icmp eq i8* %0, null
%6 = icmp eq i32 (i8*, i8*)* %3, null
%7 = or i1 %5, %6
br i1 %7, label %14, label %8
8: ; preds = %4
%9 = mul i64 %2, %1
%10 = tail call i8* @malloc(i64 %9)
%11 = add i64 %1, -1
%12 = tail call i32 @mergesort_rec(i8* %0, i8* %10, i32 0, i64 %11, i64 %2, i32 (i8*, i8*)* %3)
%13 = tail call i32 @free(i8* %10)
br label %14
14: ; preds = %4, %8
ret void
}