#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 */
uint32_t pix565(const void *buf, unsigned i) {
  uint16_t pix = ((const uint16_t *)buf)[i];
  uint32_t r = (0x1F & (pix >> 11)) * 255 / 0x1F;
  uint32_t g = (0x3F & (pix >> 5)) * 255 / 0x3F;
  uint32_t b = (0x1F & pix) * 255 / 0x1F;
  return (r << 16) | (g << 8) | (b);
}


x86 assembly (clang, -O0):
<pix565>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sar    $0xb,%eax
and    $0x1f,%eax
imul   $0xff,%eax,%eax
mov    $0x1f,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
sar    $0x5,%eax
and    $0x3f,%eax
imul   $0xff,%eax,%eax
mov    $0x3f,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
and    $0x1f,%eax
imul   $0xff,%eax,%eax
mov    $0x1f,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x10,%eax
mov    -0x18(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
or     -0x1c(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<pix565>:
mov    %esi,%eax
movzwl (%rdi,%rax,4),%eax
mov    %eax,%ecx
shr    $0xb,%ecx
mov    %ecx,%edx
shl    $0x8,%edx
sub    %ecx,%edx
movzwl %dx,%ecx
imul   $0x843,%ecx,%esi
shr    $0x10,%esi
sub    %esi,%edx
movzwl %dx,%ecx
shr    %ecx
add    %esi,%ecx
shl    $0xc,%ecx
and    $0xfff0000,%ecx
mov    %eax,%edx
shr    $0x5,%edx
and    $0x3f,%edx
mov    %edx,%esi
shl    $0x8,%esi
sub    %edx,%esi
movzwl %si,%edx
imul   $0x411,%edx,%edi
shr    $0x10,%edi
sub    %edi,%esi
movzwl %si,%edx
shr    %edx
add    %edi,%edx
shl    $0x3,%edx
and    $0x7ff00,%edx
and    $0x1f,%eax
mov    %eax,%esi
shl    $0x8,%esi
sub    %eax,%esi
movzwl %si,%eax
imul   $0x843,%eax,%edi
shr    $0x10,%edi
sub    %edi,%esi
movzwl %si,%eax
shr    %eax
add    %edi,%eax
shr    $0x4,%eax
or     %ecx,%eax
or     %edx,%eax
ret

LLVM IR (-O0):
define i32 @pix565(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 i8*, i8** %3
%10 = bitcast i8* %9 to i32*
%11 = load i32, i32* %4
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i32, i32* %10, i64 %12
%14 = load i32, i32* %13
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = ashr i32 %15, 11
%17 = and i32 31, %16
%18 = mul nsw i32 %17, 255
%19 = sdiv i32 %18, 31
store i32 %19, i32* %6
%20 = load i32, i32* %5
%21 = ashr i32 %20, 5
%22 = and i32 63, %21
%23 = mul nsw i32 %22, 255
%24 = sdiv i32 %23, 63
store i32 %24, i32* %7
%25 = load i32, i32* %5
%26 = and i32 31, %25
%27 = mul nsw i32 %26, 255
%28 = sdiv i32 %27, 31
store i32 %28, i32* %8
%29 = load i32, i32* %6
%30 = shl i32 %29, 16
%31 = load i32, i32* %7
%32 = shl i32 %31, 8
%33 = or i32 %30, %32
%34 = load i32, i32* %8
%35 = or i32 %33, %34
ret i32 %35
}

LLVM IR (-O2):
define i32 @pix565(i8* nocapture %0, i32 %1) {
%3 = bitcast i8* %0 to i32*
%4 = zext i32 %1 to i64
%5 = getelementptr inbounds i32, i32* %3, i64 %4
%6 = load i32, i32* %5
%7 = trunc i32 %6 to i16
%8 = lshr i16 %7, 11
%9 = mul nuw nsw i16 %8, 255
%10 = udiv i16 %9, 31
%11 = zext i16 %10 to i32
%12 = lshr i16 %7, 5
%13 = and i16 %12, 63
%14 = mul nuw nsw i16 %13, 255
%15 = udiv i16 %14, 63
%16 = zext i16 %15 to i32
%17 = and i16 %7, 31
%18 = mul nuw nsw i16 %17, 255
%19 = udiv i16 %18, 31
%20 = zext i16 %19 to i32
%21 = shl nuw nsw i32 %11, 16
%22 = shl nuw nsw i32 %16, 8
%23 = or i32 %21, %20
%24 = or i32 %23, %22
ret i32 %24
}