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     L12
  mov    (%rax),%rdi
  mov    %rax,%r13
  test   %rdi,%rdi
  je     L9
  xor    %ebp,%ebp
  xor    %ebx,%ebx
  xor    %r12d,%r12d
  nopw   0x0(%rax,%rax,1)
L69:
  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    L69
  lea    0x1e(%r12),%edi
  add    $0xe,%r12d
  imul   %ebp,%edi
  add    %ebx,%edi
L70:
  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     L87
  mov    (%rsp),%rbp
  lea    D2(%rip),%r15
  lea    D16(%rip),%r14
  mov    0x0(%rbp),%rdi
  test   %rdi,%rdi
  je     L16
  xchg   %ax,%ax
L51:
  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    L51
L16:
  movb   $0x0,(%r12)
  mov    (%rsp),%rdi
  call   <freedirlist@plt>
  mov    %r13,%rdi
  call   <free@plt>
L13:
  mov    0x8(%rsp),%rax
  add    $0x18,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  nopl   0x0(%rax)
L9:
  mov    $0xe,%r12d
  xor    %edi,%edi
  jmp    L70
L12:
  movq   $0x0,0x8(%rsp)
  jmp    L13
L87:
  mov    (%rsp),%rdi
  call   <freedirlist@plt>
  jmp    L13
```

tool_calls: name: unpack_data
arguments: data_tag: D2
data_type: string
name: unpack_data
arguments: data_tag: D16
data_type: string

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

tool: D16: </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;
}
```