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

```asm
<pathsearch>:
  endbr64
  push   %r15
  push   %r14
  mov    %rdi,%r14
  push   %r13
  mov    %edx,%r13d
  push   %r12
  mov    %rsi,%r12
  mov    $0x2f,%esi
  push   %rbp
  push   %rbx
  sub    $0x18,%rsp
  call   <strrchr@plt>
  test   %rax,%rax
  je     L60
  movzbl (%rax),%r13d
  movb   $0x0,(%rax)
  mov    %r14,%rdi
  mov    %rax,%rbx
  call   <strlen@plt>
  mov    %r12,%rdi
  mov    %rax,%rbp
  call   <strlen@plt>
  lea    0x2(%rbp,%rax,1),%edi
  call   <malloc@plt>
  mov    %rax,%r15
  test   %rax,%rax
  jne    L106
L59:
  mov    %r13b,(%rbx)
L101:
  add    $0x18,%rsp
  mov    %r15,%rax
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  nopl   (%rax)
L106:
  mov    %r12,%rcx
  mov    %r14,%rdx
  lea    D19(%rip),%rsi
  mov    %rax,%rdi
  call   <sprintf@plt>
  jmp    L59
  nopw   0x0(%rax,%rax,1)
L60:
  lea    D26(%rip),%rdi
  call   <getenv@plt>
  mov    %rax,%rbp
  test   %rax,%rax
  je     L27
  mov    %rax,%rdi
  call   <strlen@plt>
  lea    0x1(%rax),%edi
L84:
  call   <malloc@plt>
  mov    %rbp,%rdi
  mov    %rax,0x8(%rsp)
  mov    %rax,%r14
  call   <strlen@plt>
  mov    %r12,%rdi
  mov    %rax,%rbx
  call   <strlen@plt>
  lea    0x2(%rbx,%rax,1),%edi
  call   <malloc@plt>
  mov    %rax,%r15
  test   %r14,%r14
  je     L101
  test   %rax,%rax
  je     L101
  mov    %rbp,%rsi
  mov    %r14,%rdi
  mov    %r14,%rbp
  call   <strcpy@plt>
  jmp    L15
  nopl   0x0(%rax)
L25:
  movb   $0x0,0x0(%rbp)
L107:
  mov    %r13d,%esi
  mov    %r15,%rdi
  call   <access@plt>
  test   %rax,%rax
  je     L102
L15:
  cmpb   $0x0,0x0(%rbp)
  je     L102
  mov    $0x3a,%esi
  mov    %rbp,%rdi
  call   <strchr@plt>
  mov    %rax,%rbx
  test   %rax,%rax
  je     L146
L99:
  movzbl (%rbx),%r14d
  movb   $0x0,(%rbx)
  mov    %r12,%rcx
  mov    %rbp,%rdx
  lea    D19(%rip),%rsi
  mov    %r15,%rdi
  call   <sprintf@plt>
  mov    %r14b,(%rbx)
  test   %r14b,%r14b
  je     L25
  lea    0x1(%rbx),%rbp
  jmp    L107
  nopl   0x0(%rax)
L146:
  mov    %rbp,%rdi
  call   <strlen@plt>
  movslq %eax,%rbx
  add    %rbp,%rbx
  jmp    L99
  nopl   0x0(%rax,%rax,1)
L102:
  mov    0x8(%rsp),%rdi
  call   <free@plt>
  jmp    L101
  nop
L27:
  mov    $0x10,%edi
  lea    D22(%rip),%rbp
  jmp    L84
```

tool_calls: name: parse_data
arguments: data_tag: D19
datatype: string
name: parse_data
arguments: data_tag: D26
datatype: string
name: parse_data
arguments: data_tag: D22
datatype: string

tool: D19: %s/%s

tool: D26: PATH

tool: D22: .:/bin:/usr/bin

assistant: ```c
char *pathsearch(char *argv0, char *name, int modemask) {
  const char *pathlist;
  char *pathbufptr;
  char *pathbuf;
  char *path, *cp;
  char c;
  cp = strrchr(argv0, '/');
  if (cp) {
    c = *cp;
    *cp = 0;
    path = (char *)malloc(((int)strlen(argv0)) + ((int)strlen(name)) + 2);
    if (path)
      sprintf(path, "%s/%s", argv0, name);
    *cp = c;
  } else {
    pathlist = getenv("PATH");
    if (pathlist == 0)
      pathlist = ".:/bin:/usr/bin";
    pathbuf = (char *)malloc(((int)strlen(pathlist)) + 1);
    path = (char *)malloc(((int)strlen(pathlist)) + ((int)strlen(name)) + 2);
    if ((pathbuf != 0) && (path != 0)) {
      pathbufptr = pathbuf;
      strcpy(pathbuf, pathlist);
      while (*pathbuf) {
        cp = strchr(pathbuf, ':');
        if (cp == 0)
          cp = &pathbuf[((int)strlen(pathbuf))];
        c = *cp;
        *cp = 0;
        sprintf(path, "%s/%s", pathbuf, name);
        *cp = c;
        if (c == 0)
          pathbuf[0] = 0;
        else
          pathbuf = &cp[1];
        if (access(path, modemask) == 0)
          break;
      }
      free(pathbufptr);
    }
  }
  return path;
}
```