#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *bbcompress(int /*<<< orphan*/ *, int, int *);
int /*<<< orphan*/ *bbdecompress(int /*<<< orphan*/ *, int, int *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *const, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(int /*<<< orphan*/ *, int, int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ malloc(int const);
scalar_t__ memcmp(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ stderr;
int main(const int argc, const char *const argv[]) {
  FILE *file;
  uint8_t *in_buffer, *mid_buffer, *out_buffer;
  const int in_buffer_size = 1 << 20;
  int in_size, mid_size = 0, out_size = 0;
  if (argc < 2) {
    fprintf(stderr, "Usage: bbexample filename\n");
    return 1;
  }
  file = fopen(argv[1], "rb");
  if (!file) {
    fprintf(stderr, "bbexample: Can't open file '%s' for reading\n", argv[1]);
    return 1;
  }
  in_buffer = (uint8_t *)malloc(in_buffer_size);
  if (!in_buffer) {
    fprintf(stderr, "bbexample: Not enough memory.\n");
    return 1;
  }
  in_size = fread(in_buffer, 1, in_buffer_size, file);
  if (in_size >= in_buffer_size) {
    fprintf(stderr, "bbexample: Input file '%s' is too big.\n", argv[1]);
    return 1;
  }
  fclose(file);
  mid_buffer = bbcompress(in_buffer, in_size, &mid_size);
  if (!mid_buffer) {
    fprintf(stderr, "bbexample: Not enough memory or compress error.\n");
    return 1;
  }
  out_buffer = bbdecompress(mid_buffer, mid_size, &out_size);
  if (!out_buffer) {
    fprintf(stderr, "bbexample: Not enough memory or decompress error.\n");
    return 1;
  }
  if (in_size != out_size ||
      (in_size > 0 && memcmp(in_buffer, out_buffer, in_size) != 0)) {
    fprintf(stderr, "bbexample: Decompressed data differs from original.\n");
    return 1;
  }
  free(out_buffer);
  free(mid_buffer);
  free(in_buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x100000,-0x34(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11e8 <main+0x58>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c6 <main+0x236>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe24(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1236 <main+0xa6>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdfb(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c6 <main+0x236>
mov    $0x100000,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1272 <main+0xe2>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c6 <main+0x236>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x100000,%edx
call   1040 <fread@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x100000,-0x38(%rbp)
jl     12c4 <main+0x134>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdb9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c6 <main+0x236>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%esi
lea    -0x3c(%rbp),%rdx
call   1050 <bbcompress@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    130f <main+0x17f>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c6 <main+0x236>
mov    -0x28(%rbp),%rdi
mov    -0x3c(%rbp),%esi
lea    -0x40(%rbp),%rdx
call   10b0 <bbdecompress@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1351 <main+0x1c1>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
lea    0xd85(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c6 <main+0x236>
mov    -0x38(%rbp),%eax
cmp    -0x40(%rbp),%eax
jne    1381 <main+0x1f1>
cmpl   $0x0,-0x38(%rbp)
jle    13a4 <main+0x214>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     13a4 <main+0x214>
mov    0x2c58(%rip),%rax
mov    (%rax),%edi
lea    0xd65(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c6 <main+0x236>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
cmp    $0x1,%edi
jg     11cd <main+0x3d>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1253 <main+0xc3>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe40(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1238 <main+0xa8>
mov    %rax,%r12
mov    $0x100000,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1267 <main+0xd7>
mov    %rax,%r14
mov    $0x1,%ebp
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x100000,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
cmp    $0x100000,%eax
jl     127c <main+0xec>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe3b(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1258 <main+0xc8>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xdd2(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
jmp    11c1 <main+0x31>
mov    %eax,%r15d
mov    %r12,%rdi
call   1060 <fclose@plt>
lea    0xc(%rsp),%rdx
mov    %r14,%rdi
mov    %r15d,%esi
call   1050 <bbcompress@plt>
test   %rax,%rax
je     12ec <main+0x15c>
mov    %rax,%rbx
mov    0xc(%rsp),%esi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   10b0 <bbdecompress@plt>
test   %rax,%rax
je     1301 <main+0x171>
cmp    0x8(%rsp),%r15d
jne    12d7 <main+0x147>
mov    %rax,%rbp
test   %r15d,%r15d
jle    1316 <main+0x186>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1070 <memcmp@plt>
test   %rax,%rax
je     1316 <main+0x186>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
jmp    11c1 <main+0x31>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
jmp    11c1 <main+0x31>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
jmp    11c1 <main+0x31>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
jmp    1258 <main+0xc8>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: bbexample filename\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [45 x i8] c"bbexample: Can't open file '%s' for reading\0A\00"
@.str.3 = [31 x i8] c"bbexample: Not enough memory.\0A\00"
@.str.4 = [40 x i8] c"bbexample: Input file '%s' is too big.\0A\00"
@.str.5 = [49 x i8] c"bbexample: Not enough memory or compress error.\0A\00"
@.str.6 = [51 x i8] c"bbexample: Not enough memory or decompress error.\0A\00"
@.str.7 = [53 x i8] c"bbexample: Decompressed data differs from original.\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 i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1048576, i32* %10
store i32 0, i32* %12
store i32 0, i32* %13
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %95
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %6
%24 = load i32*, i32** %6
%25 = icmp ne i32* %24, null
br i1 %25, label %32, label %26
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %30)
store i32 1, i32* %3
br label %95
32: ; preds = %19
%33 = call i64 @malloc(i32 1048576)
%34 = inttoptr i64 %33 to i32*
store i32* %34, i32** %7
%35 = load i32*, i32** %7
%36 = icmp ne i32* %35, null
br i1 %36, label %40, label %37
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %95
40: ; preds = %32
%41 = load i32*, i32** %7
%42 = load i32*, i32** %6
%43 = call i32 @fread(i32* %41, i32 1, i32 1048576, i32* %42)
store i32 %43, i32* %11
%44 = load i32, i32* %11
%45 = icmp sge i32 %44, 1048576
br i1 %45, label %46, label %52
46: ; preds = %40
%47 = load i32, i32* @stderr
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* %50)
store i32 1, i32* %3
br label %95
52: ; preds = %40
%53 = load i32*, i32** %6
%54 = call i32 @fclose(i32* %53)
%55 = load i32*, i32** %7
%56 = load i32, i32* %11
%57 = call i32* @bbcompress(i32* %55, i32 %56, i32* %12)
store i32* %57, i32** %8
%58 = load i32*, i32** %8
%59 = icmp ne i32* %58, null
br i1 %59, label %63, label %60
60: ; preds = %52
%61 = load i32, i32* @stderr
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %95
63: ; preds = %52
%64 = load i32*, i32** %8
%65 = load i32, i32* %12
%66 = call i32* @bbdecompress(i32* %64, i32 %65, i32* %13)
store i32* %66, i32** %9
%67 = load i32*, i32** %9
%68 = icmp ne i32* %67, null
br i1 %68, label %72, label %69
69: ; preds = %63
%70 = load i32, i32* @stderr
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %95
72: ; preds = %63
%73 = load i32, i32* %11
%74 = load i32, i32* %13
%75 = icmp ne i32 %73, %74
br i1 %75, label %85, label %76
76: ; preds = %72
%77 = load i32, i32* %11
%78 = icmp sgt i32 %77, 0
br i1 %78, label %79, label %88
79: ; preds = %76
%80 = load i32*, i32** %7
%81 = load i32*, i32** %9
%82 = load i32, i32* %11
%83 = call i64 @memcmp(i32* %80, i32* %81, i32 %82)
%84 = icmp ne i64 %83, 0
br i1 %84, label %85, label %88
85: ; preds = %79, %72
%86 = load i32, i32* @stderr
%87 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %95
88: ; preds = %79, %76
%89 = load i32*, i32** %9
%90 = call i32 @free(i32* %89)
%91 = load i32*, i32** %8
%92 = call i32 @free(i32* %91)
%93 = load i32*, i32** %7
%94 = call i32 @free(i32* %93)
store i32 0, i32* %3
br label %95
95: ; preds = %88, %85, %69, %60, %46, %37, %26, %16
%96 = load i32, i32* %3
ret i32 %96
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: bbexample filename\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [45 x i8] c"bbexample: Can't open file '%s' for reading\0A\00"
@.str.3 = [31 x i8] c"bbexample: Not enough memory.\0A\00"
@.str.4 = [40 x i8] c"bbexample: Input file '%s' is too big.\0A\00"
@.str.5 = [49 x i8] c"bbexample: Not enough memory or compress error.\0A\00"
@.str.6 = [51 x i8] c"bbexample: Not enough memory or decompress error.\0A\00"
@.str.7 = [53 x i8] c"bbexample: Decompressed data differs from original.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %63
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %12
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %63
20: ; preds = %11
%21 = tail call i64 @malloc(i32 1048576)
%22 = inttoptr i64 %21 to i32*
%23 = icmp eq i64 %21, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %63
27: ; preds = %20
%28 = tail call i32 @fread(i32* %22, i32 1, i32 1048576, i32* %14)
%29 = icmp sgt i32 %28, 1048575
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %12
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* %32)
br label %63
34: ; preds = %27
%35 = tail call i32 @fclose(i32* %14)
%36 = call i32* @bbcompress(i32* %22, i32 %28, i32* %3)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0))
br label %63
41: ; preds = %34
%42 = load i32, i32* %3
%43 = call i32* @bbdecompress(i32* %36, i32 %42, i32* %4)
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0))
br label %63
48: ; preds = %41
%49 = load i32, i32* %4
%50 = icmp eq i32 %28, %49
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = icmp sgt i32 %28, 0
br i1 %52, label %53, label %59
53: ; preds = %51
%54 = call i64 @memcmp(i32* %22, i32* %43, i32 %28)
%55 = icmp eq i64 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %53, %48
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0))
br label %63
59: ; preds = %53, %51
%60 = call i32 @free(i32* %43)
%61 = call i32 @free(i32* %36)
%62 = call i32 @free(i32* %22)
br label %63
63: ; preds = %59, %56, %45, %38, %30, %24, %16, %8
%64 = phi i32 [ 1, %8 ], [ 1, %30 ], [ 1, %56 ], [ 0, %59 ], [ 1, %45 ], [ 1, %38 ], [ 1, %24 ], [ 1, %16 ]
ret i32 %64
}