user: Converte this assembly snippet into its equivalent C code:

```asm
<Tcl_ConvertCountedElement>:
  endbr64
  push   %r12
  push   %rbp
  mov    %rdx,%rbp
  push   %rbx
  test   %rdi,%rdi
  je     L92
  mov    %rdi,%rbx
  mov    %ecx,%r12d
  cmp    $0xffffffff,%esi
  jne    L104
  call   <strlen@plt>
  mov    %eax,%esi
L104:
  test   %rbx,%rbx
  je     L92
  test   %esi,%esi
  je     L92
  mov    %r12d,%eax
  movslq %esi,%rsi
  and    $0x3,%eax
  lea    (%rbx,%rsi,1),%rdi
  cmp    $0x2,%eax
  je     L147
  cmpb   $0x7b,(%rbx)
  mov    %rbp,%rax
  je     L8
L253:
  and    $0x4,%r12d
  mov    $0x1,%r8d
  movabs $0x800001500000000,%rsi
  cmp    %rbx,%rdi
  jne    L188
L125:
  movb   $0x0,(%rax)
  sub    %ebp,%eax
  pop    %rbx
  pop    %rbp
  pop    %r12
  ret
  nopl   0x0(%rax)
L102:
  cmp    $0xa,%cl
  je     L138
  cmp    $0xb,%cl
  jne    L285
  mov    $0x765c,%edx
  add    $0x2,%rax
  mov    %dx,-0x2(%rax)
  nopl   0x0(%rax,%rax,1)
L36:
  add    $0x1,%rbx
  cmp    %rbx,%rdi
  je     L125
L188:
  movzbl (%rbx),%ecx
  cmp    $0x3b,%cl
  jg     L296
  cmp    $0xb,%cl
  jle    L102
  mov    %r8,%rdx
  shl    %cl,%rdx
  test   %rsi,%rdx
  jne    L5
  cmp    $0xd,%cl
  je     L51
  cmp    $0xc,%cl
  je     L39
L215:
  mov    %cl,(%rax)
  add    $0x1,%rax
  jmp    L36
  nopl   0x0(%rax,%rax,1)
L285:
  cmp    $0x9,%cl
  jne    L215
  mov    $0x745c,%ecx
  add    $0x2,%rax
  mov    %cx,-0x2(%rax)
  jmp    L36
  nopl   0x0(%rax)
L296:
  cmp    $0x7b,%cl
  je     L19
  jle    L307
  cmp    $0x7d,%cl
  jne    L215
L19:
  test   %r12d,%r12d
  je     L215
  movb   $0x5c,(%rax)
  add    $0x1,%rax
  movzbl (%rbx),%ecx
  jmp    L215
  nopl   (%rax)
L307:
  lea    -0x5b(%rcx),%edx
  cmp    $0x2,%dl
  ja     L215
L5:
  movb   $0x5c,(%rax)
  add    $0x1,%rax
  movzbl (%rbx),%ecx
  jmp    L215
  nopl   0x0(%rax)
L138:
  mov    $0x6e5c,%r10d
  add    $0x2,%rax
  mov    %r10w,-0x2(%rax)
  jmp    L36
  nopl   0x0(%rax)
L51:
  mov    $0x725c,%r9d
  add    $0x2,%rax
  mov    %r9w,-0x2(%rax)
  jmp    L36
  nopl   0x0(%rax)
L39:
  mov    $0x665c,%r11d
  add    $0x2,%rax
  mov    %r11w,-0x2(%rax)
  jmp    L36
  nopl   0x0(%rax)
L147:
  movb   $0x7b,0x0(%rbp)
  xor    %edx,%edx
  xchg   %ax,%ax
L33:
  movzbl (%rbx,%rdx,1),%eax
  mov    %al,0x1(%rbp,%rdx,1)
  mov    %rdx,%rax
  add    $0x1,%rdx
  cmp    %rdx,%rsi
  jne    L33
  lea    0x2(%rbp,%rax,1),%rax
  movb   $0x7d,(%rax)
  add    $0x1,%rax
  jmp    L125
  nopl   (%rax)
L92:
  mov    $0x7d7b,%eax
  pop    %rbx
  movb   $0x0,0x2(%rbp)
  mov    %ax,0x0(%rbp)
  mov    $0x2,%eax
  pop    %rbp
  pop    %r12
  ret
  nop
L8:
  mov    $0x7b5c,%eax
  add    $0x1,%rbx
  or     $0x4,%r12d
  mov    %ax,0x0(%rbp)
  lea    0x2(%rbp),%rax
  jmp    L253
```

assistant: ```c
int Tcl_ConvertCountedElement(src, length, dst, flags) const char *src;
int length;
char *dst;
int flags;
{
  char *p = dst;
  const char *lastChar;
  if (src && length == -1) {
    length = strlen(src);
  }
  if ((src == 0) || (length == 0)) {
    p[0] = '{';
    p[1] = '}';
    p[2] = 0;
    return 2;
  }
  lastChar = src + length;
  if ((flags & 2) && !(flags & 1)) {
    *p = '{';
    p++;
    for (; src != lastChar; src++, p++) {
      *p = *src;
    }
    *p = '}';
    p++;
  } else {
    if (*src == '{') {
      p[0] = '\\';
      p[1] = '{';
      p += 2;
      src++;
      flags |= 4;
    }
    for (; src != lastChar; src++) {
      switch (*src) {
      case ']':
      case '[':
      case '$':
      case ';':
      case ' ':
      case '\\':
      case '"':
        *p = '\\';
        p++;
        break;
      case '{':
      case '}':
        if (flags & 4) {
          *p = '\\';
          p++;
        }
        break;
      case '\f':
        *p = '\\';
        p++;
        *p = 'f';
        p++;
        continue;
      case '\n':
        *p = '\\';
        p++;
        *p = 'n';
        p++;
        continue;
      case '\r':
        *p = '\\';
        p++;
        *p = 'r';
        p++;
        continue;
      case '\t':
        *p = '\\';
        p++;
        *p = 't';
        p++;
        continue;
      case '\v':
        *p = '\\';
        p++;
        *p = 'v';
        p++;
        continue;
      }
      *p = *src;
      p++;
    }
  }
  *p = '\0';
  return p - dst;
}
```