user: Please show me the C implementation that matches the assembly code listed below:

```asm
<main>:
  endbr64
  push   %rbp
  lea    D6(%rip),%rdi
  mov    %rsp,%rbp
  push   %r15
  push   %r14
  push   %r13
  lea    D9(%rip),%r13
  push   %r12
  push   %rbx
  sub    $0x38,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,-0x38(%rbp)
  xor    %eax,%eax
  call   <printf@plt>
  lea    -0x3c(%rbp),%rsi
  mov    %r13,%rdi
  call   <scanf@plt>
  movslq -0x3c(%rbp),%rdx
  mov    %rsp,%rsi
  lea    0x0(,%rdx,4),%rcx
  mov    %rdx,%rax
  imul   %rdx,%rdx
  mov    %rcx,-0x60(%rbp)
  lea    0xf(,%rdx,4),%rdx
  mov    %rdx,%rcx
  and    $0xfffffffffffff000,%rdx
  sub    %rdx,%rsi
  and    $0xfffffffffffffff0,%rcx
  cmp    %rsi,%rsp
  je     L91
L166:
  sub    $0x1000,%rsp
  orq    $0x0,0xff8(%rsp)
  cmp    %rsi,%rsp
  jne    L166
L91:
  and    $0xfff,%ecx
  sub    %rcx,%rsp
  test   %rcx,%rcx
  jne    L183
L49:
  mov    %rsp,-0x48(%rbp)
  test   %eax,%eax
  jle    L39
  mov    -0x60(%rbp),%rcx
  movq   $0x0,-0x50(%rbp)
  xor    %r12d,%r12d
  lea    D2(%rip),%r14
  shr    $0x2,%rcx
  mov    %rcx,-0x58(%rbp)
  test   %eax,%eax
  jle    L39
  nopl   0x0(%rax)
L114:
  mov    -0x48(%rbp),%rax
  mov    -0x50(%rbp),%rcx
  xor    %r15d,%r15d
  lea    (%rax,%rcx,4),%rbx
  nop
L23:
  mov    %r15d,%edx
  mov    %r12d,%esi
  mov    %r14,%rdi
  xor    %eax,%eax
  call   <printf@plt>
  mov    %rbx,%rsi
  mov    %r13,%rdi
  add    $0x1,%r15d
  call   <scanf@plt>
  mov    -0x3c(%rbp),%eax
  add    $0x4,%rbx
  cmp    %r15d,%eax
  jg     L23
  add    $0x1,%r12d
  cmp    %r12d,%eax
  jle    L39
  mov    -0x58(%rbp),%rdx
  add    %rdx,-0x50(%rbp)
  test   %eax,%eax
  jg     L114
L39:
  lea    -0x1(%rax),%ecx
  imul   %eax,%eax
  mov    %ecx,-0x50(%rbp)
  mov    %eax,%r8d
  test   %eax,%eax
  jne    L174
L62:
  mov    $0xa,%edi
  call   <putchar@plt>
  mov    -0x38(%rbp),%rax
  sub    %fs:0x28,%rax
  jne    L34
  lea    -0x28(%rbp),%rsp
  xor    %eax,%eax
  pop    %rbx
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  pop    %rbp
  ret
L183:
  orq    $0x0,-0x8(%rsp,%rcx,1)
  jmp    L49
L174:
  mov    -0x60(%rbp),%rax
  movl   $0x0,-0x58(%rbp)
  xor    %r15d,%r15d
  xor    %ebx,%ebx
  xor    %r13d,%r13d
  xor    %r12d,%r12d
  xor    %r14d,%r14d
  shr    $0x2,%rax
  mov    %rax,-0x60(%rbp)
  jmp    L222
  nopl   0x0(%rax)
L118:
  cmp    $0x2,%eax
  je     L42
  cmp    $0x3,%eax
  jne    L131
  sub    $0x1,%r14d
L131:
  add    $0x1,%r13d
  lea    0x1(%r15),%eax
  cmp    -0x50(%rbp),%r13d
  je     L197
L102:
  and    $0x1,%eax
  or     -0x58(%rbp),%eax
  mov    %r15d,%eax
  je     L148
L67:
  mov    -0x3c(%rbp),%r8d
  add    $0x1,%ebx
  imul   %r8d,%r8d
  cmp    %ebx,%r8d
  jle    L62
L222:
  movslq %r14d,%rdx
  imul   -0x60(%rbp),%rdx
  mov    -0x48(%rbp),%rcx
  movslq %r12d,%rax
  mov    %r14d,%esi
  lea    D7(%rip),%rdi
  add    %rdx,%rax
  mov    %r12d,%edx
  mov    (%rcx,%rax,4),%r9d
  xor    %eax,%eax
  mov    %ebx,%ecx
  call   <printf@plt>
  mov    %r15d,%eax
  and    $0x3,%eax
  cmp    $0x1,%eax
  adc    $0x0,%r12d
  cmp    $0x1,%eax
  jne    L118
  add    $0x1,%r14d
  add    $0x1,%r13d
  lea    0x1(%r15),%eax
  cmp    -0x50(%rbp),%r13d
  jne    L102
L197:
  and    $0x1,%r15d
  je     L101
  movl   $0x0,-0x58(%rbp)
  mov    %eax,%r15d
  mov    $0xffffffff,%r13d
  jmp    L67
  nop
L42:
  sub    $0x1,%r12d
  jmp    L131
  nopl   0x0(%rax)
L101:
  mov    $0xffffffff,%r13d
L148:
  subl   $0x1,-0x50(%rbp)
  mov    %eax,%r15d
  movl   $0x1,-0x58(%rbp)
  jmp    L67
L34:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: disassemble_data
arguments: tag: D6
type_infered: string
name: disassemble_data
arguments: tag: D9
type_infered: string
name: disassemble_data
arguments: tag: D2
type_infered: string
name: disassemble_data
arguments: tag: D7
type_infered: string

tool: D6: Inserisci dimensione della matrice: 

tool: D9: %d

tool: D2: Inserisci numero [%d:%d]: 

tool: D7: 
[i=%d, k=%d; %d/%d]: %d

assistant: ```c
int main() {
  int i, k, j;
  int size;
  int direction, step, max_step;
  bool changed;
  printf("Inserisci dimensione della matrice: ");
  scanf("%d", &size);
  int matrix[size][size];
  for (i = 0; i < size; i++) {
    for (k = 0; k < size; k++) {
      printf("Inserisci numero [%d:%d]: ", i, k);
      scanf("%d", &matrix[i][k]);
    }
  }
  for (j = 0, i = 0, k = 0, direction = 0, step = 0, max_step = size - 1, changed = false; j < size * size; j++) {
    printf("\n[i=%d, k=%d; %d/%d]: %d", i, k, j, size * size, matrix[i][k]);
    if (direction % 4 == 0)
      k++;
    if (direction % 4 == 1)
      i++;
    if (direction % 4 == 2)
      k--;
    if (direction % 4 == 3)
      i--;
    step++;
    if (step == max_step)
      step = -1, direction++, changed = false;
    if ((direction + 1) % 2 == 0 && !changed)
      max_step--, changed = true;
  }
  printf("\n");
  return 0;
}
```