#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_RGB161616_to_BGR565(unsigned char *(*output), uint16_t *input) {
  uint16_t r, g, b;
  r = *input++;
  g = *input++;
  b = *input;
  *(uint16_t *)(*output) =
      (b & 0xf800) | ((g & 0xfc00) >> 5) | ((r & 0xf800) >> 11);
  (*output) += 2;
}


x86 assembly (clang, -O0):
<transfer_RGB161616_to_BGR565>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
and    $0xf800,%ecx
mov    -0x18(%rbp),%eax
and    $0xfc00,%eax
sar    $0x5,%eax
or     %eax,%ecx
mov    -0x14(%rbp),%eax
and    $0xf800,%eax
sar    $0xb,%eax
or     %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_RGB161616_to_BGR565>:
mov    (%rsi),%eax
mov    0x4(%rsi),%ecx
mov    $0xf800,%edx
and    0x8(%rsi),%edx
shr    $0x5,%ecx
and    $0x7e0,%ecx
shr    $0xb,%eax
and    $0x1f,%eax
or     %ecx,%eax
or     %edx,%eax
mov    (%rdi),%rcx
mov    %eax,(%rcx)
add    $0x2,%rcx
mov    %rcx,(%rdi)
ret

LLVM IR (-O0):
define void @transfer_RGB161616_to_BGR565(i8** %0, i32* %1) {
%3 = alloca i8**
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8** %0, i8*** %3
store i32* %1, i32** %4
%8 = load i32*, i32** %4
%9 = getelementptr inbounds i32, i32* %8, i32 1
store i32* %9, i32** %4
%10 = load i32, i32* %8
store i32 %10, i32* %5
%11 = load i32*, i32** %4
%12 = getelementptr inbounds i32, i32* %11, i32 1
store i32* %12, i32** %4
%13 = load i32, i32* %11
store i32 %13, i32* %6
%14 = load i32*, i32** %4
%15 = load i32, i32* %14
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = and i32 %16, 63488
%18 = load i32, i32* %6
%19 = and i32 %18, 64512
%20 = ashr i32 %19, 5
%21 = or i32 %17, %20
%22 = load i32, i32* %5
%23 = and i32 %22, 63488
%24 = ashr i32 %23, 11
%25 = or i32 %21, %24
%26 = load i8**, i8*** %3
%27 = load i8*, i8** %26
%28 = bitcast i8* %27 to i32*
store i32 %25, i32* %28
%29 = load i8**, i8*** %3
%30 = load i8*, i8** %29
%31 = getelementptr inbounds i8, i8* %30, i64 2
store i8* %31, i8** %29
ret void
}

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