#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 */

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
int read(int /*<<< orphan*/, unsigned char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, unsigned short *, int);
void to_565_rle(void) {
  unsigned char in[3];
  unsigned short last, color, count;
  unsigned total = 0;
  count = 0;
  while (read(0, in, 3) == 3) {
    color = ((((in[0]) >> 3) << 11) | (((in[1]) >> 2) << 5) | ((in[2]) >> 3));
    if (count) {
      if ((color == last) && (count != 65535)) {
        count++;
        continue;
      } else {
        write(1, &count, 2);
        write(1, &last, 2);
        total += count;
      }
    }
    last = color;
    count = 1;
  }
  if (count) {
    write(1, &count, 2);
    write(1, &last, 2);
    total += count;
  }
  fprintf(stderr, "%d pixels\n", total);
}


x86 assembly (clang, -O0):
<to_565_rle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x10(%rbp)
movw   $0x0,-0xa(%rbp)
lea    -0x3(%rbp),%rsi
xor    %edi,%edi
mov    $0x3,%edx
call   1040 <read@plt>
cmp    $0x3,%eax
jne    1204 <to_565_rle+0xd4>
movzbl -0x3(%rbp),%eax
sar    $0x3,%eax
shl    $0xb,%eax
movzbl -0x2(%rbp),%ecx
sar    $0x2,%ecx
shl    $0x5,%ecx
or     %ecx,%eax
movzbl -0x1(%rbp),%ecx
sar    $0x3,%ecx
or     %ecx,%eax
mov    %ax,-0x8(%rbp)
cmpw   $0x0,-0xa(%rbp)
je     11f1 <to_565_rle+0xc1>
movzwl -0x8(%rbp),%eax
movzwl -0x6(%rbp),%ecx
cmp    %ecx,%eax
jne    11bc <to_565_rle+0x8c>
movzwl -0xa(%rbp),%eax
cmp    $0xffff,%eax
je     11bc <to_565_rle+0x8c>
mov    -0xa(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0xa(%rbp)
jmp    1145 <to_565_rle+0x15>
mov    $0x1,%edi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    $0x1,%edi
lea    -0x6(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
movzwl -0xa(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11f1 <to_565_rle+0xc1>
mov    -0x8(%rbp),%ax
mov    %ax,-0x6(%rbp)
movw   $0x1,-0xa(%rbp)
jmp    1145 <to_565_rle+0x15>
cmpw   $0x0,-0xa(%rbp)
je     123f <to_565_rle+0x10f>
mov    $0x1,%edi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    $0x1,%edi
lea    -0x6(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
movzwl -0xa(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
lea    0xdae(%rip),%rsi
call   1050 <fprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_565_rle>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
lea    0x3(%rsp),%rsi
xor    %edi,%edi
mov    $0x3,%edx
call   1040 <read@plt>
cmp    $0x3,%eax
jne    1242 <to_565_rle+0x112>
xor    %ebp,%ebp
lea    0x3(%rsp),%r13
lea    0x6(%rsp),%r15
xor    %r12d,%r12d
mov    %ebx,%r14d
movzwl %bp,%ebp
nopl   0x0(%rax)
movzbl 0x3(%rsp),%eax
and    $0xfffffff8,%eax
shl    $0x8,%eax
movzbl 0x4(%rsp),%ecx
and    $0xfc,%ecx
lea    (%rax,%rcx,8),%eax
movzbl 0x5(%rsp),%ebx
shr    $0x3,%ebx
or     %eax,%ebx
test   %bp,%bp
je     11eb <to_565_rle+0xbb>
cmp    %r14w,%bx
jne    11c0 <to_565_rle+0x90>
cmp    $0xffff,%bp
je     11c0 <to_565_rle+0x90>
xor    %edi,%edi
mov    %r13,%rsi
mov    $0x3,%edx
call   1040 <read@plt>
add    $0x1,%ebp
cmp    $0x3,%eax
je     1170 <to_565_rle+0x40>
jmp    1212 <to_565_rle+0xe2>
nopl   (%rax)
mov    %bp,(%rsp)
mov    $0x1,%edi
mov    %rsp,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %r15,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
add    %ebp,%r12d
mov    %bx,0x6(%rsp)
movw   $0x1,(%rsp)
xor    %edi,%edi
mov    %r13,%rsi
mov    $0x3,%edx
call   1040 <read@plt>
mov    $0x1,%bp
cmp    $0x3,%eax
je     1166 <to_565_rle+0x36>
mov    %bp,(%rsp)
mov    %rsp,%rsi
mov    $0x1,%edi
mov    $0x2,%edx
call   1030 <write@plt>
lea    0x6(%rsp),%rsi
mov    $0x1,%edi
mov    $0x2,%edx
call   1030 <write@plt>
movzwl %bp,%ebx
add    %r12d,%ebx
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
mov    %ebx,%edx
add    $0x8,%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 @to_565_rle() {
%1 = alloca [3 x i8]
%2 = alloca i16
%3 = alloca i16
%4 = alloca i16
%5 = alloca i32
store i32 0, i32* %5
store i16 0, i16* %4
br label %6
6: ; preds = %51, %40, %0
%7 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
%8 = call i32 @read(i32 0, i8* %7, i32 3)
%9 = icmp eq i32 %8, 3
br i1 %9, label %10, label %53
10: ; preds = %6
%11 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
%12 = load i8, i8* %11
%13 = zext i8 %12 to i32
%14 = ashr i32 %13, 3
%15 = shl i32 %14, 11
%16 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 1
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = ashr i32 %18, 2
%20 = shl i32 %19, 5
%21 = or i32 %15, %20
%22 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 2
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = ashr i32 %24, 3
%26 = or i32 %21, %25
%27 = trunc i32 %26 to i16
store i16 %27, i16* %3
%28 = load i16, i16* %4
%29 = icmp ne i16 %28, 0
br i1 %29, label %30, label %51
30: ; preds = %10
%31 = load i16, i16* %3
%32 = zext i16 %31 to i32
%33 = load i16, i16* %2
%34 = zext i16 %33 to i32
%35 = icmp eq i32 %32, %34
br i1 %35, label %36, label %43
36: ; preds = %30
%37 = load i16, i16* %4
%38 = zext i16 %37 to i32
%39 = icmp ne i32 %38, 65535
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i16, i16* %4
%42 = add i16 %41, 1
store i16 %42, i16* %4
br label %6
43: ; preds = %36, %30
%44 = call i32 @write(i32 1, i16* %4, i32 2)
%45 = call i32 @write(i32 1, i16* %2, i32 2)
%46 = load i16, i16* %4
%47 = zext i16 %46 to i32
%48 = load i32, i32* %5
%49 = add i32 %48, %47
store i32 %49, i32* %5
br label %50
50: ; preds = %43
br label %51
51: ; preds = %50, %10
%52 = load i16, i16* %3
store i16 %52, i16* %2
store i16 1, i16* %4
br label %6
53: ; preds = %6
%54 = load i16, i16* %4
%55 = icmp ne i16 %54, 0
br i1 %55, label %56, label %63
56: ; preds = %53
%57 = call i32 @write(i32 1, i16* %4, i32 2)
%58 = call i32 @write(i32 1, i16* %2, i32 2)
%59 = load i16, i16* %4
%60 = zext i16 %59 to i32
%61 = load i32, i32* %5
%62 = add i32 %61, %60
store i32 %62, i32* %5
br label %63
63: ; preds = %56, %53
%64 = load i32, i32* @stderr
%65 = load i32, i32* %5
%66 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %65)
ret void
}

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