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

```asm
<encodeUTF8str>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x38,%rsp
  mov    %rdi,%r13
  mov    %fs:0x28,%rax
  mov    %rax,0x28(%rsp)
  xor    %eax,%eax
  call   <strlen@plt>
  mov    %rax,0x18(%rsp)
  lea    0x1(%rax),%edi
  call   <malloc@plt>
  mov    %rax,%rbx
  mov    0x18(%rsp),%rax
  lea    0x1(%rax),%rdx
  mov    $0x0,%esi
  mov    %rbx,%rdi
  call   <memset@plt>
  movzbl 0x0(%r13),%edi
  test   %dil,%dil
  je     L16
  mov    %r13,%rax
  mov    $0x0,%r12d
  mov    $0x0,%ebp
  movabs $0x400000000002001,%r14
  jmp    L167
L58:
  movq   $0x0,0x20(%rsp)
  lea    0x20(%rsp),%rsi
  movsbl %dil,%edi
  call   <encodeControlChar@plt>
  mov    %rax,%r15
  lea    0x18(%rsp),%rsi
  mov    %r12,%r8
  mov    0x20(%rsp),%rcx
  mov    %rax,%rdx
  mov    %rbx,%rdi
  call   <insertSubString@plt>
  mov    %rax,%rbx
  mov    %r15,%rdi
  call   <free@plt>
  add    0x20(%rsp),%r12
  add    $0x1,%rbp
L1:
  lea    0x0(%r13,%rbp,1),%rax
  movzbl (%rax),%edi
  test   %dil,%dil
  je     L16
L167:
  mov    %edi,%ecx
  cmp    $0x1f,%dil
  jbe    L58
  lea    -0x22(%rdi),%edx
  cmp    $0x3a,%dl
  ja     L146
  bt     %rdx,%r14
  jb     L58
L35:
  mov    %dil,(%rbx,%r12,1)
  add    $0x1,%r12
  add    $0x1,%rbp
  jmp    L1
L146:
  test   %dil,%dil
  jns    L35
  movq   $0x0,0x20(%rsp)
  test   $0x40,%dil
  je     L179
  mov    %edi,%edx
  and    $0x60,%edx
  cmp    $0x40,%dl
  je     L174
  cmp    $0x60,%dl
  je     L124
L81:
  and    $0x78,%ecx
  cmp    $0x70,%cl
  sete   %r15b
  movzbl %r15b,%r15d
  shl    $0x2,%r15
  jne    L151
L90:
  lea    0x20(%rsp),%rdx
  mov    %r15,%rsi
  mov    %rax,%rdi
  call   <highCharToUnicode@plt>
  lea    0x18(%rsp),%rsi
  mov    %r12,%r8
  mov    0x20(%rsp),%rcx
  mov    %rax,0x8(%rsp)
  mov    %rax,%rdx
  mov    %rbx,%rdi
  call   <insertSubString@plt>
  mov    %rax,%rbx
  mov    0x8(%rsp),%rdi
  call   <free@plt>
  add    0x20(%rsp),%r12
  add    %r15,%rbp
  jmp    L1
L124:
  test   $0x10,%dil
  jne    L81
  mov    $0x3,%r15d
  jmp    L90
L179:
  mov    $0x1,%r15d
L151:
  add    %r15,%rbp
  jmp    L1
L174:
  mov    $0x2,%r15d
  jmp    L90
L16:
  mov    0x28(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L159
  mov    %rbx,%rax
  add    $0x38,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L159:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
char *encodeUTF8str(const char *str) {
  char *out = NULL;
  size_t len = strlen(str);
  size_t i = 0, o = 0;
  out = malloc(sizeof(char) * (len + 1));
  memset(out, 0, len + 1);
  while (str[i] != '\0') {
    unsigned char s = (unsigned char)str[i];
    if (s < 32 || str[i] == '"' || str[i] == '\\' || str[i] == '/') {
      size_t sublen = 0;
      char *substr = encodeControlChar(str[i], &sublen);
      out = insertSubString(out, &len, substr, sublen, o);
      free(substr);
      o += sublen;
      i++;
    } else if (s & 128) {
      char *substr = NULL;
      size_t sublen = 0;
      size_t ulen = 0;
      if ((s & 128) && !(s & 64)) {
        ulen = 1;
      } else if ((s & 128) && (s & 64) && !(s & 32)) {
        ulen = 2;
      } else if ((s & 128) && (s & 64) && (s & 32) && !(s & 16)) {
        ulen = 3;
      } else if ((s & 128) && (s & 64) && (s & 32) && (s & 16) && !(s & 8)) {
        ulen = 4;
      }
      if (ulen == 1 || ulen == 4) {
        i += ulen;
        continue;
      }
      substr = highCharToUnicode(&str[i], ulen, &sublen);
      out = insertSubString(out, &len, substr, sublen, o);
      free(substr);
      o += sublen;
      i += ulen;
    } else {
      out[o] = str[i];
      o++;
      i++;
    }
  }
  return out;
}
```