#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_RGB8(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;
  *(*output) = (unsigned char)(((r & 0xc00000) >> 16) + ((g & 0xe00000) >> 18) +
                               ((b & 0xe00000) >> 21));
  (*output)++;
}


x86 assembly (clang, -O0):
<transfer_RGBA16161616_to_RGB8>:
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    -0x14(%rbp),%eax
and    $0xc00000,%eax
shr    $0x10,%eax
mov    -0x18(%rbp),%ecx
and    $0xe00000,%ecx
shr    $0x12,%ecx
add    %ecx,%eax
mov    -0x1c(%rbp),%ecx
and    $0xe00000,%ecx
shr    $0x15,%ecx
add    %ecx,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<transfer_RGBA16161616_to_RGB8>:
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    $0x10,%ecx
and    $0xc0,%ecx
shr    $0x12,%edx
and    $0x38,%edx
or     %ecx,%edx
shr    $0x15,%eax
and    $0x7,%eax
or     %edx,%eax
mov    (%rdi),%rcx
mov    %al,(%rcx)
addq   $0x1,(%rdi)
ret

LLVM IR (-O0):
define void @transfer_RGBA16161616_to_RGB8(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* %5
%29 = and i32 %28, 12582912
%30 = lshr i32 %29, 16
%31 = load i32, i32* %6
%32 = and i32 %31, 14680064
%33 = lshr i32 %32, 18
%34 = add i32 %30, %33
%35 = load i32, i32* %7
%36 = and i32 %35, 14680064
%37 = lshr i32 %36, 21
%38 = add i32 %34, %37
%39 = trunc i32 %38 to i8
%40 = load i8**, i8*** %3
%41 = load i8*, i8** %40
store i8 %39, i8* %41
%42 = load i8**, i8*** %3
%43 = load i8*, i8** %42
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %42
ret void
}

LLVM IR (-O2):
define void @transfer_RGBA16161616_to_RGB8(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 %7, 16
%15 = and i32 %14, 192
%16 = lshr i32 %10, 18
%17 = and i32 %16, 56
%18 = or i32 %17, %15
%19 = lshr i32 %13, 21
%20 = and i32 %19, 7
%21 = or i32 %18, %20
%22 = trunc i32 %21 to i8
%23 = load i8*, i8** %0
store i8 %22, i8* %23
%24 = load i8*, i8** %0
%25 = getelementptr inbounds i8, i8* %24, i64 1
store i8* %25, i8** %0
ret void
}