#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 */
unsigned int brute_inversion_cnt(int *, int /*<<< orphan*/, unsigned int,
                                 int (*)(int *, int *));
int /*<<< orphan*/ free(int *);
int *malloc(unsigned int);
int /*<<< orphan*/ memcpy(int *, int *, unsigned int);
unsigned int merge_sort_recurse_impl(int *, int *, int /*<<< orphan*/,
                                     unsigned int, int (*)(int *, int *));
int /*<<< orphan*/ printf(char *, unsigned int, ...);
void merge_sort_recurse_inversion_cnt(int objs[], unsigned int n,
                                      bool (*cmp)(int *x, int *y)) {
  unsigned int inv, inv_brute;
  int *tmp = malloc(n * sizeof(int));
  if (tmp == NULL) {
    printf("Error: merge_sort(%d) -- malloc() failed\n", n);
    return;
  }
  memcpy(tmp, objs, (n * sizeof(int)));
  inv_brute = brute_inversion_cnt(objs, 0, n - 1, cmp);
  inv = merge_sort_recurse_impl(objs, tmp, 0, n - 1, cmp);
  if (inv != inv_brute)
    printf("Error: Inversion count not correct merge=%u, brute=%u\n", inv,
           inv_brute);
  else
    printf("Inversion count = %u\n", inv);
  free(tmp);
}


x86 assembly (clang, -O0):
<merge_sort_recurse_inversion_cnt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a6 <merge_sort_recurse_inversion_cnt+0x46>
mov    -0xc(%rbp),%esi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122f <merge_sort_recurse_inversion_cnt+0xcf>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%eax
shl    $0x2,%rax
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
mov    -0x18(%rbp),%rcx
xor    %esi,%esi
call   1070 <brute_inversion_cnt@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
mov    -0x18(%rbp),%r8
xor    %edx,%edx
call   1080 <merge_sort_recurse_impl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
je     1215 <merge_sort_recurse_inversion_cnt+0xb5>
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1226 <merge_sort_recurse_inversion_cnt+0xc6>
mov    -0x1c(%rbp),%esi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort_recurse_inversion_cnt>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %esi,%ebx
mov    %rdi,%r12
lea    0x0(,%rbx,4),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11cf <merge_sort_recurse_inversion_cnt+0x6f>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
add    $0xffffffff,%ebx
mov    %r12,%rdi
xor    %esi,%esi
mov    %ebx,%edx
mov    %r15,%rcx
call   1070 <brute_inversion_cnt@plt>
mov    %eax,%ebp
mov    %r12,%rdi
mov    %r14,%rsi
xor    %edx,%edx
mov    %ebx,%ecx
mov    %r15,%r8
call   1080 <merge_sort_recurse_impl@plt>
cmp    %ebp,%eax
jne    11e7 <merge_sort_recurse_inversion_cnt+0x87>
lea    0xe9d(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11f9 <merge_sort_recurse_inversion_cnt+0x99>
lea    0xe2a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
lea    0xe3c(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [42 x i8] c"Error: merge_sort(%d) -- malloc() failed\0A\00"
@.str.1 = [55 x i8] c"Error: Inversion count not correct merge=%u, brute=%u\0A\00"
@.str.2 = [22 x i8] c"Inversion count = %u\0A\00"
define void @merge_sort_recurse_inversion_cnt(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*
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 (i32*, i32*)* %2, i32 (i32*, i32*)** %6
%10 = load i32, i32* %5
%11 = zext i32 %10 to i64
%12 = mul i64 %11, 4
%13 = trunc i64 %12 to i32
%14 = call i32* @malloc(i32 %13)
store i32* %14, i32** %9
%15 = load i32*, i32** %9
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* %5
%19 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %52
20: ; preds = %3
%21 = load i32*, i32** %9
%22 = load i32*, i32** %4
%23 = load i32, i32* %5
%24 = zext i32 %23 to i64
%25 = mul i64 %24, 4
%26 = trunc i64 %25 to i32
%27 = call i32 @memcpy(i32* %21, i32* %22, i32 %26)
%28 = load i32*, i32** %4
%29 = load i32, i32* %5
%30 = sub i32 %29, 1
%31 = load i32 (i32*, i32*)*, i32 (i32*, i32*)** %6
%32 = call i32 @brute_inversion_cnt(i32* %28, i32 0, i32 %30, i32 (i32*, i32*)* %31)
store i32 %32, i32* %8
%33 = load i32*, i32** %4
%34 = load i32*, i32** %9
%35 = load i32, i32* %5
%36 = sub i32 %35, 1
%37 = load i32 (i32*, i32*)*, i32 (i32*, i32*)** %6
%38 = call i32 @merge_sort_recurse_impl(i32* %33, i32* %34, i32 0, i32 %36, i32 (i32*, i32*)* %37)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = load i32, i32* %8
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %46
42: ; preds = %20
%43 = load i32, i32* %7
%44 = load i32, i32* %8
%45 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %43, i32 %44)
br label %49
46: ; preds = %20
%47 = load i32, i32* %7
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %46, %42
%50 = load i32*, i32** %9
%51 = call i32 @free(i32* %50)
br label %52
52: ; preds = %49, %17
ret void
}

LLVM IR (-O2):
@.str = [42 x i8] c"Error: merge_sort(%d) -- malloc() failed\0A\00"
@.str.1 = [55 x i8] c"Error: Inversion count not correct merge=%u, brute=%u\0A\00"
@.str.2 = [22 x i8] c"Inversion count = %u\0A\00"
define void @merge_sort_recurse_inversion_cnt(i32* %0, i32 %1, i32 (i32*, i32*)* %2) {
%4 = shl i32 %1, 2
%5 = tail call i32* @malloc(i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %21
9: ; preds = %3
%10 = tail call i32 @memcpy(i32* %5, i32* %0, i32 %4)
%11 = add i32 %1, -1
%12 = tail call i32 @brute_inversion_cnt(i32* %0, i32 0, i32 %11, i32 (i32*, i32*)* %2)
%13 = tail call i32 @merge_sort_recurse_impl(i32* %0, i32* %5, i32 0, i32 %11, i32 (i32*, i32*)* %2)
%14 = icmp eq i32 %13, %12
br i1 %14, label %17, label %15
15: ; preds = %9
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %12)
br label %19
17: ; preds = %9
%18 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %12)
br label %19
19: ; preds = %17, %15
%20 = tail call i32 @free(i32* %5)
br label %21
21: ; preds = %19, %7
ret void
}