#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*/ checkInput(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getline(char **, size_t *, int /*<<< orphan*/ *);
int getopt(int, char **, char *);
char *huffmanComplex(char *, char *);
char *huffmanSimple(char *);
char *optarg;
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int main(int argc, char *argv[]) {
  char *fileName = NULL;
  int c;
  while ((c = getopt(argc, argv, "hf:")) != -1) {
    switch (c) {
    case 'f':
      fileName = optarg;
      break;
    case 'h':
    default:
      printf("Uso: encode -f fileToCompress\n");
      return -1;
    }
  }
  if (fileName == NULL) {
    printf("Uso: encode -f fileToCompress\n");
  }
  FILE *file = fopen(fileName, "r");
  if (file == NULL) {
    printf("Error abriendo fichero %s.\n", fileName);
    return -1;
  }
  char *input = NULL;
  size_t len = 0;
  getline(&input, &len, file);
  input[strlen(input) - 1] = '\0';
  fclose(file);
  if (!checkInput(input)) {
    printf("Error en la entrada. Solo son validos los simbolos 'A', 'T', 'C' o "
           "'G'.\n");
    return -1;
  }
  char *binSimple = huffmanSimple(input);
  char *binComplex = huffmanComplex(input, fileName);
  int sizeHuffmanSimple = strlen(binSimple);
  int sizeHuffmanComplex = strlen(binComplex);
  int sizeNoCompress = strlen(input) * 8;
  double rcSimple = (double)sizeNoCompress / (double)sizeHuffmanSimple;
  double rcComplex = (double)sizeNoCompress / (double)sizeHuffmanComplex;
  printf("Relacion de compresion para el algoritmo simple: %d/%d=%.3f\n",
         sizeNoCompress, sizeHuffmanSimple, rcSimple);
  printf("Relacion de compresion para el algoritmo complejo: %d/%d=%.3f\n",
         sizeNoCompress, sizeHuffmanComplex, rcComplex);
  free(binSimple);
  free(binComplex);
  free(input);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe31(%rip),%rdx
call   10a0 <getopt@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     123c <main+0x9c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x6c(%rbp)
sub    $0x66,%eax
je     1205 <main+0x65>
jmp    11f4 <main+0x54>
mov    -0x6c(%rbp),%eax
sub    $0x68,%eax
je     1218 <main+0x78>
jmp    121d <main+0x7d>
mov    0x2dec(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1237 <main+0x97>
jmp    121d <main+0x7d>
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c8 <main+0x228>
jmp    11c1 <main+0x21>
cmpq   $0x0,-0x18(%rbp)
jne    1255 <main+0xb5>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xdc3(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1292 <main+0xf2>
mov    -0x18(%rbp),%rsi
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c8 <main+0x228>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10b0 <getline@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x78(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1080 <checkInput@plt>
cmp    $0x0,%eax
jne    1309 <main+0x169>
lea    0xd4b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c8 <main+0x228>
mov    -0x30(%rbp),%rdi
call   10c0 <huffmanSimple@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <huffmanComplex@plt>
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
shl    $0x3,%eax
mov    %eax,-0x54(%rbp)
cvtsi2sdl -0x54(%rbp),%xmm0
cvtsi2sdl -0x4c(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x60(%rbp)
cvtsi2sdl -0x54(%rbp),%xmm0
cvtsi2sdl -0x50(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x54(%rbp),%esi
mov    -0x4c(%rbp),%edx
movsd  -0x60(%rbp),%xmm0
lea    0xd04(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    -0x54(%rbp),%esi
mov    -0x50(%rbp),%edx
movsd  -0x68(%rbp),%xmm0
lea    0xd28(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %r15d,%r15d
lea    0xe35(%rip),%r14
mov    0x2e26(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10b0 <getopt@plt>
cmp    $0x66,%eax
jne    11f8 <main+0x48>
mov    (%r12),%r15
jmp    11e0 <main+0x30>
cmp    $0xffffffff,%eax
jne    132d <main+0x17d>
test   %r15,%r15
jne    1212 <main+0x62>
lea    0xed9(%rip),%rdi
call   1040 <puts@plt>
lea    0xdeb(%rip),%rsi
mov    %r15,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1336 <main+0x186>
mov    %rax,%r14
movq   $0x0,0x8(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
mov    %rax,%rdx
call   10c0 <getline@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    0x8(%rsp),%rdi
call   1090 <checkInput@plt>
test   %eax,%eax
je     1349 <main+0x199>
mov    0x8(%rsp),%rdi
call   10d0 <huffmanSimple@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rdi
mov    %r15,%rsi
call   1080 <huffmanComplex@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%r12d
mov    0x8(%rsp),%rdi
call   1060 <strlen@plt>
lea    0x0(,%rax,8),%ebx
cvtsi2sd %ebx,%xmm2
cvtsi2sd %ebp,%xmm1
movapd %xmm2,%xmm0
divsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %r12d,%xmm1
divsd  %xmm1,%xmm2
movsd  %xmm2,0x10(%rsp)
lea    0xd37(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    $0x1,%al
call   1070 <printf@plt>
lea    0xd62(%rip),%rdi
mov    %ebx,%esi
mov    %r12d,%edx
movsd  0x10(%rsp),%xmm0
mov    $0x1,%al
call   1070 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    135a <main+0x1aa>
lea    0xdb2(%rip),%rdi
jmp    1350 <main+0x1a0>
lea    0xcc9(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1355 <main+0x1a5>
lea    0xd4e(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"hf:\00"
@optarg = global i8* null
@.str.1 = [31 x i8] c"Uso: encode -f fileToCompress\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [28 x i8] c"Error abriendo fichero %s.\0A\00"
@.str.4 = [73 x i8] c"Error en la entrada. Solo son validos los simbolos 'A', 'T', 'C' o 'G'.\0A\00"
@.str.5 = [61 x i8] c"Relacion de compresion para el algoritmo simple: %d/%d=%.3f\0A\00"
@.str.6 = [63 x i8] c"Relacion de compresion para el algoritmo complejo: %d/%d=%.3f\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca double
%17 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
br label %18
18: ; preds = %30, %2
%19 = load i32, i32* %4
%20 = load i8**, i8*** %5
%21 = call i32 @getopt(i32 %19, i8** %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 %21, i32* %7
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %31
23: ; preds = %18
%24 = load i32, i32* %7
switch i32 %24, label %28 [
i32 102, label %25
i32 104, label %27
]
25: ; preds = %23
%26 = load i8*, i8** @optarg
store i8* %26, i8** %6
br label %30
27: ; preds = %23
br label %28
28: ; preds = %23, %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %97
30: ; preds = %25
br label %18
31: ; preds = %18
%32 = load i8*, i8** %6
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %31
%37 = load i8*, i8** %6
%38 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %38, i32** %8
%39 = load i32*, i32** %8
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i8*, i8** %6
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %42)
store i32 -1, i32* %3
br label %97
44: ; preds = %36
store i8* null, i8** %9
store i64 0, i64* %10
%45 = load i32*, i32** %8
%46 = call i32 @getline(i8** %9, i64* %10, i32* %45)
%47 = load i8*, i8** %9
%48 = load i8*, i8** %9
%49 = call i32 @strlen(i8* %48)
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %47, i64 %51
store i8 0, i8* %52
%53 = load i32*, i32** %8
%54 = call i32 @fclose(i32* %53)
%55 = load i8*, i8** %9
%56 = call i32 @checkInput(i8* %55)
%57 = icmp ne i32 %56, 0
br i1 %57, label %60, label %58
58: ; preds = %44
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %97
60: ; preds = %44
%61 = load i8*, i8** %9
%62 = call i8* @huffmanSimple(i8* %61)
store i8* %62, i8** %11
%63 = load i8*, i8** %9
%64 = load i8*, i8** %6
%65 = call i8* @huffmanComplex(i8* %63, i8* %64)
store i8* %65, i8** %12
%66 = load i8*, i8** %11
%67 = call i32 @strlen(i8* %66)
store i32 %67, i32* %13
%68 = load i8*, i8** %12
%69 = call i32 @strlen(i8* %68)
store i32 %69, i32* %14
%70 = load i8*, i8** %9
%71 = call i32 @strlen(i8* %70)
%72 = mul nsw i32 %71, 8
store i32 %72, i32* %15
%73 = load i32, i32* %15
%74 = sitofp i32 %73 to double
%75 = load i32, i32* %13
%76 = sitofp i32 %75 to double
%77 = fdiv double %74, %76
store double %77, double* %16
%78 = load i32, i32* %15
%79 = sitofp i32 %78 to double
%80 = load i32, i32* %14
%81 = sitofp i32 %80 to double
%82 = fdiv double %79, %81
store double %82, double* %17
%83 = load i32, i32* %15
%84 = load i32, i32* %13
%85 = load double, double* %16
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.5, i64 0, i64 0), i32 %83, i32 %84, double %85)
%87 = load i32, i32* %15
%88 = load i32, i32* %14
%89 = load double, double* %17
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.6, i64 0, i64 0), i32 %87, i32 %88, double %89)
%91 = load i8*, i8** %11
%92 = call i32 @free(i8* %91)
%93 = load i8*, i8** %12
%94 = call i32 @free(i8* %93)
%95 = load i8*, i8** %9
%96 = call i32 @free(i8* %95)
store i32 0, i32* %3
br label %97
97: ; preds = %60, %58, %41, %28
%98 = load i32, i32* %3
ret i32 %98
}

LLVM IR (-O2):
@.str = [4 x i8] c"hf:\00"
@optarg = global i8* null
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [28 x i8] c"Error abriendo fichero %s.\0A\00"
@.str.5 = [61 x i8] c"Relacion de compresion para el algoritmo simple: %d/%d=%.3f\0A\00"
@.str.6 = [63 x i8] c"Relacion de compresion para el algoritmo complejo: %d/%d=%.3f\0A\00"
@str = [72 x i8] c"Error en la entrada. Solo son validos los simbolos 'A', 'T', 'C' o 'G'.\00"
@str.8 = [30 x i8] c"Uso: encode -f fileToCompress\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i64
br label %5
5: ; preds = %8, %2
%6 = phi i8* [ null, %2 ], [ %9, %8 ]
%7 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
switch i32 %7, label %10 [
i32 -1, label %12
i32 102, label %8
]
8: ; preds = %5
%9 = load i8*, i8** @optarg
br label %5
10: ; preds = %5
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.8, i64 0, i64 0))
br label %59
12: ; preds = %5
%13 = icmp eq i8* %6, null
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.8, i64 0, i64 0))
br label %16
16: ; preds = %14, %12
%17 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %6)
br label %59
21: ; preds = %16
%22 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%23 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%24 = call i32 @getline(i8** %3, i64* %4, i32* %17)
%25 = load i8*, i8** %3
%26 = call i32 @strlen(i8* %25)
%27 = add nsw i32 %26, -1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
store i8 0, i8* %29
%30 = call i32 @fclose(i32* %17)
%31 = load i8*, i8** %3
%32 = call i32 @checkInput(i8* %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %21
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @str, i64 0, i64 0))
br label %57
36: ; preds = %21
%37 = load i8*, i8** %3
%38 = call i8* @huffmanSimple(i8* %37)
%39 = load i8*, i8** %3
%40 = call i8* @huffmanComplex(i8* %39, i8* %6)
%41 = call i32 @strlen(i8* %38)
%42 = call i32 @strlen(i8* %40)
%43 = load i8*, i8** %3
%44 = call i32 @strlen(i8* %43)
%45 = shl nsw i32 %44, 3
%46 = sitofp i32 %45 to double
%47 = sitofp i32 %41 to double
%48 = fdiv double %46, %47
%49 = sitofp i32 %42 to double
%50 = fdiv double %46, %49
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.5, i64 0, i64 0), i32 %45, i32 %41, double %48)
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @.str.6, i64 0, i64 0), i32 %45, i32 %42, double %50)
%53 = call i32 @free(i8* %38)
%54 = call i32 @free(i8* %40)
%55 = load i8*, i8** %3
%56 = call i32 @free(i8* %55)
br label %57
57: ; preds = %36, %34
%58 = phi i32 [ 0, %36 ], [ -1, %34 ]
br label %59
59: ; preds = %19, %57, %10
%60 = phi i32 [ -1, %10 ], [ -1, %19 ], [ %58, %57 ]
ret i32 %60
}