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

```asm
<build_template>:
  endbr64
  push   %r15
  mov    %rsi,%r15
  push   %r14
  push   %r13
  push   %r12
  mov    %rdi,%r12
  push   %rbp
  mov    %rdx,%rbp
  push   %rbx
  sub    $0x28,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x18(%rsp)
  xor    %eax,%eax
  mov    $0x5858,%eax
  movl   $0x58585858,0x11(%rsp)
  mov    %ax,0x15(%rsp)
  movb   $0x0,0x17(%rsp)
  call   <strlen@plt>
  test   %rax,%rax
  je     L47
  cmpb   $0x2f,-0x1(%r12,%rax,1)
  lea    -0x1(%rax),%rdx
  mov    %r15,%rdi
  cmovne %rax,%rdx
  mov    %rdx,%rbx
  call   <strlen@plt>
  mov    %rbp,%rdi
  mov    %rax,%r14
  call   <strlen@plt>
  lea    0x1(%rbx,%r14,1),%r13
  add    %rax,%r13
  mov    %rax,(%rsp)
  lea    0x7(%r13),%rdi
  call   <malloc@plt>
  test   %rax,%rax
  je     L45
  mov    %rbx,%rdx
  mov    %r12,%rsi
  mov    %rax,%rdi
  mov    %rax,0x8(%rsp)
  call   <strncpy@plt>
  mov    0x8(%rsp),%r9
  lea    (%r9,%rbx,1),%rax
  movb   $0x2f,(%rax)
  lea    0x1(%rax),%rbx
  jmp    L0
  nopl   0x0(%rax,%rax,1)
L47:
  mov    %r15,%rdi
  call   <strlen@plt>
  mov    %rbp,%rdi
  mov    %rax,%r14
  call   <strlen@plt>
  lea    (%r14,%rax,1),%r13
  mov    %rax,(%rsp)
  lea    0x7(%r13),%rdi
  call   <malloc@plt>
  mov    %rax,%rbx
  test   %rax,%rax
  je     L45
  xor    %edx,%edx
  mov    %r12,%rsi
  mov    %rbx,%rdi
  call   <strncpy@plt>
  mov    %rbx,%r9
L0:
  mov    %rbx,%rdi
  mov    %r14,%rdx
  mov    %r15,%rsi
  add    %r14,%rbx
  mov    %r9,0x8(%rsp)
  call   <strncpy@plt>
  lea    0x11(%rsp),%rsi
  mov    $0x6,%edx
  mov    %rbx,%rdi
  call   <strncpy@plt>
  mov    (%rsp),%rdx
  lea    0x6(%rbx),%rdi
  mov    %rbp,%rsi
  call   <strncpy@plt>
  mov    0x8(%rsp),%r9
  movb   $0x0,0x6(%r9,%r13,1)
L4:
  mov    0x18(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L10
  add    $0x28,%rsp
  mov    %r9,%rax
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  nopl   0x0(%rax)
L45:
  lea    D5(%rip),%rdi
  call   <perror@plt>
  xor    %r9d,%r9d
  jmp    L4
L10:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: parse_data
arguments: tag: D5
data_type: string

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