#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*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int main() {
  FILE *fp;
  long fileSize;
  fp = fopen("test.bmp", "rb");
  fseek(fp, 0, SEEK_END);
  fileSize = ftell(fp);
  rewind(fp);
  unsigned char *buffer;
  buffer = (unsigned char *)malloc(sizeof(unsigned char) * fileSize);
  fread(buffer, 1, fileSize, fp);
  unsigned char current = buffer[0];
  int count = 1;
  unsigned char *output =
      (unsigned char *)malloc(sizeof(unsigned char) * fileSize);
  int output_index = 0;
  for (int i = 1; i < fileSize; i++) {
    printf("%x ", current);
    if (current == buffer[i]) {
      count++;
    } else {
      if (count > 1) {
        output[output_index++] = current;
        output[output_index++] = current;
        output[output_index++] = count;
      } else {
        output[output_index++] = current;
      }
      count = 1;
    }
    current = buffer[i];
  }
  fclose(fp);
  printf("\n\n");
  printf("%d\n", output_index);
  for (int i = 0; i < output_index; i++) {
    printf("%x ", output[i]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x21(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x1,-0x38(%rbp)
movslq -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1304 <main+0x184>
movzbl -0x21(%rbp),%esi
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movzbl -0x21(%rbp),%eax
mov    -0x20(%rbp),%rcx
movslq -0x38(%rbp),%rdx
movzbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    1270 <main+0xf0>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    12e8 <main+0x168>
cmpl   $0x1,-0x28(%rbp)
jle    12c9 <main+0x149>
mov    -0x21(%rbp),%dl
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x21(%rbp),%dl
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
mov    %al,%dl
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    12e1 <main+0x161>
mov    -0x21(%rbp),%dl
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movl   $0x1,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    122a <main+0xaa>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xcfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x34(%rbp),%esi
lea    0xcee(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1367 <main+0x1e7>
mov    -0x30(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xcba(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1333 <main+0x1b3>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe5b(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r14
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r14,%rdi
call   1080 <ftell@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1070 <rewind@plt>
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    $0x1,%ebp
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r14,0x8(%rsp)
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %r12,(%rsp)
mov    (%r12),%r12b
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %r15,0x10(%rsp)
cmp    $0x2,%r15
jl     128e <main+0xfe>
mov    $0x1,%r13d
xor    %ebx,%ebx
jmp    123e <main+0xae>
nopl   0x0(%rax)
add    $0x1,%ebp
add    $0x1,%r13
cmp    %r13,0x10(%rsp)
je     128e <main+0xfe>
movzbl %r12b,%r15d
lea    0xdc3(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rsp),%rax
cmp    (%rax,%r13,1),%r15b
je     1230 <main+0xa0>
cmp    $0x2,%ebp
jl     1274 <main+0xe4>
movslq %ebx,%rax
mov    %r12b,(%r14,%rax,1)
lea    0x2(%rax),%ebx
mov    %r12b,0x1(%r14,%rax,1)
mov    %ebp,%r12d
movslq %ebx,%rbx
mov    %r12b,(%r14,%rbx,1)
add    $0x1,%ebx
mov    (%rsp),%rax
movzbl (%rax,%r13,1),%r12d
mov    $0x1,%ebp
jmp    1233 <main+0xa3>
mov    0x8(%rsp),%rdi
call   1050 <fclose@plt>
lea    0xd73(%rip),%rdi
call   1030 <puts@plt>
lea    0xd65(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebx,%ebx
jle    12e8 <main+0x158>
mov    %ebx,%ebp
lea    0xd4b(%rip),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%r14,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    12d0 <main+0x140>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.bmp\00"
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.2 = [4 x i8] c"%x \00"
@.str.3 = [3 x i8] c"\0A\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
%11 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %2
%12 = load i32*, i32** %2
%13 = load i32, i32* @SEEK_END
%14 = call i32 @fseek(i32* %12, i32 0, i32 %13)
%15 = load i32*, i32** %2
%16 = call i64 @ftell(i32* %15)
store i64 %16, i64* %3
%17 = load i32*, i32** %2
%18 = call i32 @rewind(i32* %17)
%19 = load i64, i64* %3
%20 = mul i64 1, %19
%21 = trunc i64 %20 to i32
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = load i64, i64* %3
%26 = load i32*, i32** %2
%27 = call i32 @fread(i8* %24, i32 1, i64 %25, i32* %26)
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i64 0
%30 = load i8, i8* %29
store i8 %30, i8* %5
store i32 1, i32* %6
%31 = load i64, i64* %3
%32 = mul i64 1, %31
%33 = trunc i64 %32 to i32
%34 = call i64 @malloc(i32 %33)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %7
store i32 0, i32* %8
store i32 1, i32* %9
br label %36
36: ; preds = %94, %0
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = load i64, i64* %3
%40 = icmp slt i64 %38, %39
br i1 %40, label %41, label %97
41: ; preds = %36
%42 = load i8, i8* %5
%43 = zext i8 %42 to i32
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %43)
%45 = load i8, i8* %5
%46 = zext i8 %45 to i32
%47 = load i8*, i8** %4
%48 = load i32, i32* %9
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = icmp eq i32 %46, %52
br i1 %53, label %54, label %57
54: ; preds = %41
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 1
store i32 %56, i32* %6
br label %88
57: ; preds = %41
%58 = load i32, i32* %6
%59 = icmp sgt i32 %58, 1
br i1 %59, label %60, label %80
60: ; preds = %57
%61 = load i8, i8* %5
%62 = load i8*, i8** %7
%63 = load i32, i32* %8
%64 = add nsw i32 %63, 1
store i32 %64, i32* %8
%65 = sext i32 %63 to i64
%66 = getelementptr inbounds i8, i8* %62, i64 %65
store i8 %61, i8* %66
%67 = load i8, i8* %5
%68 = load i8*, i8** %7
%69 = load i32, i32* %8
%70 = add nsw i32 %69, 1
store i32 %70, i32* %8
%71 = sext i32 %69 to i64
%72 = getelementptr inbounds i8, i8* %68, i64 %71
store i8 %67, i8* %72
%73 = load i32, i32* %6
%74 = trunc i32 %73 to i8
%75 = load i8*, i8** %7
%76 = load i32, i32* %8
%77 = add nsw i32 %76, 1
store i32 %77, i32* %8
%78 = sext i32 %76 to i64
%79 = getelementptr inbounds i8, i8* %75, i64 %78
store i8 %74, i8* %79
br label %87
80: ; preds = %57
%81 = load i8, i8* %5
%82 = load i8*, i8** %7
%83 = load i32, i32* %8
%84 = add nsw i32 %83, 1
store i32 %84, i32* %8
%85 = sext i32 %83 to i64
%86 = getelementptr inbounds i8, i8* %82, i64 %85
store i8 %81, i8* %86
br label %87
87: ; preds = %80, %60
store i32 1, i32* %6
br label %88
88: ; preds = %87, %54
%89 = load i8*, i8** %4
%90 = load i32, i32* %9
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8, i8* %89, i64 %91
%93 = load i8, i8* %92
store i8 %93, i8* %5
br label %94
94: ; preds = %88
%95 = load i32, i32* %9
%96 = add nsw i32 %95, 1
store i32 %96, i32* %9
br label %36
97: ; preds = %36
%98 = load i32*, i32** %2
%99 = call i32 @fclose(i32* %98)
%100 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%101 = load i32, i32* %8
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %101)
store i32 0, i32* %10
br label %103
103: ; preds = %115, %97
%104 = load i32, i32* %10
%105 = load i32, i32* %8
%106 = icmp slt i32 %104, %105
br i1 %106, label %107, label %118
107: ; preds = %103
%108 = load i8*, i8** %7
%109 = load i32, i32* %10
%110 = sext i32 %109 to i64
%111 = getelementptr inbounds i8, i8* %108, i64 %110
%112 = load i8, i8* %111
%113 = zext i8 %112 to i32
%114 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %113)
br label %115
115: ; preds = %107
%116 = load i32, i32* %10
%117 = add nsw i32 %116, 1
store i32 %117, i32* %10
br label %103
118: ; preds = %103
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.bmp\00"
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.2 = [4 x i8] c"%x \00"
@.str.4 = [4 x i8] c"%d\0A\00"
@str = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%2 = load i32, i32* @SEEK_END
%3 = tail call i32 @fseek(i32* %1, i32 0, i32 %2)
%4 = tail call i64 @ftell(i32* %1)
%5 = tail call i32 @rewind(i32* %1)
%6 = trunc i64 %4 to i32
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = tail call i32 @fread(i8* %8, i32 1, i64 %4, i32* %1)
%10 = load i8, i8* %8
%11 = tail call i64 @malloc(i32 %6)
%12 = inttoptr i64 %11 to i8*
%13 = icmp sgt i64 %4, 1
br i1 %13, label %22, label %14
14: ; preds = %51, %0
%15 = phi i32 [ 0, %0 ], [ %54, %51 ]
%16 = tail call i32 @fclose(i32* %1)
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %15)
%19 = icmp sgt i32 %15, 0
br i1 %19, label %20, label %57
20: ; preds = %14
%21 = zext i32 %15 to i64
br label %58
22: ; preds = %0, %51
%23 = phi i64 [ %55, %51 ], [ 1, %0 ]
%24 = phi i32 [ %54, %51 ], [ 0, %0 ]
%25 = phi i32 [ %53, %51 ], [ 1, %0 ]
%26 = phi i8 [ %52, %51 ], [ %10, %0 ]
%27 = zext i8 %26 to i32
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = getelementptr inbounds i8, i8* %8, i64 %23
%30 = load i8, i8* %29
%31 = icmp eq i8 %26, %30
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = add nsw i32 %25, 1
br label %51
34: ; preds = %22
%35 = icmp sgt i32 %25, 1
br i1 %35, label %36, label %44
36: ; preds = %34
%37 = add nsw i32 %24, 1
%38 = sext i32 %24 to i64
%39 = getelementptr inbounds i8, i8* %12, i64 %38
store i8 %26, i8* %39
%40 = add nsw i32 %24, 2
%41 = sext i32 %37 to i64
%42 = getelementptr inbounds i8, i8* %12, i64 %41
store i8 %26, i8* %42
%43 = trunc i32 %25 to i8
br label %44
44: ; preds = %34, %36
%45 = phi i32 [ %40, %36 ], [ %24, %34 ]
%46 = phi i8 [ %43, %36 ], [ %26, %34 ]
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %12, i64 %47
store i8 %46, i8* %48
%49 = add nsw i32 %45, 1
%50 = load i8, i8* %29
br label %51
51: ; preds = %44, %32
%52 = phi i8 [ %26, %32 ], [ %50, %44 ]
%53 = phi i32 [ %33, %32 ], [ 1, %44 ]
%54 = phi i32 [ %24, %32 ], [ %49, %44 ]
%55 = add nuw nsw i64 %23, 1
%56 = icmp eq i64 %55, %4
br i1 %56, label %14, label %22
57: ; preds = %58, %14
ret i32 0
58: ; preds = %20, %58
%59 = phi i64 [ 0, %20 ], [ %64, %58 ]
%60 = getelementptr inbounds i8, i8* %12, i64 %59
%61 = load i8, i8* %60
%62 = zext i8 %61 to i32
%63 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %62)
%64 = add nuw nsw i64 %59, 1
%65 = icmp eq i64 %64, %21
br i1 %65, label %57, label %58
}