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

```asm
<smtp_recv_mail_data>:
  endbr64
  push   %r15
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  push   %rbx
  sub    $0x38,%rsp
  mov    %edi,0x4(%rsp)
  mov    %rsi,0x18(%rsp)
  mov    %rdx,0x10(%rsp)
  mov    %fs:0x28,%rax
  mov    %rax,0x28(%rsp)
  xor    %eax,%eax
  mov    $0x200,%edi
  call   <malloc@plt>
  test   %rax,%rax
  je     L75
  mov    %rax,%r12
  mov    %rax,0x8(%rsp)
  mov    $0x200,%r14d
  mov    $0x0,%ebp
  mov    $0x0,%ebx
  lea    0x27(%rsp),%r15
  jmp    L204
L173:
  cmp    $0xa,%dl
  je     L211
  cmp    $0xd,%dl
  sete   %bl
  movzbl %bl,%ebx
L37:
  add    $0x1,%rbp
  cmp    %rbp,%r14
  je     L38
L103:
  mov    %r13,%r12
L204:
  mov    %r15,%rsi
  mov    0x4(%rsp),%edi
  call   <buf_read@plt>
  cmp    $0x1,%eax
  jne    L119
  lea    0x1(%r12),%r13
  movzbl 0x27(%rsp),%edx
  mov    %dl,(%r12)
  cmp    $0x4,%ebx
  je     L173
  cmp    $0x3,%ebx
  je     L68
  cmp    $0x2,%ebx
  je     L214
  cmp    $0x1,%ebx
  je     L114
  cmp    $0xd,%dl
  cmove  %eax,%ebx
  jmp    L37
L211:
  lea    -0x2(%rbp),%rax
  movb   $0x0,-0x2(%r12)
  mov    0x18(%rsp),%rcx
  mov    0x8(%rsp),%rsi
  mov    %rsi,(%rcx)
  cmpq   $0x0,0x10(%rsp)
  je     L60
  mov    0x10(%rsp),%rcx
  mov    %r14,(%rcx)
  jmp    L60
L68:
  cmp    $0xd,%dl
  sete   %bl
  movzbl %bl,%ebx
  shl    $0x2,%ebx
  jmp    L37
L214:
  mov    $0x3,%ebx
  cmp    $0x2e,%dl
  je     L37
  cmp    $0xd,%dl
  sete   %bl
  movzbl %bl,%ebx
  jmp    L37
L114:
  mov    $0x2,%ebx
  cmp    $0xa,%dl
  je     L37
  cmp    $0xd,%dl
  sete   %bl
  movzbl %bl,%ebx
  jmp    L37
L119:
  mov    0x18(%rsp),%rax
  movq   $0x0,(%rax)
  mov    0x10(%rsp),%rax
  movq   $0x0,(%rax)
  mov    0x8(%rsp),%rdi
  call   <free@plt>
  mov    $0xffffffff,%eax
L60:
  mov    0x28(%rsp),%rdx
  sub    %fs:0x28,%rdx
  jne    L150
  add    $0x38,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L38:
  add    %r14,%r14
  mov    %r14,%rdi
  call   <malloc@plt>
  mov    %rax,%r12
  test   %rax,%rax
  je     L1
  lea    (%rax,%rbp,1),%r13
  mov    %rbp,%rdx
  mov    0x8(%rsp),%rsi
  mov    %rax,%rdi
  call   <memcpy@plt>
  mov    0x8(%rsp),%rdi
  call   <free@plt>
  mov    %r12,0x8(%rsp)
  jmp    L103
L1:
  mov    0x8(%rsp),%rdi
  call   <free@plt>
  mov    $0xffffffff,%eax
  jmp    L60
L75:
  mov    $0xffffffff,%eax
  jmp    L60
L150:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
ssize_t smtp_recv_mail_data(int sockfd, char **buf_ptr, size_t *buf_size) {
  int rc, state;
  size_t n, buflen;
  char c, *ptr, *buf, *temp_buf;
  if (NULL == (buf = malloc(512 * sizeof(char))))
    return -1;
  state = 0;
  ptr = buf;
  buflen = 512;
  n = 0;
  for (;;) {
    if ((rc = buf_read(sockfd, &c)) == 1) {
      *ptr++ = c;
      if (4 == state) {
        if (c == '\n') {
          n -= 2;
          *(ptr - 3) = '\0';
          break;
        } else if (c == '\r')
          state = 1;
        else
          state = 0;
      } else if (3 == state) {
        if (c == '\r')
          state = 4;
        else if (c == '\r')
          state = 1;
        else
          state = 0;
      } else if (2 == state) {
        if (c == '.')
          state = 3;
        else if (c == '\r')
          state = 1;
        else
          state = 0;
      } else if (1 == state) {
        if (c == '\n')
          state = 2;
        else if (c == '\r')
          state = 1;
        else
          state = 0;
      } else if (c == '\r')
        state = 1;
    } else {
      *buf_ptr = NULL;
      *buf_size = 0;
      free(buf);
      if (rc == 1)
        return 0;
      else
        return -1;
    }
    ++n;
    if (n == buflen) {
      buflen *= 2;
      temp_buf = buf;
      if (NULL == (buf = malloc(buflen * sizeof(char)))) {
        free(temp_buf);
        return -1;
      }
      ptr = buf + n;
      memcpy(buf, temp_buf, n);
      free(temp_buf);
      temp_buf = NULL;
    }
  }
  *buf_ptr = buf;
  if (NULL != buf_size)
    *buf_size = buflen;
  return n;
}
```