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

```asm
<command_format>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x48,%rsp
  mov    %rdi,%r13
  mov    %esi,%r14d
  mov    %rdx,%r12
  mov    %rcx,0x8(%rsp)
  mov    %r8d,%edx
  mov    %fs:0x28,%rax
  mov    %rax,0x38(%rsp)
  xor    %eax,%eax
  lea    0x10(%rsp),%rdi
  lea    D0(%rip),%rsi
  call   <sprintf@plt>
  movzbl (%r12),%eax
  test   %al,%al
  je     L139
  mov    $0x0,%ebx
  lea    0x10(%rsp),%r15
  jmp    L24
L142:
  movzbl 0x1(%r12),%edx
  test   %dl,%dl
  je     L46
  lea    0x1(%r12),%rbp
  cmp    $0x48,%dl
  je     L67
  cmp    $0x50,%dl
  je     L60
  cmp    $0x25,%dl
  je     L218
  test   %r13,%r13
  setne  %dl
  cmp    %ebx,%r14d
  jle    L201
  test   %dl,%dl
  je     L201
  movslq %ebx,%rax
  movb   $0x25,0x0(%r13,%rax,1)
L201:
  lea    0x1(%rbx),%eax
  cmp    %eax,%r14d
  jle    L135
  test   %dl,%dl
  je     L135
  movzbl 0x1(%r12),%edx
  cltq
  mov    %dl,0x0(%r13,%rax,1)
L135:
  add    $0x2,%ebx
  jmp    L26
L67:
  mov    0x8(%rsp),%rdi
  call   <strlen@plt>
  mov    %eax,%r12d
  test   %r13,%r13
  je     L290
  cmp    %ebx,%r14d
  jg     L37
L290:
  add    %r12d,%ebx
  jmp    L26
L37:
  mov    %r14d,%eax
  sub    %ebx,%eax
  cmp    %r12d,%eax
  cmovg  %r12d,%eax
  mov    %eax,%edx
  movslq %ebx,%rdi
  add    %r13,%rdi
  mov    0x8(%rsp),%rsi
  call   <strncpy@plt>
  jmp    L290
L60:
  mov    %r15,%rdi
  call   <strlen@plt>
  mov    %eax,%r12d
  test   %r13,%r13
  je     L158
  cmp    %ebx,%r14d
  jg     L222
L158:
  add    %r12d,%ebx
  jmp    L26
L222:
  mov    %r14d,%eax
  sub    %ebx,%eax
  cmp    %r12d,%eax
  cmovg  %r12d,%eax
  mov    %eax,%edx
  movslq %ebx,%rdi
  add    %r13,%rdi
  mov    %r15,%rsi
  call   <strncpy@plt>
  jmp    L158
L218:
  test   %r13,%r13
  je     L220
  cmp    %ebx,%r14d
  jle    L220
  movslq %ebx,%rax
  movb   $0x25,0x0(%r13,%rax,1)
L220:
  add    $0x1,%ebx
  jmp    L26
L49:
  movslq %ebx,%rdx
  mov    %al,0x0(%r13,%rdx,1)
L14:
  add    $0x1,%ebx
  mov    %r12,%rbp
L26:
  lea    0x1(%rbp),%r12
  movzbl 0x1(%rbp),%eax
  test   %al,%al
  je     L119
L24:
  cmp    $0x25,%al
  je     L142
L46:
  test   %r13,%r13
  je     L14
  cmp    %ebx,%r14d
  jg     L49
  jmp    L14
L139:
  mov    $0x0,%ebx
L119:
  test   %r13,%r13
  je     L66
  cmp    %ebx,%r14d
  jle    L66
  movslq %ebx,%rax
  movb   $0x0,0x0(%r13,%rax,1)
L66:
  lea    0x1(%rbx),%eax
  mov    0x38(%rsp),%rdx
  sub    %fs:0x28,%rdx
  jne    L50
  add    $0x48,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L50:
  call   <__stack_chk_fail@plt>
```

tool_calls: name: read_data
arguments: tag: D0
type: string

tool: D0: %d

assistant: ```c
int command_format(char *buffer, int buflen, const char *format, const char *host, int port) {
  int len = 0;
  char portbuf[32];
  sprintf(portbuf, "%d", port);
  for (const char *f = format; *f; f++) {
    if (*f == '%' && f[1]) {
      switch (*++f) {
      case 'H':
        do {
          const char *scp = (host);
          int sl = strlen(scp);
          if (buffer && len < buflen) {
            int cp = buflen - len;
            if (sl < cp)
              cp = sl;
            strncpy(buffer + len, (scp), cp);
          }
          len += sl;
        } while (0);
        break;
      case 'P':
        do {
          const char *scp = (portbuf);
          int sl = strlen(scp);
          if (buffer && len < buflen) {
            int cp = buflen - len;
            if (sl < cp)
              cp = sl;
            strncpy(buffer + len, (scp), cp);
          }
          len += sl;
        } while (0);
        break;
      case '%':
        do {
          if (buffer && len < buflen) {
            buffer[len] = ('%');
          }
          len++;
        } while (0);
        break;
      default:
        do {
          if (buffer && len < buflen) {
            buffer[len] = ('%');
          }
          len++;
        } while (0);
        do {
          if (buffer && len < buflen) {
            buffer[len] = (*f);
          }
          len++;
        } while (0);
        break;
      }
    } else {
      do {
        if (buffer && len < buflen) {
          buffer[len] = (*f);
        }
        len++;
      } while (0);
    }
  }
  do {
    if (buffer && len < buflen) {
      buffer[len] = ('\0');
    }
    len++;
  } while (0);
  return len;
}
```