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

```asm
<generate_index>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x18,%rsp
  call   <listdir@plt>
  mov    %rax,(%rsp)
  test   %rax,%rax
  je     L84
  mov    (%rax),%rdi
  mov    %rax,%r13
  test   %rdi,%rdi
  je     L56
  xor    %ebp,%ebp
  xor    %ebx,%ebx
  xor    %r12d,%r12d
  nopw   0x0(%rax,%rax,1)
L57:
  call   <strlen@plt>
  add    $0x8,%r13
  add    $0x1,%ebp
  mov    0x0(%r13),%rdi
  cmp    %eax,%r12d
  cmovl  %eax,%r12d
  cltq
  add    %rax,%rbx
  test   %rdi,%rdi
  jne    L57
  lea    0x1e(%r12),%edi
  add    $0xe,%r12d
  imul   %ebp,%edi
  add    %ebx,%edi
L6:
  call   <malloc@plt>
  mov    %r12d,%edi
  mov    %rax,%rbx
  mov    %rax,0x8(%rsp)
  call   <malloc@plt>
  mov    %rbx,%r12
  mov    %rax,%r13
  test   %rbx,%rbx
  je     L71
  mov    (%rsp),%rbp
  lea    D17(%rip),%r15
  lea    D1(%rip),%r14
  mov    0x0(%rbp),%rdi
  test   %rdi,%rdi
  je     L16
  xchg   %ax,%ax
L31:
  call   <strlen@plt>
  mov    0x0(%rbp),%rdx
  mov    %r15,%rsi
  mov    %r13,%rdi
  movslq %eax,%rbx
  add    $0x8,%rbp
  call   <sprintf@plt>
  mov    %r12,%rdi
  lea    0xe(%rbx),%edx
  mov    %r13,%rsi
  call   <memcpy@plt>
  lea    0xd(%rbx),%eax
  mov    -0x8(%rbp),%rsi
  mov    %ebx,%edx
  cltq
  add    %rax,%r12
  mov    %r12,%rdi
  add    %rbx,%r12
  call   <memcpy@plt>
  mov    %r12,%rdi
  mov    $0x9,%edx
  mov    %r14,%rsi
  call   <memcpy@plt>
  mov    0x0(%rbp),%rdi
  add    $0x9,%r12
  test   %rdi,%rdi
  jne    L31
L16:
  movb   $0x0,(%r12)
  mov    (%rsp),%rdi
  call   <freedirlist@plt>
  mov    %r13,%rdi
  call   <free@plt>
L27:
  mov    0x8(%rsp),%rax
  add    $0x18,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  nopl   0x0(%rax)
L56:
  mov    $0xe,%r12d
  xor    %edi,%edi
  jmp    L6
L84:
  movq   $0x0,0x8(%rsp)
  jmp    L27
L71:
  mov    (%rsp),%rdi
  call   <freedirlist@plt>
  jmp    L27
```

tool_calls: name: extract_symbolic_data
arguments: tag: D17
type_guessed: string
name: extract_symbolic_data
arguments: tag: D1
type_guessed: string

tool: D17: <a href="/%s"/>

tool: D1: </a><br/>

assistant: ```c
char *generate_index(char *dir) {
  char **dirnames = listdir(dir);
  if (dirnames == NULL) {
    return NULL;
  }
  char **dirnamesp = dirnames;
  char *index;
  char *indexp;
  char *namebuffer;
  int len, maxlen;
  long charcount = 0;
  int dirs = 0;
  maxlen = 0;
  while (*dirnames) {
    ++dirs;
    len = strlen(*dirnames++);
    if (len > maxlen)
      maxlen = len;
    charcount += len;
  }
  index = malloc(sizeof(char) * charcount + (dirs * (30 + maxlen)));
  namebuffer = malloc(sizeof(char) * (14 + maxlen));
  indexp = index;
  if (index == NULL) {
    freedirlist(dirnamesp);
    return NULL;
  }
  dirnames = dirnamesp;
  while (*dirnames) {
    len = strlen(*dirnames);
    sprintf(namebuffer, "<a href=\"/%s\"/>", *dirnames);
    memcpy(index, namebuffer, 14 + len);
    index += (13 + len);
    memcpy(index, *dirnames++, len);
    index += len;
    memcpy(index, "</a><br/>", 9);
    index += 9;
  }
  *index = '\0';
  freedirlist(dirnamesp);
  free(namebuffer);
  return indexp;
}
```