#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 */
scalar_t__ merge(int *, int *, unsigned int, unsigned int, unsigned int, int,
                 int (*)(int *, int *));
unsigned int merge_sort_recurse_impl(int src[], int dst[], unsigned int lo,
                                     unsigned int hi,
                                     bool (*cmp)(int *x, int *y)) {
  unsigned int mid = lo + (hi - lo) / 2;
  unsigned int inv = 0;
  if (hi <= lo)
    return 0;
  inv += merge_sort_recurse_impl(dst, src, lo, mid, cmp);
  inv += merge_sort_recurse_impl(dst, src, mid + 1, hi, cmp);
  inv += merge(src, dst, lo, mid, hi, false, cmp);
  return inv;
}


x86 assembly (clang, -O0):
<merge_sort_recurse_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
shr    $0x1,%ecx
add    %ecx,%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
ja     116a <merge_sort_recurse_impl+0x4a>
movl   $0x0,-0x4(%rbp)
jmp    11db <merge_sort_recurse_impl+0xbb>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%r8
call   1040 <merge_sort_recurse_impl@plt>
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x2c(%rbp),%edx
add    $0x1,%edx
mov    -0x20(%rbp),%ecx
mov    -0x28(%rbp),%r8
call   1040 <merge_sort_recurse_impl@plt>
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x28(%rbp),%rax
xor    %r9d,%r9d
mov    %rax,(%rsp)
call   1030 <merge@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort_recurse_impl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
mov    %ecx,%ebx
sub    %edx,%ebx
jbe    1198 <merge_sort_recurse_impl+0x78>
mov    %r8,%r15
mov    %ecx,%r12d
mov    %edx,%ebp
mov    %rdi,%r13
shr    %ebx
add    %edx,%ebx
mov    %rsi,%rdi
mov    %rsi,%r14
mov    %rsi,0x10(%rsp)
mov    %r13,%rsi
mov    %ebx,%ecx
call   1040 <merge_sort_recurse_impl@plt>
mov    %eax,0xc(%rsp)
lea    0x1(%rbx),%edx
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r12d,%ecx
mov    %r15,%r8
call   1040 <merge_sort_recurse_impl@plt>
mov    %eax,%r14d
add    0xc(%rsp),%r14d
mov    %r15,(%rsp)
mov    %r13,%rdi
mov    0x10(%rsp),%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %r12d,%r8d
xor    %r9d,%r9d
call   1030 <merge@plt>
add    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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