user: 请将这段汇编代码片段转换成等效的C代码：

```asm
<wcsrtombs>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x18,%rsp
  mov    %rsi,%rbx
  mov    %fs:0x28,%rax
  mov    %rax,0x8(%rsp)
  xor    %eax,%eax
  test   %rdi,%rdi
  je     L257
  mov    %rdi,%r12
  mov    %rdx,%r14
  mov    %rdx,%rbp
  cmp    $0x3,%rdx
  ja     L251
L126:
  lea    0x4(%rsp),%r15
  test   %rbp,%rbp
  jne    L155
L83:
  mov    $0x0,%ebp
L227:
  movq   $0x0,(%rbx)
  sub    %rbp,%r14
  mov    %r14,%r13
L44:
  mov    0x8(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L124
  mov    %r13,%rax
  add    $0x18,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L257:
  mov    (%rsi),%rbx
  mov    (%rbx),%esi
  test   %esi,%esi
  je     L171
  mov    $0x0,%r13d
  lea    0x4(%rsp),%rbp
  jmp    L193
L196:
  mov    $0x0,%edx
  mov    %rbp,%rdi
  call   <wcrtomb@plt>
  cmp    $0xffffffffffffffff,%rax
  je     L121
  add    %rax,%r13
L49:
  add    $0x4,%rbx
  mov    (%rbx),%esi
  test   %esi,%esi
  je     L44
L193:
  cmp    $0x7f,%esi
  ja     L196
  add    $0x1,%r13
  jmp    L49
L136:
  mov    $0x0,%edx
  mov    %r12,%rdi
  call   <wcrtomb@plt>
  mov    %rax,%r13
  cmp    $0xffffffffffffffff,%rax
  je     L44
  add    %rax,%r12
  sub    %rax,%rbp
L59:
  addq   $0x4,(%rbx)
  cmp    $0x3,%rbp
  jbe    L126
L251:
  mov    (%rbx),%rax
  mov    (%rax),%esi
  test   %esi,%esi
  je     L126
  cmp    $0x7f,%esi
  ja     L136
  mov    %sil,(%r12)
  sub    $0x1,%rbp
  lea    0x1(%r12),%r12
  jmp    L59
L153:
  mov    $0x0,%edx
  mov    %r15,%rdi
  call   <wcrtomb@plt>
  mov    %rax,%r13
  cmp    $0xffffffffffffffff,%rax
  je     L44
  cmp    %rbp,%rax
  ja     L91
  mov    (%rbx),%rax
  mov    $0x0,%edx
  mov    (%rax),%esi
  mov    %r12,%rdi
  call   <wcrtomb@plt>
  add    %r13,%r12
  sub    %r13,%rbp
L48:
  addq   $0x4,(%rbx)
  test   %rbp,%rbp
  je     L83
L155:
  mov    (%rbx),%rax
  mov    (%rax),%esi
  test   %esi,%esi
  je     L242
  cmp    $0x7f,%esi
  ja     L153
  mov    %sil,(%r12)
  sub    $0x1,%rbp
  lea    0x1(%r12),%r12
  jmp    L48
L91:
  sub    %rbp,%r14
  mov    %r14,%r13
  jmp    L44
L242:
  test   %rbp,%rbp
  je     L227
  movb   $0x0,(%r12)
  jmp    L227
L171:
  mov    $0x0,%r13d
  jmp    L44
L121:
  mov    %rax,%r13
  jmp    L44
L124:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
size_t wcsrtombs(char *restrict s, const wchar_t **restrict ws, size_t n, mbstate_t *restrict st) {
  const wchar_t *ws2;
  char buf[4];
  size_t N = n, l;
  if (!s) {
    for (n = 0, ws2 = *ws; *ws2; ws2++) {
      if (*ws2 >= 0x80u) {
        l = wcrtomb(buf, *ws2, 0);
        if (!(l + 1))
          return -1;
        n += l;
      } else
        n++;
    }
    return n;
  }
  while (n >= 4 && **ws) {
    if (**ws >= 0x80u) {
      l = wcrtomb(s, **ws, 0);
      if (!(l + 1))
        return -1;
      s += l;
      n -= l;
    } else {
      *s++ = **ws;
      n--;
    }
    (*ws)++;
  }
  while (n && **ws) {
    if (**ws >= 0x80u) {
      l = wcrtomb(buf, **ws, 0);
      if (!(l + 1))
        return -1;
      if (l > n)
        return N - n;
      wcrtomb(s, **ws, 0);
      s += l;
      n -= l;
    } else {
      *s++ = **ws;
      n--;
    }
    (*ws)++;
  }
  if (n)
    *s = 0;
  *ws = 0;
  return N - n;
}
```