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

```asm
<setFilenameFromEnvironment>:
  endbr64
  push   %r15
  lea    D4(%rip),%rdi
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x28,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x18(%rsp)
  xor    %eax,%eax
  call   <getenv@plt>
  test   %rax,%rax
  je     L222
  cmpb   $0x0,(%rax)
  mov    %rax,%rbp
  je     L222
  pxor   %xmm0,%xmm0
  xor    %ebx,%ebx
  xor    %r13d,%r13d
  xor    %esi,%esi
  movaps %xmm0,(%rsp)
  movzbl (%rax),%edx
  lea    D1(%rip),%r12
  test   %dl,%dl
  jne    L114
  jmp    L128
  nopl   (%rax)
L7:
  movslq %eax,%rdx
  mov    %eax,%ebx
  movzbl 0x0(%rbp,%rdx,1),%edx
  test   %dl,%dl
  je     L116
L114:
  lea    0x1(%rbx),%eax
  movslq %eax,%rcx
  movzbl 0x0(%rbp,%rcx,1),%ecx
  cmp    $0x25,%dl
  jne    L7
  lea    0x2(%rbx),%eax
  cmp    $0x70,%cl
  jne    L7
  test   %esi,%esi
  je     L42
  movslq %eax,%rdx
  add    $0x1,%esi
  mov    %eax,%ebx
  movzbl 0x0(%rbp,%rdx,1),%edx
  test   %dl,%dl
  jne    L114
  nopw   0x0(%rax,%rax,1)
L116:
  test   %esi,%esi
  je     L128
  lea    -0x2(%r13),%edx
  imul   %esi,%edx
  lea    0x1(%rdx,%rax,1),%edi
  call   <malloc@plt>
  mov    %rax,%r14
  test   %rax,%rax
  je     L222
  movzbl 0x0(%rbp),%eax
  test   %al,%al
  je     L176
  xor    %r12d,%r12d
  xor    %ebx,%ebx
  xor    %edx,%edx
  mov    %rsp,%r15
  jmp    L66
  nopl   0x0(%rax)
L177:
  mov    %al,(%rdi)
  add    $0x1,%r12d
  movslq %r12d,%rdx
L79:
  add    $0x1,%ebx
  movslq %ebx,%rax
  movzbl 0x0(%rbp,%rax,1),%eax
  test   %al,%al
  je     L64
L66:
  lea    (%r14,%rdx,1),%rdi
  cmp    $0x25,%al
  jne    L177
  add    $0x1,%ebx
  movslq %ebx,%rax
  cmpb   $0x70,0x0(%rbp,%rax,1)
  jne    L79
  mov    %r13d,%edx
  mov    %r15,%rsi
  add    $0x1,%ebx
  add    %r13d,%r12d
  call   <memcpy@plt>
  movslq %ebx,%rax
  movslq %r12d,%rdx
  movzbl 0x0(%rbp,%rax,1),%eax
  test   %al,%al
  jne    L66
  nopl   (%rax)
L64:
  add    %r14,%rdx
L203:
  movb   $0x0,(%rdx)
  mov    $0x1,%esi
  mov    %r14,%rdi
  call   <setFilename@plt>
  xor    %eax,%eax
L206:
  mov    0x18(%rsp),%rdx
  sub    %fs:0x28,%rdx
  jne    L103
  add    $0x28,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  nopl   0x0(%rax)
L42:
  xor    %eax,%eax
  call   <getpid@plt>
  mov    %rsp,%rdi
  mov    %r12,%rdx
  mov    $0x10,%esi
  mov    %eax,%ecx
  call   <snprintf@plt>
  mov    %eax,%r13d
  test   %eax,%eax
  jle    L222
  mov    $0x1,%esi
  lea    0x2(%rbx),%eax
  jmp    L7
  nopl   (%rax)
L128:
  xor    %esi,%esi
  mov    %rbp,%rdi
  call   <setFilename@plt>
  xor    %eax,%eax
  jmp    L206
L176:
  mov    %r14,%rdx
  jmp    L203
L222:
  mov    $0xffffffff,%eax
  jmp    L206
L103:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: retrieve_data
arguments: tag: D4
type_infered: string
name: retrieve_data
arguments: tag: D1
type_infered: string

tool: D4: LLVM_PROFILE_FILE

tool: D1: %d

assistant: ```c
int setFilenameFromEnvironment(void) {
  const char *Filename = getenv("LLVM_PROFILE_FILE");
  if (!Filename || !Filename[0])
    return -1;
  char PidChars[16] = {0};
  int NumPids = 0;
  int PidLength = 0;
  int I;
  for (I = 0; Filename[I]; ++I)
    if (Filename[I] == '%' && Filename[++I] == 'p')
      if (!NumPids++) {
        PidLength = snprintf(PidChars, 16, "%d", getpid());
        if (PidLength <= 0)
          return -1;
      }
  if (!NumPids) {
    setFilename(Filename, 0);
    return 0;
  }
  char *Allocated = (char *)malloc(I + NumPids * (PidLength - 2) + 1);
  if (!Allocated)
    return -1;
  int J;
  for (I = 0, J = 0; Filename[I]; ++I)
    if (Filename[I] == '%') {
      if (Filename[++I] == 'p') {
        memcpy(Allocated + J, PidChars, PidLength);
        J += PidLength;
      }
    } else
      Allocated[J++] = Filename[I];
  Allocated[J] = 0;
  setFilename(Allocated, 1);
  return 0;
}
```