#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 color;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
scalar_t__ read(int /*<<< orphan*/, unsigned short *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, unsigned short *, int);
void from_565_rle(void) {
  unsigned short times, count;
  unsigned short in[2];
  unsigned short color, outR, outG, outB;
  unsigned total = 0;
  while (read(0, in, 4)) {
    count = in[0];
    color = in[1];
    total += count;
    outR = ((((color) >> 11) & 0x1f) * 255 / 31);
    outG = ((((color) >> 5) & 0x3f) * 255 / 63);
    outB = (((color)&0x1f) * 255 / 31);
    for (times = 0; times < count; times++) {
      write(1, &outR, 1);
      write(1, &outG, 1);
      write(1, &outB, 1);
    }
  }
  fprintf(stderr, "%d pixels\n", total);
}


x86 assembly (clang, -O0):
<from_565_rle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x14(%rbp)
lea    -0x8(%rbp),%rsi
xor    %edi,%edi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
je     1220 <from_565_rle+0xf0>
mov    -0x8(%rbp),%ax
mov    %ax,-0x4(%rbp)
mov    -0x6(%rbp),%ax
mov    %ax,-0xa(%rbp)
movzwl -0x4(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
movzwl -0xa(%rbp),%eax
sar    $0xb,%eax
and    $0x1f,%eax
imul   $0xff,%eax,%eax
mov    $0x1f,%ecx
cltd
idiv   %ecx
mov    %ax,-0xc(%rbp)
movzwl -0xa(%rbp),%eax
sar    $0x5,%eax
and    $0x3f,%eax
imul   $0xff,%eax,%eax
mov    $0x3f,%ecx
cltd
idiv   %ecx
mov    %ax,-0xe(%rbp)
movzwl -0xa(%rbp),%eax
and    $0x1f,%eax
imul   $0xff,%eax,%eax
mov    $0x1f,%ecx
cltd
idiv   %ecx
mov    %ax,-0x10(%rbp)
movw   $0x0,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
movzwl -0x4(%rbp),%ecx
cmp    %ecx,%eax
jge    121b <from_565_rle+0xeb>
mov    $0x1,%edx
lea    -0xc(%rbp),%rsi
mov    %edx,%edi
call   1030 <write@plt>
mov    $0x1,%edx
lea    -0xe(%rbp),%rsi
mov    %edx,%edi
call   1030 <write@plt>
mov    $0x1,%edx
lea    -0x10(%rbp),%rsi
mov    %edx,%edi
call   1030 <write@plt>
mov    -0x2(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x2(%rbp)
jmp    11ca <from_565_rle+0x9a>
jmp    113f <from_565_rle+0xf>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xdcd(%rip),%rsi
call   1050 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<from_565_rle>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %r15d,%r15d
lea    0x14(%rsp),%rsi
xor    %edi,%edi
mov    $0x4,%edx
call   1040 <read@plt>
test   %rax,%rax
je     1262 <from_565_rle+0x132>
xor    %r15d,%r15d
lea    0x12(%rsp),%r12
lea    0x10(%rsp),%rbp
lea    0xe(%rsp),%rbx
jmp    118d <from_565_rle+0x5d>
nop
add    %r13d,%r15d
xor    %edi,%edi
lea    0x14(%rsp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
test   %rax,%rax
je     1262 <from_565_rle+0x132>
movzwl 0x14(%rsp),%r13d
movzwl 0x16(%rsp),%ecx
mov    %ecx,%eax
shr    $0xb,%eax
mov    %eax,%edx
shl    $0x8,%edx
sub    %eax,%edx
movzwl %dx,%eax
imul   $0x843,%eax,%esi
shr    $0x10,%esi
sub    %esi,%edx
movzwl %dx,%eax
shr    %eax
add    %esi,%eax
shr    $0x4,%eax
mov    %ecx,%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
shr    $0x5,%edx
and    $0x1f,%ecx
mov    %ecx,%esi
shl    $0x8,%esi
sub    %ecx,%esi
movzwl %si,%ecx
imul   $0x843,%ecx,%ecx
shr    $0x10,%ecx
sub    %ecx,%esi
movzwl %si,%esi
shr    %esi
add    %ecx,%esi
shr    $0x4,%esi
mov    %ax,0x12(%rsp)
mov    %dx,0x10(%rsp)
mov    %si,0xe(%rsp)
test   %r13d,%r13d
je     1170 <from_565_rle+0x40>
mov    %r13d,%r14d
mov    $0x1,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0xffff,%r14w
jne    1220 <from_565_rle+0xf0>
jmp    1170 <from_565_rle+0x40>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
mov    %r15d,%edx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [11 x i8] c"%d pixels\0A\00"
define void @from_565_rle() {
%1 = alloca i16
%2 = alloca i16
%3 = alloca [2 x i16]
%4 = alloca i16
%5 = alloca i16
%6 = alloca i16
%7 = alloca i16
%8 = alloca i32
store i32 0, i32* %8
br label %9
9: ; preds = %55, %0
%10 = getelementptr inbounds [2 x i16], [2 x i16]* %3, i64 0, i64 0
%11 = call i64 @read(i32 0, i16* %10, i32 4)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %56
13: ; preds = %9
%14 = getelementptr inbounds [2 x i16], [2 x i16]* %3, i64 0, i64 0
%15 = load i16, i16* %14
store i16 %15, i16* %2
%16 = getelementptr inbounds [2 x i16], [2 x i16]* %3, i64 0, i64 1
%17 = load i16, i16* %16
store i16 %17, i16* %4
%18 = load i16, i16* %2
%19 = zext i16 %18 to i32
%20 = load i32, i32* %8
%21 = add i32 %20, %19
store i32 %21, i32* %8
%22 = load i16, i16* %4
%23 = zext i16 %22 to i32
%24 = ashr i32 %23, 11
%25 = and i32 %24, 31
%26 = mul nsw i32 %25, 255
%27 = sdiv i32 %26, 31
%28 = trunc i32 %27 to i16
store i16 %28, i16* %5
%29 = load i16, i16* %4
%30 = zext i16 %29 to i32
%31 = ashr i32 %30, 5
%32 = and i32 %31, 63
%33 = mul nsw i32 %32, 255
%34 = sdiv i32 %33, 63
%35 = trunc i32 %34 to i16
store i16 %35, i16* %6
%36 = load i16, i16* %4
%37 = zext i16 %36 to i32
%38 = and i32 %37, 31
%39 = mul nsw i32 %38, 255
%40 = sdiv i32 %39, 31
%41 = trunc i32 %40 to i16
store i16 %41, i16* %7
store i16 0, i16* %1
br label %42
42: ; preds = %52, %13
%43 = load i16, i16* %1
%44 = zext i16 %43 to i32
%45 = load i16, i16* %2
%46 = zext i16 %45 to i32
%47 = icmp slt i32 %44, %46
br i1 %47, label %48, label %55
48: ; preds = %42
%49 = call i32 @write(i32 1, i16* %5, i32 1)
%50 = call i32 @write(i32 1, i16* %6, i32 1)
%51 = call i32 @write(i32 1, i16* %7, i32 1)
br label %52
52: ; preds = %48
%53 = load i16, i16* %1
%54 = add i16 %53, 1
store i16 %54, i16* %1
br label %42
55: ; preds = %42
br label %9
56: ; preds = %9
%57 = load i32, i32* @stderr
%58 = load i32, i32* %8
%59 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %58)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [11 x i8] c"%d pixels\0A\00"
define void @from_565_rle() {
%1 = alloca [2 x i16]
%2 = alloca i16
%3 = alloca i16
%4 = alloca i16
%5 = bitcast [2 x i16]* %1 to i8*
%6 = bitcast i16* %2 to i8*
%7 = bitcast i16* %3 to i8*
%8 = bitcast i16* %4 to i8*
%9 = getelementptr inbounds [2 x i16], [2 x i16]* %1, i64 0, i64 0
%10 = call i64 @read(i32 0, i16* %9, i32 4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %41, label %12
12: ; preds = %0
%13 = getelementptr inbounds [2 x i16], [2 x i16]* %1, i64 0, i64 1
br label %17
14: ; preds = %34, %17
%15 = call i64 @read(i32 0, i16* %9, i32 4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %41, label %17
17: ; preds = %12, %14
%18 = phi i32 [ 0, %12 ], [ %22, %14 ]
%19 = load i16, i16* %9
%20 = load i16, i16* %13
%21 = zext i16 %19 to i32
%22 = add i32 %18, %21
%23 = lshr i16 %20, 11
%24 = mul nuw nsw i16 %23, 255
%25 = udiv i16 %24, 31
store i16 %25, i16* %2
%26 = lshr i16 %20, 5
%27 = and i16 %26, 63
%28 = mul nuw nsw i16 %27, 255
%29 = udiv i16 %28, 63
store i16 %29, i16* %3
%30 = and i16 %20, 31
%31 = mul nuw nsw i16 %30, 255
%32 = udiv i16 %31, 31
store i16 %32, i16* %4
%33 = icmp eq i16 %19, 0
br i1 %33, label %14, label %34
34: ; preds = %17, %34
%35 = phi i16 [ %39, %34 ], [ 0, %17 ]
%36 = call i32 @write(i32 1, i16* %2, i32 1)
%37 = call i32 @write(i32 1, i16* %3, i32 1)
%38 = call i32 @write(i32 1, i16* %4, i32 1)
%39 = add nuw i16 %35, 1
%40 = icmp eq i16 %39, %19
br i1 %40, label %14, label %34
41: ; preds = %14, %0
%42 = phi i32 [ 0, %0 ], [ %22, %14 ]
%43 = load i32, i32* @stderr
%44 = tail call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %42)
ret void
}