#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 stat {
  int st_size;
};

/* Variables and functions */
int LZ_Compress(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fstat(int, struct stat *);
unsigned char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, unsigned char *, int);
int /*<<< orphan*/ write(int, ...);
int main(int argc, char *argv[]) {
  int in, out, size;
  struct stat buf;
  unsigned char *uncompressed, *compressed;
  if (argc == 3) {
    in = open(argv[1], O_RDONLY);
    out = creat(argv[2], 0600);
    fstat(in, &buf);
    uncompressed = malloc(buf.st_size);
    read(in, uncompressed, buf.st_size);
    close(in);
    compressed = malloc(buf.st_size * 2);
    size = LZ_Compress(uncompressed, compressed, buf.st_size);
    write(out, "LZ77", 4);
    write(out, compressed, size);
    close(out);
    free(compressed);
    free(uncompressed);
    return 0;
  }
  printf("Usage: %s file.bin file.lz77\n", argv[0]);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    128c <main+0xec>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    $0x180,%esi
call   10b0 <creat@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10c0 <fstat@plt>
mov    -0x20(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1070 <read@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%edi
shl    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1080 <LZ_Compress@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
lea    0xdb7(%rip),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a6 <main+0x106>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd6b(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1267 <main+0xc7>
mov    0x8(%rbx),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
mov    $0x180,%esi
call   10b0 <creat@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   10c0 <fstat@plt>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    %ebp,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1070 <read@plt>
mov    %ebp,%edi
call   1060 <close@plt>
lea    (%rbx,%rbx,1),%edi
call   1090 <malloc@plt>
mov    %rax,%rbp
mov    %r15,%rdi
mov    %rax,%rsi
mov    %ebx,%edx
call   1080 <LZ_Compress@plt>
mov    %eax,%r12d
lea    0xdd4(%rip),%rsi
xor    %ebx,%ebx
mov    %r14d,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <write@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1040 <write@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    127b <main+0xdb>
mov    (%rbx),%rsi
lea    0xd94(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"LZ77\00"
@.str.1 = [30 x i8] c"Usage: %s file.bin file.lz77\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.stat
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 3
br i1 %13, label %14, label %57
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = call i32 @creat(i8* %22, i32 384)
store i32 %23, i32* %7
%24 = load i32, i32* %6
%25 = call i32 @fstat(i32 %24, %struct.stat* %9)
%26 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %10
%29 = load i32, i32* %6
%30 = load i8*, i8** %10
%31 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @read(i32 %29, i8* %30, i32 %32)
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%37 = load i32, i32* %36
%38 = mul nsw i32 %37, 2
%39 = call i8* @malloc(i32 %38)
store i8* %39, i8** %11
%40 = load i8*, i8** %10
%41 = load i8*, i8** %11
%42 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @LZ_Compress(i8* %40, i8* %41, i32 %43)
store i32 %44, i32* %8
%45 = load i32, i32* %7
%46 = call i32 (i32, ...) @write(i32 %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%47 = load i32, i32* %7
%48 = load i8*, i8** %11
%49 = load i32, i32* %8
%50 = call i32 (i32, ...) @write(i32 %47, i8* %48, i32 %49)
%51 = load i32, i32* %7
%52 = call i32 @close(i32 %51)
%53 = load i8*, i8** %11
%54 = call i32 @free(i8* %53)
%55 = load i8*, i8** %10
%56 = call i32 @free(i8* %55)
store i32 0, i32* %3
br label %62
57: ; preds = %2
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 0
%60 = load i8*, i8** %59
%61 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %60)
store i32 1, i32* %3
br label %62
62: ; preds = %57, %14
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"LZ77\00"
@.str.1 = [30 x i8] c"Usage: %s file.bin file.lz77\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %6, label %28
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @creat(i8* %12, i32 384)
%14 = call i32 @fstat(i32 %10, %struct.stat* %3)
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i8* @malloc(i32 %16)
%18 = tail call i32 @read(i32 %10, i8* %17, i32 %16)
%19 = tail call i32 @close(i32 %10)
%20 = shl nsw i32 %16, 1
%21 = tail call i8* @malloc(i32 %20)
%22 = tail call i32 @LZ_Compress(i8* %17, i8* %21, i32 %16)
%23 = tail call i32 (i32, ...) @write(i32 %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%24 = tail call i32 (i32, ...) @write(i32 %13, i8* %21, i32 %22)
%25 = tail call i32 @close(i32 %13)
%26 = tail call i32 @free(i8* %21)
%27 = tail call i32 @free(i8* %17)
br label %31
28: ; preds = %2
%29 = load i8*, i8** %1
%30 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %28, %6
%32 = phi i32 [ 0, %6 ], [ 1, %28 ]
ret i32 %32
}