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     L166
  mov    %rax,%rdi
  mov    %rax,%rbp
  call   <dirfd@plt>
  mov    %eax,0x1c(%rsp)
  cmp    $0xffffffff,%eax
  je     L239
  mov    %eax,%edi
  call   <fchdir@plt>
  cmp    $0xffffffff,%eax
  je     L251
  mov    D228(%rip),%rax
  mov    %rax,0x10(%rsp)
  movl   $0x0,(%rax)
  cs nopw 0x0(%rax,%rax,1)
L102:
  mov    %rbp,%rdi
  call   <readdir@plt>
  mov    %rax,%r15
  test   %rax,%rax
  je     L506
L185:
  mov    (%r15),%rdi
  cmpb   $0x2e,(%rdi)
  jne    L484
  cmpb   $0x0,0x1(%rdi)
  je     L102
L484:
  cmpb   $0x2e,(%rdi)
  jne    L174
  cmpb   $0x2e,0x1(%rdi)
  jne    L174
  cmpb   $0x0,0x2(%rdi)
  je     L102
  nopl   0x0(%rax,%rax,1)
L174:
  mov    D25(%rip),%rax
  xor    %r14d,%r14d
  mov    (%rax),%rax
  cmp    %rax,0x8(%r15)
  je     L303
L552:
  call   <strlen@plt>
  mov    D55(%rip),%rcx
  mov    (%rcx),%ecx
  lea    -0x1(%rcx),%edx
  mov    %ecx,0x8(%rsp)
  movslq %edx,%rdx
  cmp    %rax,%rdx
  jb     L146
  lea    0x3(%rax),%esi
  mov    $0x1,%edx
  lea    0x1(%rax),%r13
  mov    %r12,%rdi
  call   <buf_add@plt>
  test   %rax,%rax
  je     L470
  mov    D113(%rip),%rbx
  mov    0x8(%r15),%rsi
  mov    $0x44,%edx
  cmp    (%rbx),%rsi
  je     L332
  mov    D32(%rip),%rdi
  mov    $0x4c,%edx
  cmp    (%rdi),%rsi
  je     L332
  mov    D30(%rip),%rdx
  cmp    (%rdx),%rsi
  mov    $0x46,%edx
  mov    $0x55,%esi
  cmovne %esi,%edx
L332:
  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     L498
  mov    D32(%rip),%rdx
  cmp    (%rdx),%rax
  je     L436
  mov    D30(%rip),%rdx
  cmp    (%rdx),%rax
  jne    L102
  test   %r14d,%r14d
  je     L236
L259:
  mov    0x28(%rsp),%r8
  test   %r8,%r8
  js     L198
  mov    0x20(%rsp),%r13
  test   %r13,%r13
  js     L198
  mov    %r8,%rdi
  mov    %r8,0x8(%rsp)
  call   <ndigits@plt>
  mov    %r13,%rdi
  mov    %eax,%r14d
  call   <ndigits@plt>
  mov    D77(%rip),%rdx
  mov    0x8(%rsp),%r8
  cmp    %eax,%r14d
  mov    %eax,%ecx
  mov    (%rdx),%edx
  cmovge %r14d,%ecx
  cmp    %ecx,%edx
  jle    L146
  lea    0x1(%rax),%ebx
  add    $0x1,%r14d
  mov    %r8,0x8(%rsp)
  sub    %ebx,%edx
  cmp    %r14d,%edx
  jl     L146
  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     L470
  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    L185
  nopw   0x0(%rax,%rax,1)
L506:
  mov    0x10(%rsp),%rax
  mov    (%rax),%ebx
  test   %ebx,%ebx
  jne    L178
  mov    %rbp,%rdi
  mov    $0x1,%r13d
  call   <closedir@plt>
  cmp    $0xffffffff,%eax
  je     L302
  xchg   %ax,%ax
L166:
  mov    0x38(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L422
  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)
L303:
  lea    0x20(%rsp),%rsi
  call   <stat@plt>
  cmp    $0xffffffff,%eax
  je     L470
  mov    0x30(%rsp),%edi
  call   <S_ISDIR@plt>
  test   %rax,%rax
  je     L530
  mov    D113(%rip),%rax
  mov    (%r15),%rdi
  mov    $0x1,%r14d
  mov    (%rax),%rax
  mov    %rax,0x8(%r15)
  jmp    L552
  nopl   0x0(%rax)
L498:
  mov    (%r15),%rdi
  mov    %r12,%rsi
  call   <lsdir>
  test   %eax,%eax
  je     L470
  mov    0x1c(%rsp),%edi
  call   <fchdir@plt>
  cmp    $0xffffffff,%eax
  je     L383
  mov    $0x1,%edx
  mov    $0x2,%esi
  mov    %r12,%rdi
  call   <buf_add@plt>
  test   %rax,%rax
  je     L470
  mov    $0x2d,%edx
  mov    %dx,(%rax)
  jmp    L102
  nopl   (%rax)
L436:
  mov    (%r15),%rdi
  lea    0x20(%rsp),%rsi
  call   <lstat@plt>
  cmp    $0xffffffff,%eax
  je     L470
  mov    0x20(%rsp),%r14
  test   %r14,%r14
  js     L198
  mov    D55(%rip),%rax
  mov    (%rax),%eax
  sub    $0x1,%eax
  cltq
  cmp    %r14,%rax
  jb     L146
  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     L470
  mov    (%r15),%rdi
  mov    %rbx,%rdx
  mov    %rax,%rsi
  call   <readlink@plt>
  movb   $0x0,0x1(%r13,%r14,1)
  cmp    $0xffffffff,%eax
  je     L470
  test   %eax,%eax
  js     L198
  cltq
  cmp    %rbx,%rax
  je     L102
  mov    D151(%rip),%rax
  mov    (%rax),%ebx
  mov    0x10(%rsp),%rax
  mov    %ebx,(%rax)
  jmp    L178
  nopl   0x0(%rax)
L236:
  mov    (%r15),%rdi
  lea    0x20(%rsp),%rsi
  call   <stat@plt>
  cmp    $0xffffffff,%eax
  jne    L259
  xchg   %ax,%ax
L470:
  mov    0x10(%rsp),%rax
  mov    %rbp,%rdi
  mov    (%rax),%ebx
  call   <closedir@plt>
  cmp    $0xffffffff,%eax
  jne    L100
L450:
  mov    0x10(%rsp),%rcx
  mov    D203(%rip),%rax
  xor    %edi,%edi
  mov    (%rax),%eax
  cmp    %eax,(%rcx)
  setne  %dil
  call   <assert@plt>
  jmp    L100
  nopl   0x0(%rax,%rax,1)
L530:
  mov    0x30(%rsp),%edi
  call   <S_ISLNK@plt>
  test   %rax,%rax
  je     L134
  mov    D32(%rip),%rax
  mov    (%r15),%rdi
  mov    $0x1,%r14d
  mov    (%rax),%rax
  mov    %rax,0x8(%r15)
  jmp    L552
L134:
  mov    0x30(%rsp),%edi
  call   <S_ISREG@plt>
  test   %rax,%rax
  je     L224
  mov    D30(%rip),%rax
  mov    (%rax),%rax
  mov    %rax,0x8(%r15)
L224:
  mov    (%r15),%rdi
  mov    $0x1,%r14d
  jmp    L552
L251:
  mov    D228(%rip),%rbx
  mov    D203(%rip),%rax
  mov    %rbx,0x10(%rsp)
L148:
  mov    (%rax),%eax
  xor    %edi,%edi
  cmp    %eax,(%rbx)
  setne  %dil
  call   <assert@plt>
  mov    (%rbx),%ebx
L178:
  mov    %rbp,%rdi
  call   <closedir@plt>
  cmp    $0xffffffff,%eax
  je     L450
L100:
  mov    0x10(%rsp),%rax
  xor    %r13d,%r13d
  mov    %ebx,(%rax)
  jmp    L166
  nopl   0x0(%rax,%rax,1)
L239:
  mov    D228(%rip),%rbx
  mov    D151(%rip),%rax
  mov    %rbx,0x10(%rsp)
  jmp    L148
  nopl   (%rax)
L146:
  mov    D198(%rip),%rax
  mov    (%rax),%ebx
  mov    0x10(%rsp),%rax
  mov    %ebx,(%rax)
  jmp    L178
L302:
  mov    0x10(%rsp),%rbx
  mov    D203(%rip),%rax
  xor    %edi,%edi
  mov    (%rax),%eax
  cmp    %eax,(%rbx)
  setne  %dil
  call   <assert@plt>
  jmp    L166
L198:
  mov    D130(%rip),%rax
  mov    (%rax),%ebx
  mov    0x10(%rsp),%rax
  mov    %ebx,(%rax)
  jmp    L178
L383:
  mov    D203(%rip),%rax
  mov    0x10(%rsp),%rbx
  jmp    L148
L422:
  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);
}
}
```