#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 opt(int *, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int n, l;
  int i, j;
  int train[50 + 10];
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &l);
    for (j = 0; j < l; j++)
      scanf("%d", &train[j]);
    printf("Optimal train swapping takes %d swaps.\n", opt(train, l));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeb7(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11df <main+0xaf>
lea    0xe94(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b4 <main+0x84>
movslq -0x14(%rbp),%rax
lea    -0x110(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe5f(%rip),%rdi
call   1040 <scanf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117c <main+0x4c>
lea    -0x110(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <opt@plt>
mov    %eax,%esi
lea    0xe37(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1159 <main+0x29>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
lea    0xeb8(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1040 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
jle    11cf <main+0x9f>
xor    %ebp,%ebp
lea    0xe9e(%rip),%r13
lea    0x10(%rsp),%r15
lea    0xe95(%rip),%r12
jmp    118b <main+0x5b>
mov    %r15,%rdi
call   1050 <opt@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
add    $0x1,%ebp
cmp    0xc(%rsp),%ebp
jge    11cf <main+0x9f>
mov    %r13,%rdi
lea    0x8(%rsp),%rsi
call   1040 <scanf@plt>
mov    0x8(%rsp),%esi
test   %esi,%esi
jle    1170 <main+0x40>
mov    %r15,%rbx
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
call   1040 <scanf@plt>
add    $0x1,%r14
movslq 0x8(%rsp),%rsi
add    $0x4,%rbx
cmp    %rsi,%r14
jl     11b0 <main+0x80>
jmp    1170 <main+0x40>
xor    %eax,%eax
add    $0x108,%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 = [40 x i8] c"Optimal train swapping takes %d swaps.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [60 x i32]
store i32 0, i32* %1
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
store i32 0, i32* %4
br label %8
8: ; preds = %31, %0
%9 = load i32, i32* %4
%10 = load i32, i32* %2
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %34
12: ; preds = %8
%13 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
store i32 0, i32* %5
br label %14
14: ; preds = %23, %12
%15 = load i32, i32* %5
%16 = load i32, i32* %3
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %26
18: ; preds = %14
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [60 x i32], [60 x i32]* %6, i64 0, i64 %20
%22 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %21)
br label %23
23: ; preds = %18
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %14
26: ; preds = %14
%27 = getelementptr inbounds [60 x i32], [60 x i32]* %6, i64 0, i64 0
%28 = load i32, i32* %3
%29 = call i32 @opt(i32* %27, i32 %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %26
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
br label %8
34: ; preds = %8
ret i32 0
}

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