#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_BGR565(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)(((b & 0xf8) << 8) + ((g & 0xfc) << 3) + ((r & 0xf8) >> 3));
  (*output) += 2;
}


x86 assembly (clang, -O0):
<transfer_RGBA8888_to_BGR565>:
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    -0x1c(%rbp),%eax
and    $0xf8,%eax
shl    $0x8,%eax
mov    -0x18(%rbp),%ecx
and    $0xfc,%ecx
shl    $0x3,%ecx
add    %ecx,%eax
mov    -0x14(%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_BGR565>:
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,%eax
and    $0xf800,%eax
shr    $0x14,%edx
and    $0x7e0,%edx
shr    $0x1a,%ecx
or     %edx,%ecx
or     %rax,%rcx
mov    (%rdi),%rax
mov    %rcx,(%rax)
add    $0x2,%rax
mov    %rax,(%rdi)
ret

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