#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 */
void *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int *spiralOrder(int **, int, int *, int *);
int main(int argc, char **argv) {
  int i, j, count = 0;
  int row = 3;
  int *cols = malloc(row * sizeof(int));
  int **mat = malloc(row * sizeof(int *));
  for (i = 0; i < row; i++) {
    cols[i] = row;
    mat[i] = malloc(cols[i] * sizeof(int));
    for (j = 0; j < cols[i]; j++) {
      mat[i][j] = ++count;
      printf("%d ", mat[i][j]);
    }
    printf("\n");
  }
  int *nums = spiralOrder(mat, row, cols, &count);
  for (i = 0; i < count; i++) {
    printf("%d ", nums[i]);
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x3,-0x20(%rbp)
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
movslq -0x20(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1243 <main+0x113>
mov    -0x20(%rbp),%edx
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
movslq (%rax,%rcx,4),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%rdx
mov    -0x30(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x28(%rbp),%rcx
movslq -0x14(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    1227 <main+0xf7>
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c8 <main+0x98>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1181 <main+0x51>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
call   1040 <spiralOrder@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1295 <main+0x165>
mov    -0x38(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1262 <main+0x132>
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    $0x18,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
movl   $0x3,(%r14)
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,(%r15)
movl   $0x1,(%rax)
lea    0xe72(%rip),%r12
mov    %r12,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x2,0x4(%rbx)
mov    %r12,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x3,0x8(%rbx)
mov    %r12,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
movl   $0x3,0x4(%r14)
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,0x8(%r15)
movl   $0x4,(%rax)
mov    %r12,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x5,0x4(%rbx)
mov    %r12,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x6,0x8(%rbx)
mov    %r12,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
movl   $0x3,0x8(%r14)
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,0x10(%r15)
movl   $0x7,(%rax)
mov    %r12,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x8,0x4(%rbx)
mov    %r12,%rdi
mov    $0x8,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x9,0x8(%rbx)
mov    %r12,%rdi
mov    $0x9,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x9,0x4(%rsp)
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0x4(%rsp),%rcx
mov    %r15,%rdi
mov    $0x3,%esi
mov    %r14,%rdx
call   1050 <spiralOrder@plt>
cmpl   $0x0,0x4(%rsp)
jle    12ec <main+0x1ac>
mov    %rax,%r15
lea    0xd37(%rip),%r14
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r15,%rbx,4),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
movslq 0x4(%rsp),%rax
cmp    %rax,%rbx
jl     12d0 <main+0x190>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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