#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 uint32_t;
typedef int uint16_t;

/* Variables and functions */
void transfer_RGBA16161616_to_BGR8888(unsigned char *(*output),
                                      uint16_t *input) {
  uint32_t r, g, b, a;
  a = input[3];
  r = (input)[0] * a;
  g = (input)[1] * a;
  b = (input)[2] * a;
  (*output)[0] = (unsigned char)(b >> 24);
  (*output)[1] = (unsigned char)(g >> 24);
  (*output)[2] = (unsigned char)(r >> 24);
  (*output) += 4;
}


x86 assembly (clang, -O0):
<transfer_RGBA16161616_to_BGR8888>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%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),%eax
sar    $0x18,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
sar    $0x18,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %cl,0x1(%rax)
mov    -0x14(%rbp),%eax
sar    $0x18,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %cl,0x2(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0x4,%rcx
mov    %rcx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<transfer_RGBA16161616_to_BGR8888>:
mov    0xc(%rsi),%eax
mov    (%rsi),%ecx
imul   %eax,%ecx
mov    0x4(%rsi),%edx
imul   %eax,%edx
imul   0x8(%rsi),%eax
shr    $0x18,%eax
mov    (%rdi),%rsi
mov    %al,(%rsi)
shr    $0x18,%edx
mov    (%rdi),%rax
mov    %dl,0x1(%rax)
shr    $0x18,%ecx
mov    (%rdi),%rax
mov    %cl,0x2(%rax)
addq   $0x4,(%rdi)
ret

LLVM IR (-O0):
define void @transfer_RGBA16161616_to_BGR8888(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
store i32 %11, i32* %8
%12 = load i32*, i32** %4
%13 = getelementptr inbounds i32, i32* %12, i64 0
%14 = load i32, i32* %13
%15 = load i32, i32* %8
%16 = mul nsw i32 %14, %15
store i32 %16, i32* %5
%17 = load i32*, i32** %4
%18 = getelementptr inbounds i32, i32* %17, i64 1
%19 = load i32, i32* %18
%20 = load i32, i32* %8
%21 = mul nsw i32 %19, %20
store i32 %21, i32* %6
%22 = load i32*, i32** %4
%23 = getelementptr inbounds i32, i32* %22, i64 2
%24 = load i32, i32* %23
%25 = load i32, i32* %8
%26 = mul nsw i32 %24, %25
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = ashr i32 %27, 24
%29 = trunc i32 %28 to i8
%30 = load i8**, i8*** %3
%31 = load i8*, i8** %30
%32 = getelementptr inbounds i8, i8* %31, i64 0
store i8 %29, i8* %32
%33 = load i32, i32* %6
%34 = ashr i32 %33, 24
%35 = trunc i32 %34 to i8
%36 = load i8**, i8*** %3
%37 = load i8*, i8** %36
%38 = getelementptr inbounds i8, i8* %37, i64 1
store i8 %35, i8* %38
%39 = load i32, i32* %5
%40 = ashr i32 %39, 24
%41 = trunc i32 %40 to i8
%42 = load i8**, i8*** %3
%43 = load i8*, i8** %42
%44 = getelementptr inbounds i8, i8* %43, i64 2
store i8 %41, i8* %44
%45 = load i8**, i8*** %3
%46 = load i8*, i8** %45
%47 = getelementptr inbounds i8, i8* %46, i64 4
store i8* %47, i8** %45
ret void
}

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