#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 */
char *aloca(int);
int casasDecimais(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
char *getCaminhoTabela(char *);
int /*<<< orphan*/ help(int);
int /*<<< orphan*/ intToString(int, char **);
int /*<<< orphan*/ printf(char *);
void runLengthEncodeComp(char *arquivoEntrada) {
  FILE *ptre, *ptrs;
  char c1, c2, hashtag, *contString, *caminho;
  int retorno, cont = 1, num;
  caminho = getCaminhoTabela(arquivoEntrada);
  ptre = fopen(caminho, "rb");
  if (ptre == NULL) {
    help(3);
    exit(-1);
  }
  ptrs = fopen("../dados/saida.ori", "wb");
  if (ptrs == NULL) {
    printf("Erro ao abrir o arquivo.\n");
    exit(-1);
  }
  fread(&c1, sizeof(char), 1, ptre);
  hashtag = '#';
  while (!feof(ptre)) {
    cont = 1;
    while ((fread(&c2, sizeof(char), 1, ptre) == 1) && (c1 == c2)) {
      cont++;
    }
    if (cont > 1) {
      num = casasDecimais(cont);
      contString = aloca(num);
      intToString(cont, &contString);
      fwrite(&hashtag, sizeof(char), 1, ptrs);
      fwrite(contString, sizeof(char), num, ptrs);
      fwrite(&c1, sizeof(char), 1, ptrs);
      fwrite(&hashtag, sizeof(char), 1, ptrs);
    } else
      fwrite(&c1, sizeof(char), 1, ptrs);
    c1 = c2;
  }
  fclose(ptre);
  fclose(ptrs);
}


x86 assembly (clang, -O0):
<runLengthEncodeComp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <getCaminhoTabela@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe15(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1213 <runLengthEncodeComp+0x53>
mov    $0x3,%edi
call   10c0 <help@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xde9(%rip),%rdi
lea    0xdf5(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    124b <runLengthEncodeComp+0x8b>
lea    0xddd(%rip),%rdi
call   1070 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rcx
lea    -0x19(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
movb   $0x23,-0x1b(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1281 <runLengthEncodeComp+0xc1>
jmp    137c <runLengthEncodeComp+0x1bc>
movl   $0x1,-0x38(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x1a(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
mov    %al,-0x3d(%rbp)
jne    12bc <runLengthEncodeComp+0xfc>
movsbl -0x19(%rbp),%eax
movsbl -0x1a(%rbp),%ecx
cmp    %ecx,%eax
sete   %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    12cc <runLengthEncodeComp+0x10c>
jmp    12da <runLengthEncodeComp+0x11a>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1288 <runLengthEncodeComp+0xc8>
cmpl   $0x1,-0x38(%rbp)
jle    135d <runLengthEncodeComp+0x19d>
mov    -0x38(%rbp),%edi
call   1090 <casasDecimais@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   1030 <aloca@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <intToString@plt>
mov    -0x18(%rbp),%rcx
lea    -0x1b(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10e0 <fwrite@plt>
mov    -0x28(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10e0 <fwrite@plt>
mov    -0x18(%rbp),%rcx
lea    -0x19(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10e0 <fwrite@plt>
mov    -0x18(%rbp),%rcx
lea    -0x1b(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10e0 <fwrite@plt>
jmp    1371 <runLengthEncodeComp+0x1b1>
mov    -0x18(%rbp),%rcx
lea    -0x19(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10e0 <fwrite@plt>
mov    -0x1a(%rbp),%al
mov    %al,-0x19(%rbp)
jmp    1263 <runLengthEncodeComp+0xa3>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runLengthEncodeComp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
call   1060 <getCaminhoTabela@plt>
lea    0xe16(%rip),%rsi
mov    %rax,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     131e <runLengthEncodeComp+0x14e>
mov    %rax,%rbx
lea    0xdfe(%rip),%rdi
lea    0xe0a(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1332 <runLengthEncodeComp+0x162>
mov    %rax,%r13
lea    0x7(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    0x7(%rsp),%r12b
mov    %rbx,%rdi
call   10b0 <feof@plt>
test   %eax,%eax
je     1264 <runLengthEncodeComp+0x94>
mov    %rbx,%rdi
call   1070 <fclose@plt>
mov    %r13,%rdi
call   1070 <fclose@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x6(%rsp),%r15
mov    %r13,0x8(%rsp)
jmp    128a <runLengthEncodeComp+0xba>
movsbl %r14b,%ebp
mov    %ebp,%edi
mov    %r13,%rsi
call   1090 <fputc@plt>
mov    %rbx,%rdi
call   10b0 <feof@plt>
test   %eax,%eax
jne    1245 <runLengthEncodeComp+0x75>
mov    %r12d,%r14d
xor    %ebp,%ebp
nop
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
add    $0x1,%ebp
movzbl 0x6(%rsp),%r12d
cmp    $0x1,%eax
jne    12b8 <runLengthEncodeComp+0xe8>
cmp    %r12b,%r14b
je     1290 <runLengthEncodeComp+0xc0>
cmp    $0x2,%ebp
jb     1270 <runLengthEncodeComp+0xa0>
mov    %ebp,%edi
call   10a0 <casasDecimais@plt>
mov    %eax,%r13d
mov    %eax,%edi
call   1030 <aloca@plt>
mov    %rax,0x10(%rsp)
mov    %ebp,%edi
lea    0x10(%rsp),%rsi
call   1080 <intToString@plt>
mov    $0x23,%ebp
mov    $0x23,%edi
mov    0x8(%rsp),%rsi
call   1090 <fputc@plt>
mov    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    0x8(%rsp),%r13
mov    %r13,%rcx
call   10f0 <fwrite@plt>
movsbl %r14b,%edi
mov    %r13,%rsi
call   1090 <fputc@plt>
jmp    1274 <runLengthEncodeComp+0xa4>
mov    $0x3,%edi
call   10d0 <help@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
lea    0xce0(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [19 x i8] c"../dados/saida.ori\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [26 x i8] c"Erro ao abrir o arquivo.\0A\00"
define void @runLengthEncodeComp(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %2
store i32 1, i32* %11
%13 = load i8*, i8** %2
%14 = call i8* @getCaminhoTabela(i8* %13)
store i8* %14, i8** %9
%15 = load i8*, i8** %9
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %3
%17 = load i32*, i32** %3
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %1
%20 = call i32 @help(i32 3)
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %1
%23 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %23, i32** %4
%24 = load i32*, i32** %4
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %22
%30 = load i32*, i32** %3
%31 = call i32 @fread(i8* %5, i32 1, i32 1, i32* %30)
store i8 35, i8* %7
br label %32
32: ; preds = %76, %29
%33 = load i32*, i32** %3
%34 = call i32 @feof(i32* %33)
%35 = icmp ne i32 %34, 0
%36 = xor i1 %35, true
br i1 %36, label %37, label %78
37: ; preds = %32
store i32 1, i32* %11
br label %38
38: ; preds = %50, %37
%39 = load i32*, i32** %3
%40 = call i32 @fread(i8* %6, i32 1, i32 1, i32* %39)
%41 = icmp eq i32 %40, 1
br i1 %41, label %42, label %48
42: ; preds = %38
%43 = load i8, i8* %5
%44 = sext i8 %43 to i32
%45 = load i8, i8* %6
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %44, %46
br label %48
48: ; preds = %42, %38
%49 = phi i1 [ false, %38 ], [ %47, %42 ]
br i1 %49, label %50, label %53
50: ; preds = %48
%51 = load i32, i32* %11
%52 = add nsw i32 %51, 1
store i32 %52, i32* %11
br label %38
53: ; preds = %48
%54 = load i32, i32* %11
%55 = icmp sgt i32 %54, 1
br i1 %55, label %56, label %73
56: ; preds = %53
%57 = load i32, i32* %11
%58 = call i32 @casasDecimais(i32 %57)
store i32 %58, i32* %12
%59 = load i32, i32* %12
%60 = call i8* @aloca(i32 %59)
store i8* %60, i8** %8
%61 = load i32, i32* %11
%62 = call i32 @intToString(i32 %61, i8** %8)
%63 = load i32*, i32** %4
%64 = call i32 @fwrite(i8* %7, i32 1, i32 1, i32* %63)
%65 = load i8*, i8** %8
%66 = load i32, i32* %12
%67 = load i32*, i32** %4
%68 = call i32 @fwrite(i8* %65, i32 1, i32 %66, i32* %67)
%69 = load i32*, i32** %4
%70 = call i32 @fwrite(i8* %5, i32 1, i32 1, i32* %69)
%71 = load i32*, i32** %4
%72 = call i32 @fwrite(i8* %7, i32 1, i32 1, i32* %71)
br label %76
73: ; preds = %53
%74 = load i32*, i32** %4
%75 = call i32 @fwrite(i8* %5, i32 1, i32 1, i32* %74)
br label %76
76: ; preds = %73, %56
%77 = load i8, i8* %6
store i8 %77, i8* %5
br label %32
78: ; preds = %32
%79 = load i32*, i32** %3
%80 = call i32 @fclose(i32* %79)
%81 = load i32*, i32** %4
%82 = call i32 @fclose(i32* %81)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [19 x i8] c"../dados/saida.ori\00"
@.str.2 = [3 x i8] c"wb\00"
@str = [25 x i8] c"Erro ao abrir o arquivo.\00"
define void @runLengthEncodeComp(i8* %0) {
%2 = alloca i8
%3 = alloca i8
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = tail call i8* @getCaminhoTabela(i8* %0)
%7 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = tail call i32 @help(i32 3)
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %1
%13 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %12
%19 = call i32 @fread(i8* %2, i32 1, i32 1, i32* %7)
%20 = load i8, i8* %2
%21 = call i32 @feof(i32* %7)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %51
23: ; preds = %18, %46
%24 = phi i8 [ %29, %46 ], [ %20, %18 ]
br label %25
25: ; preds = %25, %23
%26 = phi i32 [ %32, %25 ], [ 1, %23 ]
%27 = call i32 @fread(i8* %3, i32 1, i32 1, i32* %7)
%28 = icmp eq i32 %27, 1
%29 = load i8, i8* %3
%30 = icmp eq i8 %24, %29
%31 = select i1 %28, i1 %30, i1 false
%32 = add nuw nsw i32 %26, 1
br i1 %31, label %25, label %33
33: ; preds = %25
%34 = icmp ugt i32 %26, 1
br i1 %34, label %35, label %44
35: ; preds = %33
%36 = call i32 @casasDecimais(i32 %26)
%37 = call i8* @aloca(i32 %36)
store i8* %37, i8** %4
%38 = call i32 @intToString(i32 %26, i8** %4)
%39 = call i32 @fputc(i32 35, i32* %13)
%40 = load i8*, i8** %4
%41 = call i32 @fwrite(i8* %40, i32 1, i32 %36, i32* %13)
%42 = sext i8 %24 to i32
%43 = call i32 @fputc(i32 %42, i32* %13)
br label %46
44: ; preds = %33
%45 = sext i8 %24 to i32
br label %46
46: ; preds = %44, %35
%47 = phi i32 [ %45, %44 ], [ 35, %35 ]
%48 = call i32 @fputc(i32 %47, i32* %13)
%49 = call i32 @feof(i32* %7)
%50 = icmp eq i32 %49, 0
br i1 %50, label %23, label %51
51: ; preds = %46, %18
%52 = call i32 @fclose(i32* %7)
%53 = call i32 @fclose(i32* %13)
ret void
}