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

```asm
<wma_utf16toutf8>:
  endbr64
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  mov    $0x0,%ebp
  test   %esi,%esi
  je     L12
  mov    %rdi,%r13
  mov    %esi,%r12d
  lea    0x1(%rsi,%rsi,1),%r14d
  mov    %r14d,%edi
  call   <malloc@plt>
  mov    %rax,%rbx
  test   %rax,%rax
  je     L12
  mov    %rax,%rbp
  mov    %r14d,%edx
  mov    $0x0,%esi
  mov    %rax,%rdi
  call   <memset@plt>
  movslq %r12d,%r12
  lea    0x0(%r13,%r12,1),%r11
  lea    0x2(%r13),%rax
  cmp    %rax,%r11
  jb     L12
  lea    0x4(%r13),%rdi
  sub    $0x2,%r12
  and    $0xfffffffffffffffe,%r12
  lea    0x6(%r13,%r12,1),%r10
  mov    %rbx,%rdx
  mov    $0x2,%r14d
  mov    $0x1,%r13d
  mov    $0x3,%r12d
  jmp    L16
L95:
  cmp    %rdi,%r11
  jb     L130
  movzbl 0x1(%rdi),%ecx
  shl    $0x8,%ecx
  movzbl (%rdi),%esi
  or     %ecx,%esi
  and    $0xfc00,%ecx
  cmp    $0xdc00,%ecx
  jne    L102
  shl    $0xa,%eax
  and    $0xffc00,%eax
  and    $0x3ff,%esi
  or     %eax,%esi
  add    $0x10000,%esi
  lea    0x1(%rdx),%r9
  mov    %esi,%eax
  shr    $0x12,%eax
  or     $0xfffffff0,%eax
  mov    %r12d,%r8d
L80:
  mov    %al,(%rdx)
  mov    %r9,%rdx
  lea    -0x3(%r8,%r8,2),%ecx
  add    %ecx,%ecx
L39:
  mov    %esi,%eax
  shr    %cl,%eax
  and    $0x3f,%eax
  or     $0xffffff80,%eax
  mov    %al,(%rdx)
  add    $0x1,%rdx
  sub    $0x6,%ecx
  cmp    $0xfffffffa,%ecx
  jne    L39
  mov    %r8d,%edx
  add    %r9,%rdx
L110:
  add    $0x2,%rdi
  cmp    %r10,%rdi
  je     L12
L16:
  movzbl -0x3(%rdi),%eax
  shl    $0x8,%eax
  movzbl -0x4(%rdi),%ecx
  or     %ecx,%eax
  mov    %eax,%esi
  mov    %eax,%ecx
  and    $0xfc00,%ecx
  cmp    $0xd800,%ecx
  je     L95
  cmp    $0x7f,%eax
  ja     L78
  mov    %al,(%rdx)
  lea    0x1(%rdx),%rdx
  jmp    L110
L130:
  lea    D6(%rip),%rdx
  mov    $0x8,%esi
  mov    $0x5,%edi
  call   <err_log@plt>
  mov    %rbx,%rdi
  call   <free@plt>
  mov    $0x0,%ebp
L12:
  mov    %rbp,%rax
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  ret
L102:
  lea    D6(%rip),%rdx
  mov    $0x8,%esi
  mov    $0x5,%edi
  call   <err_log@plt>
  mov    %rbx,%rdi
  call   <free@plt>
  mov    $0x0,%ebp
  jmp    L12
L78:
  cmp    $0x7ff,%eax
  ja     L106
  lea    0x1(%rdx),%r9
  shr    $0x6,%eax
  or     $0xffffffc0,%eax
  mov    %r13d,%r8d
  jmp    L80
L106:
  lea    0x1(%rdx),%r9
  shr    $0xc,%eax
  or     $0xffffffe0,%eax
  mov    %r14d,%r8d
  jmp    L80
```

tool_calls: name: retrieve_data
arguments: tag: D6
type_infered: string

tool: D6: Invalid utf-16 in file


assistant: ```c
char *wma_utf16toutf8(unsigned char *utf16, int len) {
  char *utf8;
  unsigned char *src = utf16;
  char *dst;
  unsigned int w1, w2;
  int bytes;
  if (!len)
    return NULL;
  utf8 = (char *)malloc(len * 2 + 1);
  if (!utf8)
    return NULL;
  memset(utf8, 0x0, len * 2 + 1);
  dst = utf8;
  while ((src + 2) <= utf16 + len) {
    w1 = src[1] << 8 | src[0];
    src += 2;
    if ((w1 & 0xFC00) == 0xD800) {
      if (src + 2 > utf16 + len) {
        err_log(5, 0x00000008, "Invalid utf-16 in file\n");
        free(utf8);
        return NULL;
      }
      w2 = src[3] << 8 | src[2];
      if ((w2 & 0xFC00) != 0xDC00) {
        err_log(5, 0x00000008, "Invalid utf-16 in file\n");
        free(utf8);
        return NULL;
      }
      w1 = w1 & 0x03FF;
      w1 = w1 << 10;
      w1 = w1 | (w2 & 0x03FF);
      w1 += 0x10000;
    }
    if (w1 < 0x80) {
      *dst++ = w1;
      bytes = 0;
    } else if (w1 < 0x800) {
      *dst++ = 0xC0 | (w1 >> 6);
      bytes = 1;
    } else if (w1 < 0x10000) {
      *dst++ = 0xE0 | (w1 >> 12);
      bytes = 2;
    } else {
      *dst++ = 0xF0 | (w1 >> 18);
      bytes = 3;
    }
    while (bytes) {
      *dst++ = 0x80 | ((w1 >> (6 * (bytes - 1))) & 0x3f);
      bytes--;
    }
  }
  return utf8;
}
```