#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*/ merge(void *, void *, void *, size_t,
                         int (*)(void *, void *));
void merge_sort(void *l, void *r, size_t size,
                int (*comp)(void *key1, void *key2)) {
  void *q = l;
  if (r - l > size) {
    q = l + ((r - l + size) / (2 * size)) * size;
    merge_sort(l, q, size, comp);
    merge_sort(q + size, r, size, comp);
  }
  merge(l, q, r, size, comp);
}


x86 assembly (clang, -O0):
<merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
cmp    -0x18(%rbp),%rax
jbe    11ba <merge_sort+0x9a>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    -0x18(%rbp),%rax
mov    -0x18(%rbp),%rcx
shl    $0x1,%rcx
xor    %edx,%edx
div    %rcx
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <merge_sort@plt>
mov    -0x28(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <merge_sort@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
call   1030 <merge@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsi,%rax
sub    %rdi,%rax
mov    %rdi,%rbx
cmp    %rdx,%rax
jbe    1192 <merge_sort+0x72>
add    %r13,%rax
mov    %r13,%rcx
add    %r13,%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     1162 <merge_sort+0x42>
xor    %edx,%edx
div    %rcx
mov    %rax,%rbx
jmp    1168 <merge_sort+0x48>
xor    %edx,%edx
div    %ecx
mov    %eax,%ebx
imul   %r13,%rbx
add    %r12,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
mov    %r14,%rcx
call   1040 <merge_sort@plt>
lea    (%rbx,%r13,1),%rdi
mov    %r15,%rsi
mov    %r13,%rdx
mov    %r14,%rcx
call   1040 <merge_sort@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r13,%rcx
mov    %r14,%r8
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <merge@plt>

LLVM IR (-O0):
define void @merge_sort(i8* %0, i8* %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32 (i8*, i8*)*
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 (i8*, i8*)* %3, i32 (i8*, i8*)** %8
%10 = load i8*, i8** %5
store i8* %10, i8** %9
%11 = load i8*, i8** %6
%12 = load i8*, i8** %5
%13 = ptrtoint i8* %11 to i64
%14 = ptrtoint i8* %12 to i64
%15 = sub i64 %13, %14
%16 = load i64, i64* %7
%17 = icmp ugt i64 %15, %16
br i1 %17, label %18, label %43
18: ; preds = %4
%19 = load i8*, i8** %5
%20 = load i8*, i8** %6
%21 = load i8*, i8** %5
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = load i64, i64* %7
%26 = add i64 %24, %25
%27 = load i64, i64* %7
%28 = mul i64 2, %27
%29 = udiv i64 %26, %28
%30 = load i64, i64* %7
%31 = mul i64 %29, %30
%32 = getelementptr i8, i8* %19, i64 %31
store i8* %32, i8** %9
%33 = load i8*, i8** %5
%34 = load i8*, i8** %9
%35 = load i64, i64* %7
%36 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%37 = load i8*, i8** %9
%38 = load i64, i64* %7
%39 = getelementptr i8, i8* %37, i64 %38
%40 = load i8*, i8** %6
%41 = load i64, i64* %7
%42 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
br label %43
43: ; preds = %18, %4
%44 = load i8*, i8** %5
%45 = load i8*, i8** %9
%46 = load i8*, i8** %6
%47 = load i64, i64* %7
%48 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%49 = call i32 @merge(i8* %44, i8* %45, i8* %46, i64 %47, i32 (i8*, i8*)* %48)
ret void
}

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