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

```asm
<RGB24toRGB565>:
  endbr64
  push   %r15
  mov    %edx,%r15d
  push   %r14
  mov    %ecx,%r14d
  push   %r13
  mov    %r9d,%r13d
  push   %r12
  push   %rbp
  push   %rbx
  mov    %r8d,%ebx
  sub    $0x48,%rsp
  mov    %fs:0x28,%rax
  mov    %rax,0x38(%rsp)
  xor    %eax,%eax
  lea    0x34(%rsp),%rax
  mov    0x90(%rsp),%r11
  mov    0xa8(%rsp),%r10d
  mov    0xb0(%rsp),%r12d
  mov    0xc0(%rsp),%ebp
  push   %rax
  lea    0x38(%rsp),%rax
  push   %rax
  mov    0xd8(%rsp),%eax
  push   %rax
  push   %rbp
  mov    0xd8(%rsp),%eax
  push   %rax
  push   %r12
  push   %r10
  mov    0xd8(%rsp),%eax
  mov    %r10d,0xe0(%rsp)
  push   %rax
  mov    0xd8(%rsp),%eax
  push   %rax
  push   %r11
  mov    0xd8(%rsp),%eax
  mov    %r11,0x60(%rsp)
  push   %rax
  mov    0xd8(%rsp),%eax
  push   %rax
  mov    %rdi,0x68(%rsp)
  call   <chk_args@plt>
  add    $0x60,%rsp
  test   %eax,%eax
  je     L12
  movslq 0xa8(%rsp),%r10
  mov    0x8(%rsp),%rdi
  mov    0x10(%rsp),%r11
  test   %r10d,%r10d
  jns    L123
  mov    0xa0(%rsp),%eax
  sub    $0x1,%eax
  imul   %r10d,%eax
  cltq
  sub    %rax,%r11
L123:
  test   %r14d,%r14d
  jns    L108
  sub    $0x1,%r15d
  imul   %r14d,%r15d
  movslq %r15d,%rax
  sub    %rax,%rdi
L108:
  mov    0x30(%rsp),%eax
  cmp    $0x1,%eax
  je     L264
  cmp    $0x2,%eax
  jne    L12
  cmpl   $0x2,0x34(%rsp)
  jne    L12
  mov    0xb8(%rsp),%eax
  imul   %r14d,%r13d
  lea    (%r12,%r12,2),%r15d
  mov    0xc8(%rsp),%ecx
  movslq %r15d,%r15
  imul   %r10d,%eax
  movslq %r13d,%rdx
  cltq
  add    %rax,%r15
  lea    (%rbx,%rbx,1),%eax
  cltq
  add    %r11,%r15
  add    %rdx,%rax
  add    %rdi,%rax
  test   %ecx,%ecx
  jle    L190
  lea    0x0(,%rbp,4),%ebx
  xor    %r13d,%r13d
  movslq %ebx,%rdx
  neg    %rdx
  mov    %rdx,0x8(%rsp)
  lea    0x0(%rbp,%rbp,2),%edx
  movslq %edx,%rdx
  sub    %rdx,%r10
  lea    (%r14,%r14,1),%edx
  movslq %edx,%rdi
  mov    %r10,0x18(%rsp)
  mov    %rdi,0x10(%rsp)
  movslq %r14d,%rdi
  mov    %rdi,0x20(%rsp)
  mov    %r12d,%edi
  and    $0x3,%edi
  mov    %edi,0x28(%rsp)
  lea    0x1(%rbp),%edi
  mov    %edi,0x2c(%rsp)
  nopl   (%rax)
L13:
  mov    0x28(%rsp),%edx
  test   %edx,%edx
  je     L161
  mov    0x2c(%rsp),%ecx
  mov    %ebp,%edi
  add    %r12d,%ecx
  test   %ebp,%ebp
  jne    L47
  jmp    L29
  cs nopw 0x0(%rax,%rax,1)
L38:
  test   %edi,%edi
  je     L29
L47:
  movzbl 0x1(%r15),%edx
  movzbl 0x2(%r15),%esi
  add    $0x4,%rax
  add    $0x3,%r15
  shl    $0x3,%edx
  shl    $0x8,%esi
  and    $0xf800,%si
  and    $0x7e0,%dx
  or     %esi,%edx
  movzbl -0x3(%r15),%esi
  shr    $0x3,%sil
  movzbl %sil,%esi
  or     %esi,%edx
  mov    %dx,-0x4(%rax)
  mov    %dx,-0x2(%rax)
  mov    %ecx,%edx
  sub    %edi,%edx
  sub    $0x1,%edi
  and    $0x3,%edx
  jne    L38
L185:
  mov    %rax,%r9
  cmp    $0x3,%edi
  jle    L181
  sub    $0x4,%edi
  mov    %r15,%r8
  mov    %edi,%r11d
  shr    $0x2,%r11d
  lea    0x1(%r11),%r10d
  mov    %r10,%r9
  shl    $0x4,%r9
  add    %rax,%r9
  cs nopw 0x0(%rax,%rax,1)
L263:
  mov    (%r8),%edx
  mov    0x4(%r8),%ecx
  mov    %edx,%esi
  mov    %edx,%r14d
  shr    $0x3,%esi
  shr    $0x5,%r14d
  and    $0x7e0,%r14d
  and    $0x1f,%esi
  or     %r14d,%esi
  mov    %edx,%r14d
  shr    $0xb,%edx
  shr    $0x8,%r14d
  and    $0x1f0000,%edx
  and    $0xf800,%r14d
  or     %r14d,%esi
  mov    %esi,%r14d
  shl    $0x10,%r14d
  or     %r14d,%esi
  mov    %esi,(%rax)
  mov    %ecx,%esi
  shl    $0x13,%esi
  and    $0x7e00000,%esi
  or     %esi,%edx
  mov    %ecx,%esi
  shl    $0x10,%esi
  and    $0xf8000000,%esi
  or     %esi,%edx
  mov    %edx,%esi
  shr    $0x10,%esi
  or     %esi,%edx
  mov    %edx,0x4(%rax)
  mov    %ecx,%edx
  mov    0x8(%r8),%esi
  shr    $0x15,%ecx
  shr    $0x13,%edx
  and    $0x7e0,%ecx
  and    $0x1f,%edx
  or     %ecx,%edx
  mov    %esi,%ecx
  shl    $0x8,%ecx
  and    $0xf800,%ecx
  or     %ecx,%edx
  mov    %edx,%ecx
  shl    $0x10,%ecx
  or     %ecx,%edx
  lea    0x0(,%rsi,8),%ecx
  mov    %edx,0x8(%rax)
  mov    %esi,%edx
  shl    $0x5,%edx
  and    $0x1f0000,%edx
  and    $0x7e00000,%ecx
  add    $0x10,%rax
  and    $0xf8000000,%esi
  or     %ecx,%edx
  add    $0xc,%r8
  or     %esi,%edx
  mov    %edx,%ecx
  shr    $0x10,%ecx
  or     %ecx,%edx
  mov    %edx,-0x4(%rax)
  cmp    %r9,%rax
  jne    L263
  lea    (%r10,%r10,2),%rax
  neg    %r11d
  lea    (%r15,%rax,4),%r15
  lea    (%rdi,%r11,4),%edi
L181:
  test   %edi,%edi
  je     L183
  lea    (%r9,%rdi,4),%rax
  mov    %r15,%rcx
L244:
  movzbl 0x1(%rcx),%edx
  movzbl 0x2(%rcx),%esi
  add    $0x4,%r9
  add    $0x3,%rcx
  shl    $0x3,%edx
  shl    $0x8,%esi
  and    $0xf800,%si
  and    $0x7e0,%dx
  or     %esi,%edx
  movzbl -0x3(%rcx),%esi
  shr    $0x3,%sil
  movzbl %sil,%esi
  or     %esi,%edx
  mov    %dx,-0x4(%r9)
  mov    %dx,-0x2(%r9)
  cmp    %rax,%r9
  jne    L244
  lea    (%rdi,%rdi,2),%rdx
  add    %rdx,%r15
L29:
  mov    0x8(%rsp),%rdi
  mov    %ebx,%edx
  add    $0x1,%r13d
  lea    (%rax,%rdi,1),%r14
  mov    0x20(%rsp),%rax
  mov    %r14,%rsi
  lea    (%r14,%rax,1),%rdi
  call   <memcpy@plt>
  mov    0x10(%rsp),%rax
  add    0x18(%rsp),%r15
  add    %r14,%rax
  cmp    %r13d,0xc8(%rsp)
  jne    L13
L190:
  xor    %eax,%eax
L83:
  mov    0x38(%rsp),%rdx
  sub    %fs:0x28,%rdx
  jne    L109
  add    $0x48,%rsp
  pop    %rbx
  pop    %rbp
  pop    %r12
  pop    %r13
  pop    %r14
  pop    %r15
  ret
L161:
  mov    %ebp,%edi
  jmp    L185
L264:
  cmpl   $0x1,0x34(%rsp)
  mov    $0xffffffff,%eax
  jne    L83
  mov    0xb8(%rsp),%eax
  mov    %r13d,%edx
  lea    (%r12,%r12,2),%r8d
  imul   %r14d,%edx
  movslq %r8d,%r8
  imul   %r10d,%eax
  movslq %edx,%rdx
  cltq
  add    %rax,%r8
  lea    (%rbx,%rbx,1),%eax
  cltq
  add    %r11,%r8
  add    %rdx,%rax
  lea    (%rdi,%rax,1),%rcx
  mov    0x88(%rsp),%edi
  test   %edi,%edi
  jle    L190
  mov    0x80(%rsp),%eax
  mov    0x80(%rsp),%r13d
  xor    %ebp,%ebp
  shl    $0x2,%eax
  sub    %eax,%r13d
  mov    0x80(%rsp),%eax
  add    %r10d,%r13d
  add    %eax,%eax
  movslq %r13d,%r13
  sub    %eax,%r14d
  mov    %r12d,%eax
  and    $0x3,%eax
  movslq %r14d,%r15
  mov    %eax,0x8(%rsp)
  mov    0x80(%rsp),%eax
  lea    0x1(%rax,%r12,1),%ebx
  mov    %eax,%r14d
L27:
  mov    0x8(%rsp),%esi
  test   %esi,%esi
  je     L107
  mov    %r14d,%edx
  test   %r14d,%r14d
  jne    L104
  jmp    L245
  nopl   (%rax)
L26:
  test   %edx,%edx
  je     L245
L104:
  movzbl 0x1(%r8),%eax
  movzbl 0x2(%r8),%esi
  add    $0x2,%rcx
  add    $0x3,%r8
  shl    $0x3,%eax
  shl    $0x8,%esi
  and    $0xf800,%si
  and    $0x7e0,%ax
  or     %esi,%eax
  movzbl -0x3(%r8),%esi
  shr    $0x3,%sil
  movzbl %sil,%esi
  or     %esi,%eax
  mov    %ax,-0x2(%rcx)
  mov    %ebx,%eax
  sub    %edx,%eax
  sub    $0x1,%edx
  test   $0x3,%al
  jne    L26
L150:
  mov    %rcx,%r10
  cmp    $0x3,%edx
  jle    L350
  lea    -0x4(%rdx),%eax
  mov    %r8,%r9
  mov    %eax,%r12d
  mov    %eax,0x10(%rsp)
  shr    $0x2,%r12d
  lea    0x1(%r12),%r11d
  lea    (%rcx,%r11,8),%r10
  nopw   0x0(%rax,%rax,1)
L200:
  mov    (%r9),%edx
  mov    0x4(%r9),%esi
  mov    %edx,%eax
  mov    %edx,%edi
  shr    $0x3,%eax
  shr    $0x5,%edi
  and    $0x7e0,%edi
  and    $0x1f,%eax
  or     %edi,%eax
  mov    %edx,%edi
  shr    $0xb,%edx
  shr    $0x8,%edi
  and    $0x1f0000,%edx
  and    $0xf800,%edi
  or     %edi,%eax
  or     %edx,%eax
  mov    %esi,%edx
  shl    $0x13,%edx
  and    $0x7e00000,%edx
  or     %edx,%eax
  mov    %esi,%edx
  shl    $0x10,%edx
  and    $0xf8000000,%edx
  or     %edx,%eax
  mov    %eax,(%rcx)
  mov    %esi,%eax
  mov    0x8(%r9),%edx
  shr    $0x15,%esi
  shr    $0x13,%eax
  and    $0x7e0,%esi
  and    $0x1f,%eax
  or     %esi,%eax
  mov    %edx,%esi
  and    $0xf8000000,%esi
  or     %esi,%eax
  mov    %edx,%esi
  shl    $0x8,%esi
  and    $0xf800,%esi
  or     %esi,%eax
  mov    %edx,%esi
  shl    $0x3,%edx
  shl    $0x5,%esi
  and    $0x7e00000,%edx
  and    $0x1f0000,%esi
  or     %esi,%eax
  or     %edx,%eax
  add    $0x8,%rcx
  add    $0xc,%r9
  mov    %eax,-0x4(%rcx)
  cmp    %r10,%rcx
  jne    L200
  lea    (%r11,%r11,2),%rax
  neg    %r12d
  lea    (%r8,%rax,4),%r8
  mov    0x10(%rsp),%eax
  lea    (%rax,%r12,4),%edx
L350:
  test   %edx,%edx
  je     L67
  lea    (%r10,%rdx,2),%rcx
  mov    %r8,%rsi
L259:
  movzbl 0x1(%rsi),%eax
  movzbl 0x2(%rsi),%edi
  add    $0x2,%r10
  add    $0x3,%rsi
  shl    $0x3,%eax
  shl    $0x8,%edi
  and    $0xf800,%di
  and    $0x7e0,%ax
  or     %edi,%eax
  movzbl -0x3(%rsi),%edi
  shr    $0x3,%dil
  movzbl %dil,%edi
  or     %edi,%eax
  mov    %ax,-0x2(%r10)
  cmp    %rcx,%r10
  jne    L259
  lea    (%rdx,%rdx,2),%rax
  add    %rax,%r8
L245:
  add    %r13,%r8
  add    %r15,%rcx
  add    $0x1,%ebp
  cmp    %ebp,0x88(%rsp)
  jne    L27
  jmp    L190
L12:
  mov    $0xffffffff,%eax
  jmp    L83
L107:
  mov    %r14d,%edx
  jmp    L150
L109:
  call   <__stack_chk_fail@plt>
L183:
  mov    %r9,%rax
  jmp    L29
L67:
  mov    %r10,%rcx
  jmp    L245
```

assistant: ```c
int RGB24toRGB565(unsigned char *dest_ptr, int dest_width, int dest_height, int dest_pitch, int dest_x, int dest_y, int dest_dx, int dest_dy, unsigned char *src_ptr, int src_width, int src_height, int src_pitch, int src_x, int src_y, int src_dx, int src_dy) {
  int scale_x, scale_y;
  if (!chk_args(dest_ptr, dest_width, dest_height, dest_pitch, dest_x, dest_y, dest_dx, dest_dy, src_ptr, src_width, src_height, src_pitch, src_x, src_y, src_dx, src_dy, &scale_x, &scale_y))
    return -1;
  if (src_pitch < 0)
    src_ptr -= (src_height - 1) * src_pitch;
  if (dest_pitch < 0)
    dest_ptr -= (dest_height - 1) * dest_pitch;
  if (scale_x == 1 && scale_y == 1) {
    unsigned char *s, *d;
    register int i;
    s = src_ptr + src_x * 3 + src_y * src_pitch;
    d = dest_ptr + dest_x * 2 + dest_y * dest_pitch;
    for (i = 0; i < dest_dy; i++) {
      register int sx = src_x, ddx = dest_dx;
      while ((sx & 3) && ddx) {
        *(unsigned short *)d = (s[0] >> 3) | ((s[1] & 0xFC) << 3) | ((s[2] & 0xF8) << 8);
        d += 2;
        s += 3;
        sx++;
        ddx--;
      }
      while (ddx >= 4) {
        register unsigned int a, b;
        a = *(unsigned int *)s;
        b = *(unsigned int *)(s + 4);
        *(unsigned int *)d = ((a & 0xF8) >> 3) | ((a & 0xFC00) >> 5) | ((a & 0xF80000) >> 8) | ((a & 0xF8000000) >> 11) | ((b & 0xFC) << 19) | ((b & 0xF800) << 16);
        a = *(unsigned int *)(s + 8);
        *(unsigned int *)(d + 4) = ((b & 0xF80000) >> 19) | ((b & 0xFC000000) >> 21) | ((a & 0xF8) << 8) | ((a & 0xF800) << 5) | ((a & 0xFC0000) << 3) | (a & 0xF8000000);
        d += 2 * 4;
        s += 3 * 4;
        ddx -= 4;
      }
      while (ddx) {
        *(unsigned short *)d = (s[0] >> 3) | ((s[1] & 0xFC) << 3) | ((s[2] & 0xF8) << 8);
        d += 2;
        s += 3;
        ddx--;
      }
      s += src_pitch - dest_dx * 3;
      d += dest_pitch - dest_dx * 2;
    }
    return 0;
  }
  if (scale_x == 2 && scale_y == 2) {
    unsigned char *s, *d;
    register int i;
    s = src_ptr + src_x * 3 + src_y * src_pitch;
    d = dest_ptr + dest_x * 2 + dest_y * dest_pitch;
    for (i = 0; i < src_dy; i++) {
      register int sx = src_x, sdx = src_dx;
      while ((sx & 3) && sdx) {
        register unsigned short a;
        a = (s[0] >> 3) | ((s[1] & 0xFC) << 3) | ((s[2] & 0xF8) << 8);
        *(unsigned short *)d = a;
        *(unsigned short *)(d + 2) = a;
        d += 2 * 2;
        s += 3;
        sx++;
        sdx--;
      }
      while (sdx >= 4) {
        register unsigned int a, b, c;
        a = *(unsigned int *)s;
        b = *(unsigned int *)(s + 4);
        c = ((a & 0xF8) >> 3) | ((a & 0xFC00) >> 5) | ((a & 0xF80000) >> 8);
        *(unsigned int *)d = c | (c << 16);
        c = ((a & 0xF8000000) >> 11) | ((b & 0xFC) << 19) | ((b & 0xF800) << 16);
        *(unsigned int *)(d + 2 * 2) = c | (c >> 16);
        a = *(unsigned int *)(s + 8);
        c = ((b & 0xF80000) >> 19) | ((b & 0xFC000000) >> 21) | ((a & 0xF8) << 8);
        *(unsigned int *)(d + 2 * 2 * 2) = c | (c << 16);
        c = ((a & 0xF800) << 5) | ((a & 0xFC0000) << 3) | (a & 0xF8000000);
        *(unsigned int *)(d + 3 * 2 * 2) = c | (c >> 16);
        d += 2 * 4 * 2;
        s += 4 * 3;
        sdx -= 4;
      }
      while (sdx) {
        register unsigned short a;
        a = (s[0] >> 3) | ((s[1] & 0xFC) << 3) | ((s[2] & 0xF8) << 8);
        *(unsigned short *)d = a;
        *(unsigned short *)(d + 2) = a;
        d += 2 * 2;
        s += 3;
        sdx--;
      }
      s -= src_dx * 3;
      d -= src_dx * 2 * 2;
      memcpy(d + dest_pitch, d, src_dx * 2 * 2);
      s += src_pitch;
      d += dest_pitch * 2;
    }
    return 0;
  }
  return -1;
}
```