user: What is the c source code of the assembly code below?

```asm
<utf8_to_latin1>:
  endbr64
  push   %r13
  push   %r12
  push   %rbp
  mov    %rsi,%rbp
  push   %rbx
  sub    $0x8,%rsp
  mov    (%rsi),%esi
  cmp    $0x2,%esi
  jle    L114
  mov    %rdi,%rbx
  sub    $0x3,%esi
  je     L181
  xor    %r13d,%r13d
  xor    %r12d,%r12d
  xor    %edx,%edx
  jmp    L167
  nopw   0x0(%rax,%rax,1)
L12:
  add    $0x1,%edx
  cmp    %esi,%edx
  jge    L199
L167:
  movslq %edx,%rcx
  cmpb   $0x0,(%rbx,%rcx,1)
  jns    L12
  cmpb   $0x0,0x1(%rbx,%rcx,1)
  lea    0x1(%rcx),%rax
  js     L8
  add    $0x1,%r12d
  nopl   0x0(%rax,%rax,1)
L196:
  movzbl (%rbx,%rax,1),%ecx
  mov    %eax,%edx
  add    $0x1,%rax
  test   %cl,%cl
  js     L196
  add    $0x1,%edx
  cmp    %esi,%edx
  jl     L167
  nopl   (%rax)
L199:
  xor    %eax,%eax
  mov    %r13d,%edx
  lea    D28(%rip),%rdi
  mov    %r12d,%esi
  call   <printf@plt>
  cmp    %r13d,%r12d
  jg     L114
L86:
  lea    D24(%rip),%rdi
  call   <puts@plt>
  mov    0x0(%rbp),%edi
  call   <malloc@plt>
  movslq 0x0(%rbp),%rcx
  mov    %rax,%r12
  mov    %rcx,%r8
  add    %rbx,%rcx
  cmp    %rcx,%rbx
  jae    L177
  mov    %rbx,%rdx
  mov    %rax,%rsi
  mov    $0x3f,%r9d
  jmp    L135
  nopl   (%rax)
L156:
  cmp    $0xe0,%al
  ja     L13
  cmp    %rdi,%rcx
  jbe    L149
  movzbl 0x1(%rdx),%ecx
  shl    $0x6,%eax
  and    $0x7c0,%eax
  and    $0x3f,%ecx
  or     %ecx,%eax
  cmp    $0x100,%eax
  cmovge %r9d,%eax
  add    $0x2,%rdx
L83:
  mov    %al,-0x1(%rsi)
  movslq 0x0(%rbp),%rcx
  mov    %rcx,%r8
  add    %rbx,%rcx
  cmp    %rcx,%rdx
  jae    L4
L135:
  movzbl (%rdx),%eax
  mov    %rsi,%r8
  lea    0x1(%rdx),%rdi
  add    $0x1,%rsi
  test   %al,%al
  js     L156
  mov    %rdi,%rdx
  jmp    L83
  nopl   0x0(%rax)
L13:
  mov    %rdi,%rdx
  mov    $0x3f,%eax
  jmp    L83
  nopw   0x0(%rax,%rax,1)
L8:
  add    $0x1,%r13d
  jmp    L196
  nopl   0x0(%rax)
L114:
  add    $0x8,%rsp
  xor    %r12d,%r12d
  pop    %rbx
  mov    %r12,%rax
  pop    %rbp
  pop    %r12
  pop    %r13
  ret
  nopl   0x0(%rax)
L4:
  mov    %esi,%r13d
  sub    %r12d,%r13d
L180:
  mov    %r8d,%esi
  lea    D7(%rip),%rdi
  xor    %eax,%eax
  call   <printf@plt>
  mov    %r13d,0x0(%rbp)
  mov    %r13d,%esi
  xor    %eax,%eax
  lea    D34(%rip),%rdi
  call   <printf@plt>
  add    $0x8,%rsp
  mov    %r12,%rax
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  ret
  nopw   0x0(%rax,%rax,1)
L149:
  mov    %esi,%r13d
  movb   $0x3f,(%r8)
  mov    0x0(%rbp),%r8d
  sub    %r12d,%r13d
  jmp    L180
L181:
  xor    %edx,%edx
  xor    %esi,%esi
  lea    D28(%rip),%rdi
  xor    %eax,%eax
  call   <printf@plt>
  jmp    L86
L177:
  xor    %r13d,%r13d
  jmp    L180
```

tool_calls: name: read_data
arguments: label: D28
type: string
name: read_data
arguments: label: D24
type: string
name: read_data
arguments: label: D7
type: string
name: read_data
arguments: label: D34
type: string

tool: D28: singlebyte %d multibyte %d


tool: D24: converting utf-8 -> latin1

tool: D7: previous length %d


tool: D34: new length %d


assistant: ```c
unsigned char *utf8_to_latin1(unsigned char *text, int *length) {
  int i;
  int singlebyte = 0;
  int multibyte = 0;
  if (*length < 3)
    return NULL;
  for (i = 0; i < *length - 3; i++) {
    if (text[i] >= 128) {
      if (text[i + 1] < 128)
        singlebyte++;
      else
        multibyte++;
      while (text[i] >= 128)
        i++;
    }
  }
  printf("singlebyte %d multibyte %d\n", singlebyte, multibyte);
  if (singlebyte > multibyte)
    return NULL;
  printf("converting utf-8 -> latin1\n");
  unsigned char *convtext = malloc(*length);
  unsigned char *cc = convtext;
  unsigned char *tt = text;
  int ch;
  while (tt < text + *length) {
    if (*tt < 128) {
      *cc++ = *tt++;
      continue;
    }
    if (*tt > 0xe0) {
      *cc++ = '?';
      tt++;
      continue;
    }
    if (tt + 1 >= text + *length) {
      *cc++ = '?';
      break;
    }
    ch = ((tt[0] & 0x1f) << 6) | (tt[1] & 0x3f);
    if (ch > 255) {
      *cc++ = '?';
    } else
      *cc++ = ch;
    tt += 2;
  }
  printf("previous length %d\n", *length);
  *length = cc - convtext;
  printf("new length %d\n", *length);
  return convtext;
}
```