#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(char *);
char *malloc(size_t);
int /*<<< orphan*/ merge(char *, char *, size_t, size_t, size_t,
                         int (*)(void const *, void const *));
void merge_sort(void *base, size_t n, size_t width,
                int (*cmp)(const void *, const void *)) {
  size_t length = 1;
  char *temp = malloc(width * n);
  char *base1 = (char *)base;
  while (length <= n) {
    merge(base1, temp, n, width, length, cmp);
    length *= 2;
    merge(temp, base1, n, width, length, cmp);
    length *= 2;
  }
  free(temp);
}


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)
movq   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
imul   -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
ja     11cf <merge_sort+0x9f>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x20(%rbp),%r9
call   1040 <merge@plt>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x20(%rbp),%r9
call   1040 <merge@plt>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    116a <merge_sort+0x3a>
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    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rdx,%rdi
imul   %rsi,%rdi
call   1050 <malloc@plt>
mov    %rax,%r13
test   %rbx,%rbx
je     119a <merge_sort+0x6a>
mov    $0x1,%ebp
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
mov    %rbp,%r8
mov    %r14,%r9
call   1040 <merge@plt>
mov    %rbp,%r8
add    %rbp,%r8
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
mov    %r14,%r9
call   1040 <merge@plt>
shl    $0x2,%rbp
cmp    %rbx,%rbp
jbe    1160 <merge_sort+0x30>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @merge_sort(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 i64
%10 = alloca i8*
%11 = 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
store i64 1, i64* %9
%12 = load i64, i64* %7
%13 = load i64, i64* %6
%14 = mul i64 %12, %13
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %10
%16 = load i8*, i8** %5
store i8* %16, i8** %11
br label %17
17: ; preds = %21, %4
%18 = load i64, i64* %9
%19 = load i64, i64* %6
%20 = icmp ule i64 %18, %19
br i1 %20, label %21, label %40
21: ; preds = %17
%22 = load i8*, i8** %11
%23 = load i8*, i8** %10
%24 = load i64, i64* %6
%25 = load i64, i64* %7
%26 = load i64, i64* %9
%27 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%28 = call i32 @merge(i8* %22, i8* %23, i64 %24, i64 %25, i64 %26, i32 (i8*, i8*)* %27)
%29 = load i64, i64* %9
%30 = mul i64 %29, 2
store i64 %30, i64* %9
%31 = load i8*, i8** %10
%32 = load i8*, i8** %11
%33 = load i64, i64* %6
%34 = load i64, i64* %7
%35 = load i64, i64* %9
%36 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%37 = call i32 @merge(i8* %31, i8* %32, i64 %33, i64 %34, i64 %35, i32 (i8*, i8*)* %36)
%38 = load i64, i64* %9
%39 = mul i64 %38, 2
store i64 %39, i64* %9
br label %17
40: ; preds = %17
%41 = load i8*, i8** %10
%42 = call i32 @free(i8* %41)
ret void
}

LLVM IR (-O2):
define void @merge_sort(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = mul i64 %2, %1
%6 = tail call i8* @malloc(i64 %5)
%7 = icmp eq i64 %1, 0
br i1 %7, label %15, label %8
8: ; preds = %4, %8
%9 = phi i64 [ %13, %8 ], [ 1, %4 ]
%10 = tail call i32 @merge(i8* %0, i8* %6, i64 %1, i64 %2, i64 %9, i32 (i8*, i8*)* %3)
%11 = shl i64 %9, 1
%12 = tail call i32 @merge(i8* %6, i8* %0, i64 %1, i64 %2, i64 %11, i32 (i8*, i8*)* %3)
%13 = shl i64 %9, 2
%14 = icmp ugt i64 %13, %1
br i1 %14, label %15, label %8
15: ; preds = %8, %4
%16 = tail call i32 @free(i8* %6)
ret void
}