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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int fread(int *, int, int, int /*<<< orphan*/ *);
int fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 3) {
    fprintf(stderr, "Usage: %s file-in.rgb24 file-out.rgb16\n", argv[0]);
    return -1;
  }
  FILE *file_in = fopen(argv[1], "rb");
  if (!file_in) {
    fprintf(stderr, "error: %s, could not load input file '%s' for reading\n",
            argv[0], argv[1]);
    exit(1);
  }
  FILE *file_out = fopen(argv[2], "wb");
  if (!file_out) {
    fprintf(stderr, "error: %s, could not load output file '%s' for writing\n",
            argv[0], argv[2]);
    fclose(file_in);
    exit(1);
  }
  uint8_t rgb24[3];
  uint8_t rgb16[2];
  while (1) {
    int num_read = fread(rgb24, sizeof(uint8_t), 3, file_in);
    if (feof(file_in)) {
      break;
    } else if (num_read != 3) {
      fprintf(stderr, "error: %s, could not read in data from '%s'\n", argv[0],
              argv[1]);
      exit(1);
    }
    uint8_t red = rgb24[0];
    uint8_t green = rgb24[1];
    uint8_t blue = rgb24[2];
    rgb16[0] = (red & 0xf8) | ((green >> 5) & 0x07);
    rgb16[1] = ((green << 3) & 0xe0) | ((blue >> 3) & 0x1f);
    int num_written = fwrite(rgb16, sizeof(uint8_t), 2, file_out);
    if (num_written != 2) {
      fprintf(stderr, "error: %s, could not write data...\n", argv[0]);
      exit(1);
    }
  };
  fclose(file_in);
  fclose(file_out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11ba <main+0x4a>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138a <main+0x21a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe5f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120d <main+0x9d>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xe2f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe46(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1269 <main+0xf9>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    126e <main+0xfe>
lea    -0x2c(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x4,%esi
mov    $0x3,%edx
call   1030 <fread@plt>
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     12a0 <main+0x130>
jmp    1371 <main+0x201>
cmpl   $0x3,-0x38(%rbp)
je     12da <main+0x16a>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    12df <main+0x16f>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x3c(%rbp),%eax
and    $0xf8,%eax
mov    -0x40(%rbp),%ecx
sar    $0x5,%ecx
and    $0x7,%ecx
or     %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x40(%rbp),%eax
shl    $0x3,%eax
and    $0xe0,%eax
mov    -0x44(%rbp),%ecx
sar    $0x3,%ecx
and    $0x1f,%ecx
or     %ecx,%eax
mov    %eax,-0x30(%rbp)
lea    -0x34(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x4,%esi
mov    $0x2,%edx
call   1090 <fwrite@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x2,-0x48(%rbp)
je     136c <main+0x1fc>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd6f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    126e <main+0xfe>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbp
cmp    $0x2,%edi
jg     11ab <main+0x3b>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xe66(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    12af <main+0x13f>
mov    0x8(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12eb <main+0x17b>
mov    %rax,%rbx
mov    0x10(%rbp),%rdi
lea    0xe90(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1305 <main+0x195>
mov    %rax,%r15
mov    %rbp,0x8(%rsp)
lea    0x1c(%rsp),%r12
mov    $0xf8,%r14d
lea    0x14(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x4,%esi
mov    $0x3,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %rax,%rax
jne    129d <main+0x12d>
cmp    $0x3,%ebp
jne    12be <main+0x14e>
mov    0x20(%rsp),%eax
mov    0x24(%rsp),%ecx
mov    0x1c(%rsp),%edx
and    %r14d,%edx
mov    %eax,%esi
shr    $0x5,%esi
and    $0x7,%esi
or     %edx,%esi
mov    %esi,0x14(%rsp)
and    $0x1c,%eax
shr    $0x3,%ecx
and    $0x1f,%ecx
lea    (%rcx,%rax,8),%eax
mov    %eax,0x18(%rsp)
mov    %r13,%rdi
mov    $0x4,%esi
mov    $0x2,%edx
mov    %r15,%rcx
call   1090 <fwrite@plt>
cmp    $0x2,%eax
je     1200 <main+0x90>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rax
mov    (%rax),%rdx
lea    0xe3e(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rax
mov    (%rax),%rdx
mov    0x8(%rax),%rcx
lea    0xdc3(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
mov    0x8(%rbp),%rcx
lea    0xd28(%rip),%rsi
jmp    12da <main+0x16a>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
mov    0x10(%rbp),%rcx
lea    0xd48(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: %s file-in.rgb24 file-out.rgb16\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [55 x i8] c"error: %s, could not load input file '%s' for reading\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [56 x i8] c"error: %s, could not load output file '%s' for writing\0A\00"
@.str.5 = [45 x i8] c"error: %s, could not read in data from '%s'\0A\00"
@.str.6 = [36 x i8] c"error: %s, could not write data...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [3 x i32]
%9 = alloca [2 x i32]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 3
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 -1, i32* %3
br label %122
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %6
%28 = load i32*, i32** %6
%29 = icmp ne i32* %28, null
br i1 %29, label %40, label %30
30: ; preds = %23
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %34, i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %23
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 2
%43 = load i8*, i8** %42
%44 = call i32* @fopen(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %44, i32** %7
%45 = load i32*, i32** %7
%46 = icmp ne i32* %45, null
br i1 %46, label %59, label %47
47: ; preds = %40
%48 = load i32, i32* @stderr
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 0
%51 = load i8*, i8** %50
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 2
%54 = load i8*, i8** %53
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0), i8* %51, i8* %54)
%56 = load i32*, i32** %6
%57 = call i32 @fclose(i32* %56)
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %40
br label %60
60: ; preds = %59, %116
%61 = getelementptr inbounds [3 x i32], [3 x i32]* %8, i64 0, i64 0
%62 = load i32*, i32** %6
%63 = call i32 @fread(i32* %61, i32 4, i32 3, i32* %62)
store i32 %63, i32* %10
%64 = load i32*, i32** %6
%65 = call i64 @feof(i32* %64)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %60
br label %117
68: ; preds = %60
%69 = load i32, i32* %10
%70 = icmp ne i32 %69, 3
br i1 %70, label %71, label %81
71: ; preds = %68
%72 = load i32, i32* @stderr
%73 = load i8**, i8*** %5
%74 = getelementptr inbounds i8*, i8** %73, i64 0
%75 = load i8*, i8** %74
%76 = load i8**, i8*** %5
%77 = getelementptr inbounds i8*, i8** %76, i64 1
%78 = load i8*, i8** %77
%79 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i8* %75, i8* %78)
%80 = call i32 @exit(i32 1)
unreachable
81: ; preds = %68
br label %82
82: ; preds = %81
%83 = getelementptr inbounds [3 x i32], [3 x i32]* %8, i64 0, i64 0
%84 = load i32, i32* %83
store i32 %84, i32* %11
%85 = getelementptr inbounds [3 x i32], [3 x i32]* %8, i64 0, i64 1
%86 = load i32, i32* %85
store i32 %86, i32* %12
%87 = getelementptr inbounds [3 x i32], [3 x i32]* %8, i64 0, i64 2
%88 = load i32, i32* %87
store i32 %88, i32* %13
%89 = load i32, i32* %11
%90 = and i32 %89, 248
%91 = load i32, i32* %12
%92 = ashr i32 %91, 5
%93 = and i32 %92, 7
%94 = or i32 %90, %93
%95 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
store i32 %94, i32* %95
%96 = load i32, i32* %12
%97 = shl i32 %96, 3
%98 = and i32 %97, 224
%99 = load i32, i32* %13
%100 = ashr i32 %99, 3
%101 = and i32 %100, 31
%102 = or i32 %98, %101
%103 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
store i32 %102, i32* %103
%104 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%105 = load i32*, i32** %7
%106 = call i32 @fwrite(i32* %104, i32 4, i32 2, i32* %105)
store i32 %106, i32* %14
%107 = load i32, i32* %14
%108 = icmp ne i32 %107, 2
br i1 %108, label %109, label %116
109: ; preds = %82
%110 = load i32, i32* @stderr
%111 = load i8**, i8*** %5
%112 = getelementptr inbounds i8*, i8** %111, i64 0
%113 = load i8*, i8** %112
%114 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %110, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %113)
%115 = call i32 @exit(i32 1)
unreachable
116: ; preds = %82
br label %60
117: ; preds = %67
%118 = load i32*, i32** %6
%119 = call i32 @fclose(i32* %118)
%120 = load i32*, i32** %7
%121 = call i32 @fclose(i32* %120)
store i32 0, i32* %3
br label %122
122: ; preds = %117, %17
%123 = load i32, i32* %3
ret i32 %123
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: %s file-in.rgb24 file-out.rgb16\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [55 x i8] c"error: %s, could not load input file '%s' for reading\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [56 x i8] c"error: %s, could not load output file '%s' for writing\0A\00"
@.str.5 = [45 x i8] c"error: %s, could not read in data from '%s'\0A\00"
@.str.6 = [36 x i8] c"error: %s, could not write data...\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [3 x i32]
%4 = alloca [2 x i32]
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %76
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %21
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %1
%18 = load i8*, i8** %11
%19 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %17, i8* %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %10
%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.3, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %33
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %1
%29 = load i8*, i8** %22
%30 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0), i8* %28, i8* %29)
%31 = tail call i32 @fclose(i32* %13)
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %21
%34 = bitcast [3 x i32]* %3 to i8*
%35 = bitcast [2 x i32]* %4 to i8*
%36 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
%37 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 1
%38 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 2
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
br label %41
41: ; preds = %53, %33
%42 = call i32 @fread(i32* %36, i32 4, i32 3, i32* %13)
%43 = tail call i64 @feof(i32* %13)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %73
45: ; preds = %41
%46 = icmp eq i32 %42, 3
br i1 %46, label %53, label %47
47: ; preds = %45
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %1
%50 = load i8*, i8** %11
%51 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i8* %49, i8* %50)
%52 = tail call i32 @exit(i32 1)
unreachable
53: ; preds = %45
%54 = load i32, i32* %36
%55 = load i32, i32* %37
%56 = load i32, i32* %38
%57 = and i32 %54, 248
%58 = lshr i32 %55, 5
%59 = and i32 %58, 7
%60 = or i32 %59, %57
store i32 %60, i32* %39
%61 = shl i32 %55, 3
%62 = and i32 %61, 224
%63 = lshr i32 %56, 3
%64 = and i32 %63, 31
%65 = or i32 %64, %62
store i32 %65, i32* %40
%66 = call i32 @fwrite(i32* %39, i32 4, i32 2, i32* %24)
%67 = icmp eq i32 %66, 2
br i1 %67, label %41, label %68
68: ; preds = %53
%69 = load i32, i32* @stderr
%70 = load i8*, i8** %1
%71 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %70)
%72 = tail call i32 @exit(i32 1)
unreachable
73: ; preds = %41
%74 = tail call i32 @fclose(i32* %13)
%75 = tail call i32 @fclose(i32* %24)
br label %76
76: ; preds = %73, %6
%77 = phi i32 [ -1, %6 ], [ 0, %73 ]
ret i32 %77
}