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

/* Variables and functions */
int /*<<< orphan*/ assert(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*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char **argv) {
  assert(argc == 2);
  FILE *fpIn = NULL;
  FILE *fpOut = NULL;
  fpIn = fopen(argv[1], "rb");
  char outputFile[256];
  strcpy(outputFile, argv[1]);
  strcat(outputFile, ".rle");
  fpOut = fopen(outputFile, "wb");
  unsigned char current = 0;
  unsigned char previous = 0;
  unsigned char counter = 0;
  fread(&previous, sizeof(previous), 1, fpIn);
  while (1 == fread(&current, sizeof(current), 1, fpIn)) {
    if (counter == 255) {
      fwrite(&counter, sizeof(counter), 1, fpOut);
      fwrite(&previous, sizeof(previous), 1, fpOut);
      counter = 0;
    } else if (current == previous) {
      counter++;
    } else {
      fwrite(&counter, sizeof(counter), 1, fpOut);
      fwrite(&previous, sizeof(previous), 1, fpOut);
      counter = 0;
    }
    previous = current;
  }
  fwrite(&counter, sizeof(counter), 1, fpOut);
  fwrite(&current, sizeof(current), 1, fpOut);
  fclose(fpIn);
  fclose(fpOut);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe47(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
lea    -0x120(%rbp),%rdi
lea    0xe1f(%rip),%rsi
call   1080 <strcat@plt>
lea    -0x120(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
movb   $0x0,-0x121(%rbp)
movb   $0x0,-0x122(%rbp)
movb   $0x0,-0x123(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0x122(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    -0x18(%rbp),%rcx
lea    -0x121(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    1312 <main+0x1a2>
movzbl -0x123(%rbp),%eax
cmp    $0xff,%eax
jne    129e <main+0x12e>
mov    -0x20(%rbp),%rcx
lea    -0x123(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <fwrite@plt>
mov    -0x20(%rbp),%rcx
lea    -0x122(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <fwrite@plt>
movb   $0x0,-0x123(%rbp)
jmp    1301 <main+0x191>
movzbl -0x121(%rbp),%eax
movzbl -0x122(%rbp),%ecx
cmp    %ecx,%eax
jne    12c7 <main+0x157>
mov    -0x123(%rbp),%al
add    $0x1,%al
mov    %al,-0x123(%rbp)
jmp    12fc <main+0x18c>
mov    -0x20(%rbp),%rcx
lea    -0x123(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <fwrite@plt>
mov    -0x20(%rbp),%rcx
lea    -0x122(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <fwrite@plt>
movb   $0x0,-0x123(%rbp)
jmp    1301 <main+0x191>
mov    -0x121(%rbp),%al
mov    %al,-0x122(%rbp)
jmp    122c <main+0xbc>
mov    -0x20(%rbp),%rcx
lea    -0x123(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <fwrite@plt>
mov    -0x20(%rbp),%rcx
lea    -0x121(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
cmp    $0x2,%edi
sete   %al
mov    %eax,%edi
call   1070 <assert@plt>
mov    0x8(%rbx),%rdi
lea    0xe62(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
mov    0x8(%rbx),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe45(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
lea    0xe3b(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r12
movb   $0x0,0xe(%rsp)
movb   $0x0,0xf(%rsp)
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    0xf(%rsp),%r13b
lea    0xe(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
xor    %ebp,%ebp
cmp    $0x1,%eax
jne    12a3 <main+0x133>
lea    0xe(%rsp),%r15
jmp    1267 <main+0xf7>
nopw   0x0(%rax,%rax,1)
movsbl %bpl,%edi
mov    %r12,%rsi
call   1060 <fputc@plt>
movsbl %r13b,%edi
mov    %r12,%rsi
call   1060 <fputc@plt>
xor    %ebp,%ebp
mov    %ebx,%r13d
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    12a3 <main+0x133>
cmp    $0xff,%bpl
je     1280 <main+0x110>
movzbl 0xe(%rsp),%ebx
cmp    %r13b,%bl
jne    1230 <main+0xc0>
add    $0x1,%bpl
jmp    124d <main+0xdd>
nopl   (%rax)
mov    $0xffffffff,%edi
mov    %r12,%rsi
call   1060 <fputc@plt>
movsbl %r13b,%edi
mov    %r12,%rsi
call   1060 <fputc@plt>
xor    %ebp,%ebp
movzbl 0xe(%rsp),%r13d
jmp    124d <main+0xdd>
movsbl %bpl,%edi
mov    %r12,%rsi
call   1060 <fputc@plt>
movsbl 0xe(%rsp),%edi
mov    %r12,%rsi
call   1060 <fputc@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [5 x i8] c".rle\00"
@.str.2 = [3 x i8] c"wb\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 [256 x i8]
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 2
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
store i32* null, i32** %6
store i32* null, i32** %7
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %6
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @strcpy(i8* %20, i8* %23)
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%26 = call i32 @strcat(i8* %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %28, i32** %7
store i8 0, i8* %9
store i8 0, i8* %10
store i8 0, i8* %11
%29 = load i32*, i32** %6
%30 = call i32 @fread(i8* %10, i32 1, i32 1, i32* %29)
br label %31
31: ; preds = %59, %2
%32 = load i32*, i32** %6
%33 = call i32 @fread(i8* %9, i32 1, i32 1, i32* %32)
%34 = icmp eq i32 1, %33
br i1 %34, label %35, label %61
35: ; preds = %31
%36 = load i8, i8* %11
%37 = zext i8 %36 to i32
%38 = icmp eq i32 %37, 255
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = load i32*, i32** %7
%41 = call i32 @fwrite(i8* %11, i32 1, i32 1, i32* %40)
%42 = load i32*, i32** %7
%43 = call i32 @fwrite(i8* %10, i32 1, i32 1, i32* %42)
store i8 0, i8* %11
br label %59
44: ; preds = %35
%45 = load i8, i8* %9
%46 = zext i8 %45 to i32
%47 = load i8, i8* %10
%48 = zext i8 %47 to i32
%49 = icmp eq i32 %46, %48
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = load i8, i8* %11
%52 = add i8 %51, 1
store i8 %52, i8* %11
br label %58
53: ; preds = %44
%54 = load i32*, i32** %7
%55 = call i32 @fwrite(i8* %11, i32 1, i32 1, i32* %54)
%56 = load i32*, i32** %7
%57 = call i32 @fwrite(i8* %10, i32 1, i32 1, i32* %56)
store i8 0, i8* %11
br label %58
58: ; preds = %53, %50
br label %59
59: ; preds = %58, %39
%60 = load i8, i8* %9
store i8 %60, i8* %10
br label %31
61: ; preds = %31
%62 = load i32*, i32** %7
%63 = call i32 @fwrite(i8* %11, i32 1, i32 1, i32* %62)
%64 = load i32*, i32** %7
%65 = call i32 @fwrite(i8* %9, i32 1, i32 1, i32* %64)
%66 = load i32*, i32** %6
%67 = call i32 @fclose(i32* %66)
%68 = load i32*, i32** %7
%69 = call i32 @fclose(i32* %68)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [5 x i8] c".rle\00"
@.str.2 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = alloca i8
%5 = alloca i8
%6 = icmp eq i32 %0, 2
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%13 = load i8*, i8** %9
%14 = call i32 @strcpy(i8* %12, i8* %13)
%15 = call i32 @strcat(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i8 0, i8* %4
store i8 0, i8* %5
%17 = call i32 @fread(i8* %5, i32 1, i32 1, i32* %11)
%18 = load i8, i8* %5
%19 = call i32 @fread(i8* %4, i32 1, i32 1, i32* %11)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %45
21: ; preds = %2, %40
%22 = phi i8 [ %42, %40 ], [ 0, %2 ]
%23 = phi i8 [ %41, %40 ], [ %18, %2 ]
%24 = icmp eq i8 %22, -1
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = call i32 @fputc(i32 -1, i32* %16)
%27 = sext i8 %23 to i32
%28 = call i32 @fputc(i32 %27, i32* %16)
%29 = load i8, i8* %4
br label %40
30: ; preds = %21
%31 = load i8, i8* %4
%32 = icmp eq i8 %31, %23
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = add nuw i8 %22, 1
br label %40
35: ; preds = %30
%36 = sext i8 %22 to i32
%37 = call i32 @fputc(i32 %36, i32* %16)
%38 = sext i8 %23 to i32
%39 = call i32 @fputc(i32 %38, i32* %16)
br label %40
40: ; preds = %33, %35, %25
%41 = phi i8 [ %29, %25 ], [ %23, %33 ], [ %31, %35 ]
%42 = phi i8 [ 0, %25 ], [ %34, %33 ], [ 0, %35 ]
%43 = call i32 @fread(i8* %4, i32 1, i32 1, i32* %11)
%44 = icmp eq i32 %43, 1
br i1 %44, label %21, label %45
45: ; preds = %40, %2
%46 = phi i8 [ 0, %2 ], [ %42, %40 ]
%47 = sext i8 %46 to i32
%48 = call i32 @fputc(i32 %47, i32* %16)
%49 = load i8, i8* %4
%50 = sext i8 %49 to i32
%51 = call i32 @fputc(i32 %50, i32* %16)
%52 = call i32 @fclose(i32* %11)
%53 = call i32 @fclose(i32* %16)
ret i32 0
}