#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 scalar_t__ uint16_t;

/* Variables and functions */
void transfer_RGBA8888_to_RGB565(unsigned char *(*output),
                                 unsigned char *input) {
  unsigned int r, g, b, a;
  a = input[3];
  r = ((unsigned int)input[0] * a) / 0xff;
  g = ((unsigned int)input[1] * a) / 0xff;
  b = ((unsigned int)input[2] * a) / 0xff;
  *(uint16_t *)(*output) =
      (uint16_t)(((r & 0xf8) << 8) + ((g & 0xfc) << 3) + ((b & 0xf8) >> 3));
  (*output) += 2;
}


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

x86 assembly (clang, -O2):
<transfer_RGBA8888_to_RGB565>:
movzbl 0x3(%rsi),%eax
movzbl (%rsi),%ecx
imul   %eax,%ecx
imul   $0x8081,%ecx,%ecx
movzbl 0x1(%rsi),%edx
imul   %eax,%edx
imul   $0x8081,%edx,%edx
movzbl 0x2(%rsi),%esi
imul   %eax,%esi
imul   $0x8081,%esi,%eax
shr    $0xf,%ecx
and    $0xf800,%ecx
shr    $0x14,%edx
and    $0x7e0,%edx
or     %rcx,%rdx
shr    $0x1a,%eax
or     %rdx,%rax
mov    (%rdi),%rcx
mov    %rax,(%rcx)
add    $0x2,%rcx
mov    %rcx,(%rdi)
ret

LLVM IR (-O0):
define void @transfer_RGBA8888_to_RGB565(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8** %0, i8*** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = getelementptr inbounds i8, i8* %9, i64 3
%11 = load i8, i8* %10
%12 = zext i8 %11 to i32
store i32 %12, i32* %8
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = load i32, i32* %8
%18 = mul i32 %16, %17
%19 = udiv i32 %18, 255
store i32 %19, i32* %5
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = load i32, i32* %8
%25 = mul i32 %23, %24
%26 = udiv i32 %25, 255
store i32 %26, i32* %6
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i64 2
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = load i32, i32* %8
%32 = mul i32 %30, %31
%33 = udiv i32 %32, 255
store i32 %33, i32* %7
%34 = load i32, i32* %5
%35 = and i32 %34, 248
%36 = shl i32 %35, 8
%37 = load i32, i32* %6
%38 = and i32 %37, 252
%39 = shl i32 %38, 3
%40 = add i32 %36, %39
%41 = load i32, i32* %7
%42 = and i32 %41, 248
%43 = lshr i32 %42, 3
%44 = add i32 %40, %43
%45 = zext i32 %44 to i64
%46 = load i8**, i8*** %3
%47 = load i8*, i8** %46
%48 = bitcast i8* %47 to i64*
store i64 %45, i64* %48
%49 = load i8**, i8*** %3
%50 = load i8*, i8** %49
%51 = getelementptr inbounds i8, i8* %50, i64 2
store i8* %51, i8** %49
ret void
}

LLVM IR (-O2):
define void @transfer_RGBA8888_to_RGB565(i8** nocapture %0, i8* nocapture %1) {
%3 = getelementptr inbounds i8, i8* %1, i64 3
%4 = load i8, i8* %3
%5 = zext i8 %4 to i32
%6 = load i8, i8* %1
%7 = zext i8 %6 to i32
%8 = mul nuw nsw i32 %7, %5
%9 = trunc i32 %8 to i16
%10 = udiv i16 %9, 255
%11 = zext i16 %10 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 1
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = mul nuw nsw i32 %14, %5
%16 = trunc i32 %15 to i16
%17 = udiv i16 %16, 255
%18 = getelementptr inbounds i8, i8* %1, i64 2
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = mul nuw nsw i32 %20, %5
%22 = trunc i32 %21 to i16
%23 = udiv i16 %22, 255
%24 = shl nuw nsw i64 %11, 8
%25 = and i64 %24, 63488
%26 = shl nuw nsw i16 %17, 3
%27 = and i16 %26, 2016
%28 = zext i16 %27 to i64
%29 = or i64 %25, %28
%30 = lshr i16 %23, 3
%31 = zext i16 %30 to i64
%32 = or i64 %29, %31
%33 = bitcast i8** %0 to i64**
%34 = load i64*, i64** %33
store i64 %32, i64* %34
%35 = bitcast i64* %34 to i8*
%36 = getelementptr inbounds i8, i8* %35, i64 2
store i8* %36, i8** %0
ret void
}