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

```asm
<close_nonstd_fds>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x48,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x38(%rsp)
  xor    %eax,%eax
  movq   $0x0,(%rsp)
  movq   $0x0,0x8(%rsp)
  lea    D3(%rip),%rdi
  call   <opendir@plt>
  mov    %rax,%r12
  test   %rax,%rax
  je     L30
  lea    D42(%rip),%r13
  lea    D2(%rip),%r14
  lea    D47(%rip),%r15
L60:
  mov    %r12,%rdi
  call   <readdir@plt>
  mov    %rax,%rbx
  test   %rax,%rax
  je     L53
  mov    (%rsp),%rdi
  call   <free@plt>
  mov    0x8(%rsp),%rdi
  call   <free@plt>
  mov    %r13,%rsi
  mov    (%rbx),%rdi
  call   <strcmp@plt>
  test   %rax,%rax
  je     L60
  mov    %r14,%rsi
  mov    (%rbx),%rdi
  call   <strcmp@plt>
  test   %rax,%rax
  je     L60
  lea    0x8(%rsp),%rdi
  mov    (%rbx),%rdx
  mov    %r15,%rsi
  call   <asprintf@plt>
  mov    %eax,%ebp
  cmp    $0xffffffff,%eax
  je     L100
  mov    $0x0,%eax
  call   <getpid@plt>
  mov    %rax,%rdx
  mov    %rsp,%rdi
  lea    D27(%rip),%rsi
  call   <asprintf@plt>
  mov    %eax,%ebp
  cmp    $0xffffffff,%eax
  je     L100
  lea    0x10(%rsp),%rbp
  mov    $0x20,%esi
  mov    %rbp,%rdi
  call   <bzero@plt>
  mov    $0x1f,%edx
  mov    %rbp,%rsi
  mov    0x8(%rsp),%rdi
  call   <readlink@plt>
  mov    %eax,%ebp
  cmp    $0xffffffff,%eax
  je     L100
  lea    0x10(%rsp),%rsi
  mov    (%rsp),%rdi
  call   <strcmp@plt>
  test   %rax,%rax
  je     L60
  mov    (%rbx),%rdi
  call   <atoi@plt>
  cmp    $0x2,%eax
  jbe    L60
  mov    %eax,%edi
  call   <close@plt>
  jmp    L60
L53:
  mov    $0x0,%ebp
L100:
  mov    %r12,%rdi
  call   <closedir@plt>
  mov    (%rsp),%rdi
  call   <free@plt>
  mov    0x8(%rsp),%rdi
  call   <free@plt>
  mov    0x38(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L10
  mov    %ebp,%eax
  add    $0x48,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L30:
  mov    $0xffffffff,%ebp
  jmp    L100
L10:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: unpack_data
arguments: data_tag: D3
type_guessed: string
name: unpack_data
arguments: data_tag: D42
type_guessed: string
name: unpack_data
arguments: data_tag: D2
type_guessed: string
name: unpack_data
arguments: data_tag: D47
type_guessed: string
name: unpack_data
arguments: data_tag: D27
type_guessed: string

tool: D3: /proc/self/fd

tool: D42: .

tool: D2: ..

tool: D47: /proc/self/fd/%s

tool: D27: /proc/%d/fd

assistant: ```c
int close_nonstd_fds(void) {
  const int procpidfdmax = 32;
  int rv = -1;
  int fd = -1;
  char *cmp = NULL;
  char *sym = NULL;
  char phys[procpidfdmax];
  DIR *dir = NULL;
  struct dirent *ent = NULL;
  dir = opendir("/proc/self/fd");
  if (!dir) {
    goto out;
  }
  while ((ent = readdir(dir)) != NULL) {
    free(cmp);
    free(sym);
    if (strcmp(ent->d_name, ".") == 0)
      continue;
    if (strcmp(ent->d_name, "..") == 0)
      continue;
    if (asprintf(&sym, "/proc/self/fd/%s", ent->d_name) == -1)
      goto out;
    if (asprintf(&cmp, "/proc/%d/fd", getpid()) == -1)
      goto out;
    bzero(phys, sizeof(phys));
    if (readlink(sym, phys, procpidfdmax - 1) == -1)
      goto out;
    if (strcmp(cmp, phys) == 0)
      continue;
    fd = atoi(ent->d_name);
    switch (fd) {
    case 0:
    case 1:
    case 2:
      break;
    default:
      close(fd);
    }
  }
  rv = 0;
out:
  closedir(dir);
  dir = NULL;
  ent = NULL;
  free(cmp);
  free(sym);
  cmp = sym = NULL;
  return rv;
}
```