#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint16_t;

/* Variables and functions */
void transfer_RGB888_to_RGB565(unsigned char *(*output), unsigned char *input) {
  uint16_t r, g, b;
  r = *input++;
  g = *input++;
  b = *input;
  *(uint16_t *)(*output) =
      ((r & 0xf8) << 8) + ((g & 0xfc) << 3) + ((b & 0xf8) >> 3);
  (*output) += 2;
}


x86 assembly (clang, -O0):
<transfer_RGB888_to_RGB565>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movzbl (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movzbl (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%ecx
and    $0xf8,%ecx
shl    $0x8,%ecx
mov    -0x18(%rbp),%eax
and    $0xfc,%eax
shl    $0x3,%eax
add    %eax,%ecx
mov    -0x1c(%rbp),%eax
and    $0xf8,%eax
sar    $0x3,%eax
add    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0x2,%rcx
mov    %rcx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<transfer_RGB888_to_RGB565>:
movzbl (%rsi),%eax
movzbl 0x1(%rsi),%ecx
movzbl 0x2(%rsi),%edx
and    $0xfffffff8,%eax
shl    $0x8,%eax
and    $0xfffffffc,%ecx
lea    (%rax,%rcx,8),%eax
shr    $0x3,%edx
or     %eax,%edx
mov    (%rdi),%rax
mov    %edx,(%rax)
add    $0x2,%rax
mov    %rax,(%rdi)
ret

LLVM IR (-O0):
define void @transfer_RGB888_to_RGB565(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8** %0, i8*** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = getelementptr inbounds i8, i8* %8, i32 1
store i8* %9, i8** %4
%10 = load i8, i8* %8
%11 = zext i8 %10 to i32
store i32 %11, i32* %5
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i32 1
store i8* %13, i8** %4
%14 = load i8, i8* %12
%15 = zext i8 %14 to i32
store i32 %15, i32* %6
%16 = load i8*, i8** %4
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
store i32 %18, i32* %7
%19 = load i32, i32* %5
%20 = and i32 %19, 248
%21 = shl i32 %20, 8
%22 = load i32, i32* %6
%23 = and i32 %22, 252
%24 = shl i32 %23, 3
%25 = add nsw i32 %21, %24
%26 = load i32, i32* %7
%27 = and i32 %26, 248
%28 = ashr i32 %27, 3
%29 = add nsw i32 %25, %28
%30 = load i8**, i8*** %3
%31 = load i8*, i8** %30
%32 = bitcast i8* %31 to i32*
store i32 %29, i32* %32
%33 = load i8**, i8*** %3
%34 = load i8*, i8** %33
%35 = getelementptr inbounds i8, i8* %34, i64 2
store i8* %35, i8** %33
ret void
}

LLVM IR (-O2):
define void @transfer_RGB888_to_RGB565(i8** nocapture %0, i8* nocapture %1) {
%3 = getelementptr inbounds i8, i8* %1, i64 1
%4 = load i8, i8* %1
%5 = getelementptr inbounds i8, i8* %1, i64 2
%6 = load i8, i8* %3
%7 = load i8, i8* %5
%8 = and i8 %4, -8
%9 = zext i8 %8 to i32
%10 = shl nuw nsw i32 %9, 8
%11 = and i8 %6, -4
%12 = zext i8 %11 to i32
%13 = shl nuw nsw i32 %12, 3
%14 = or i32 %13, %10
%15 = lshr i8 %7, 3
%16 = zext i8 %15 to i32
%17 = or i32 %14, %16
%18 = bitcast i8** %0 to i32**
%19 = load i32*, i32** %18
store i32 %17, i32* %19
%20 = bitcast i32* %19 to i8*
%21 = getelementptr inbounds i8, i8* %20, i64 2
store i8* %21, i8** %0
ret void
}