#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 MERGE_SORT(int A[], int p, int r) {
  if (p < r) {
    int inversions = 0;
    int q = (p + r) / 2;
    inversions += MERGE_SORT(A, p, q);
    inversions += MERGE_SORT(A, q + 1, r);
    inversions += MERGE(A, p, q, r);
    return inversions;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<MERGE_SORT>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11ad <MERGE_SORT+0x8d>
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1030 <MERGE_SORT@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
mov    -0x18(%rbp),%edx
call   1030 <MERGE_SORT@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1040 <MERGE@plt>
mov    %rax,%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b4 <MERGE_SORT+0x94>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

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

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