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

```asm
<do_checksum>:
  endbr64
  push   %rbp
  mov    %rsp,%rbp
  sub    $0x50,%rsp
  mov    %rdi,-0x48(%rbp)
  mov    %esi,-0x4c(%rbp)
  mov    %edx,-0x50(%rbp)
  movq   $0x0,-0x30(%rbp)
  movq   $0x0,-0x28(%rbp)
  movl   $0x0,-0x3c(%rbp)
  mov    -0x48(%rbp),%rax
  movzbl (%rax),%eax
  sar    $0x4,%al
  movsbl %al,%eax
  mov    %eax,-0x34(%rbp)
  cmpl   $0x6,-0x34(%rbp)
  jne    L31
  mov    -0x48(%rbp),%rax
  mov    %rax,-0x28(%rbp)
  mov    -0x28(%rbp),%rax
  mov    0x4(%rax),%eax
  mov    %eax,-0x38(%rbp)
  movl   $0x8,-0x40(%rbp)
  jmp    L65
L59:
  cmpl   $0x85,-0x38(%rbp)
  je     L85
  cmpl   $0x86,-0x38(%rbp)
  je     L149
  jmp    L239
L85:
  mov    -0x40(%rbp),%eax
  movslq %eax,%rdx
  mov    -0x48(%rbp),%rax
  add    %rdx,%rax
  mov    (%rax),%eax
  mov    %eax,-0x38(%rbp)
  mov    -0x40(%rbp),%eax
  add    $0x4,%eax
  mov    %eax,-0x40(%rbp)
  jmp    L65
L149:
  mov    -0x40(%rbp),%eax
  movslq %eax,%rdx
  mov    -0x48(%rbp),%rax
  add    %rdx,%rax
  mov    (%rax),%eax
  mov    %eax,-0x38(%rbp)
  mov    -0x40(%rbp),%eax
  movslq %eax,%rdx
  mov    -0x48(%rbp),%rax
  add    %rdx,%rax
  mov    0x4(%rax),%eax
  add    $0x1,%eax
  shl    $0x3,%eax
  add    %eax,-0x40(%rbp)
  jmp    L65
L239:
  mov    $0x0,%eax
  jmp    L24
L65:
  mov    -0x38(%rbp),%eax
  cmp    -0x4c(%rbp),%eax
  jne    L59
  jmp    L252
L31:
  cmpl   $0x4,-0x34(%rbp)
  jne    L166
  mov    -0x48(%rbp),%rax
  mov    %rax,-0x30(%rbp)
  mov    -0x30(%rbp),%rax
  mov    (%rax),%eax
  shl    $0x2,%eax
  mov    %eax,-0x40(%rbp)
  jmp    L252
L166:
  mov    $0x0,%eax
  jmp    L24
L252:
  cmpl   $0x0,-0x40(%rbp)
  jns    L150
  mov    $0xfffffffe,%eax
  jmp    L24
L150:
  mov    -0x4c(%rbp),%eax
  add    $0xffffff80,%eax
  cmp    $0x4,%eax
  ja     L7
  mov    %eax,%eax
  lea    0x0(,%rax,4),%rdx
  lea    D18(%rip),%rax
  mov    (%rdx,%rax,1),%eax
  cltq
  lea    D18(%rip),%rdx
  add    %rdx,%rax
  notrack jmp *%rax
  mov    -0x40(%rbp),%eax
  movslq %eax,%rdx
  mov    -0x48(%rbp),%rax
  add    %rdx,%rax
  mov    %rax,-0x8(%rbp)
  mov    -0x8(%rbp),%rax
  movl   $0x0,(%rax)
  cmpl   $0x6,-0x34(%rbp)
  jne    L89
  mov    -0x28(%rbp),%rax
  mov    $0x20,%esi
  mov    %rax,%rdi
  call   <in_cksum@plt>
  mov    %eax,-0x3c(%rbp)
  jmp    L13
L89:
  mov    -0x30(%rbp),%rax
  add    $0x8,%rax
  mov    $0x8,%esi
  mov    %rax,%rdi
  call   <in_cksum@plt>
  mov    %eax,-0x3c(%rbp)
L13:
  mov    -0x50(%rbp),%eax
  sub    $0xffffff80,%eax
  mov    %eax,%edi
  call   <ntohs@plt>
  add    %eax,-0x3c(%rbp)
  mov    -0x50(%rbp),%edx
  mov    -0x8(%rbp),%rax
  mov    %edx,%esi
  mov    %rax,%rdi
  call   <in_cksum@plt>
  add    %eax,-0x3c(%rbp)
  mov    -0x3c(%rbp),%eax
  sar    $0x10,%eax
  mov    %eax,%edx
  mov    -0x3c(%rbp),%eax
  movzwl %ax,%eax
  add    %edx,%eax
  mov    %eax,-0x3c(%rbp)
  mov    -0x3c(%rbp),%eax
  sar    $0x10,%eax
  mov    %eax,%edx
  mov    -0x3c(%rbp),%eax
  add    %edx,%eax
  not    %eax
  movzwl %ax,%edx
  mov    -0x8(%rbp),%rax
  mov    %edx,(%rax)
  jmp    L159
  mov    -0x40(%rbp),%eax
  movslq %eax,%rdx
  mov    -0x48(%rbp),%rax
  add    %rdx,%rax
  mov    %rax,-0x10(%rbp)
  mov    -0x10(%rbp),%rax
  movl   $0x0,(%rax)
  cmpl   $0x6,-0x34(%rbp)
  jne    L208
  mov    -0x28(%rbp),%rax
  mov    $0x20,%esi
  mov    %rax,%rdi
  call   <in_cksum@plt>
  mov    %eax,-0x3c(%rbp)
  jmp    L33
L208:
  mov    -0x30(%rbp),%rax
  add    $0x8,%rax
  mov    $0x8,%esi
  mov    %rax,%rdi
  call   <in_cksum@plt>
  mov    %eax,-0x3c(%rbp)
L33:
  mov    -0x50(%rbp),%eax
  add    $0x81,%eax
  mov    %eax,%edi
  call   <ntohs@plt>
  add    %eax,-0x3c(%rbp)
  mov    -0x50(%rbp),%edx
  mov    -0x10(%rbp),%rax
  mov    %edx,%esi
  mov    %rax,%rdi
  call   <in_cksum@plt>
  add    %eax,-0x3c(%rbp)
  mov    -0x3c(%rbp),%eax
  sar    $0x10,%eax
  mov    %eax,%edx
  mov    -0x3c(%rbp),%eax
  movzwl %ax,%eax
  add    %edx,%eax
  mov    %eax,-0x3c(%rbp)
  mov    -0x3c(%rbp),%eax
  sar    $0x10,%eax
  mov    %eax,%edx
  mov    -0x3c(%rbp),%eax
  add    %edx,%eax
  not    %eax
  movzwl %ax,%edx
  mov    -0x10(%rbp),%rax
  mov    %edx,(%rax)
  jmp    L159
  mov    -0x40(%rbp),%eax
  movslq %eax,%rdx
  mov    -0x48(%rbp),%rax
  add    %rdx,%rax
  mov    %rax,-0x20(%rbp)
  mov    -0x20(%rbp),%rax
  movl   $0x0,(%rax)
  mov    -0x50(%rbp),%edx
  mov    -0x20(%rbp),%rax
  mov    %edx,%esi
  mov    %rax,%rdi
  call   <in_cksum@plt>
  mov    %eax,-0x3c(%rbp)
  mov    -0x3c(%rbp),%eax
  sar    $0x10,%eax
  mov    %eax,%edx
  mov    -0x3c(%rbp),%eax
  movzwl %ax,%eax
  add    %edx,%eax
  mov    %eax,-0x3c(%rbp)
  mov    -0x3c(%rbp),%eax
  sar    $0x10,%eax
  mov    %eax,%edx
  mov    -0x3c(%rbp),%eax
  add    %edx,%eax
  not    %eax
  movzwl %ax,%edx
  mov    -0x20(%rbp),%rax
  mov    %edx,(%rax)
  jmp    L159
  mov    -0x40(%rbp),%eax
  movslq %eax,%rdx
  mov    -0x48(%rbp),%rax
  add    %rdx,%rax
  mov    %rax,-0x18(%rbp)
  cmpl   $0x6,-0x34(%rbp)
  jne    L145
  mov    -0x28(%rbp),%rax
  mov    $0x20,%esi
  mov    %rax,%rdi
  call   <in_cksum@plt>
  mov    %eax,-0x3c(%rbp)
  mov    -0x50(%rbp),%eax
  add    $0x83,%eax
  mov    %eax,%edi
  call   <ntohs@plt>
  add    %eax,-0x3c(%rbp)
  mov    -0x18(%rbp),%rax
  movl   $0x0,(%rax)
  mov    -0x50(%rbp),%edx
  mov    -0x18(%rbp),%rax
  mov    %edx,%esi
  mov    %rax,%rdi
  call   <in_cksum@plt>
  add    %eax,-0x3c(%rbp)
  mov    -0x3c(%rbp),%eax
  sar    $0x10,%eax
  mov    %eax,%edx
  mov    -0x3c(%rbp),%eax
  movzwl %ax,%eax
  add    %edx,%eax
  mov    %eax,-0x3c(%rbp)
  mov    -0x3c(%rbp),%eax
  sar    $0x10,%eax
  mov    %eax,%edx
  mov    -0x3c(%rbp),%eax
  add    %edx,%eax
  not    %eax
  movzwl %ax,%edx
  mov    -0x18(%rbp),%rax
  mov    %edx,(%rax)
  jmp    L159
L145:
  mov    $0x0,%eax
  jmp    L24
  mov    -0x30(%rbp),%rax
  movl   $0x0,0x4(%rax)
  mov    -0x50(%rbp),%edx
  mov    -0x30(%rbp),%rax
  mov    %edx,%esi
  mov    %rax,%rdi
  call   <in_cksum@plt>
  mov    %eax,-0x3c(%rbp)
  mov    -0x3c(%rbp),%eax
  sar    $0x10,%eax
  mov    %eax,%edx
  mov    -0x3c(%rbp),%eax
  movzwl %ax,%eax
  add    %edx,%eax
  mov    %eax,-0x3c(%rbp)
  mov    -0x3c(%rbp),%eax
  sar    $0x10,%eax
  mov    %eax,%edx
  mov    -0x3c(%rbp),%eax
  add    %edx,%eax
  not    %eax
  movzwl %ax,%edx
  mov    -0x30(%rbp),%rax
  mov    %edx,0x4(%rax)
  jmp    L159
L7:
  mov    $0xffffffff,%eax
  jmp    L24
L159:
  mov    $0x1,%eax
L24:
  leave
  ret
```

assistant: ```c
int do_checksum(char *buf, int protocol, int len) {
  struct ip *iph_p = NULL;
  struct ip6_hdr *ip6h_p = NULL;
  int ip_hl;
  int sum = 0;
  int ip_version = buf[0] >> 4;
  if (ip_version == 6) {
    int next_header;
    ip6h_p = (struct ip6_hdr *)buf;
    next_header = ip6h_p->ip6_ctlun.ip6_un1.ip6_un1_nxt;
    ip_hl = sizeof(struct ip6_hdr);
    while (next_header != protocol) {
      switch (next_header) {
      case IPPROTO_FRAGMENT:
        next_header = ((struct ip6_frag *)(buf + ip_hl))->ip6f_nxt;
        ip_hl += sizeof(struct ip6_frag);
        break;
      case IPPROTO_DSTOPTS:
        next_header = ((struct ip6_dest *)(buf + ip_hl))->ip6d_nxt;
        ip_hl += (((struct ip6_dest *)(buf + ip_hl))->ip6d_len * 8) + 8;
        break;
      default:
        return 0;
      }
    }
  } else if (ip_version == 4) {
    iph_p = (struct ip *)buf;
    ip_hl = iph_p->ip_hl << 2;
  } else {
    return (0);
  }
  if (ip_hl < 0)
    return -2;
  switch (protocol) {
  case IPPROTO_UDP: {
    struct udphdr *udph_p = (struct udphdr *)(buf + ip_hl);
    udph_p->uh_sum = 0;
    if (ip_version == 6) {
      sum = in_cksum((unsigned short *)&ip6h_p->ip6_src, 32);
    } else {
      sum = in_cksum((void *)&iph_p->ip_src, 8);
    }
    sum += ntohs(IPPROTO_UDP + len);
    sum += in_cksum((unsigned short *)udph_p, len);
    udph_p->uh_sum = (sum = (sum >> 16) + (sum & 0xffff), (~(sum + (sum >> 16)) & 0xffff));
    break;
  }
  case IPPROTO_TCP: {
    struct tcphdr *tcph_p = (struct tcphdr *)(buf + ip_hl);
    tcph_p->th_sum = 0;
    if (ip_version == 6) {
      sum = in_cksum((unsigned short *)&ip6h_p->ip6_src, 32);
    } else {
      sum = in_cksum((void *)&iph_p->ip_src, 8);
    }
    sum += ntohs(IPPROTO_TCP + len);
    sum += in_cksum((unsigned short *)tcph_p, len);
    tcph_p->th_sum = (sum = (sum >> 16) + (sum & 0xffff), (~(sum + (sum >> 16)) & 0xffff));
    break;
  }
  case IPPROTO_ICMP: {
    struct icmp *icmph_p = (struct icmp *)(buf + ip_hl);
    icmph_p->icmp_cksum = 0;
    sum = in_cksum((unsigned short *)icmph_p, len);
    icmph_p->icmp_cksum = (sum = (sum >> 16) + (sum & 0xffff), (~(sum + (sum >> 16)) & 0xffff));
    break;
  }
  case IPPROTO_ICMPV6: {
    struct icmp6_hdr *icmp6h_p = (struct icmp6_hdr *)(buf + ip_hl);
    if (ip_version == 6) {
      sum = in_cksum((unsigned short *)&ip6h_p->ip6_src, 32);
    } else {
      return 0;
    }
    sum += ntohs(IPPROTO_ICMPV6 + len);
    icmp6h_p->icmp6_cksum = 0;
    sum += in_cksum((unsigned short *)icmp6h_p, len);
    icmp6h_p->icmp6_cksum = (sum = (sum >> 16) + (sum & 0xffff), (~(sum + (sum >> 16)) & 0xffff));
    break;
  }
  case IPPROTO_IP: {
    iph_p->ip_sum = 0;
    sum = in_cksum((unsigned short *)iph_p, len);
    iph_p->ip_sum = (sum = (sum >> 16) + (sum & 0xffff), (~(sum + (sum >> 16)) & 0xffff));
    break;
  }
  default: {
    return (-1);
  }
  }
  return (1);
}
```