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

```asm
<to_565_raw_dither>:
  endbr64
  push   %r15
  mov    $0x1,%esi
  push   %r14
  push   %r13
  push   %r12
  push   %rbp
  lea    0x6(%rdi,%rdi,2),%ebp
  push   %rbx
  movslq %ebp,%rbp
  mov    %edi,%ebx
  shl    $0x2,%rbp
  mov    %rbp,%rdi
  sub    $0x48,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x38(%rsp)
  xor    %eax,%eax
  lea    0x32(%rsp),%r12
  call   <calloc@plt>
  mov    $0x1,%esi
  mov    %rbp,%rdi
  mov    %rax,%r13
  call   <calloc@plt>
  lea    0xc(%r13),%rsi
  add    $0xc,%rax
  mov    %rsi,0x10(%rsp)
  mov    %rax,0x18(%rsp)
  lea    0x3(%rbx,%rbx,2),%eax
  cltq
  shl    $0x2,%rax
  mov    %rax,0x28(%rsp)
  lea    -0x1(%rbx),%eax
  lea    0x35(%rsp),%rbx
  lea    0x3(%rax,%rax,2),%r15
  lea    0x0(,%r15,4),%rax
  mov    %rax,0x20(%rsp)
L47:
  mov    0x18(%rsp),%rsi
  mov    0x20(%rsp),%rax
  mov    0x10(%rsp),%r15
  add    %rsi,%rax
  mov    %rsi,%r14
  mov    %rax,0x8(%rsp)
  jmp    L45
  nopl   0x0(%rax,%rax,1)
L36:
  movzbl 0x35(%rsp),%r8d
  mov    $0xff,%eax
  add    (%r15),%r8d
  mov    $0x1,%edi
  movzbl 0x36(%rsp),%ecx
  movzbl 0x37(%rsp),%r13d
  mov    %r8d,0x4(%rsp)
  add    0x4(%r15),%ecx
  add    0x8(%r15),%r13d
  cmp    %eax,%r8d
  cmovle %r8d,%eax
  xor    %edx,%edx
  mov    %ecx,%ebp
  test   %eax,%eax
  cmovs  %edx,%eax
  mov    $0xff,%edx
  sar    $0x3,%eax
  shl    $0xb,%eax
  cmp    %edx,%ecx
  cmovle %ecx,%edx
  xor    %esi,%esi
  test   %edx,%edx
  cmovs  %esi,%edx
  sar    $0x2,%edx
  shl    $0x5,%edx
  or     %edx,%eax
  mov    $0xff,%edx
  cmp    %edx,%r13d
  cmovle %r13d,%edx
  test   %edx,%edx
  cmovs  %esi,%edx
  mov    %r12,%rsi
  sar    $0x3,%edx
  or     %edx,%eax
  mov    $0x2,%edx
  mov    %ax,0x32(%rsp)
  xor    %eax,%eax
  call   <write@plt>
  movzwl 0x32(%rsp),%esi
  mov    0x4(%rsp),%r8d
  mov    %esi,%edx
  shr    $0xb,%dx
  movzwl %dx,%edx
  mov    %edx,%eax
  shl    $0x8,%eax
  sub    %edx,%eax
  movslq %eax,%rdx
  imul   $0xffffffff84210843,%rdx,%rdx
  shr    $0x20,%rdx
  add    %eax,%edx
  sar    $0x1f,%eax
  sar    $0x4,%edx
  sub    %edx,%eax
  add    %r8d,%eax
  lea    (%rax,%rax,2),%edi
  lea    (%rax,%rax,4),%r8d
  test   %edi,%edi
  lea    0xf(%rdi),%edx
  cmovns %edi,%edx
  lea    0xf(%r8),%edi
  sar    $0x4,%edx
  add    %edx,-0xc(%r14)
  test   %r8d,%r8d
  cmovns %r8d,%edi
  lea    0xf(%rax),%r8d
  sar    $0x4,%edi
  add    %edi,(%r14)
  test   %eax,%eax
  cmovns %eax,%r8d
  sar    $0x4,%r8d
  add    %r8d,0xc(%r14)
  add    %r8d,%edx
  add    %edi,%edx
  sub    %edx,%eax
  mov    %esi,%edx
  add    %eax,0xc(%r15)
  shr    $0x5,%dx
  and    $0x3f,%edx
  mov    %edx,%eax
  shl    $0x8,%eax
  sub    %edx,%eax
  movslq %eax,%rdx
  imul   $0xffffffff82082083,%rdx,%rdx
  shr    $0x20,%rdx
  add    %eax,%edx
  sar    $0x1f,%eax
  sar    $0x5,%edx
  sub    %edx,%eax
  add    %ebp,%eax
  lea    (%rax,%rax,2),%ecx
  lea    (%rax,%rax,4),%edi
  test   %ecx,%ecx
  lea    0xf(%rcx),%edx
  cmovns %ecx,%edx
  lea    0xf(%rdi),%ecx
  sar    $0x4,%edx
  add    %edx,-0x8(%r14)
  test   %edi,%edi
  cmovns %edi,%ecx
  lea    0xf(%rax),%edi
  sar    $0x4,%ecx
  add    %ecx,0x4(%r14)
  test   %eax,%eax
  cmovns %eax,%edi
  and    $0x1f,%esi
  sar    $0x4,%edi
  add    %edi,0x10(%r14)
  add    %edi,%edx
  add    %ecx,%edx
  sub    %edx,%eax
  add    %eax,0x10(%r15)
  mov    %esi,%eax
  shl    $0x8,%eax
  sub    %esi,%eax
  movslq %eax,%rdx
  imul   $0xffffffff84210843,%rdx,%rdx
  shr    $0x20,%rdx
  add    %eax,%edx
  sar    $0x1f,%eax
  sar    $0x4,%edx
  sub    %edx,%eax
  add    %r13d,%eax
  lea    (%rax,%rax,2),%ecx
  lea    (%rax,%rax,4),%esi
  test   %ecx,%ecx
  lea    0xf(%rcx),%edx
  cmovns %ecx,%edx
  lea    0xf(%rsi),%ecx
  sar    $0x4,%edx
  add    %edx,-0x4(%r14)
  test   %esi,%esi
  cmovns %esi,%ecx
  lea    0xf(%rax),%esi
  sar    $0x4,%ecx
  add    %ecx,0x8(%r14)
  test   %eax,%eax
  cmovns %eax,%esi
  sar    $0x4,%esi
  add    %esi,0x14(%r14)
  add    $0xc,%r14
  add    %esi,%edx
  add    %ecx,%edx
  sub    %edx,%eax
  add    %eax,0x14(%r15)
  add    $0xc,%r15
  cmp    0x8(%rsp),%r14
  je     L40
L45:
  xor    %edi,%edi
  xor    %eax,%eax
  mov    $0x3,%edx
  mov    %rbx,%rsi
  call   <read@plt>
  cmp    $0x3,%eax
  je     L36
  mov    0x10(%rsp),%rdi
  sub    $0xc,%rdi
  call   <free@plt>
  mov    0x38(%rsp),%rax
  sub    %fs:0x28,%rax
  jne    L10
  mov    0x18(%rsp),%rdi
  add    $0x48,%rsp
  pop    %rbx
  pop    %rbp
  sub    $0xc,%rdi
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  jmp    10a0 <free@plt>
  nopl   0x0(%rax)
L40:
  mov    0x10(%rsp),%rcx
  mov    0x28(%rsp),%rdx
  xor    %esi,%esi
  mov    %rcx,%rdi
  call   <memset@plt>
  mov    %rax,%rcx
  mov    0x18(%rsp),%rax
  mov    %rcx,0x18(%rsp)
  mov    %rax,0x10(%rsp)
  jmp    L47
L10:
  call   <__stack_chk_fail@plt>
```

assistant: ```c
void to_565_raw_dither(int width) {
  unsigned char in[3];
  unsigned short out;
  int i = 0;
  int e;
  int *error = malloc((width + 2) * 3 * sizeof(int));
  int *next_error = malloc((width + 2) * 3 * sizeof(int));
  memset(error, 0, (width + 2) * 3 * sizeof(int));
  memset(next_error, 0, (width + 2) * 3 * sizeof(int));
  error += 3;
  next_error += 3;
  while (read(0, in, 3) == 3) {
    int r = in[0] + error[i * 3 + 0];
    int rb = (r < 0) ? 0 : ((r > 255) ? 255 : r);
    int g = in[1] + error[i * 3 + 1];
    int gb = (g < 0) ? 0 : ((g > 255) ? 255 : g);
    int b = in[2] + error[i * 3 + 2];
    int bb = (b < 0) ? 0 : ((b > 255) ? 255 : b);
    out = ((((rb) >> 3) << 11) | (((gb) >> 2) << 5) | ((bb) >> 3));
    write(1, &out, 2);
    e = r - ((((out) >> 11) & 0x1f) * 255 / 31);
    {
      next_error[(i - 1) * 3 + 0] += e * 3 / 16;
      next_error[(i)*3 + 0] += e * 5 / 16;
      next_error[(i + 1) * 3 + 0] += e * 1 / 16;
      error[(i + 1) * 3 + 0] += e - ((e * 1 / 16) + (e * 3 / 16) + (e * 5 / 16));
    };
    e = g - ((((out) >> 5) & 0x3f) * 255 / 63);
    {
      next_error[(i - 1) * 3 + 1] += e * 3 / 16;
      next_error[(i)*3 + 1] += e * 5 / 16;
      next_error[(i + 1) * 3 + 1] += e * 1 / 16;
      error[(i + 1) * 3 + 1] += e - ((e * 1 / 16) + (e * 3 / 16) + (e * 5 / 16));
    };
    e = b - (((out)&0x1f) * 255 / 31);
    {
      next_error[(i - 1) * 3 + 2] += e * 3 / 16;
      next_error[(i)*3 + 2] += e * 5 / 16;
      next_error[(i + 1) * 3 + 2] += e * 1 / 16;
      error[(i + 1) * 3 + 2] += e - ((e * 1 / 16) + (e * 3 / 16) + (e * 5 / 16));
    };
    ++i;
    if (i == width) {
      int *temp = error;
      error = next_error;
      next_error = temp;
      memset(next_error, 0, (width + 1) * 3 * sizeof(int));
      i = 0;
    }
  }
  free(error - 3);
  free(next_error - 3);
  return;
}
```