user: Converte this assembly snippet into its equivalent C code:

```asm
<canonicalize>:
  endbr64
  push   %rbp
  mov    %rsp,%rbp
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbx
  sub    $0x38,%rsp
  mov    %edx,-0x44(%rbp)
  mov    %fs:0x28,%rax
  mov    %rax,-0x38(%rbp)
  xor    %eax,%eax
  mov    D2(%rip),%rax
  cmp    %edx,(%rax)
  jg     L45
  mov    %rdi,%rbx
  mov    %rsi,%r14
  call   <strlen@plt>
  mov    D2(%rip),%rdx
  cmp    (%rdx),%eax
  jg     L1
  mov    %r14,%rsi
  mov    %rbx,%rdi
  call   <realpath@plt>
  test   %rax,%rax
  je     L227
  mov    %r14,%rdi
  call   <collapse@plt>
  mov    $0x0,%eax
L49:
  mov    -0x38(%rbp),%rdx
  sub    %fs:0x28,%rdx
  jne    L21
  lea    -0x28(%rbp),%rsp
  pop    %rbx
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  pop    %rbp
  ret
L45:
  mov    D7(%rip),%rax
  mov    (%rax),%rdx
  mov    D87(%rip),%rax
  mov    %rdx,(%rax)
  mov    $0xffffffff,%eax
  jmp    L49
L1:
  mov    D63(%rip),%rax
  mov    (%rax),%rdx
  mov    D87(%rip),%rax
  mov    %rdx,(%rax)
  mov    $0xffffffff,%eax
  jmp    L49
L227:
  mov    %rsp,-0x58(%rbp)
  mov    D2(%rip),%rax
  mov    (%rax),%edx
  lea    0x1(%rdx),%eax
  cltq
  add    $0xf,%rax
  mov    %rax,%rsi
  and    $0xfffffffffffffff0,%rsi
  and    $0xfffffffffffff000,%rax
  mov    %rsp,%rcx
  sub    %rax,%rcx
L169:
  cmp    %rcx,%rsp
  je     L271
  sub    $0x1000,%rsp
  orq    $0x0,0xff8(%rsp)
  jmp    L169
L271:
  mov    %rsi,%rax
  and    $0xfff,%eax
  sub    %rax,%rsp
  test   %rax,%rax
  je     L221
  orq    $0x0,-0x8(%rsp,%rax,1)
L221:
  mov    %rsp,%r12
  mov    %rbx,%rsi
  mov    %rsp,%rdi
  call   <strncpy@plt>
  mov    D2(%rip),%rax
  movslq (%rax),%rax
  movb   $0x0,(%rsp,%rax,1)
  mov    %rsp,%rdi
  call   <strlen@plt>
  movslq %eax,%r15
  add    %rsp,%r15
  cmp    %r15,%rsp
  jae    L42
  mov    %r15,%rbx
  jmp    L60
L213:
  je     L42
L151:
  movb   $0x0,(%rbx)
  mov    %r14,%rsi
  mov    %r12,%rdi
  call   <realpath@plt>
  mov    %rax,%rcx
  mov    %rax,-0x50(%rbp)
  cmp    %rbx,%r15
  mov    $0x2f,%eax
  mov    $0x0,%esi
  cmove  %esi,%eax
  mov    %al,(%rbx)
  test   %rcx,%rcx
  jne    L153
  mov    D87(%rip),%rax
  mov    (%rax),%rax
  mov    D84(%rip),%rdx
  cmp    (%rdx),%rax
  je     L186
  mov    D119(%rip),%rdx
  cmp    (%rdx),%rax
  je     L186
  mov    D43(%rip),%rdx
  cmp    (%rdx),%rax
  je     L186
  mov    $0xffffffff,%eax
L243:
  mov    -0x58(%rbp),%rsp
  jmp    L49
L186:
  cmp    %rbx,%r12
  jae    L42
L60:
  mov    %rbx,%r13
  sub    $0x1,%rbx
  cmp    %rbx,%r12
  jae    L213
  cmpb   $0x2f,(%rbx)
  jne    L60
  jmp    L151
L215:
  mov    D63(%rip),%rax
  mov    (%rax),%rdx
  mov    D87(%rip),%rax
  mov    %rdx,(%rax)
  mov    $0xffffffff,%eax
  jmp    L243
L198:
  cmpb   $0x2f,(%rbx)
  cmove  %r13,%rbx
  jmp    L18
L42:
  mov    %r12,%rsi
  mov    %r14,%rdi
  call   <strcpy@plt>
  mov    %r14,%rdi
  call   <collapse@plt>
  mov    $0x0,%eax
  jmp    L243
L153:
  mov    -0x50(%rbp),%rdi
  call   <strlen@plt>
  mov    %eax,%r12d
  mov    %rbx,%rdi
  call   <strlen@plt>
  add    %r12d,%eax
  cmp    -0x44(%rbp),%eax
  jge    L215
  test   %r12d,%r12d
  jle    L18
  movslq %r12d,%rax
  mov    -0x50(%rbp),%rcx
  cmpb   $0x2f,-0x1(%rcx,%rax,1)
  je     L198
L18:
  movslq %r12d,%rdi
  mov    -0x50(%rbp),%r15
  add    %r15,%rdi
  mov    %rbx,%rsi
  call   <strcpy@plt>
  mov    %r15,%rdi
  call   <collapse@plt>
  mov    $0x0,%eax
  jmp    L243
L21:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
int canonicalize(char *original, char *resolved, int len) {
  if (len < PATH_MAX) {
    errno = EINVAL;
    return -1;
  }
  if (strlen(original) > PATH_MAX) {
    errno = ENAMETOOLONG;
    return -1;
  }
  if (realpath(original, resolved)) {
    collapse(resolved);
    return 0;
  } else {
    char *p, *end, *r = NULL;
    char path[PATH_MAX + 1];
    strncpy(path, original, PATH_MAX);
    path[PATH_MAX] = '\0';
    end = path + strlen(path);
    for (p = end; p > path;) {
      while ((--p > path) && (*p != '/'))
        ;
      if (p == path)
        break;
      *p = '\0';
      r = realpath(path, resolved);
      *p = (p == end) ? '\0' : '/';
      if (r != NULL) {
        break;
      } else if (errno == ENOENT || errno == ENOTDIR || errno == EACCES) {
        continue;
      } else {
        return -1;
      }
    }
    if (r != NULL) {
      int rn = strlen(r);
      if (rn + (int)strlen(p) >= len) {
        errno = ENAMETOOLONG;
        return -1;
      }
      if ((rn > 0) && (r[rn - 1] == '/') && (*p == '/')) {
        p++;
      }
      strcpy(r + rn, p);
      collapse(r);
      return 0;
    } else {
      strcpy(resolved, path);
      collapse(resolved);
      return 0;
    }
  }
}
```