#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 mergesort(int arr[], int low, int high) {
  int invCount = 0;
  if (low < high) {
    int mid = low + (high - low) / 2;
    invCount = mergesort(arr, low, mid);
    invCount += mergesort(arr, mid + 1, high);
    invCount += merge(arr, low, mid + 1, high);
  }
  return invCount;
}


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

x86 assembly (clang, -O2):
<mergesort>:
xor    %eax,%eax
mov    %edx,%ecx
sub    %esi,%ecx
jle    118a <mergesort+0x6a>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r13d
mov    %esi,%ebx
mov    %rdi,%r14
mov    %ecx,%ebp
shr    $0x1f,%ebp
add    %ecx,%ebp
sar    %ebp
lea    (%rbx,%rbp,1),%edx
call   1040 <mergesort@plt>
mov    %eax,%r15d
lea    (%rbx,%rbp,1),%r12d
add    $0x1,%r12d
mov    %r14,%rdi
mov    %r12d,%esi
mov    %r13d,%edx
call   1040 <mergesort@plt>
mov    %eax,%ebp
add    %r15d,%ebp
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r12d,%edx
mov    %r13d,%ecx
call   1030 <merge@plt>
add    %ebp,%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) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
%9 = load i32, i32* %5
%10 = load i32, i32* %6
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %40
12: ; preds = %3
%13 = load i32, i32* %5
%14 = load i32, i32* %6
%15 = load i32, i32* %5
%16 = sub nsw i32 %14, %15
%17 = sdiv i32 %16, 2
%18 = add nsw i32 %13, %17
store i32 %18, i32* %8
%19 = load i32*, i32** %4
%20 = load i32, i32* %5
%21 = load i32, i32* %8
%22 = call i32 @mergesort(i32* %19, i32 %20, i32 %21)
store i32 %22, i32* %7
%23 = load i32*, i32** %4
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
%26 = load i32, i32* %6
%27 = call i32 @mergesort(i32* %23, i32 %25, i32 %26)
%28 = load i32, i32* %7
%29 = add nsw i32 %28, %27
store i32 %29, i32* %7
%30 = load i32*, i32** %4
%31 = load i32, i32* %5
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 1
%34 = load i32, i32* %6
%35 = call i64 @merge(i32* %30, i32 %31, i32 %33, i32 %34)
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = add nsw i64 %37, %35
%39 = trunc i64 %38 to i32
store i32 %39, i32* %7
br label %40
40: ; preds = %12, %3
%41 = load i32, i32* %7
ret i32 %41
}

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