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

```asm
<variable_substitute>:
  endbr64
  push   %r15
  push   %r14
  mov    %rdi,%r14
  push   %r13
  push   %r12
  push   %rbp
  mov    %rsi,%rbp
  push   %rbx
  sub    $0x128,%rsp
  movzbl (%rdi),%edi
  mov    %fs:0x28,%rax
  mov    %rax,0x118(%rsp)
  xor    %eax,%eax
  test   %dil,%dil
  je     L233
  mov    %edx,%ebx
  cmp    $0x1,%edx
  jbe    L233
  lea    0x10(%rsp),%r13
  jmp    L23
  nopl   0x0(%rax)
L267:
  sub    $0x1,%ebx
  add    $0x1,%r14
  mov    %dil,0x0(%rbp)
  cmp    $0x1,%ebx
  seta   %r12b
  add    $0x1,%rbp
L154:
  movzbl (%r14),%edi
  test   %dil,%dil
  je     L233
L13:
  test   %r12b,%r12b
  je     L233
L23:
  cmp    $0x24,%dil
  jne    L267
  movsbl 0x1(%r14),%edi
  lea    0x1(%r14),%r12
  cmp    $0x24,%dil
  je     L175
  cmp    $0x7b,%dil
  je     L136
  mov    %r12,%r15
  test   %dil,%dil
  jne    L277
  jmp    L131
  nopl   0x0(%rax)
L71:
  mov    %rax,%r15
L277:
  call   <isalnum@plt>
  test   %rax,%rax
  jne    L268
  cmpb   $0x5f,(%r15)
  jne    L204
L268:
  movsbl 0x1(%r15),%edi
  lea    0x1(%r15),%rax
  test   %dil,%dil
  jne    L71
  sub    %r14,%r15
  mov    $0xff,%edx
  mov    %rax,%r14
  cmp    %rdx,%r15
  cmovle %r15,%rdx
L12:
  mov    %r12,%rsi
  mov    %r13,%rdi
  mov    %edx,0xc(%rsp)
  call   <strncpy@plt>
  mov    0xc(%rsp),%edx
  xor    %eax,%eax
  cmpb   $0x7d,(%r14)
  sete   %al
  mov    %r13,%rdi
  movb   $0x0,0x10(%rsp,%rdx,1)
  add    %rax,%r14
  call   <getenv@plt>
  mov    %rax,%r15
  test   %rax,%rax
  je     L138
  mov    %rax,%rdi
  call   <strlen@plt>
  mov    %eax,%edx
  cmp    %ebx,%eax
  jae    L29
  sub    %eax,%ebx
  cmp    $0x1,%ebx
  seta   %r12b
L230:
  mov    %rbp,%rdi
  mov    %r15,%rsi
  mov    %edx,0xc(%rsp)
  call   <strncpy@plt>
  mov    0xc(%rsp),%edx
  movzbl (%r14),%edi
  add    %rdx,%rbp
  test   %dil,%dil
  jne    L13
  nopl   (%rax)
L233:
  movb   $0x0,0x0(%rbp)
  mov    0x118(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L249
  add    $0x128,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  nopl   (%rax)
L175:
  mov    %r12,%r14
  jmp    L267
L138:
  cmp    $0x1,%ebx
  seta   %r12b
  jmp    L154
  nopl   0x0(%rax)
L29:
  lea    -0x1(%rbx),%edx
  xor    %r12d,%r12d
  mov    $0x1,%ebx
  jmp    L230
  nopl   (%rax)
L136:
  movzbl 0x2(%r14),%eax
  lea    0x2(%r14),%r12
  mov    %r12,%r14
  test   %al,%al
  je     L76
  cmp    $0x7d,%al
  jne    L289
  jmp    L76
  cs nopw 0x0(%rax,%rax,1)
L106:
  cmp    $0x7d,%al
  je     L166
L289:
  movzbl 0x1(%r14),%eax
  add    $0x1,%r14
  test   %al,%al
  jne    L106
L166:
  mov    %r14,%rdx
  mov    $0xff,%eax
  sub    %r12,%rdx
  cmp    %rax,%rdx
  cmovg  %rax,%rdx
  jmp    L12
  nopl   0x0(%rax,%rax,1)
L204:
  mov    %r15,%rdx
  mov    $0xff,%eax
  mov    %r15,%r14
  sub    %r12,%rdx
  cmp    %rax,%rdx
  cmovg  %rax,%rdx
  jmp    L12
L76:
  xor    %edx,%edx
  jmp    L12
L131:
  mov    %r12,%r14
  xor    %edx,%edx
  jmp    L12
L249:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
void variable_substitute(const char *s, char *dest, unsigned int n) {
  while (*s && n > 1) {
    if (*s == '$') {
      s++;
      if (*s != '$') {
        char buf[256];
        const char *a, *var;
        unsigned int len;
        if (*s == '{') {
          s++;
          a = s;
          while (*s && *s != '}')
            s++;
        } else {
          a = s;
          while (*s && (isalnum((int)*s) || *s == '_'))
            s++;
        }
        len = (s - a > 255) ? 255 : (s - a);
        strncpy(buf, a, len);
        buf[len] = '\0';
        if (*s == '}')
          s++;
        var = getenv(buf);
        if (var) {
          len = strlen(var);
          if (len >= n)
            len = n - 1;
          strncpy(dest, var, len);
          dest += len;
          n -= len;
        }
        continue;
      }
    }
    *dest++ = *s++;
    n--;
  }
  *dest = '\0';
}
```