#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 EOF;
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ putc(unsigned char, int /*<<< orphan*/ *);
void rle_encode(FILE *in, FILE *out) {
  int c, prev, rle_cnt = 0;
  if ((c = getc(in)) != EOF) {
    putc((unsigned char)c, out);
    prev = c;
  } else
    return;
  while ((c = getc(in)) != EOF) {
    if (c != prev) {
      if (rle_cnt) {
        putc((unsigned char)prev, out);
        putc((unsigned char)rle_cnt - 1, out);
        rle_cnt = 0;
      }
      putc((unsigned char)c, out);
      prev = c;
    } else {
      if ((++rle_cnt) == 256) {
        putc((unsigned char)prev, out);
        putc((unsigned char)255, out);
        rle_cnt = 0;
      }
    }
  }
  if (rle_cnt) {
    putc((unsigned char)prev, out);
    putc((unsigned char)rle_cnt - 1, out);
  }
}


x86 assembly (clang, -O0):
<rle_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <getc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e9e(%rip),%rcx
cmp    (%rcx),%eax
je     116c <rle_encode+0x4c>
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rsi
movzbl %al,%edi
call   1030 <putc@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1171 <rle_encode+0x51>
jmp    125c <rle_encode+0x13c>
jmp    1176 <rle_encode+0x56>
mov    -0x8(%rbp),%rdi
call   1040 <getc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%eax
je     122e <rle_encode+0x10e>
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     11ec <rle_encode+0xcc>
cmpl   $0x0,-0x1c(%rbp)
je     11d2 <rle_encode+0xb2>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%rsi
movzbl %al,%edi
call   1030 <putc@plt>
mov    -0x1c(%rbp),%eax
movzbl %al,%eax
sub    $0x1,%eax
mov    -0x10(%rbp),%rsi
movzbl %al,%edi
call   1030 <putc@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rsi
movzbl %al,%edi
call   1030 <putc@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1229 <rle_encode+0x109>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x100,%eax
jne    1224 <rle_encode+0x104>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%rsi
movzbl %al,%edi
call   1030 <putc@plt>
mov    -0x10(%rbp),%rsi
mov    $0xff,%edi
call   1030 <putc@plt>
movl   $0x0,-0x1c(%rbp)
jmp    1229 <rle_encode+0x109>
jmp    1176 <rle_encode+0x56>
cmpl   $0x0,-0x1c(%rbp)
je     125c <rle_encode+0x13c>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%rsi
movzbl %al,%edi
call   1030 <putc@plt>
mov    -0x1c(%rbp),%eax
movzbl %al,%eax
sub    $0x1,%eax
mov    -0x10(%rbp),%rsi
movzbl %al,%edi
call   1030 <putc@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rle_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
call   1040 <getc@plt>
mov    0x2eab(%rip),%r15
cmp    (%r15),%eax
je     120c <rle_encode+0xec>
mov    %eax,%ebp
movzbl %bpl,%edi
mov    %r12,%rsi
call   1030 <putc@plt>
mov    %r14,%rdi
call   1040 <getc@plt>
cmp    (%r15),%eax
je     120c <rle_encode+0xec>
xor    %ebx,%ebx
jmp    1190 <rle_encode+0x70>
nopl   0x0(%rax)
mov    %r13d,%eax
movzbl %al,%edi
mov    %r12,%rsi
call   1030 <putc@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1040 <getc@plt>
mov    %r13d,%ebp
cmp    (%r15),%eax
je     11e0 <rle_encode+0xc0>
mov    %eax,%r13d
cmp    %ebp,%eax
jne    11c0 <rle_encode+0xa0>
add    $0x1,%ebx
cmp    $0x100,%ebx
jne    1180 <rle_encode+0x60>
movzbl %r13b,%edi
mov    %r12,%rsi
call   1030 <putc@plt>
mov    $0xff,%al
jmp    1173 <rle_encode+0x53>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %ebx,%ebx
je     1170 <rle_encode+0x50>
movzbl %bpl,%edi
mov    %r12,%rsi
call   1030 <putc@plt>
add    $0xff,%bl
movzbl %bl,%edi
mov    %r12,%rsi
call   1030 <putc@plt>
jmp    1170 <rle_encode+0x50>
test   %ebx,%ebx
je     120c <rle_encode+0xec>
movzbl %r13b,%edi
mov    %r12,%rsi
call   1030 <putc@plt>
add    $0xff,%bl
movzbl %bl,%edi
mov    %r12,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putc@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
define void @rle_encode(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32 0, i32* %7
%8 = load i32*, i32** %3
%9 = call i32 @getc(i32* %8)
store i32 %9, i32* %5
%10 = load i32, i32* @EOF
%11 = icmp ne i32 %9, %10
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* %5
%14 = trunc i32 %13 to i8
%15 = load i32*, i32** %4
%16 = call i32 @putc(i8 zeroext %14, i32* %15)
%17 = load i32, i32* %5
store i32 %17, i32* %6
br label %19
18: ; preds = %2
br label %78
19: ; preds = %12
br label %20
20: ; preds = %62, %19
%21 = load i32*, i32** %3
%22 = call i32 @getc(i32* %21)
store i32 %22, i32* %5
%23 = load i32, i32* @EOF
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %63
25: ; preds = %20
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %50
29: ; preds = %25
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %44
32: ; preds = %29
%33 = load i32, i32* %6
%34 = trunc i32 %33 to i8
%35 = load i32*, i32** %4
%36 = call i32 @putc(i8 zeroext %34, i32* %35)
%37 = load i32, i32* %7
%38 = trunc i32 %37 to i8
%39 = zext i8 %38 to i32
%40 = sub nsw i32 %39, 1
%41 = trunc i32 %40 to i8
%42 = load i32*, i32** %4
%43 = call i32 @putc(i8 zeroext %41, i32* %42)
store i32 0, i32* %7
br label %44
44: ; preds = %32, %29
%45 = load i32, i32* %5
%46 = trunc i32 %45 to i8
%47 = load i32*, i32** %4
%48 = call i32 @putc(i8 zeroext %46, i32* %47)
%49 = load i32, i32* %5
store i32 %49, i32* %6
br label %62
50: ; preds = %25
%51 = load i32, i32* %7
%52 = add nsw i32 %51, 1
store i32 %52, i32* %7
%53 = icmp eq i32 %52, 256
br i1 %53, label %54, label %61
54: ; preds = %50
%55 = load i32, i32* %6
%56 = trunc i32 %55 to i8
%57 = load i32*, i32** %4
%58 = call i32 @putc(i8 zeroext %56, i32* %57)
%59 = load i32*, i32** %4
%60 = call i32 @putc(i8 zeroext -1, i32* %59)
store i32 0, i32* %7
br label %61
61: ; preds = %54, %50
br label %62
62: ; preds = %61, %44
br label %20
63: ; preds = %20
%64 = load i32, i32* %7
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %78
66: ; preds = %63
%67 = load i32, i32* %6
%68 = trunc i32 %67 to i8
%69 = load i32*, i32** %4
%70 = call i32 @putc(i8 zeroext %68, i32* %69)
%71 = load i32, i32* %7
%72 = trunc i32 %71 to i8
%73 = zext i8 %72 to i32
%74 = sub nsw i32 %73, 1
%75 = trunc i32 %74 to i8
%76 = load i32*, i32** %4
%77 = call i32 @putc(i8 zeroext %75, i32* %76)
br label %78
78: ; preds = %18, %66, %63
ret void
}

LLVM IR (-O2):
@EOF = global i32 0
define void @rle_encode(i32* nocapture %0, i32* nocapture %1) {
%3 = tail call i32 @getc(i32* %0)
%4 = load i32, i32* @EOF
%5 = icmp eq i32 %3, %4
br i1 %5, label %49, label %6
6: ; preds = %2
%7 = trunc i32 %3 to i8
%8 = tail call i32 @putc(i8 zeroext %7, i32* %1)
%9 = tail call i32 @getc(i32* %0)
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %49, label %12
12: ; preds = %6, %36
%13 = phi i32 [ %38, %36 ], [ %9, %6 ]
%14 = phi i32 [ %37, %36 ], [ 0, %6 ]
%15 = phi i32 [ %13, %36 ], [ %3, %6 ]
%16 = icmp eq i32 %13, %15
br i1 %16, label %27, label %17
17: ; preds = %12
%18 = icmp eq i32 %14, 0
br i1 %18, label %25, label %19
19: ; preds = %17
%20 = trunc i32 %15 to i8
%21 = tail call i32 @putc(i8 zeroext %20, i32* %1)
%22 = trunc i32 %14 to i8
%23 = add i8 %22, -1
%24 = tail call i32 @putc(i8 zeroext %23, i32* %1)
br label %25
25: ; preds = %19, %17
%26 = trunc i32 %13 to i8
br label %33
27: ; preds = %12
%28 = add nsw i32 %14, 1
%29 = icmp eq i32 %28, 256
br i1 %29, label %30, label %36
30: ; preds = %27
%31 = trunc i32 %13 to i8
%32 = tail call i32 @putc(i8 zeroext %31, i32* %1)
br label %33
33: ; preds = %25, %30
%34 = phi i8 [ -1, %30 ], [ %26, %25 ]
%35 = tail call i32 @putc(i8 zeroext %34, i32* %1)
br label %36
36: ; preds = %33, %27
%37 = phi i32 [ %28, %27 ], [ 0, %33 ]
%38 = tail call i32 @getc(i32* %0)
%39 = load i32, i32* @EOF
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %12
41: ; preds = %36
%42 = icmp eq i32 %37, 0
br i1 %42, label %49, label %43
43: ; preds = %41
%44 = trunc i32 %13 to i8
%45 = tail call i32 @putc(i8 zeroext %44, i32* %1)
%46 = trunc i32 %37 to i8
%47 = add i8 %46, -1
%48 = tail call i32 @putc(i8 zeroext %47, i32* %1)
br label %49
49: ; preds = %6, %41, %43, %2
ret void
}