#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
unsigned char round(float);
int main(int argc, char **argv) {
  unsigned char rgb888[3];
  unsigned char rgb565[2];
  unsigned char red, green, blue;
  FILE *infile, *outfile;
  if (argc != 3) {
    printf("Incorrect usage...\n");
    printf("Try %s <infile> <outfile>\n", argv[0]);
    exit(1);
  }
  infile = fopen(argv[1], "r");
  if (infile == 0) {
    printf("Unable to open input file: %s for reading.\n");
    exit(1);
  }
  outfile = fopen(argv[2], "w+");
  if (outfile == 0) {
    printf("Unable to open output file: %s for writing.\n");
    fclose(infile);
    exit(1);
  }
  while (fread(rgb888, 1, 3, infile) == 3) {
    red = round(((float)rgb888[2]) / 8.0);
    green = round(((float)rgb888[1]) / 4.0);
    blue = round(((float)rgb888[0]) / 8.0);
    rgb565[0] = red | (green << 5);
    rgb565[1] = (green >> 3) | (blue << 3);
    fwrite(rgb565, 1, 2, outfile);
  }
  fclose(infile);
  fclose(outfile);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11bd <main+0x4d>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe73(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f8 <main+0x88>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe66(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123c <main+0xcc>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1241 <main+0xd1>
lea    -0x13(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   1030 <fread@plt>
cmp    $0x3,%eax
jne    130c <main+0x19c>
movzbl -0x11(%rbp),%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xd8b(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1070 <round@plt>
mov    %al,-0x16(%rbp)
movzbl -0x12(%rbp),%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xd6f(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1070 <round@plt>
mov    %al,-0x17(%rbp)
movzbl -0x13(%rbp),%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xd43(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1070 <round@plt>
mov    %al,-0x18(%rbp)
movzbl -0x16(%rbp),%eax
movzbl -0x17(%rbp),%ecx
shl    $0x5,%ecx
or     %ecx,%eax
mov    %al,-0x15(%rbp)
movzbl -0x17(%rbp),%eax
sar    $0x3,%eax
movzbl -0x18(%rbp),%ecx
shl    $0x3,%ecx
or     %ecx,%eax
mov    %al,-0x14(%rbp)
lea    -0x15(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1090 <fwrite@plt>
jmp    1241 <main+0xd1>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    12cb <main+0x14b>
mov    0x8(%rbx),%rdi
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12f2 <main+0x172>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
lea    0xe9b(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     130a <main+0x18a>
mov    %rax,%r13
lea    0x3(%rsp),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    $0x3,%eax
jne    12aa <main+0x12a>
lea    0x6(%rsp),%r15
lea    0x3(%rsp),%r12
nopl   0x0(%rax)
movzbl 0x5(%rsp),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  0xdec(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1080 <round@plt>
movzbl 0x4(%rsp),%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
mov    %eax,%ebp
mulsd  0xdd1(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
call   1080 <round@plt>
movzbl 0x3(%rsp),%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
mov    %eax,%ebx
mulsd  0xdaa(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
call   1080 <round@plt>
mov    %ebx,%ecx
shl    $0x5,%cl
or     %bpl,%cl
mov    %cl,0x6(%rsp)
shr    $0x3,%bl
shl    $0x3,%al
or     %bl,%al
mov    %al,0x7(%rsp)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %r13,%rcx
call   10a0 <fwrite@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    $0x3,%eax
je     1200 <main+0x80>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb7(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%rsi
lea    0xd2f(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd34(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd4b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"Incorrect usage...\0A\00"
@.str.1 = [27 x i8] c"Try %s <infile> <outfile>\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [44 x i8] c"Unable to open input file: %s for reading.\0A\00"
@.str.4 = [3 x i8] c"w+\00"
@.str.5 = [45 x i8] c"Unable to open output file: %s for writing.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [3 x i8]
%7 = alloca [2 x i8]
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
%11 = alloca i32*
%12 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %22
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %26, i32** %11
%27 = load i32*, i32** %11
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %22
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %36, i32** %12
%37 = load i32*, i32** %12
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %44
39: ; preds = %32
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
%41 = load i32*, i32** %11
%42 = call i32 @fclose(i32* %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %32
br label %45
45: ; preds = %50, %44
%46 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%47 = load i32*, i32** %11
%48 = call i32 @fread(i8* %46, i32 1, i32 3, i32* %47)
%49 = icmp eq i32 %48, 3
br i1 %49, label %50, label %92
50: ; preds = %45
%51 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 2
%52 = load i8, i8* %51
%53 = uitofp i8 %52 to float
%54 = fpext float %53 to double
%55 = fdiv double %54, 8.000000e+00
%56 = fptrunc double %55 to float
%57 = call zeroext i8 @round(float %56)
store i8 %57, i8* %8
%58 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 1
%59 = load i8, i8* %58
%60 = uitofp i8 %59 to float
%61 = fpext float %60 to double
%62 = fdiv double %61, 4.000000e+00
%63 = fptrunc double %62 to float
%64 = call zeroext i8 @round(float %63)
store i8 %64, i8* %9
%65 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%66 = load i8, i8* %65
%67 = uitofp i8 %66 to float
%68 = fpext float %67 to double
%69 = fdiv double %68, 8.000000e+00
%70 = fptrunc double %69 to float
%71 = call zeroext i8 @round(float %70)
store i8 %71, i8* %10
%72 = load i8, i8* %8
%73 = zext i8 %72 to i32
%74 = load i8, i8* %9
%75 = zext i8 %74 to i32
%76 = shl i32 %75, 5
%77 = or i32 %73, %76
%78 = trunc i32 %77 to i8
%79 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
store i8 %78, i8* %79
%80 = load i8, i8* %9
%81 = zext i8 %80 to i32
%82 = ashr i32 %81, 3
%83 = load i8, i8* %10
%84 = zext i8 %83 to i32
%85 = shl i32 %84, 3
%86 = or i32 %82, %85
%87 = trunc i32 %86 to i8
%88 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 1
store i8 %87, i8* %88
%89 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%90 = load i32*, i32** %12
%91 = call i32 @fwrite(i8* %89, i32 1, i32 2, i32* %90)
br label %45
92: ; preds = %45
%93 = load i32*, i32** %11
%94 = call i32 @fclose(i32* %93)
%95 = load i32*, i32** %12
%96 = call i32 @fclose(i32* %95)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [27 x i8] c"Try %s <infile> <outfile>\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [44 x i8] c"Unable to open input file: %s for reading.\0A\00"
@.str.4 = [3 x i8] c"w+\00"
@.str.5 = [45 x i8] c"Unable to open output file: %s for writing.\0A\00"
@str = [19 x i8] c"Incorrect usage...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [3 x i8]
%4 = alloca [2 x i8]
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 3
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = tail call i32* @fopen(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %33, label %26
26: ; preds = %21
%27 = call i32 @fread(i8* %5, i32 1, i32 3, i32* %16)
%28 = icmp eq i32 %27, 3
br i1 %28, label %29, label %61
29: ; preds = %26
%30 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 2
%31 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
%32 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
br label %37
33: ; preds = %21
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
%35 = tail call i32 @fclose(i32* %16)
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %29, %37
%38 = load i8, i8* %30
%39 = uitofp i8 %38 to double
%40 = fmul double %39, 1.250000e-01
%41 = fptrunc double %40 to float
%42 = tail call zeroext i8 @round(float %41)
%43 = load i8, i8* %31
%44 = uitofp i8 %43 to double
%45 = fmul double %44, 2.500000e-01
%46 = fptrunc double %45 to float
%47 = tail call zeroext i8 @round(float %46)
%48 = load i8, i8* %5
%49 = uitofp i8 %48 to double
%50 = fmul double %49, 1.250000e-01
%51 = fptrunc double %50 to float
%52 = tail call zeroext i8 @round(float %51)
%53 = shl i8 %47, 5
%54 = or i8 %53, %42
store i8 %54, i8* %6
%55 = lshr i8 %47, 3
%56 = shl i8 %52, 3
%57 = or i8 %56, %55
store i8 %57, i8* %32
%58 = call i32 @fwrite(i8* %6, i32 1, i32 2, i32* %24)
%59 = call i32 @fread(i8* %5, i32 1, i32 3, i32* %16)
%60 = icmp eq i32 %59, 3
br i1 %60, label %37, label %61
61: ; preds = %37, %26
%62 = tail call i32 @fclose(i32* %16)
%63 = tail call i32 @fclose(i32* %24)
ret i32 0
}