#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 item {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int encode_file(char **, int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                unsigned short, char *);
int fail(char *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ get_codetable(struct item **, int, char **);
int get_freqdata(struct item ***, int /*<<< orphan*/ *, unsigned short *);
scalar_t__ prefixcompress(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int /*<<< orphan*/ *tmpfile();
int hzip(const char *filename, char *key) {
  struct item **list;
  char *table[65536 + 1];
  int n;
  char out[65536];
  FILE *f, *f2, *tempfile;
  unsigned short termword;
  strcpy(out, filename);
  strcat(out, ".hz");
  f = fopen(filename, "r");
  if (!f)
    return fail("hzip: %s: Permission denied\n", filename);
  tempfile = tmpfile();
  if (!tempfile) {
    fclose(f);
    return fail("hzip: cannot create temporary file\n", NULL);
  }
  f2 = fopen(out, "wb");
  if (!f2) {
    fclose(tempfile);
    fclose(f);
    return fail("hzip: %s: Permission denied\n", out);
  }
  for (n = 0; n < 65536; n++)
    table[n] = NULL;
  if (prefixcompress(f, tempfile) != 0) {
    fclose(f2);
    fclose(tempfile);
    fclose(f);
    return fail("hzip: cannot write file\n", NULL);
  }
  rewind(tempfile);
  n = get_freqdata(&list, tempfile, &termword);
  get_codetable(list, n, table);
  rewind(tempfile);
  n = encode_file(table, n, tempfile, f2, termword, key);
  fclose(f2);
  fclose(tempfile);
  fclose(f);
  if (n != 0)
    return fail("hzip: cannot write file\n", NULL);
  return n;
}


x86 assembly (clang, -O0):
<hzip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90060,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x90040(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x90040(%rbp),%rdi
lea    0xe1f(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    0xe13(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x90048(%rbp)
cmpq   $0x0,-0x90048(%rbp)
jne    1223 <hzip+0x73>
mov    -0x10(%rbp),%rsi
lea    0xdf0(%rip),%rdi
call   1040 <fail@plt>
mov    %eax,-0x4(%rbp)
jmp    142a <hzip+0x27a>
mov    $0x0,%al
call   1080 <tmpfile@plt>
mov    %rax,-0x90058(%rbp)
cmpq   $0x0,-0x90058(%rbp)
jne    1263 <hzip+0xb3>
mov    -0x90048(%rbp),%rdi
call   1060 <fclose@plt>
lea    0xdd1(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <fail@plt>
mov    %eax,-0x4(%rbp)
jmp    142a <hzip+0x27a>
lea    -0x90040(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x90050(%rbp)
cmpq   $0x0,-0x90050(%rbp)
jne    12be <hzip+0x10e>
mov    -0x90058(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x90048(%rbp),%rdi
call   1060 <fclose@plt>
lea    -0x90040(%rbp),%rsi
lea    0xd55(%rip),%rdi
call   1040 <fail@plt>
mov    %eax,-0x4(%rbp)
jmp    142a <hzip+0x27a>
movl   $0x0,-0x80034(%rbp)
cmpl   $0x10000,-0x80034(%rbp)
jge    12ff <hzip+0x14f>
movslq -0x80034(%rbp),%rax
movq   $0x0,-0x80030(%rbp,%rax,8)
mov    -0x80034(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x80034(%rbp)
jmp    12c8 <hzip+0x118>
mov    -0x90048(%rbp),%rdi
mov    -0x90058(%rbp),%rsi
call   10d0 <prefixcompress@plt>
cmp    $0x0,%rax
je     1358 <hzip+0x1a8>
mov    -0x90050(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x90058(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x90048(%rbp),%rdi
call   1060 <fclose@plt>
lea    0xd03(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <fail@plt>
mov    %eax,-0x4(%rbp)
jmp    142a <hzip+0x27a>
mov    -0x90058(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x90058(%rbp),%rsi
lea    -0x20(%rbp),%rdi
lea    -0x9005a(%rbp),%rdx
call   1090 <get_freqdata@plt>
mov    %eax,-0x80034(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x80034(%rbp),%esi
lea    -0x80030(%rbp),%rdx
call   10a0 <get_codetable@plt>
mov    -0x90058(%rbp),%rdi
call   1070 <rewind@plt>
lea    -0x80030(%rbp),%rdi
mov    -0x80034(%rbp),%esi
mov    -0x90058(%rbp),%rdx
mov    -0x90050(%rbp),%rcx
mov    -0x9005a(%rbp),%ax
mov    -0x18(%rbp),%r9
movzwl %ax,%r8d
call   1050 <encode_file@plt>
mov    %eax,-0x80034(%rbp)
mov    -0x90050(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x90058(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x90048(%rbp),%rdi
call   1060 <fclose@plt>
cmpl   $0x0,-0x80034(%rbp)
je     1421 <hzip+0x271>
lea    0xc3a(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <fail@plt>
mov    %eax,-0x4(%rbp)
jmp    142a <hzip+0x27a>
mov    -0x80034(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hzip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x90018,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
lea    0xe12(%rip),%rsi
mov    %rbx,%rdi
call   10d0 <strcat@plt>
lea    0xe07(%rip),%rsi
mov    %rbp,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1279 <hzip+0xb9>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <tmpfile@plt>
test   %rax,%rax
je     1288 <hzip+0xc8>
mov    %rax,%rbp
lea    0xe24(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     129c <hzip+0xdc>
mov    %rax,%r12
lea    0x10010(%rsp),%rdi
xor    %r15d,%r15d
mov    $0x80000,%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10e0 <prefixcompress@plt>
test   %rax,%rax
je     12bd <hzip+0xfd>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    1335 <hzip+0x175>
lea    0xd86(%rip),%rdi
mov    %rbp,%rsi
jmp    133e <hzip+0x17e>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xd8c(%rip),%rdi
jmp    133c <hzip+0x17c>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xd53(%rip),%rdi
lea    0x10(%rsp),%rsi
jmp    133e <hzip+0x17e>
mov    %rbp,%rdi
call   1070 <rewind@plt>
lea    0x8(%rsp),%rdi
lea    0x6(%rsp),%rdx
mov    %rbp,%rsi
call   10a0 <get_freqdata@plt>
mov    %eax,%r13d
mov    0x8(%rsp),%rdi
lea    0x10010(%rsp),%rdx
mov    %eax,%esi
call   10b0 <get_codetable@plt>
mov    %rbp,%rdi
call   1070 <rewind@plt>
movzwl 0x6(%rsp),%r8d
lea    0x10010(%rsp),%rdi
mov    %r13d,%esi
mov    %rbp,%rdx
mov    %r12,%rcx
mov    %r14,%r9
call   1050 <encode_file@plt>
mov    %eax,%r14d
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
test   %r14d,%r14d
je     1346 <hzip+0x186>
lea    0xd0e(%rip),%rdi
xor    %esi,%esi
call   1040 <fail@plt>
mov    %eax,%r15d
mov    %r15d,%eax
add    $0x90018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.item = type { i32 }
@.str = [4 x i8] c".hz\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [29 x i8] c"hzip: %s: Permission denied\0A\00"
@.str.3 = [36 x i8] c"hzip: cannot create temporary file\0A\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [25 x i8] c"hzip: cannot write file\0A\00"
define i32 @hzip(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.item**
%7 = alloca [65537 x i8*]
%8 = alloca i32
%9 = alloca [65536 x i8]
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i16
store i8* %0, i8** %4
store i8* %1, i8** %5
%14 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 0
%15 = load i8*, i8** %4
%16 = call i32 @strcpy(i8* %14, i8* %15)
%17 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 0
%18 = call i32 @strcat(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%19 = load i8*, i8** %4
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %10
%21 = load i32*, i32** %10
%22 = icmp ne i32* %21, null
br i1 %22, label %26, label %23
23: ; preds = %2
%24 = load i8*, i8** %4
%25 = call i32 @fail(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 %25, i32* %3
br label %100
26: ; preds = %2
%27 = call i32* (...) @tmpfile()
store i32* %27, i32** %12
%28 = load i32*, i32** %12
%29 = icmp ne i32* %28, null
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i32*, i32** %10
%32 = call i32 @fclose(i32* %31)
%33 = call i32 @fail(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* null)
store i32 %33, i32* %3
br label %100
34: ; preds = %26
%35 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 0
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %36, i32** %11
%37 = load i32*, i32** %11
%38 = icmp ne i32* %37, null
br i1 %38, label %46, label %39
39: ; preds = %34
%40 = load i32*, i32** %12
%41 = call i32 @fclose(i32* %40)
%42 = load i32*, i32** %10
%43 = call i32 @fclose(i32* %42)
%44 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 0
%45 = call i32 @fail(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %44)
store i32 %45, i32* %3
br label %100
46: ; preds = %34
store i32 0, i32* %8
br label %47
47: ; preds = %54, %46
%48 = load i32, i32* %8
%49 = icmp slt i32 %48, 65536
br i1 %49, label %50, label %57
50: ; preds = %47
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [65537 x i8*], [65537 x i8*]* %7, i64 0, i64 %52
store i8* null, i8** %53
br label %54
54: ; preds = %50
%55 = load i32, i32* %8
%56 = add nsw i32 %55, 1
store i32 %56, i32* %8
br label %47
57: ; preds = %47
%58 = load i32*, i32** %10
%59 = load i32*, i32** %12
%60 = call i64 @prefixcompress(i32* %58, i32* %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %70
62: ; preds = %57
%63 = load i32*, i32** %11
%64 = call i32 @fclose(i32* %63)
%65 = load i32*, i32** %12
%66 = call i32 @fclose(i32* %65)
%67 = load i32*, i32** %10
%68 = call i32 @fclose(i32* %67)
%69 = call i32 @fail(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* null)
store i32 %69, i32* %3
br label %100
70: ; preds = %57
%71 = load i32*, i32** %12
%72 = call i32 @rewind(i32* %71)
%73 = load i32*, i32** %12
%74 = call i32 @get_freqdata(%struct.item*** %6, i32* %73, i16* %13)
store i32 %74, i32* %8
%75 = load %struct.item**, %struct.item*** %6
%76 = load i32, i32* %8
%77 = getelementptr inbounds [65537 x i8*], [65537 x i8*]* %7, i64 0, i64 0
%78 = call i32 @get_codetable(%struct.item** %75, i32 %76, i8** %77)
%79 = load i32*, i32** %12
%80 = call i32 @rewind(i32* %79)
%81 = getelementptr inbounds [65537 x i8*], [65537 x i8*]* %7, i64 0, i64 0
%82 = load i32, i32* %8
%83 = load i32*, i32** %12
%84 = load i32*, i32** %11
%85 = load i16, i16* %13
%86 = load i8*, i8** %5
%87 = call i32 @encode_file(i8** %81, i32 %82, i32* %83, i32* %84, i16 zeroext %85, i8* %86)
store i32 %87, i32* %8
%88 = load i32*, i32** %11
%89 = call i32 @fclose(i32* %88)
%90 = load i32*, i32** %12
%91 = call i32 @fclose(i32* %90)
%92 = load i32*, i32** %10
%93 = call i32 @fclose(i32* %92)
%94 = load i32, i32* %8
%95 = icmp ne i32 %94, 0
br i1 %95, label %96, label %98
96: ; preds = %70
%97 = call i32 @fail(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* null)
store i32 %97, i32* %3
br label %100
98: ; preds = %70
%99 = load i32, i32* %8
store i32 %99, i32* %3
br label %100
100: ; preds = %98, %96, %62, %39, %30, %23
%101 = load i32, i32* %3
ret i32 %101
}

LLVM IR (-O2):
%struct.item = type { i32 }
@.str = [4 x i8] c".hz\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [29 x i8] c"hzip: %s: Permission denied\0A\00"
@.str.3 = [36 x i8] c"hzip: cannot create temporary file\0A\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [25 x i8] c"hzip: cannot write file\0A\00"
define i32 @hzip(i8* %0, i8* %1) {
%3 = alloca %struct.item**
%4 = alloca [65537 x i8*]
%5 = bitcast [65537 x i8*]* %4 to i8*
%6 = alloca [65536 x i8]
%7 = alloca i16
%8 = bitcast %struct.item*** %3 to i8*
%9 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%10 = bitcast i16* %7 to i8*
%11 = call i32 @strcpy(i8* %9, i8* %0)
%12 = call i32 @strcat(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%13 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @fail(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %53
17: ; preds = %2
%18 = call i32* (...) @tmpfile()
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 @fclose(i32* %13)
%22 = call i32 @fail(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* null)
br label %53
23: ; preds = %17
%24 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = call i64 @prefixcompress(i32* %13, i32* %18)
%28 = icmp eq i64 %27, 0
br i1 %28, label %38, label %33
29: ; preds = %23
%30 = call i32 @fclose(i32* %18)
%31 = call i32 @fclose(i32* %13)
%32 = call i32 @fail(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %9)
br label %53
33: ; preds = %26
%34 = call i32 @fclose(i32* %24)
%35 = call i32 @fclose(i32* %18)
%36 = call i32 @fclose(i32* %13)
%37 = call i32 @fail(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* null)
br label %53
38: ; preds = %26
%39 = call i32 @rewind(i32* %18)
%40 = call i32 @get_freqdata(%struct.item*** %3, i32* %18, i16* %7)
%41 = load %struct.item**, %struct.item*** %3
%42 = getelementptr inbounds [65537 x i8*], [65537 x i8*]* %4, i64 0, i64 0
%43 = call i32 @get_codetable(%struct.item** %41, i32 %40, i8** %42)
%44 = call i32 @rewind(i32* %18)
%45 = load i16, i16* %7
%46 = call i32 @encode_file(i8** %42, i32 %40, i32* %18, i32* %24, i16 zeroext %45, i8* %1)
%47 = call i32 @fclose(i32* %24)
%48 = call i32 @fclose(i32* %18)
%49 = call i32 @fclose(i32* %13)
%50 = icmp eq i32 %46, 0
br i1 %50, label %53, label %51
51: ; preds = %38
%52 = call i32 @fail(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* null)
br label %53
53: ; preds = %38, %51, %33, %29, %20, %15
%54 = phi i32 [ %37, %33 ], [ %52, %51 ], [ %32, %29 ], [ %22, %20 ], [ %16, %15 ], [ 0, %38 ]
ret i32 %54
}