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

```asm
<write_escaped_string>:
  endbr64
  push   %r15
  mov    %rsi,%r15
  push   %r14
  mov    %edx,%r14d
  push   %r13
  push   %r12
  push   %rbp
  mov    %rdi,%rbp
  mov    %rsi,%rdi
  push   %rbx
  sub    $0x18,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x8(%rsp)
  xor    %eax,%eax
  call   <strlen@plt>
  movslq %eax,%rbx
  add    %r15,%rbx
  cmp    %rbx,%r15
  jae    L120
  mov    $0x1,%r12d
  lea    0x4(%rsp),%r13
  jmp    L49
  nop
L149:
  cmp    $0x9,%edi
  je     L72
  cmp    $0xa,%edi
  je     L43
  cmp    $0xd,%edi
  je     L75
  cmp    $0xc,%edi
  je     L37
  lea    -0x21(%rdi),%eax
  cmp    $0x3b,%eax
  ja     L176
  movabs $0xf7ffffffedfffffa,%rcx
  bt     %rax,%rcx
  jb     L118
  mov    %rbp,%rsi
  mov    $0x5c,%edi
  call   <putc@plt>
  mov    0x4(%rsp),%edi
  mov    %rbp,%rsi
  call   <putc@plt>
L207:
  xor    %r12d,%r12d
  cmp    %r15,%rbx
  jbe    L120
L49:
  mov    %ebx,%edx
  mov    %r15,%rsi
  mov    %r13,%rdi
  sub    %r15d,%edx
  call   <u8_mbtouc@plt>
  mov    0x4(%rsp),%edi
  cltq
  add    %rax,%r15
  cmp    $0x20,%edi
  jne    L149
  or     %r14d,%r12d
  jne    L61
L176:
  lea    -0x20(%rdi),%eax
  cmp    $0x5e,%eax
  jbe    L118
  cmp    $0xffff,%edi
  ja     L168
  mov    %edi,%eax
  mov    %edi,%edx
  mov    %edi,%esi
  shr    $0xc,%edi
  shr    $0x4,%eax
  shr    $0x8,%edx
  lea    D48(%rip),%r10
  and    $0xf,%esi
  and    $0xf,%eax
  and    $0xf,%edx
  movsbl (%r10,%rsi,1),%r9d
  xor    %r12d,%r12d
  movsbl (%r10,%rdx,1),%ecx
  movsbl (%r10,%rax,1),%r8d
  lea    D54(%rip),%rsi
  movsbl (%r10,%rdi,1),%edx
  mov    %rbp,%rdi
  call   <fprintf@plt>
  cmp    %r15,%rbx
  ja     L49
  cs nopw 0x0(%rax,%rax,1)
L120:
  mov    0x8(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L186
  add    $0x18,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
  nopl   0x0(%rax,%rax,1)
L72:
  mov    %rbp,%rsi
  mov    $0x5c,%edi
  call   <putc@plt>
  mov    %rbp,%rsi
  mov    $0x74,%edi
  call   <putc@plt>
  jmp    L207
  nopw   0x0(%rax,%rax,1)
L43:
  mov    %rbp,%rsi
  mov    $0x5c,%edi
  call   <putc@plt>
  mov    %rbp,%rsi
  mov    $0x6e,%edi
  call   <putc@plt>
  jmp    L207
  nop
L75:
  mov    %rbp,%rsi
  mov    $0x5c,%edi
  call   <putc@plt>
  mov    %rbp,%rsi
  mov    $0x72,%edi
  call   <putc@plt>
  jmp    L207
  nop
L61:
  mov    %rbp,%rsi
  mov    $0x5c,%edi
  call   <putc@plt>
  mov    %rbp,%rsi
  mov    $0x20,%edi
  call   <putc@plt>
  jmp    L207
  nop
L37:
  mov    %rbp,%rsi
  mov    $0x5c,%edi
  call   <putc@plt>
  mov    %rbp,%rsi
  mov    $0x66,%edi
  call   <putc@plt>
  jmp    L207
  nop
L168:
  lea    -0x10000(%rdi),%edx
  and    $0x3ff,%edi
  shr    $0xa,%edx
  lea    0xdc00(%rdi),%r12d
  lea    D48(%rip),%rdi
  lea    0xd800(%rdx),%eax
  and    $0xf,%edx
  mov    %eax,%esi
  mov    %eax,%ecx
  shr    $0xc,%eax
  movsbl (%rdi,%rdx,1),%r9d
  shr    $0x4,%esi
  and    $0xf,%eax
  shr    $0x8,%ecx
  movsbl (%rdi,%rax,1),%r11d
  and    $0xf,%esi
  and    $0xf,%ecx
  movsbl (%rdi,%rsi,1),%r8d
  movsbl (%rdi,%rcx,1),%ecx
  lea    D54(%rip),%rsi
  mov    %rbp,%rdi
  mov    %r11d,%edx
  call   <fprintf@plt>
  mov    %r12d,%eax
  mov    %r12d,%edx
  shr    $0x8,%r12d
  shr    $0x4,%eax
  lea    D48(%rip),%rsi
  and    $0xf,%edx
  mov    %rbp,%rdi
  and    $0xf,%eax
  and    $0xf,%r12d
  movsbl (%rsi,%rdx,1),%r9d
  mov    $0x64,%edx
  movsbl (%rsi,%r12,1),%ecx
  movsbl (%rsi,%rax,1),%r8d
  lea    D54(%rip),%rsi
  call   <fprintf@plt>
  jmp    L207
  nopw   0x0(%rax,%rax,1)
L118:
  mov    %rbp,%rsi
  call   <putc@plt>
  jmp    L207
L186:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: parse_data
arguments: tag: D48
data_type: string
name: parse_data
arguments: tag: D54
data_type: string

tool: D48: 0123456789abcdef

tool: D54: \u%c%c%c%c

assistant: ```c
void write_escaped_string(FILE *fp, const char *str, bool in_key) {
  static const char hexdigit[] = "0123456789abcdef";
  const char *str_limit = str + strlen(str);
  bool first = true;
  while (str < str_limit) {
    unsigned int uc;
    str += u8_mbtouc(&uc, (const unsigned char *)str, str_limit - str);
    if (uc == 0x0020 && (first || in_key)) {
      putc('\\', fp);
      putc(' ', fp);
    } else if (uc == 0x0009) {
      putc('\\', fp);
      putc('t', fp);
    } else if (uc == 0x000a) {
      putc('\\', fp);
      putc('n', fp);
    } else if (uc == 0x000d) {
      putc('\\', fp);
      putc('r', fp);
    } else if (uc == 0x000c) {
      putc('\\', fp);
      putc('f', fp);
    } else if (uc == '\\' || uc == '#' || uc == '!' || uc == '=' || uc == ':') {
      putc('\\', fp);
      putc(uc, fp);
    } else if (uc >= 0x0020 && uc <= 0x007e) {
      putc(uc, fp);
    } else if (uc < 0x10000) {
      fprintf(fp, "\\u%c%c%c%c", hexdigit[(uc >> 12) & 0x0f], hexdigit[(uc >> 8) & 0x0f], hexdigit[(uc >> 4) & 0x0f], hexdigit[uc & 0x0f]);
    } else {
      unsigned int uc1 = 0xd800 + ((uc - 0x10000) >> 10);
      unsigned int uc2 = 0xdc00 + ((uc - 0x10000) & 0x3ff);
      fprintf(fp, "\\u%c%c%c%c", hexdigit[(uc1 >> 12) & 0x0f], hexdigit[(uc1 >> 8) & 0x0f], hexdigit[(uc1 >> 4) & 0x0f], hexdigit[uc1 & 0x0f]);
      fprintf(fp, "\\u%c%c%c%c", hexdigit[(uc2 >> 12) & 0x0f], hexdigit[(uc2 >> 8) & 0x0f], hexdigit[(uc2 >> 4) & 0x0f], hexdigit[uc2 & 0x0f]);
    }
    first = false;
  }
}
```