#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*/ MIN(unsigned int, unsigned int);
int /*<<< orphan*/ free(int *);
int *malloc(unsigned int);
int /*<<< orphan*/ merge(int *, int *, unsigned int, unsigned int,
                         int /*<<< orphan*/, int, int (*)(int *, int *));
int /*<<< orphan*/ printf(char *, unsigned int);
void merge_sort(int objs[], unsigned int n, bool (*cmp)(int *x, int *y)) {
  unsigned int lo, m, hi = n - 1;
  int *tmp = malloc(n * sizeof(int));
  if (tmp == NULL) {
    printf("Error: merge_sort(%d) -- malloc() failed\n", n);
    return;
  }
  for (m = 1; m < n; m = m + m) {
    for (lo = 0; lo < (n - m); lo += (m + m))
      merge(objs, tmp, lo, lo + m - 1, MIN((lo + m + m - 1), hi), true, cmp);
  }
  free(tmp);
}


x86 assembly (clang, -O0):
<merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    119d <merge_sort+0x4d>
mov    -0xc(%rbp),%esi
lea    0xe6d(%rip),%rdi
call   1050 <printf@plt>
jmp    124f <merge_sort+0xff>
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    1246 <merge_sort+0xf6>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    -0x20(%rbp),%ecx
cmp    %ecx,%eax
jae    1233 <merge_sort+0xe3>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
add    -0x20(%rbp),%edi
add    -0x20(%rbp),%edi
sub    $0x1,%edi
mov    -0x24(%rbp),%esi
call   1040 <MIN@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%edx
mov    -0x34(%rbp),%ecx
mov    %eax,%r8d
mov    -0x18(%rbp),%rax
mov    $0x1,%r9d
mov    %rax,(%rsp)
call   1060 <merge@plt>
mov    -0x20(%rbp),%eax
add    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11b7 <merge_sort+0x67>
jmp    1238 <merge_sort+0xe8>
mov    -0x20(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11a4 <merge_sort+0x54>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%rbp
lea    0x0(,%rbx,4),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1238 <merge_sort+0xe8>
mov    %rax,0x18(%rsp)
mov    %rbp,0x28(%rsp)
mov    %r14,0x30(%rsp)
cmp    $0x2,%ebx
jb     1220 <merge_sort+0xd0>
lea    -0x1(%rbx),%r12d
mov    $0x1,%r15d
mov    %rbx,0x20(%rsp)
jmp    11c1 <merge_sort+0x71>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x10(%rsp),%rax
mov    %eax,%r15d
mov    0x20(%rsp),%rbx
cmp    %ebx,%eax
jae    1220 <merge_sort+0xd0>
lea    (%r15,%r15,1),%eax
mov    %rax,0x10(%rsp)
mov    %ebx,%r13d
sub    %r15d,%r13d
je     11b0 <merge_sort+0x60>
mov    0x10(%rsp),%rax
lea    -0x1(%rax),%ebp
add    $0xffffffff,%r15d
xor    %ebx,%ebx
lea    (%r15,%rbx,1),%r14d
lea    (%rbx,%rbp,1),%edi
mov    %r12d,%esi
call   1040 <MIN@plt>
mov    0x30(%rsp),%rcx
mov    %rcx,(%rsp)
mov    0x28(%rsp),%rdi
mov    0x18(%rsp),%rsi
mov    %ebx,%edx
mov    %r14d,%ecx
mov    %eax,%r8d
mov    $0x1,%r9d
call   1060 <merge@plt>
add    0x10(%rsp),%ebx
cmp    %r13d,%ebx
jb     11e0 <merge_sort+0x90>
jmp    11b0 <merge_sort+0x60>
mov    0x18(%rsp),%rdi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xdc1(%rip),%rdi
mov    %ebx,%esi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [42 x i8] c"Error: merge_sort(%d) -- malloc() failed\0A\00"
define void @merge_sort(i32* %0, i32 %1, i32 (i32*, i32*)* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32 (i32*, i32*)*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 (i32*, i32*)* %2, i32 (i32*, i32*)** %6
%11 = load i32, i32* %5
%12 = sub i32 %11, 1
store i32 %12, i32* %9
%13 = load i32, i32* %5
%14 = zext i32 %13 to i64
%15 = mul i64 %14, 4
%16 = trunc i64 %15 to i32
%17 = call i32* @malloc(i32 %16)
store i32* %17, i32** %10
%18 = load i32*, i32** %10
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %3
%21 = load i32, i32* %5
%22 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %67
23: ; preds = %3
store i32 1, i32* %8
br label %24
24: ; preds = %60, %23
%25 = load i32, i32* %8
%26 = load i32, i32* %5
%27 = icmp ult i32 %25, %26
br i1 %27, label %28, label %64
28: ; preds = %24
store i32 0, i32* %7
br label %29
29: ; preds = %53, %28
%30 = load i32, i32* %7
%31 = load i32, i32* %5
%32 = load i32, i32* %8
%33 = sub i32 %31, %32
%34 = icmp ult i32 %30, %33
br i1 %34, label %35, label %59
35: ; preds = %29
%36 = load i32*, i32** %4
%37 = load i32*, i32** %10
%38 = load i32, i32* %7
%39 = load i32, i32* %7
%40 = load i32, i32* %8
%41 = add i32 %39, %40
%42 = sub i32 %41, 1
%43 = load i32, i32* %7
%44 = load i32, i32* %8
%45 = add i32 %43, %44
%46 = load i32, i32* %8
%47 = add i32 %45, %46
%48 = sub i32 %47, 1
%49 = load i32, i32* %9
%50 = call i32 @MIN(i32 %48, i32 %49)
%51 = load i32 (i32*, i32*)*, i32 (i32*, i32*)** %6
%52 = call i32 @merge(i32* %36, i32* %37, i32 %38, i32 %42, i32 %50, i32 1, i32 (i32*, i32*)* %51)
br label %53
53: ; preds = %35
%54 = load i32, i32* %8
%55 = load i32, i32* %8
%56 = add i32 %54, %55
%57 = load i32, i32* %7
%58 = add i32 %57, %56
store i32 %58, i32* %7
br label %29
59: ; preds = %29
br label %60
60: ; preds = %59
%61 = load i32, i32* %8
%62 = load i32, i32* %8
%63 = add i32 %61, %62
store i32 %63, i32* %8
br label %24
64: ; preds = %24
%65 = load i32*, i32** %10
%66 = call i32 @free(i32* %65)
br label %67
67: ; preds = %64, %20
ret void
}

LLVM IR (-O2):
@.str = [42 x i8] c"Error: merge_sort(%d) -- malloc() failed\0A\00"
define void @merge_sort(i32* %0, i32 %1, i32 (i32*, i32*)* %2) {
%4 = add i32 %1, -1
%5 = shl i32 %1, 2
%6 = tail call i32* @malloc(i32 %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = icmp ugt i32 %1, 1
br i1 %9, label %12, label %28
10: ; preds = %3
%11 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %30
12: ; preds = %8, %26
%13 = phi i32 [ %16, %26 ], [ 1, %8 ]
%14 = sub i32 %1, %13
%15 = icmp eq i32 %14, 0
%16 = shl i32 %13, 1
br i1 %15, label %26, label %17
17: ; preds = %12, %17
%18 = phi i32 [ %24, %17 ], [ 0, %12 ]
%19 = add i32 %18, %13
%20 = add i32 %19, -1
%21 = add i32 %20, %13
%22 = tail call i32 @MIN(i32 %21, i32 %4)
%23 = tail call i32 @merge(i32* %0, i32* %6, i32 %18, i32 %20, i32 %22, i32 1, i32 (i32*, i32*)* %2)
%24 = add i32 %18, %16
%25 = icmp ult i32 %24, %14
br i1 %25, label %17, label %26
26: ; preds = %17, %12
%27 = icmp ult i32 %16, %1
br i1 %27, label %12, label %28
28: ; preds = %26, %8
%29 = tail call i32 @free(i32* %6)
br label %30
30: ; preds = %28, %10
ret void
}