#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 */
struct timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
struct SmazBranch {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ calloc(long, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
long fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
struct SmazBranch *smaz_build_trie();
int /*<<< orphan*/ smaz_compress(struct SmazBranch *, char *, long, char *,
                                 long);
int /*<<< orphan*/ smaz_free_trie(struct SmazBranch *);
void bench_trie_smaz() {
  FILE *infile;
  char *in;
  char *comp_out;
  char *de_comp_out;
  long numbytes;
  int num_loops = 1000;
  infile = fopen("war_of_the_worlds.txt", "r");
  if (infile == NULL) {
    printf("Missing war of the worlds text, you can download the text here: "
           "http://www.gutenberg.org/ebooks/36 and save it as "
           "war_of_the_worlds.txt\n");
    exit(1);
  }
  fseek(infile, 0L, SEEK_END);
  numbytes = ftell(infile);
  printf("Processing %d bytes, %d times\n", (int)numbytes, num_loops);
  fseek(infile, 0L, SEEK_SET);
  in = (char *)calloc(numbytes, sizeof(char));
  comp_out = (char *)calloc(numbytes, sizeof(char));
  de_comp_out = (char *)calloc(numbytes, sizeof(char));
  numbytes = fread(in, sizeof(char), numbytes, infile);
  fclose(infile);
  {
    struct timeval t1, t2;
    int x;
    struct SmazBranch *trie;
    trie = smaz_build_trie();
    gettimeofday(&t1, NULL);
    for (x = 0; x < num_loops; x++) {
      smaz_compress(trie, in, numbytes, comp_out, numbytes);
    }
    gettimeofday(&t2, NULL);
    printf("time = %u.%06u\n", (unsigned int)t1.tv_sec,
           (unsigned int)t1.tv_usec);
    printf("time = %u.%06u\n", (unsigned int)t2.tv_sec,
           (unsigned int)t2.tv_usec);
    smaz_free_trie(trie);
  }
  free(in);
  free(comp_out);
  free(de_comp_out);
}


x86 assembly (clang, -O0):
<bench_trie_smaz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x3e8,-0x2c(%rbp)
lea    0xe1a(%rip),%rdi
lea    0xe29(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1219 <bench_trie_smaz+0x49>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10c0 <fseek@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %eax,%esi
mov    -0x2c(%rbp),%edx
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10c0 <fseek@plt>
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1090 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1090 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1090 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fclose@plt>
mov    $0x0,%al
call   10d0 <smaz_build_trie@plt>
mov    %rax,-0x60(%rbp)
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <gettimeofday@plt>
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1314 <bench_trie_smaz+0x144>
mov    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   10b0 <smaz_compress@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    12e1 <bench_trie_smaz+0x111>
lea    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <gettimeofday@plt>
mov    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x40(%rbp),%rax
mov    %eax,%edx
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x48(%rbp),%rax
mov    %eax,%esi
mov    -0x50(%rbp),%rax
mov    %eax,%edx
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x60(%rbp),%rdi
call   1040 <smaz_free_trie@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bench_trie_smaz>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xeae(%rip),%rdi
lea    0xe04(%rip),%rsi
call   10f0 <fopen@plt>
test   %rax,%rax
je     1340 <bench_trie_smaz+0x160>
mov    %rax,%r12
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %r12,%rdi
call   10b0 <ftell@plt>
mov    %rax,%rbx
lea    0xdd0(%rip),%rdi
mov    $0x3e8,%r15d
mov    %ebx,%esi
mov    $0x3e8,%edx
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   10a0 <calloc@plt>
mov    %rax,%r13
mov    %rbx,%rdi
mov    $0x1,%esi
call   10a0 <calloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x1,%esi
call   10a0 <calloc@plt>
mov    %rax,(%rsp)
mov    %r13,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1060 <fread@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1070 <fclose@plt>
xor    %eax,%eax
call   10e0 <smaz_build_trie@plt>
mov    %rax,%rbx
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1090 <gettimeofday@plt>
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
mov    %r14,%rcx
mov    %rbp,%r8
call   10c0 <smaz_compress@plt>
add    $0xffffffff,%r15d
jne    12c0 <bench_trie_smaz+0xe0>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1090 <gettimeofday@plt>
mov    0x18(%rsp),%edx
mov    0x20(%rsp),%esi
lea    0xd2c(%rip),%rbp
mov    %rbp,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x8(%rsp),%edx
mov    0x10(%rsp),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1040 <smaz_free_trie@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xcea(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
%struct.SmazBranch = type { i32 }
@.str = [22 x i8] c"war_of_the_worlds.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [137 x i8] c"Missing war of the worlds text, you can download the text here: http://www.gutenberg.org/ebooks/36 and save it as war_of_the_worlds.txt\0A\00"
@SEEK_END = global i32 0
@.str.3 = [31 x i8] c"Processing %d bytes, %d times\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [16 x i8] c"time = %u.%06u\0A\00"
define void @bench_trie_smaz() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca %struct.timeval
%9 = alloca i32
%10 = alloca %struct.SmazBranch*
store i32 1000, i32* %6
%11 = call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %1
%12 = load i32*, i32** %1
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([137 x i8], [137 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32*, i32** %1
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i64 0, i32 %19)
%21 = load i32*, i32** %1
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %5
%23 = load i64, i64* %5
%24 = trunc i64 %23 to i32
%25 = load i32, i32* %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32*, i32** %1
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %27, i64 0, i32 %28)
%30 = load i64, i64* %5
%31 = call i64 @calloc(i64 %30, i32 1)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %2
%33 = load i64, i64* %5
%34 = call i64 @calloc(i64 %33, i32 1)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %3
%36 = load i64, i64* %5
%37 = call i64 @calloc(i64 %36, i32 1)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %4
%39 = load i8*, i8** %2
%40 = load i64, i64* %5
%41 = load i32*, i32** %1
%42 = call i64 @fread(i8* %39, i32 1, i64 %40, i32* %41)
store i64 %42, i64* %5
%43 = load i32*, i32** %1
%44 = call i32 @fclose(i32* %43)
%45 = call %struct.SmazBranch* (...) @smaz_build_trie()
store %struct.SmazBranch* %45, %struct.SmazBranch** %10
%46 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
store i32 0, i32* %9
br label %47
47: ; preds = %58, %17
%48 = load i32, i32* %9
%49 = load i32, i32* %6
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %61
51: ; preds = %47
%52 = load %struct.SmazBranch*, %struct.SmazBranch** %10
%53 = load i8*, i8** %2
%54 = load i64, i64* %5
%55 = load i8*, i8** %3
%56 = load i64, i64* %5
%57 = call i32 @smaz_compress(%struct.SmazBranch* %52, i8* %53, i64 %54, i8* %55, i64 %56)
br label %58
58: ; preds = %51
%59 = load i32, i32* %9
%60 = add nsw i32 %59, 1
store i32 %60, i32* %9
br label %47
61: ; preds = %47
%62 = call i32 @gettimeofday(%struct.timeval* %8, i32* null)
%63 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%64 = load i64, i64* %63
%65 = trunc i64 %64 to i32
%66 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%67 = load i64, i64* %66
%68 = trunc i64 %67 to i32
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %65, i32 %68)
%70 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%71 = load i64, i64* %70
%72 = trunc i64 %71 to i32
%73 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%74 = load i64, i64* %73
%75 = trunc i64 %74 to i32
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %72, i32 %75)
%77 = load %struct.SmazBranch*, %struct.SmazBranch** %10
%78 = call i32 @smaz_free_trie(%struct.SmazBranch* %77)
%79 = load i8*, i8** %2
%80 = call i32 @free(i8* %79)
%81 = load i8*, i8** %3
%82 = call i32 @free(i8* %81)
%83 = load i8*, i8** %4
%84 = call i32 @free(i8* %83)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
%struct.SmazBranch = type { i32 }
@.str = [22 x i8] c"war_of_the_worlds.txt\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.3 = [31 x i8] c"Processing %d bytes, %d times\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [16 x i8] c"time = %u.%06u\0A\00"
@str = [136 x i8] c"Missing war of the worlds text, you can download the text here: http://www.gutenberg.org/ebooks/36 and save it as war_of_the_worlds.txt\00"
define void @bench_trie_smaz() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([136 x i8], [136 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %3, i64 0, i32 %9)
%11 = tail call i64 @ftell(i32* %3)
%12 = trunc i64 %11 to i32
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %12, i32 1000)
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @fseek(i32* %3, i64 0, i32 %14)
%16 = tail call i64 @calloc(i64 %11, i32 1)
%17 = inttoptr i64 %16 to i8*
%18 = tail call i64 @calloc(i64 %11, i32 1)
%19 = inttoptr i64 %18 to i8*
%20 = tail call i64 @calloc(i64 %11, i32 1)
%21 = tail call i64 @fread(i8* %17, i32 1, i64 %11, i32* %3)
%22 = tail call i32 @fclose(i32* %3)
%23 = bitcast %struct.timeval* %1 to i8*
%24 = bitcast %struct.timeval* %2 to i8*
%25 = tail call %struct.SmazBranch* (...) @smaz_build_trie()
%26 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
br label %27
27: ; preds = %8, %27
%28 = phi i32 [ 0, %8 ], [ %30, %27 ]
%29 = tail call i32 @smaz_compress(%struct.SmazBranch* %25, i8* %17, i64 %21, i8* %19, i64 %21)
%30 = add nuw nsw i32 %28, 1
%31 = icmp eq i32 %30, 1000
br i1 %31, label %32, label %27
32: ; preds = %27
%33 = inttoptr i64 %20 to i8*
%34 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%36 = load i64, i64* %35
%37 = trunc i64 %36 to i32
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%39 = load i64, i64* %38
%40 = trunc i64 %39 to i32
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %37, i32 %40)
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%43 = load i64, i64* %42
%44 = trunc i64 %43 to i32
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%46 = load i64, i64* %45
%47 = trunc i64 %46 to i32
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %44, i32 %47)
%49 = tail call i32 @smaz_free_trie(%struct.SmazBranch* %25)
%50 = tail call i32 @free(i8* %17)
%51 = tail call i32 @free(i8* %19)
%52 = tail call i32 @free(i8* %33)
ret void
}