#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__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int *spiralOrder(int **, int, int);
int main(void) {
  int **arr = NULL;
  int i, j;
  int *res = NULL;
  arr = (int **)malloc(sizeof(int *) * 3);
  for (i = 0; i < 3; i++) {
    arr[i] = (int *)malloc(sizeof(int) * 3);
  }
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      arr[i][j] = i * 3 + j;
    }
  }
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      printf("%d ", arr[i][j]);
    }
    printf("\n");
  }
  res = spiralOrder(arr, 3, 3);
  for (i = 0; i < 9; i++) {
    printf("%d ", res[i]);
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x18,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    1195 <main+0x65>
mov    $0xc,%edi
call   1050 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1164 <main+0x34>
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    11f2 <main+0xc2>
movl   $0x0,-0x18(%rbp)
cmpl   $0x3,-0x18(%rbp)
jge    11df <main+0xaf>
imul   $0x3,-0x14(%rbp),%edx
add    -0x18(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ad <main+0x7d>
jmp    11e4 <main+0xb4>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    119c <main+0x6c>
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    125f <main+0x12f>
movl   $0x0,-0x18(%rbp)
cmpl   $0x3,-0x18(%rbp)
jge    1243 <main+0x113>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    120a <main+0xda>
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11f9 <main+0xc9>
mov    -0x10(%rbp),%rdi
mov    $0x3,%edx
mov    %edx,%esi
call   1040 <spiralOrder@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x9,-0x14(%rbp)
jge    12ab <main+0x17b>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    127a <main+0x14a>
lea    0xd52(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x18,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,(%rbx)
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,0x8(%rbx)
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,0x10(%rbx)
mov    (%rbx),%rcx
movabs $0x100000000,%rdx
mov    %rdx,(%rcx)
movl   $0x2,0x8(%rcx)
mov    0x8(%rbx),%rdx
movabs $0x400000003,%rsi
mov    %rsi,(%rdx)
movl   $0x5,0x8(%rdx)
movabs $0x700000006,%rdx
mov    %rdx,(%rax)
movl   $0x8,0x8(%rax)
mov    (%rcx),%esi
lea    0xe3a(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx),%rax
mov    0x4(%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx),%rax
mov    0x8(%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x8(%rbx),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rbx),%rax
mov    0x4(%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rbx),%rax
mov    0x8(%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x10(%rbx),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%rbx),%rax
mov    0x4(%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%rbx),%rax
mov    0x8(%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
mov    $0x3,%edx
call   1050 <spiralOrder@plt>
mov    %rax,%rbx
mov    (%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x14(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x18(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x1c(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x20(%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d \00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32**
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i32 0, i32* %1
store i32** null, i32*** %2
store i32* null, i32** %5
%6 = call i64 @malloc(i32 24)
%7 = inttoptr i64 %6 to i32**
store i32** %7, i32*** %2
store i32 0, i32* %3
br label %8
8: ; preds = %18, %0
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %21
11: ; preds = %8
%12 = call i64 @malloc(i32 12)
%13 = inttoptr i64 %12 to i32*
%14 = load i32**, i32*** %2
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i32*, i32** %14, i64 %16
store i32* %13, i32** %17
br label %18
18: ; preds = %11
%19 = load i32, i32* %3
%20 = add nsw i32 %19, 1
store i32 %20, i32* %3
br label %8
21: ; preds = %8
store i32 0, i32* %3
br label %22
22: ; preds = %46, %21
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 3
br i1 %24, label %25, label %49
25: ; preds = %22
store i32 0, i32* %4
br label %26
26: ; preds = %42, %25
%27 = load i32, i32* %4
%28 = icmp slt i32 %27, 3
br i1 %28, label %29, label %45
29: ; preds = %26
%30 = load i32, i32* %3
%31 = mul nsw i32 %30, 3
%32 = load i32, i32* %4
%33 = add nsw i32 %31, %32
%34 = load i32**, i32*** %2
%35 = load i32, i32* %3
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32*, i32** %34, i64 %36
%38 = load i32*, i32** %37
%39 = load i32, i32* %4
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
store i32 %33, i32* %41
br label %42
42: ; preds = %29
%43 = load i32, i32* %4
%44 = add nsw i32 %43, 1
store i32 %44, i32* %4
br label %26
45: ; preds = %26
br label %46
46: ; preds = %45
%47 = load i32, i32* %3
%48 = add nsw i32 %47, 1
store i32 %48, i32* %3
br label %22
49: ; preds = %22
store i32 0, i32* %3
br label %50
50: ; preds = %73, %49
%51 = load i32, i32* %3
%52 = icmp slt i32 %51, 3
br i1 %52, label %53, label %76
53: ; preds = %50
store i32 0, i32* %4
br label %54
54: ; preds = %68, %53
%55 = load i32, i32* %4
%56 = icmp slt i32 %55, 3
br i1 %56, label %57, label %71
57: ; preds = %54
%58 = load i32**, i32*** %2
%59 = load i32, i32* %3
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32*, i32** %58, i64 %60
%62 = load i32*, i32** %61
%63 = load i32, i32* %4
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i32, i32* %62, i64 %64
%66 = load i32, i32* %65
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %66)
br label %68
68: ; preds = %57
%69 = load i32, i32* %4
%70 = add nsw i32 %69, 1
store i32 %70, i32* %4
br label %54
71: ; preds = %54
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %73
73: ; preds = %71
%74 = load i32, i32* %3
%75 = add nsw i32 %74, 1
store i32 %75, i32* %3
br label %50
76: ; preds = %50
%77 = load i32**, i32*** %2
%78 = call i32* @spiralOrder(i32** %77, i32 3, i32 3)
store i32* %78, i32** %5
store i32 0, i32* %3
br label %79
79: ; preds = %89, %76
%80 = load i32, i32* %3
%81 = icmp slt i32 %80, 9
br i1 %81, label %82, label %92
82: ; preds = %79
%83 = load i32*, i32** %5
%84 = load i32, i32* %3
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i32, i32* %83, i64 %85
%87 = load i32, i32* %86
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %87)
br label %89
89: ; preds = %82
%90 = load i32, i32* %3
%91 = add nsw i32 %90, 1
store i32 %91, i32* %3
br label %79
92: ; preds = %79
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d \00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 24)
%2 = inttoptr i64 %1 to i32**
%3 = tail call i64 @malloc(i32 12)
%4 = inttoptr i64 %3 to i32*
store i32* %4, i32** %2
%5 = tail call i64 @malloc(i32 12)
%6 = inttoptr i64 %5 to i32*
%7 = getelementptr inbounds i32*, i32** %2, i64 1
store i32* %6, i32** %7
%8 = tail call i64 @malloc(i32 12)
%9 = inttoptr i64 %8 to i32*
%10 = getelementptr inbounds i32*, i32** %2, i64 2
store i32* %9, i32** %10
%11 = load i32*, i32** %2
store i32 0, i32* %11
%12 = getelementptr inbounds i32, i32* %11, i64 1
store i32 1, i32* %12
%13 = getelementptr inbounds i32, i32* %11, i64 2
store i32 2, i32* %13
%14 = load i32*, i32** %7
store i32 3, i32* %14
%15 = getelementptr inbounds i32, i32* %14, i64 1
store i32 4, i32* %15
%16 = getelementptr inbounds i32, i32* %14, i64 2
store i32 5, i32* %16
store i32 6, i32* %9
%17 = getelementptr inbounds i32, i32* %9, i64 1
store i32 7, i32* %17
%18 = getelementptr inbounds i32, i32* %9, i64 2
store i32 8, i32* %18
%19 = load i32, i32* %11
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i32*, i32** %2
%22 = getelementptr inbounds i32, i32* %21, i64 1
%23 = load i32, i32* %22
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i32*, i32** %2
%26 = getelementptr inbounds i32, i32* %25, i64 2
%27 = load i32, i32* %26
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = tail call i32 @putchar(i32 10)
%30 = load i32*, i32** %7
%31 = load i32, i32* %30
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = load i32*, i32** %7
%34 = getelementptr inbounds i32, i32* %33, i64 1
%35 = load i32, i32* %34
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %35)
%37 = load i32*, i32** %7
%38 = getelementptr inbounds i32, i32* %37, i64 2
%39 = load i32, i32* %38
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %39)
%41 = tail call i32 @putchar(i32 10)
%42 = load i32*, i32** %10
%43 = load i32, i32* %42
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %43)
%45 = load i32*, i32** %10
%46 = getelementptr inbounds i32, i32* %45, i64 1
%47 = load i32, i32* %46
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %47)
%49 = load i32*, i32** %10
%50 = getelementptr inbounds i32, i32* %49, i64 2
%51 = load i32, i32* %50
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %51)
%53 = tail call i32 @putchar(i32 10)
%54 = tail call i32* @spiralOrder(i32** %2, i32 3, i32 3)
%55 = load i32, i32* %54
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %55)
%57 = getelementptr inbounds i32, i32* %54, i64 1
%58 = load i32, i32* %57
%59 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %58)
%60 = getelementptr inbounds i32, i32* %54, i64 2
%61 = load i32, i32* %60
%62 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %61)
%63 = getelementptr inbounds i32, i32* %54, i64 3
%64 = load i32, i32* %63
%65 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %64)
%66 = getelementptr inbounds i32, i32* %54, i64 4
%67 = load i32, i32* %66
%68 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %67)
%69 = getelementptr inbounds i32, i32* %54, i64 5
%70 = load i32, i32* %69
%71 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %70)
%72 = getelementptr inbounds i32, i32* %54, i64 6
%73 = load i32, i32* %72
%74 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %73)
%75 = getelementptr inbounds i32, i32* %54, i64 7
%76 = load i32, i32* %75
%77 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %76)
%78 = getelementptr inbounds i32, i32* %54, i64 8
%79 = load i32, i32* %78
%80 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %79)
%81 = tail call i32 @putchar(i32 10)
ret i32 0
}