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

```asm
<fwrite_base32hex_nonquote>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x1000,%rsp
  orq    $0x0,(%rsp)
  sub    $0x1000,%rsp
  orq    $0x0,(%rsp)
  sub    $0x18,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x2008(%rsp)
  xor    %eax,%eax
  mov    %rdx,%r12
  mov    %rsi,%rax
  mov    %rdi,%rbx
  movabs $0xcccccccccccccccd,%rdx
  mov    %rsi,%r15
  mul    %rdx
  mov    %rsi,%rax
  shr    $0x2,%rdx
  lea    0x0(,%rdx,8),%r13d
  lea    (%rdx,%rdx,4),%rdx
  sub    %rdx,%rax
  mov    %r13d,%ebp
  cmp    $0x3,%rax
  je     L233
  cmp    $0x4,%rax
  je     L273
  cmp    $0x1,%rax
  je     L289
  cmp    $0x2,%rax
  jne    L206
  add    $0x4,%ebp
  add    $0x8,%r13d
L206:
  mov    %rsp,%r14
  cmp    $0x1fff,%r13d
  jg     L16
L99:
  lea    -0x1(%r15),%rcx
  test   %r15,%r15
  je     L26
  movzbl (%rbx),%edx
  mov    %r14,%rax
  jmp    L76
  nop
L165:
  movzbl 0x5(%rbx),%edx
  mov    %rsi,%rcx
  mov    %rdi,%rbx
L76:
  mov    %edx,%esi
  shl    $0x2,%edx
  shr    $0x3,%sil
  and    $0x1c,%edx
  mov    %sil,(%rax)
  mov    %dl,0x1(%rax)
  test   %rcx,%rcx
  je     L26
  movzbl 0x1(%rbx),%edx
  mov    %edx,%esi
  shr    $0x6,%sil
  or     %sil,0x1(%rax)
  mov    %edx,%esi
  shl    $0x4,%edx
  shr    %sil
  and    $0x10,%edx
  and    $0x1f,%esi
  mov    %dl,0x3(%rax)
  mov    %sil,0x2(%rax)
  cmp    $0x1,%rcx
  je     L26
  movzbl 0x2(%rbx),%edx
  mov    %edx,%esi
  add    %edx,%edx
  and    $0x1e,%edx
  shr    $0x4,%sil
  or     %sil,0x3(%rax)
  mov    %dl,0x4(%rax)
  cmp    $0x2,%rcx
  je     L26
  movzbl 0x3(%rbx),%edx
  mov    %edx,%esi
  shr    $0x7,%sil
  or     %sil,0x4(%rax)
  mov    %edx,%esi
  shl    $0x3,%edx
  shr    $0x2,%sil
  and    $0x18,%edx
  and    $0x1f,%esi
  mov    %dl,0x6(%rax)
  mov    %sil,0x5(%rax)
  cmp    $0x3,%rcx
  je     L26
  movzbl 0x4(%rbx),%edx
  lea    0x5(%rbx),%rdi
  mov    %edx,%esi
  and    $0x1f,%edx
  shr    $0x5,%sil
  mov    %dl,0x7(%rax)
  or     %sil,0x6(%rax)
  add    $0x8,%rax
  lea    -0x5(%rcx),%rsi
  cmp    $0x4,%rcx
  jne    L165
L26:
  test   %r13d,%r13d
  jle    L169
  test   %ebp,%ebp
  jle    L28
  cmp    %ebp,%r13d
  mov    %ebp,%r15d
  mov    %r14,%rbp
  cmovle %r13d,%r15d
  xor    %ebx,%ebx
  jmp    L86
  nopl   0x0(%rax)
L143:
  add    $0x30,%eax
  mov    %al,0x0(%rbp)
L81:
  add    $0x1,%ebx
  add    $0x1,%rbp
  cmp    %r15d,%ebx
  jge    L152
L86:
  movzbl 0x0(%rbp),%eax
  cmp    $0x9,%al
  jbe    L143
  cmp    $0x1f,%al
  ja     L218
  add    $0x57,%eax
  add    $0x1,%ebx
  add    $0x1,%rbp
  mov    %al,-0x1(%rbp)
  cmp    %r15d,%ebx
  jl     L86
  nopl   0x0(%rax)
L152:
  cmp    %r13d,%ebx
  jl     L115
  jmp    L256
  nop
L28:
  xor    %ebx,%ebx
L115:
  lea    -0x1(%r13),%edx
  mov    $0x1,%eax
  movslq %ebx,%rdi
  mov    $0x3d,%esi
  sub    %ebx,%edx
  add    $0x1,%rdx
  cmp    %ebx,%r13d
  cmovle %rax,%rdx
  add    %r14,%rdi
  call   <memset@plt>
L256:
  movslq %r13d,%rax
  mov    %r12,%rcx
  mov    $0x1,%edx
  mov    %r13d,%esi
  movb   $0x0,(%r14,%rax,1)
  mov    %r14,%rdi
  call   <fwrite@plt>
  cmp    $0x1fff,%r13d
  jle    L56
  mov    %r14,%rdi
  call   <free@plt>
L56:
  mov    0x2008(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L198
  add    $0x2018,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  nopl   (%rax)
L218:
  xor    %edi,%edi
  call   <assert@plt>
  jmp    L81
  nopl   0x0(%rax)
L16:
  lea    0x1(%r13),%edi
  call   <malloc@plt>
  mov    %rax,%r14
  jmp    L99
  nopl   0x0(%rax)
L273:
  add    $0x7,%ebp
  add    $0x8,%r13d
  jmp    L206
  nopl   0x0(%rax)
L233:
  add    $0x5,%ebp
  add    $0x8,%r13d
  jmp    L206
  nopl   0x0(%rax)
L289:
  add    $0x2,%ebp
  add    $0x8,%r13d
  jmp    L206
  nopl   0x0(%rax)
L169:
  movslq %r13d,%rax
  mov    %r12,%rcx
  mov    $0x1,%edx
  mov    %r13d,%esi
  movb   $0x0,(%r14,%rax,1)
  mov    %r14,%rdi
  call   <fwrite@plt>
  jmp    L56
L198:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
void fwrite_base32hex_nonquote(char *p, size_t len, FILE *fp) {
  int out_len = 8 * (len / 5);
  int bufsize, i;
  int byte = 0;
  unsigned char s[8193];
  unsigned char *buf, *start;
  switch (len % 5) {
  case 1:
    out_len += 2;
    break;
  case 2:
    out_len += 4;
    break;
  case 3:
    out_len += 5;
    break;
  case 4:
    out_len += 7;
    break;
  }
  bufsize = 8 * (len / 5) + (len % 5 == 0 ? 0 : 8);
  if (bufsize < 8192) {
    buf = s;
  } else {
    buf = malloc(bufsize + 1);
  }
  start = buf;
  while (len--) {
    unsigned char n = *p;
    switch (byte) {
    case 0:
      buf[0] = n >> 3;
      buf[1] = (n & 0x07) << 2;
      break;
    case 1:
      buf[1] |= (n >> 6) & 0x03;
      buf[2] = (n >> 1) & 0x1f;
      buf[3] = (n & 0x01) << 4;
      break;
    case 2:
      buf[3] |= (n >> 4) & 0x0f;
      buf[4] = (n & 0x0f) << 1;
      break;
    case 3:
      buf[4] |= (n >> 7) & 0x01;
      buf[5] = (n >> 2) & 0x1f;
      buf[6] = (n & 0x03) << 3;
      break;
    case 4:
      buf[6] |= (n >> 5) & 0x07;
      buf[7] = n & 0x1f;
      break;
    }
    p++;
    byte = (byte + 1) % 5;
    if (byte == 0) {
      buf += 8;
    }
  }
  buf = start;
  for (i = 0; i < bufsize; i++) {
    if (i < out_len) {
      if (buf[i] < 10) {
        buf[i] += '0';
      } else if (buf[i] < 32) {
        buf[i] += 'a' - 10;
      } else {
        assert(0);
      }
    } else {
      buf[i] = '=';
    }
  }
  buf[bufsize] = '\0';
  fwrite(buf, bufsize, 1, fp);
  if (bufsize >= 8192) {
    free(buf);
  }
}
```