#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__ mergeAndCountInversion(int *, int, int, int);
long long int mergeSortAndCountInversion(int arr[], int left, int right) {
  if (left >= right)
    return 0;
  int mid = left + ((right - left) >> 1);
  long long int numInversion;
  numInversion = mergeSortAndCountInversion(arr, left, mid);
  numInversion += mergeSortAndCountInversion(arr, mid + 1, right);
  numInversion += mergeAndCountInversion(arr, left, mid, right);
  return numInversion;
}


x86 assembly (clang, -O0):
<mergeSortAndCountInversion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jl     114b <mergeSortAndCountInversion+0x2b>
movq   $0x0,-0x8(%rbp)
jmp    11ab <mergeSortAndCountInversion+0x8b>
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    -0x14(%rbp),%ecx
sar    $0x1,%ecx
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1040 <mergeSortAndCountInversion@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
mov    -0x18(%rbp),%edx
call   1040 <mergeSortAndCountInversion@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1030 <mergeAndCountInversion@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergeSortAndCountInversion>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
sub    %esi,%ebp
jle    1176 <mergeSortAndCountInversion+0x56>
mov    %edx,%r13d
mov    %esi,%ebx
mov    %rdi,%r14
sar    %ebp
lea    (%rbx,%rbp,1),%r15d
mov    %r15d,%edx
call   1040 <mergeSortAndCountInversion@plt>
mov    %rax,%r12
lea    (%rbx,%rbp,1),%esi
add    $0x1,%esi
mov    %r14,%rdi
mov    %r13d,%edx
call   1040 <mergeSortAndCountInversion@plt>
mov    %rax,%rbp
add    %r12,%rbp
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r15d,%edx
mov    %r13d,%ecx
call   1030 <mergeAndCountInversion@plt>
add    %rbp,%rax
jmp    1178 <mergeSortAndCountInversion+0x58>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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