#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_RGBA16161616_to_BGR565(unsigned char *(*output),
                                     uint16_t *input) {
  unsigned int r, g, b, a;
  a = (input)[3] >> 8;
  r = (unsigned int)(input)[0] * a;
  g = (unsigned int)(input)[1] * a;
  b = (unsigned int)(input)[2] * a;
  *(uint16_t *)(*output) =
      (uint16_t)(((b & 0xf80000) >> 8) + ((g & 0xfc0000) >> 13) +
                 ((r & 0xf80000) >> 19));
  (*output) += 2;
}


x86 assembly (clang, -O0):
<transfer_RGBA16161616_to_BGR565>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%eax
sar    $0x8,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
imul   -0x20(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
imul   -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
imul   -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
and    $0xf80000,%ecx
shr    $0x8,%ecx
mov    -0x18(%rbp),%eax
and    $0xfc0000,%eax
shr    $0xd,%eax
add    %eax,%ecx
mov    -0x14(%rbp),%eax
and    $0xf80000,%eax
shr    $0x13,%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_RGBA16161616_to_BGR565>:
mov    0xc(%rsi),%eax
sar    $0x8,%eax
mov    (%rsi),%ecx
imul   %eax,%ecx
mov    0x4(%rsi),%edx
imul   %eax,%edx
imul   0x8(%rsi),%eax
shr    $0x8,%eax
and    $0xf800,%eax
shr    $0xd,%edx
and    $0x7e0,%edx
shr    $0x13,%ecx
and    $0x1f,%ecx
or     %edx,%ecx
or     %eax,%ecx
mov    (%rdi),%rax
mov    %ecx,(%rax)
add    $0x2,%rax
mov    %rax,(%rdi)
ret

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

LLVM IR (-O2):
define void @transfer_RGBA16161616_to_BGR565(i8** nocapture %0, i32* nocapture %1) {
%3 = getelementptr inbounds i32, i32* %1, i64 3
%4 = load i32, i32* %3
%5 = ashr i32 %4, 8
%6 = load i32, i32* %1
%7 = mul i32 %6, %5
%8 = getelementptr inbounds i32, i32* %1, i64 1
%9 = load i32, i32* %8
%10 = mul i32 %9, %5
%11 = getelementptr inbounds i32, i32* %1, i64 2
%12 = load i32, i32* %11
%13 = mul i32 %12, %5
%14 = lshr i32 %13, 8
%15 = and i32 %14, 63488
%16 = lshr i32 %10, 13
%17 = and i32 %16, 2016
%18 = lshr i32 %7, 19
%19 = and i32 %18, 31
%20 = or i32 %17, %19
%21 = or i32 %20, %15
%22 = bitcast i8** %0 to i32**
%23 = load i32*, i32** %22
store i32 %21, i32* %23
%24 = bitcast i32* %23 to i8*
%25 = getelementptr inbounds i8, i8* %24, i64 2
store i8* %25, i8** %0
ret void
}