#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 long merge(int *, int *, int, int, int);
unsigned long merge_sort(int values[], int scratch[], int first_ix,
                         int last_ix) {
  int diff = last_ix - first_ix;
  if (diff <= 0)
    return 0L;
  int mid_ix = first_ix + diff / 2;
  unsigned long first_half_inversions, second_half_inversions, merge_inversions;
  first_half_inversions = merge_sort(values, scratch, first_ix, mid_ix);
  second_half_inversions = merge_sort(values, scratch, mid_ix + 1, last_ix);
  merge_inversions = merge(values, scratch, first_ix, mid_ix, last_ix);
  return first_half_inversions + second_half_inversions + merge_inversions;
}


x86 assembly (clang, -O0):
<merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x20(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     1156 <merge_sort+0x36>
movq   $0x0,-0x8(%rbp)
jmp    11cd <merge_sort+0xad>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%ecx
call   1040 <merge_sort@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%edx
add    $0x1,%edx
mov    -0x20(%rbp),%ecx
call   1040 <merge_sort@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%r8d
call   1030 <merge@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%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    %ecx,%ebp
sub    %edx,%ebp
jle    1181 <merge_sort+0x61>
mov    %ecx,%r12d
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
shr    %ebp
lea    (%rbx,%rbp,1),%r13d
mov    %r13d,%ecx
call   1040 <merge_sort@plt>
mov    %rax,(%rsp)
lea    (%rbx,%rbp,1),%edx
add    $0x1,%edx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12d,%ecx
call   1040 <merge_sort@plt>
mov    %rax,%rbp
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %r13d,%ecx
mov    %r12d,%r8d
call   1030 <merge@plt>
add    (%rsp),%rbp
add    %rax,%rbp
jmp    1183 <merge_sort+0x63>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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