user: Demonstrate the C code that generates the assembly output below:

```asm
<lsdir>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  mov    %rsi,%r12
  push   %rbp
  mov    %rdi,%rbp
  push   %rbx
  sub    $0x48,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x38(%rsp)
  xor    %eax,%eax
  test   %rdi,%rdi
  setne  %dil
  xor    %r13d,%r13d
  movzbl %dil,%edi
  call   <assert@plt>
  mov    %rbp,%rdi
  call   <opendir@plt>
  test   %rax,%rax
  je     L127
  mov    %rax,%rdi
  mov    %rax,%rbp
  call   <dirfd@plt>
  mov    %eax,0x1c(%rsp)
  cmp    $0xffffffff,%eax
  je     L278
  mov    0x1c(%rsp),%edi
  call   <fchdir@plt>
  cmp    $0xffffffff,%eax
  je     L476
  mov    D61(%rip),%rax
  mov    %rax,0x10(%rsp)
  movl   $0x0,(%rax)
  nopl   0x0(%rax,%rax,1)
L224:
  mov    %rbp,%rdi
  call   <readdir@plt>
  mov    %rax,%r15
  test   %rax,%rax
  je     L390
L149:
  mov    (%r15),%rdi
  cmpb   $0x2e,(%rdi)
  jne    L293
  cmpb   $0x0,0x1(%rdi)
  je     L224
L293:
  cmpb   $0x2e,(%rdi)
  jne    L480
  cmpb   $0x2e,0x1(%rdi)
  jne    L480
  cmpb   $0x0,0x2(%rdi)
  je     L224
  nopl   0x0(%rax,%rax,1)
L480:
  mov    D195(%rip),%rax
  xor    %r14d,%r14d
  mov    (%rax),%rax
  cmp    %rax,0x8(%r15)
  je     L37
L193:
  call   <strlen@plt>
  mov    D205(%rip),%rcx
  mov    (%rcx),%ecx
  lea    -0x1(%rcx),%edx
  mov    %ecx,0x8(%rsp)
  movslq %edx,%rdx
  cmp    %rax,%rdx
  jb     L337
  lea    0x3(%rax),%esi
  mov    $0x1,%edx
  lea    0x1(%rax),%r13
  mov    %r12,%rdi
  call   <buf_add@plt>
  test   %rax,%rax
  je     L83
  mov    D210(%rip),%rbx
  mov    0x8(%r15),%rsi
  mov    $0x44,%edx
  cmp    (%rbx),%rsi
  je     L13
  mov    D155(%rip),%rdi
  mov    $0x4c,%edx
  cmp    (%rdi),%rsi
  je     L13
  mov    D26(%rip),%rdx
  cmp    (%rdx),%rsi
  mov    $0x46,%edx
  mov    $0x55,%esi
  cmovne %esi,%edx
L13:
  mov    %dl,(%rax)
  lea    0x2(%rax),%rdi
  mov    %r13,%rdx
  movb   $0x0,0x1(%rax)
  mov    (%r15),%rsi
  call   <memcpy@plt>
  mov    0x8(%r15),%rax
  cmp    (%rbx),%rax
  je     L32
  mov    D155(%rip),%rdx
  cmp    (%rdx),%rax
  je     L189
  mov    D26(%rip),%rdx
  cmp    (%rdx),%rax
  jne    L224
  test   %r14d,%r14d
  je     L257
L188:
  mov    0x28(%rsp),%r8
  test   %r8,%r8
  js     L44
  mov    0x20(%rsp),%r13
  test   %r13,%r13
  js     L44
  mov    %r8,%rdi
  mov    %r8,0x8(%rsp)
  call   <ndigits@plt>
  mov    %r13,%rdi
  mov    %eax,%r14d
  call   <ndigits@plt>
  mov    D216(%rip),%rdx
  mov    0x8(%rsp),%r8
  cmp    %eax,%r14d
  mov    %eax,%ecx
  mov    (%rdx),%edx
  cmovge %r14d,%ecx
  cmp    %ecx,%edx
  jle    L337
  lea    0x1(%rax),%ebx
  add    $0x1,%r14d
  mov    %r8,0x8(%rsp)
  sub    %ebx,%edx
  cmp    %r14d,%edx
  jl     L337
  lea    (%r14,%rbx,1),%esi
  mov    $0x1,%edx
  mov    %r12,%rdi
  call   <buf_add@plt>
  mov    0x8(%rsp),%r8
  test   %rax,%rax
  mov    %rax,%r15
  je     L83
  mov    %r14d,%edx
  movslq %r14d,%r14
  mov    %r8,%rsi
  mov    %rax,%rdi
  call   <ulltoa@plt>
  add    %r15,%r14
  mov    %ebx,%edx
  mov    %r13,%rsi
  movb   $0x0,(%r14)
  mov    %r14,%rdi
  movslq %ebx,%rbx
  call   <ulltoa@plt>
  movb   $0x0,(%r14,%rbx,1)
  mov    %rbp,%rdi
  call   <readdir@plt>
  mov    %rax,%r15
  test   %rax,%rax
  jne    L149
  nopw   0x0(%rax,%rax,1)
L390:
  mov    0x10(%rsp),%rax
  mov    (%rax),%ebx
  test   %ebx,%ebx
  jne    L367
  mov    %rbp,%rdi
  mov    $0x1,%r13d
  call   <closedir@plt>
  cmp    $0xffffffff,%eax
  je     L184
  xchg   %ax,%ax
L127:
  mov    0x38(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L395
  add    $0x48,%rsp
  mov    %r13d,%eax
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  cs nopw 0x0(%rax,%rax,1)
L37:
  lea    0x20(%rsp),%rsi
  call   <stat@plt>
  cmp    $0xffffffff,%eax
  je     L83
  mov    0x30(%rsp),%edi
  call   <S_ISDIR@plt>
  test   %rax,%rax
  je     L81
  mov    D210(%rip),%rax
  mov    (%r15),%rdi
  mov    $0x1,%r14d
  mov    (%rax),%rax
  mov    %rax,0x8(%r15)
  jmp    L193
  nopl   0x0(%rax)
L32:
  mov    (%r15),%rdi
  mov    %r12,%rsi
  call   <lsdir>
  test   %eax,%eax
  je     L83
  mov    0x1c(%rsp),%edi
  call   <fchdir@plt>
  cmp    $0xffffffff,%eax
  je     L434
  mov    $0x1,%edx
  mov    $0x2,%esi
  mov    %r12,%rdi
  call   <buf_add@plt>
  test   %rax,%rax
  je     L83
  mov    $0x2d,%edx
  mov    %dx,(%rax)
  jmp    L224
  nopl   (%rax)
L189:
  mov    (%r15),%rdi
  lea    0x20(%rsp),%rsi
  call   <lstat@plt>
  cmp    $0xffffffff,%eax
  je     L83
  mov    0x20(%rsp),%r14
  test   %r14,%r14
  js     L44
  mov    D205(%rip),%rax
  mov    (%rax),%eax
  sub    $0x1,%eax
  cltq
  cmp    %r14,%rax
  jb     L337
  lea    0x2(%r14),%esi
  mov    $0x1,%edx
  lea    0x1(%r14),%rbx
  mov    %r12,%rdi
  call   <buf_add@plt>
  mov    %rax,%r13
  test   %rax,%rax
  je     L83
  mov    (%r15),%rdi
  mov    %rbx,%rdx
  mov    %rax,%rsi
  call   <readlink@plt>
  movb   $0x0,0x1(%r13,%r14,1)
  cmp    $0xffffffff,%eax
  je     L83
  test   %eax,%eax
  js     L44
  cltq
  cmp    %rbx,%rax
  je     L224
  mov    D32(%rip),%rax
  mov    (%rax),%ebx
  mov    0x10(%rsp),%rax
  mov    %ebx,(%rax)
  jmp    L367
  nopl   0x0(%rax)
L257:
  mov    (%r15),%rdi
  lea    0x20(%rsp),%rsi
  call   <stat@plt>
  cmp    $0xffffffff,%eax
  jne    L188
  xchg   %ax,%ax
L83:
  mov    0x10(%rsp),%rax
  mov    %rbp,%rdi
  mov    (%rax),%ebx
  call   <closedir@plt>
  cmp    $0xffffffff,%eax
  jne    L238
L442:
  mov    0x10(%rsp),%rcx
  mov    D42(%rip),%rax
  xor    %edi,%edi
  mov    (%rax),%eax
  cmp    %eax,(%rcx)
  setne  %dil
  call   <assert@plt>
  jmp    L238
  nopl   0x0(%rax,%rax,1)
L81:
  mov    0x30(%rsp),%edi
  call   <S_ISLNK@plt>
  test   %rax,%rax
  je     L354
  mov    D155(%rip),%rax
  mov    (%r15),%rdi
  mov    $0x1,%r14d
  mov    (%rax),%rax
  mov    %rax,0x8(%r15)
  jmp    L193
L354:
  mov    0x30(%rsp),%edi
  call   <S_ISREG@plt>
  test   %rax,%rax
  je     L295
  mov    D26(%rip),%rax
  mov    (%rax),%rax
  mov    %rax,0x8(%r15)
L295:
  mov    (%r15),%rdi
  mov    $0x1,%r14d
  jmp    L193
L476:
  mov    D61(%rip),%rbx
  mov    D42(%rip),%rax
  mov    %rbx,0x10(%rsp)
L219:
  mov    (%rax),%eax
  xor    %edi,%edi
  cmp    %eax,(%rbx)
  setne  %dil
  call   <assert@plt>
  mov    (%rbx),%ebx
L367:
  mov    %rbp,%rdi
  call   <closedir@plt>
  cmp    $0xffffffff,%eax
  je     L442
L238:
  mov    0x10(%rsp),%rax
  xor    %r13d,%r13d
  mov    %ebx,(%rax)
  jmp    L127
  nopl   0x0(%rax,%rax,1)
L278:
  mov    D61(%rip),%rbx
  mov    D32(%rip),%rax
  mov    %rbx,0x10(%rsp)
  jmp    L219
  nopl   (%rax)
L337:
  mov    D146(%rip),%rax
  mov    (%rax),%ebx
  mov    0x10(%rsp),%rax
  mov    %ebx,(%rax)
  jmp    L367
L184:
  mov    0x10(%rsp),%rbx
  mov    D42(%rip),%rax
  xor    %edi,%edi
  mov    (%rax),%eax
  cmp    %eax,(%rbx)
  setne  %dil
  call   <assert@plt>
  jmp    L127
L44:
  mov    D33(%rip),%rax
  mov    (%rax),%ebx
  mov    0x10(%rsp),%rax
  mov    %ebx,(%rax)
  jmp    L367
L434:
  mov    D42(%rip),%rax
  mov    0x10(%rsp),%rbx
  jmp    L219
L395:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
bool lsdir(const char *const name, struct buf **const buf) {
  assert(name != NULL);
  DIR *dir = opendir(name);
  if (dir == NULL)
    return (false);
  int fd = dirfd(dir);
  if (fd == -1) {
    assert(errno != EINVAL);
    goto fail;
  }
  if (fchdir(fd) == -1) {
    assert(errno != EBADF);
    goto fail;
  }
  errno = 0;
  struct dirent *ent;
  while ((ent = readdir(dir)) != NULL) {
    char *p;
    struct stat st;
    bool st_valid = false;
    if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
      continue;
    if (ent->d_type == DT_UNKNOWN) {
      if (stat(ent->d_name, &st) == -1)
        goto fail;
      else
        st_valid = true;
      if (S_ISDIR(st.st_mode))
        ent->d_type = DT_DIR;
      else if (S_ISLNK(st.st_mode))
        ent->d_type = DT_LNK;
      else if (S_ISREG(st.st_mode))
        ent->d_type = DT_REG;
    }
    const size_t dname_len = strlen(ent->d_name);
    if (dname_len > SIZE_MAX - 1) {
      errno = EOVERFLOW;
      goto fail;
    }
    const size_t dname_size = dname_len + 1;
    if ((p = buf_add(buf, 2 + dname_size, 1)) == NULL)
      goto fail;
    *p++ = ent->d_type == DT_DIR ? 'D' : ent->d_type == DT_LNK ? 'L' : ent->d_type == DT_REG ? 'F' : 'U';
    *p++ = '\0';
    (void)memcpy(p, ent->d_name, dname_size);
    if (ent->d_type == DT_DIR) {
      if (lsdir(ent->d_name, buf) == false)
        goto fail;
      if (fchdir(fd) == -1) {
        assert(errno != EBADF);
        goto fail;
      }
      if ((p = buf_add(buf, 2, 1)) == NULL)
        goto fail;
      *p++ = '-';
      *p = '\0';
    } else if (ent->d_type == DT_LNK) {
      if (lstat(ent->d_name, &st) == -1)
        goto fail;
      if (st.st_size < 0) {
        errno = ERANGE;
        goto fail;
      }
      const size_t tsize = (size_t)st.st_size;
      if (tsize > SIZE_MAX - 1) {
        errno = EOVERFLOW;
        goto fail;
      }
      const size_t tlen = tsize + 1;
      if ((p = buf_add(buf, tlen + 1, 1)) == NULL)
        goto fail;
      const ssize_t llen = readlink(ent->d_name, p, tlen);
      *(p + tlen) = '\0';
      if (llen == -1)
        goto fail;
      if (llen < 0) {
        errno = ERANGE;
        goto fail;
      }
      if ((size_t)llen != tlen) {
        errno = EINVAL;
        goto fail;
      }
    } else if (ent->d_type == DT_REG) {
      if (st_valid == false) {
        if (stat(ent->d_name, &st) == -1)
          goto fail;
        st_valid = true;
      }
      if (st.st_mtim.tv_sec < 0 || st.st_size < 0) {
        errno = ERANGE;
        goto fail;
      }
      time_t mtime = st.st_mtim.tv_sec;
      long long size = st.st_size;
      const uint mtime_digits_size = ndigits((unsigned long long)mtime);
      const uint size_digits_size = ndigits((unsigned long long)size);
      if (mtime_digits_size > UINT_MAX - 1 || size_digits_size > UINT_MAX - 1) {
        errno = EOVERFLOW;
        goto fail;
      }
      const uint mtime_digits_len = mtime_digits_size + 1;
      const uint size_digits_len = size_digits_size + 1;
      if (mtime_digits_len > UINT_MAX - size_digits_len) {
        errno = EOVERFLOW;
        goto fail;
      }
      const uint total_digits_len = mtime_digits_len + size_digits_len;
      if ((p = buf_add(buf, total_digits_len, 1)) == NULL)
        goto fail;
      ulltoa(p, (unsigned long long)mtime, mtime_digits_len);
      *(p + mtime_digits_len) = '\0';
      p += mtime_digits_len;
      ulltoa(p, (unsigned long long)size, size_digits_len);
      *(p + size_digits_len) = '\0';
    }
  }
  if (errno != 0)
    goto fail;
  if (closedir(dir) == -1)
    assert(errno != EBADF);
  return (true);
fail : {
  int _errno = errno;
  if (closedir(dir) == -1)
    assert(errno != EBADF);
  errno = _errno;
  return (false);
}
}
```