#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);
long long int _mergeSort(int arr[], int temp[], int left, int right) {
  int mid;
  long long 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    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11b2 <_mergeSort+0x92>
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    %rax,-0x28(%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    -0x28(%rbp),%rax
mov    %rax,-0x28(%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>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_mergeSort>:
cmp    %edx,%ecx
jle    1190 <_mergeSort+0x70>
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    %rax,(%rsp)
add    $0x1,%ebp
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
call   1030 <_mergeSort@plt>
mov    %rax,%r13
add    (%rsp),%r13
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %r14d,%r8d
call   1040 <merge@plt>
add    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i64 @_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 i64
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i64 0, i64* %10
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp sgt i32 %11, %12
br i1 %13, label %14, label %41
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 i64 @_mergeSort(i32* %19, i32* %20, i32 %21, i32 %22)
store i64 %23, i64* %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 i64 @_mergeSort(i32* %24, i32* %25, i32 %27, i32 %28)
%30 = load i64, i64* %10
%31 = add nsw i64 %30, %29
store i64 %31, i64* %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 i64, i64* %10
%40 = add nsw i64 %39, %38
store i64 %40, i64* %10
br label %41
41: ; preds = %14, %4
%42 = load i64, i64* %10
ret i64 %42
}

LLVM IR (-O2):
define i64 @_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 i64 [ %16, %8 ], [ 0, %4 ]
ret i64 %7
8: ; preds = %4
%9 = add nsw i32 %3, %2
%10 = sdiv i32 %9, 2
%11 = tail call i64 @_mergeSort(i32* %0, i32* %1, i32 %2, i32 %10)
%12 = add nsw i32 %10, 1
%13 = tail call i64 @_mergeSort(i32* %0, i32* %1, i32 %12, i32 %3)
%14 = add nsw i64 %13, %11
%15 = tail call i64 @merge(i32* %0, i32* %1, i32 %2, i32 %12, i32 %3)
%16 = add nsw i64 %14, %15
br label %6
}