#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 /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, long long *, ...);
int /*<<< orphan*/ spiral(long long **, long long, long long);
int main() {
  long long int a[100][100];
  long long int i, j, k;
  long long int p, q, r;
  long long int t;
  long long int m, n;
  scanf("%lld", &t);
  for (p = 1; p <= t; p++) {
    scanf("%lld %lld", &m, &n);
    for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++) {
        scanf("%lld", &a[i][j]);
      }
    }
    printf("\n");
    spiral(a, m, n);
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x138e0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xebc(%rip),%rdi
lea    -0x138c8(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
movq   $0x1,-0x138b0(%rbp)
mov    -0x138b0(%rbp),%rax
cmp    -0x138c8(%rbp),%rax
jg     127d <main+0x14d>
lea    0xe83(%rip),%rdi
lea    -0x138d0(%rbp),%rsi
lea    -0x138d8(%rbp),%rdx
mov    $0x0,%al
call   1040 <scanf@plt>
movq   $0x0,-0x13898(%rbp)
mov    -0x13898(%rbp),%rax
cmp    -0x138d0(%rbp),%rax
jge    1234 <main+0x104>
movq   $0x0,-0x138a0(%rbp)
mov    -0x138a0(%rbp),%rax
cmp    -0x138d8(%rbp),%rax
jge    1218 <main+0xe8>
lea    -0x13890(%rbp),%rsi
imul   $0x320,-0x13898(%rbp),%rax
add    %rax,%rsi
mov    -0x138a0(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x138a0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x138a0(%rbp)
jmp    11bc <main+0x8c>
jmp    121d <main+0xed>
mov    -0x13898(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x13898(%rbp)
jmp    119d <main+0x6d>
lea    0xdcf(%rip),%rdi
call   1030 <printf@plt>
lea    -0x13890(%rbp),%rdi
mov    -0x138d0(%rbp),%rsi
mov    -0x138d8(%rbp),%rdx
call   1050 <spiral@plt>
lea    0xda9(%rip),%rdi
call   1030 <printf@plt>
mov    -0x138b0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x138b0(%rbp)
jmp    1162 <main+0x32>
xor    %eax,%eax
add    $0x138e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138a8,%rsp
lea    0xebd(%rip),%rdi
lea    0x18(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpq   $0x0,0x18(%rsp)
jle    1226 <main+0xf6>
mov    $0x1,%r14d
lea    0xe98(%rip),%rbx
jmp    11aa <main+0x7a>
nop
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%rdx
lea    0x20(%rsp),%rdi
call   1050 <spiral@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0x1(%r14),%rax
cmp    0x18(%rsp),%r14
mov    %rax,%r14
jge    1226 <main+0xf6>
lea    0xe4f(%rip),%rdi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x10(%rsp),%rcx
test   %rcx,%rcx
jle    1170 <main+0x40>
mov    0x8(%rsp),%rax
lea    0x20(%rsp),%r13
xor    %r12d,%r12d
jmp    11f0 <main+0xc0>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12
add    $0x320,%r13
cmp    %rcx,%r12
jge    1170 <main+0x40>
test   %rax,%rax
jle    11e0 <main+0xb0>
mov    %r13,%rbp
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
add    $0x1,%r15
mov    0x8(%rsp),%rax
add    $0x8,%rbp
cmp    %rax,%r15
jl     1200 <main+0xd0>
mov    0x10(%rsp),%rcx
jmp    11e0 <main+0xb0>
xor    %eax,%eax
add    $0x138a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [10 x i8] c"%lld %lld\00"
define i32 @main() {
%1 = alloca [100 x [100 x i64]]
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = bitcast [100 x [100 x i64]]* %1 to i8*
%6 = bitcast i64* %2 to i8*
%7 = bitcast i64* %3 to i8*
%8 = bitcast i64* %4 to i8*
%9 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %2)
%10 = load i64, i64* %2
%11 = icmp slt i64 %10, 1
br i1 %11, label %49, label %12
12: ; preds = %0
%13 = bitcast [100 x [100 x i64]]* %1 to i64**
br label %14
14: ; preds = %12, %40
%15 = phi i64 [ 1, %12 ], [ %46, %40 ]
%16 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64* %3, i64* %4)
%17 = load i64, i64* %3
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %40
19: ; preds = %14
%20 = load i64, i64* %4
br label %21
21: ; preds = %19, %35
%22 = phi i64 [ %36, %35 ], [ %17, %19 ]
%23 = phi i64 [ %37, %35 ], [ %20, %19 ]
%24 = phi i64 [ %38, %35 ], [ 0, %19 ]
%25 = icmp sgt i64 %23, 0
br i1 %25, label %26, label %35
26: ; preds = %21, %26
%27 = phi i64 [ %30, %26 ], [ 0, %21 ]
%28 = getelementptr inbounds [100 x [100 x i64]], [100 x [100 x i64]]* %1, i64 0, i64 %24, i64 %27
%29 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %28)
%30 = add nuw nsw i64 %27, 1
%31 = load i64, i64* %4
%32 = icmp slt i64 %30, %31
br i1 %32, label %26, label %33
33: ; preds = %26
%34 = load i64, i64* %3
br label %35
35: ; preds = %33, %21
%36 = phi i64 [ %34, %33 ], [ %22, %21 ]
%37 = phi i64 [ %31, %33 ], [ %23, %21 ]
%38 = add nuw nsw i64 %24, 1
%39 = icmp slt i64 %38, %36
br i1 %39, label %21, label %40
40: ; preds = %35, %14
%41 = call i32 @putchar(i32 10)
%42 = load i64, i64* %3
%43 = load i64, i64* %4
%44 = call i32 @spiral(i64** %13, i64 %42, i64 %43)
%45 = call i32 @putchar(i32 10)
%46 = add nuw nsw i64 %15, 1
%47 = load i64, i64* %2
%48 = icmp slt i64 %15, %47
br i1 %48, label %14, label %49
49: ; preds = %40, %0
ret i32 0
}