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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(scalar_t__ *, int, int, int /*<<< orphan*/ *);
int predict(int, int, int);
int /*<<< orphan*/ update(int *, int);
void compress(char *in, char *out) {
  FILE *in_file = fopen(in, "rb");
  FILE *out_file = fopen(out, "wb");
  uint16_t high = 65535;
  uint16_t low = 0;
  uint16_t context = 0;
  uint32_t len = 0;
  int cur;
  int i;
  fwrite(&len, sizeof(len), 1, out_file);
  while ((cur = fgetc(in_file)) != EOF) {
    len++;
    for (i = 0; i < 8; i++) {
      if (1 & (cur >> i)) {
        low = predict(context, high, low) + 1;
        update(&context, 1);
      } else {
        high = predict(context, high, low);
        update(&context, 0);
      }
      if (high == low) {
        fputc(high / 256, out_file);
        fputc(high % 256, out_file);
        high = 65535;
        low = 0;
      }
    }
  }
  fputc((uint8_t)(high >> 8), out_file);
  fputc((uint8_t)(high), out_file);
  fseek(out_file, 0, SEEK_SET);
  fwrite(&len, sizeof(len), 1, out_file);
  fclose(in_file);
  fclose(out_file);
}


x86 assembly (clang, -O0):
<compress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
movl   $0xffff,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rcx
lea    -0x38(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2de9(%rip),%rcx
cmp    (%rcx),%eax
je     12d9 <compress+0x159>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x40(%rbp)
cmpl   $0x8,-0x40(%rbp)
jge    12d4 <compress+0x154>
mov    -0x3c(%rbp),%eax
mov    -0x40(%rbp),%ecx
sar    %cl,%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     125f <compress+0xdf>
mov    -0x2c(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1070 <predict@plt>
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
mov    $0x1,%esi
call   1030 <update@plt>
jmp    127b <compress+0xfb>
mov    -0x2c(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1070 <predict@plt>
mov    %eax,-0x24(%rbp)
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
call   1030 <update@plt>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jne    12c1 <compress+0x141>
mov    -0x24(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
mov    -0x20(%rbp),%rsi
call   1060 <fputc@plt>
mov    -0x24(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
mov    -0x20(%rbp),%rsi
call   1060 <fputc@plt>
movl   $0xffff,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
jmp    12c6 <compress+0x146>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    121a <compress+0x9a>
jmp    11ec <compress+0x6c>
mov    -0x24(%rbp),%edi
sar    $0x8,%edi
mov    -0x20(%rbp),%rsi
call   1060 <fputc@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1060 <fputc@plt>
mov    -0x20(%rbp),%rdi
mov    0x2cd9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rcx
lea    -0x38(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compress>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
lea    0xe68(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbp
lea    0xe5c(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
movl   $0x0,0x4(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rax,0x18(%rsp)
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    0x8(%rsp),%r14
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdi
call   1050 <fgetc@plt>
mov    %eax,%r13d
mov    0x2df0(%rip),%rax
mov    $0xffff,%ebx
cmp    (%rax),%r13d
jne    126e <compress+0xee>
mov    %r14,0x8(%rsp)
mov    %ebx,%edi
sar    $0x8,%edi
mov    0x18(%rsp),%rbp
mov    %rbp,%rsi
call   1060 <fputc@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <fputc@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   10a0 <fwrite@plt>
mov    0x10(%rsp),%rdi
call   1040 <fclose@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
mov    $0xffff,%ebx
jmp    12a6 <compress+0x126>
nopw   0x0(%rax,%rax,1)
mov    0x20(%rsp),%r14
add    $0x1,%r14
mov    0x10(%rsp),%rdi
call   1050 <fgetc@plt>
mov    %eax,%r13d
mov    0x2d4b(%rip),%rax
cmp    (%rax),%r13d
je     1202 <compress+0x82>
mov    %r14,0x20(%rsp)
xor    %r14d,%r14d
jmp    12ba <compress+0x13a>
add    $0x1,%r14d
cmp    $0x8,%r14d
je     1280 <compress+0x100>
mov    %ebp,%r15d
mov    $0x1,%r12d
mov    %r14d,%ecx
shl    %cl,%r12d
mov    0x4(%rsp),%edi
mov    %ebx,%esi
mov    %ebp,%edx
call   1070 <predict@plt>
lea    0x1(%rax),%ebp
xor    %esi,%esi
test   %r13d,%r12d
setne  %sil
cmove  %r15d,%ebp
cmove  %eax,%ebx
lea    0x4(%rsp),%rdi
call   1030 <update@plt>
cmp    %ebp,%ebx
jne    12b0 <compress+0x130>
lea    0xff(%rbp),%ebx
test   %ebp,%ebp
cmovns %ebp,%ebx
mov    %ebx,%edi
sar    $0x8,%edi
mov    0x18(%rsp),%r15
mov    %r15,%rsi
call   1060 <fputc@plt>
and    $0xffffff00,%ebx
sub    %ebx,%ebp
mov    %ebp,%edi
mov    %r15,%rsi
call   1060 <fputc@plt>
mov    $0xffff,%ebx
xor    %ebp,%ebp
jmp    12b0 <compress+0x130>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
@EOF = global i32 0
@SEEK_SET = global i32 0
define void @compress(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%13 = load i8*, i8** %3
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %5
%15 = load i8*, i8** %4
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %6
store i32 65535, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i64 0, i64* %10
%17 = load i32*, i32** %6
%18 = call i32 @fwrite(i64* %10, i32 8, i32 1, i32* %17)
br label %19
19: ; preds = %66, %2
%20 = load i32*, i32** %5
%21 = call i32 @fgetc(i32* %20)
store i32 %21, i32* %11
%22 = load i32, i32* @EOF
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %67
24: ; preds = %19
%25 = load i64, i64* %10
%26 = add nsw i64 %25, 1
store i64 %26, i64* %10
store i32 0, i32* %12
br label %27
27: ; preds = %63, %24
%28 = load i32, i32* %12
%29 = icmp slt i32 %28, 8
br i1 %29, label %30, label %66
30: ; preds = %27
%31 = load i32, i32* %11
%32 = load i32, i32* %12
%33 = ashr i32 %31, %32
%34 = and i32 1, %33
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %30
%37 = load i32, i32* %9
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = call i32 @predict(i32 %37, i32 %38, i32 %39)
%41 = add nsw i32 %40, 1
store i32 %41, i32* %8
%42 = call i32 @update(i32* %9, i32 1)
br label %49
43: ; preds = %30
%44 = load i32, i32* %9
%45 = load i32, i32* %7
%46 = load i32, i32* %8
%47 = call i32 @predict(i32 %44, i32 %45, i32 %46)
store i32 %47, i32* %7
%48 = call i32 @update(i32* %9, i32 0)
br label %49
49: ; preds = %43, %36
%50 = load i32, i32* %7
%51 = load i32, i32* %8
%52 = icmp eq i32 %50, %51
br i1 %52, label %53, label %62
53: ; preds = %49
%54 = load i32, i32* %7
%55 = sdiv i32 %54, 256
%56 = load i32*, i32** %6
%57 = call i32 @fputc(i32 %55, i32* %56)
%58 = load i32, i32* %7
%59 = srem i32 %58, 256
%60 = load i32*, i32** %6
%61 = call i32 @fputc(i32 %59, i32* %60)
store i32 65535, i32* %7
store i32 0, i32* %8
br label %62
62: ; preds = %53, %49
br label %63
63: ; preds = %62
%64 = load i32, i32* %12
%65 = add nsw i32 %64, 1
store i32 %65, i32* %12
br label %27
66: ; preds = %27
br label %19
67: ; preds = %19
%68 = load i32, i32* %7
%69 = ashr i32 %68, 8
%70 = load i32*, i32** %6
%71 = call i32 @fputc(i32 %69, i32* %70)
%72 = load i32, i32* %7
%73 = load i32*, i32** %6
%74 = call i32 @fputc(i32 %72, i32* %73)
%75 = load i32*, i32** %6
%76 = load i32, i32* @SEEK_SET
%77 = call i32 @fseek(i32* %75, i32 0, i32 %76)
%78 = load i32*, i32** %6
%79 = call i32 @fwrite(i64* %10, i32 8, i32 1, i32* %78)
%80 = load i32*, i32** %5
%81 = call i32 @fclose(i32* %80)
%82 = load i32*, i32** %6
%83 = call i32 @fclose(i32* %82)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
@EOF = global i32 0
@SEEK_SET = global i32 0
define void @compress(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca i32
%4 = alloca i64
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%8 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%9 = call i32 @fwrite(i64* %4, i32 8, i32 1, i32* %6)
%10 = load i64, i64* %4
%11 = call i32 @fgetc(i32* %5)
%12 = load i32, i32* @EOF
%13 = icmp eq i32 %11, %12
br i1 %13, label %50, label %19
14: ; preds = %45
%15 = add nsw i64 %23, 1
%16 = call i32 @fgetc(i32* %5)
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %50, label %19
19: ; preds = %2, %14
%20 = phi i32 [ %16, %14 ], [ %11, %2 ]
%21 = phi i32 [ %47, %14 ], [ 65535, %2 ]
%22 = phi i32 [ %46, %14 ], [ 0, %2 ]
%23 = phi i64 [ %15, %14 ], [ %10, %2 ]
br label %24
24: ; preds = %19, %45
%25 = phi i32 [ 0, %19 ], [ %48, %45 ]
%26 = phi i32 [ %21, %19 ], [ %47, %45 ]
%27 = phi i32 [ %22, %19 ], [ %46, %45 ]
%28 = shl nuw nsw i32 1, %25
%29 = and i32 %28, %20
%30 = icmp eq i32 %29, 0
%31 = load i32, i32* %3
%32 = call i32 @predict(i32 %31, i32 %26, i32 %27)
%33 = add nsw i32 %32, 1
%34 = xor i1 %30, true
%35 = zext i1 %34 to i32
%36 = select i1 %30, i32 %27, i32 %33
%37 = select i1 %30, i32 %32, i32 %26
%38 = call i32 @update(i32* %3, i32 %35)
%39 = icmp eq i32 %37, %36
br i1 %39, label %40, label %45
40: ; preds = %24
%41 = sdiv i32 %36, 256
%42 = call i32 @fputc(i32 %41, i32* %6)
%43 = srem i32 %36, 256
%44 = call i32 @fputc(i32 %43, i32* %6)
br label %45
45: ; preds = %24, %40
%46 = phi i32 [ 0, %40 ], [ %36, %24 ]
%47 = phi i32 [ 65535, %40 ], [ %37, %24 ]
%48 = add nuw nsw i32 %25, 1
%49 = icmp eq i32 %48, 8
br i1 %49, label %14, label %24
50: ; preds = %14, %2
%51 = phi i64 [ %10, %2 ], [ %15, %14 ]
%52 = phi i32 [ 65535, %2 ], [ %47, %14 ]
store i64 %51, i64* %4
%53 = ashr i32 %52, 8
%54 = call i32 @fputc(i32 %53, i32* %6)
%55 = call i32 @fputc(i32 %52, i32* %6)
%56 = load i32, i32* @SEEK_SET
%57 = call i32 @fseek(i32* %6, i32 0, i32 %56)
%58 = call i32 @fwrite(i64* %4, i32 8, i32 1, i32* %6)
%59 = call i32 @fclose(i32* %5)
%60 = call i32 @fclose(i32* %6)
ret void
}