#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);
int count_invs(int a[], int temp[], int start, int end) {
  int invs = 0;
  if (start < end) {
    int mid = start + (end - start) / 2;
    invs = count_invs(a, temp, start, mid);
    invs += count_invs(a, temp, mid + 1, end);
    invs += merge(a, temp, start, mid, mid + 1, end);
  }
  return invs;
}


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

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

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

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