#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 */
int *malloc(int);
unsigned long long mergesort(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, unsigned long long);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int t;
  scanf("%d", &t);
  for (int a0 = 0; a0 < t; a0++) {
    int n;
    scanf("%d", &n);
    int *arr = malloc(sizeof(int) * n);
    for (int arr_i = 0; arr_i < n; arr_i++) {
      scanf("%d", &arr[arr_i]);
    }
    unsigned long long swaps = mergesort(arr, 0, n - 1);
    printf("%llu\n", swaps);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <scanf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1204 <main+0xc4>
lea    0xe87(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <scanf@plt>
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11d1 <main+0x91>
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe42(%rip),%rdi
call   1050 <scanf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119c <main+0x5c>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1060 <mergesort@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe12(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1166 <main+0x26>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xeae(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1050 <scanf@plt>
cmpl   $0x0,0x4(%rsp)
jle    11de <main+0x9e>
xor    %r14d,%r14d
lea    0xe93(%rip),%r13
jmp    1199 <main+0x59>
nop
add    $0xffffffff,%ebx
mov    %r12,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1060 <mergesort@plt>
lea    0xe7d(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
add    $0x1,%r14d
cmp    0x4(%rsp),%r14d
jge    11de <main+0x9e>
mov    %r13,%rdi
mov    %rsp,%rsi
call   1050 <scanf@plt>
mov    (%rsp),%ebx
lea    0x0(,%rbx,4),%edi
call   1040 <malloc@plt>
mov    %rax,%r12
test   %ebx,%ebx
jle    1170 <main+0x30>
mov    %r12,%rbp
xor    %r15d,%r15d
mov    %r13,%rdi
mov    %rbp,%rsi
call   1050 <scanf@plt>
add    $0x1,%r15
movslq (%rsp),%rbx
add    $0x4,%rbp
cmp    %rbx,%r15
jl     11c0 <main+0x80>
jmp    1170 <main+0x30>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"%llu\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i64
store i32 0, i32* %1
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
store i32 0, i32* %3
br label %9
9: ; preds = %40, %0
%10 = load i32, i32* %3
%11 = load i32, i32* %2
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %43
13: ; preds = %9
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%15 = load i32, i32* %4
%16 = sext i32 %15 to i64
%17 = mul i64 4, %16
%18 = trunc i64 %17 to i32
%19 = call i32* @malloc(i32 %18)
store i32* %19, i32** %5
store i32 0, i32* %6
br label %20
20: ; preds = %30, %13
%21 = load i32, i32* %6
%22 = load i32, i32* %4
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = load i32*, i32** %5
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
%29 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %28)
br label %30
30: ; preds = %24
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
store i32 %32, i32* %6
br label %20
33: ; preds = %20
%34 = load i32*, i32** %5
%35 = load i32, i32* %4
%36 = sub nsw i32 %35, 1
%37 = call i64 @mergesort(i32* %34, i32 0, i32 %36)
store i64 %37, i64* %7
%38 = load i64, i64* %7
%39 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %38)
br label %40
40: ; preds = %33
%41 = load i32, i32* %3
%42 = add nsw i32 %41, 1
store i32 %42, i32* %3
br label %9
43: ; preds = %9
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"%llu\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%5 = load i32, i32* %1
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = bitcast i32* %2 to i8*
br label %10
9: ; preds = %17, %0
ret i32 0
10: ; preds = %7, %17
%11 = phi i32 [ 0, %7 ], [ %22, %17 ]
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%13 = load i32, i32* %2
%14 = shl i32 %13, 2
%15 = call i32* @malloc(i32 %14)
%16 = icmp sgt i32 %13, 0
br i1 %16, label %25, label %17
17: ; preds = %25, %10
%18 = phi i32 [ %13, %10 ], [ %30, %25 ]
%19 = add nsw i32 %18, -1
%20 = call i64 @mergesort(i32* %15, i32 0, i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %20)
%22 = add nuw nsw i32 %11, 1
%23 = load i32, i32* %1
%24 = icmp slt i32 %22, %23
br i1 %24, label %10, label %9
25: ; preds = %10, %25
%26 = phi i64 [ %29, %25 ], [ 0, %10 ]
%27 = getelementptr inbounds i32, i32* %15, i64 %26
%28 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %27)
%29 = add nuw nsw i64 %26, 1
%30 = load i32, i32* %2
%31 = sext i32 %30 to i64
%32 = icmp slt i64 %29, %31
br i1 %32, label %25, label %17
}