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

```asm
<build_template>:
  endbr64
  push   %rbp
  mov    %rsp,%rbp
  sub    $0x70,%rsp
  mov    %rdi,-0x58(%rbp)
  mov    %rsi,-0x60(%rbp)
  mov    %rdx,-0x68(%rbp)
  mov    %fs:0x28,%rax
  mov    %rax,-0x8(%rbp)
  xor    %eax,%eax
  movl   $0x58585858,-0xf(%rbp)
  movw   $0x5858,-0xb(%rbp)
  movb   $0x0,-0x9(%rbp)
  mov    -0x58(%rbp),%rax
  mov    %rax,%rdi
  call   <strlen@plt>
  mov    %rax,-0x48(%rbp)
  cmpq   $0x0,-0x48(%rbp)
  je     L59
  movq   $0x1,-0x40(%rbp)
  mov    -0x48(%rbp),%rax
  lea    -0x1(%rax),%rdx
  mov    -0x58(%rbp),%rax
  add    %rdx,%rax
  movzbl (%rax),%eax
  cmp    $0x2f,%al
  jne    L22
  subq   $0x1,-0x48(%rbp)
  jmp    L22
L59:
  movq   $0x0,-0x40(%rbp)
L22:
  mov    -0x60(%rbp),%rax
  mov    %rax,%rdi
  call   <strlen@plt>
  mov    %rax,-0x30(%rbp)
  mov    -0x68(%rbp),%rax
  mov    %rax,%rdi
  call   <strlen@plt>
  mov    %rax,-0x28(%rbp)
  mov    -0x48(%rbp),%rdx
  mov    -0x40(%rbp),%rax
  add    %rax,%rdx
  mov    -0x30(%rbp),%rax
  add    %rax,%rdx
  mov    -0x28(%rbp),%rax
  add    %rdx,%rax
  add    $0x7,%rax
  mov    %rax,-0x20(%rbp)
  mov    -0x20(%rbp),%rax
  mov    %rax,%rdi
  call   <malloc@plt>
  mov    %rax,-0x18(%rbp)
  cmpq   $0x0,-0x18(%rbp)
  je     L33
  mov    -0x18(%rbp),%rax
  mov    %rax,-0x38(%rbp)
  mov    -0x48(%rbp),%rdx
  mov    -0x58(%rbp),%rcx
  mov    -0x38(%rbp),%rax
  mov    %rcx,%rsi
  mov    %rax,%rdi
  call   <strncpy@plt>
  mov    -0x48(%rbp),%rax
  add    %rax,-0x38(%rbp)
  cmpq   $0x1,-0x40(%rbp)
  jne    L2
  mov    -0x38(%rbp),%rax
  lea    0x1(%rax),%rdx
  mov    %rdx,-0x38(%rbp)
  movb   $0x2f,(%rax)
L2:
  mov    -0x30(%rbp),%rdx
  mov    -0x60(%rbp),%rcx
  mov    -0x38(%rbp),%rax
  mov    %rcx,%rsi
  mov    %rax,%rdi
  call   <strncpy@plt>
  mov    -0x30(%rbp),%rax
  add    %rax,-0x38(%rbp)
  lea    -0xf(%rbp),%rcx
  mov    -0x38(%rbp),%rax
  mov    $0x6,%edx
  mov    %rcx,%rsi
  mov    %rax,%rdi
  call   <strncpy@plt>
  addq   $0x6,-0x38(%rbp)
  mov    -0x28(%rbp),%rdx
  mov    -0x68(%rbp),%rcx
  mov    -0x38(%rbp),%rax
  mov    %rcx,%rsi
  mov    %rax,%rdi
  call   <strncpy@plt>
  mov    -0x20(%rbp),%rax
  lea    -0x1(%rax),%rdx
  mov    -0x18(%rbp),%rax
  add    %rdx,%rax
  movb   $0x0,(%rax)
  mov    -0x18(%rbp),%rax
  jmp    L8
L33:
  lea    D6(%rip),%rax
  mov    %rax,%rdi
  call   <perror@plt>
  mov    $0x0,%eax
L8:
  mov    -0x8(%rbp),%rdx
  sub    %fs:0x28,%rdx
  je     L21
  call   <__stack_chk_fail@plt>
L21:
  leave
  ret
```

tool_calls: name: disassemble_data
arguments: tag: D6
type: string

tool: D6: malloc

assistant: ```c
char *build_template(const char *directory, const char *prefix, const char *suffix) {
  const char pattern[] = "XXXXXX";
  size_t len, directory_len, pathsep_len, prefix_len, suffix_len;
  char *template;
  directory_len = strlen(directory);
  if (directory_len > 0) {
    pathsep_len = 1;
    if (directory[directory_len - 1] == '/') {
      --directory_len;
    }
  } else {
    pathsep_len = 0;
  }
  prefix_len = strlen(prefix);
  suffix_len = strlen(suffix);
  len = directory_len + pathsep_len + prefix_len + sizeof pattern + suffix_len;
  if ((template = malloc(len))) {
    char *tp = template;
    (void)strncpy(tp, directory, directory_len);
    tp += directory_len;
    if (pathsep_len == 1) {
      *tp++ = '/';
    }
    (void)strncpy(tp, prefix, prefix_len);
    tp += prefix_len;
    (void)strncpy(tp, pattern, sizeof pattern - 1);
    tp += sizeof pattern - 1;
    (void)strncpy(tp, suffix, suffix_len);
    template[len - 1] = '\0';
    return template;
  }
  perror("malloc");
  return NULL;
}
```