#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 *, int, int, int);
int _mergeSort(int arr[], int temp[], int left, int right) {
  int mid, inv_count = 0;
  if (right > left) {
    mid = (right + left) / 2;
    inv_count = _mergeSort(arr, temp, left, mid);
    inv_count += _mergeSort(arr, temp, mid + 1, right);
    inv_count += merge(arr, temp, left, mid + 1, right);
  }
  return inv_count;
}


x86 assembly (clang, -O0):
<_mergeSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11b3 <_mergeSort+0x93>
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x1c(%rbp),%ecx
call   1030 <_mergeSort@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
mov    -0x18(%rbp),%ecx
call   1030 <_mergeSort@plt>
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
mov    -0x18(%rbp),%r8d
call   1040 <merge@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_mergeSort>:
xor    %eax,%eax
cmp    %edx,%ecx
jle    1192 <_mergeSort+0x72>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r12
lea    (%r14,%rbx,1),%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
mov    %ebp,%ecx
call   1030 <_mergeSort@plt>
mov    %eax,0x4(%rsp)
add    $0x1,%ebp
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
call   1030 <_mergeSort@plt>
mov    %eax,%r13d
add    0x4(%rsp),%r13d
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %r14d,%r8d
call   1040 <merge@plt>
add    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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