#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*/ sort(void *, size_t, size_t,
                        int (*)(void const *, void const *, void *), void *,
                        void *, int /*<<< orphan*/, size_t);
void merge_sort(void *base, size_t nmemb, size_t size,
                int (*compare)(const void *a, const void *b, void *context),
                void *context) {
  void *aux;
  aux = malloc(nmemb * size);
  if (aux == NULL) {
    return;
  }
  sort(base, nmemb, size, compare, context, aux, 0, nmemb - 1);
  free(aux);
}


x86 assembly (clang, -O0):
<merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
imul   -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    116e <merge_sort+0x3e>
jmp    11ab <merge_sort+0x7b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
xor    %r10d,%r10d
movl   $0x0,(%rsp)
mov    %rax,0x8(%rsp)
call   1040 <sort@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rdx,%rdi
imul   %rsi,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1196 <merge_sort+0x66>
mov    %rax,%r13
lea    -0x1(%rbx),%rax
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
mov    %r14,%r8
mov    %r13,%r9
push   %rax
push   $0x0
call   1040 <sort@plt>
add    $0x10,%rsp
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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