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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int predict(int, int, int);
int /*<<< orphan*/ update(int *, int);
void decompress(char *in, char *out) {
  FILE *in_file = fopen(in, "rb");
  FILE *out_file = fopen(out, "wb");
  uint16_t high = 0;
  uint16_t mid;
  uint16_t val = 0;
  uint16_t low = 0;
  uint16_t context = 0;
  uint32_t len;
  int cur;
  int outc;
  int pos = 0;
  fread(&len, sizeof(len), 1, in_file);
  while (1) {
    while (high == low) {
      val = (val << 8) | fgetc(in_file);
      val = (val << 8) | fgetc(in_file);
      low = 0;
      high = 65535;
    }
    mid = predict(context, high, low);
    if (val > mid) {
      low = mid + 1;
      outc |= (1 << (pos++));
      update(&context, 1);
    } else {
      high = mid;
      outc &= ~(1 << (pos++));
      update(&context, 0);
    }
    if (pos == 8) {
      fputc(outc, out_file);
      if (!(--len)) {
        break;
      }
      pos = 0;
    }
  }
  fclose(in_file);
  fclose(out_file);
}


x86 assembly (clang, -O0):
<decompress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0x38(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
jmp    11e7 <decompress+0x77>
mov    -0x24(%rbp),%eax
cmp    -0x30(%rbp),%eax
jne    123e <decompress+0xce>
mov    -0x2c(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0xffff,-0x24(%rbp)
jmp    11e7 <decompress+0x77>
mov    -0x34(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%edx
call   1080 <predict@plt>
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jle    128f <decompress+0x11f>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x44(%rbp),%ecx
mov    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    $0x1,%eax
shl    %cl,%eax
or     -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
lea    -0x34(%rbp),%rdi
mov    $0x1,%esi
call   1030 <update@plt>
jmp    12bb <decompress+0x14b>
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x44(%rbp),%ecx
mov    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    $0x1,%eax
shl    %cl,%eax
xor    $0xffffffff,%eax
and    -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
lea    -0x34(%rbp),%rdi
xor    %esi,%esi
call   1030 <update@plt>
cmpl   $0x8,-0x44(%rbp)
jne    12ef <decompress+0x17f>
mov    -0x40(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1070 <fputc@plt>
mov    -0x38(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jne    12e8 <decompress+0x178>
jmp    12f4 <decompress+0x184>
movl   $0x0,-0x44(%rbp)
jmp    11e2 <decompress+0x72>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decompress>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
lea    0xe78(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r12
lea    0xe6c(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
mov    %rax,0x10(%rsp)
movl   $0x0,0x4(%rsp)
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
xor    %r14d,%r14d
mov    0xc(%rsp),%eax
xor    %ebx,%ebx
xor    %ebp,%ebp
jmp    11e6 <decompress+0x76>
mov    %r13d,%edi
mov    0x10(%rsp),%rsi
call   1070 <fputc@plt>
mov    0x8(%rsp),%eax
add    $0xffffffff,%eax
je     125a <decompress+0xea>
mov    %eax,0x8(%rsp)
xor    %r15d,%r15d
jmp    120c <decompress+0x9c>
nop
btr    %r15d,%r13d
xor    %esi,%esi
mov    %eax,%ebp
lea    0x4(%rsp),%rdi
call   1030 <update@plt>
add    $0x1,%r15d
cmp    $0x8,%r15d
je     11d0 <decompress+0x60>
cmp    %ebx,%ebp
jne    1238 <decompress+0xc8>
mov    %r12,%rdi
call   1060 <fgetc@plt>
mov    %eax,%ebx
shl    $0x10,%r14d
shl    $0x8,%ebx
mov    %r12,%rdi
call   1060 <fgetc@plt>
or     %r14d,%eax
or     %ebx,%eax
mov    $0xffff,%ebp
xor    %ebx,%ebx
mov    %eax,%r14d
mov    0x4(%rsp),%edi
mov    %ebp,%esi
mov    %ebx,%edx
call   1080 <predict@plt>
cmp    %eax,%r14d
jle    11f0 <decompress+0x80>
add    $0x1,%eax
bts    %r15d,%r13d
mov    $0x1,%esi
mov    %eax,%ebx
jmp    11f8 <decompress+0x88>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    0x10(%rsp),%rdi
call   1050 <fclose@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
define void @decompress(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%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 i32* %4 to i8*
%9 = call i32 @fread(i32* %4, i32 4, i32 1, i32* %5)
%10 = load i32, i32* %4
br label %11
11: ; preds = %54, %2
%12 = phi i32 [ %56, %54 ], [ %10, %2 ]
%13 = phi i32 [ %32, %54 ], [ 0, %2 ]
%14 = phi i32 [ %48, %54 ], [ 0, %2 ]
%15 = phi i32 [ %49, %54 ], [ 0, %2 ]
%16 = phi i32 [ %50, %54 ], [ undef, %2 ]
br label %17
17: ; preds = %11, %46
%18 = phi i32 [ %32, %46 ], [ %13, %11 ]
%19 = phi i32 [ %48, %46 ], [ %14, %11 ]
%20 = phi i32 [ %49, %46 ], [ %15, %11 ]
%21 = phi i32 [ %50, %46 ], [ %16, %11 ]
%22 = phi i32 [ %52, %46 ], [ 0, %11 ]
%23 = icmp eq i32 %20, %19
br i1 %23, label %24, label %31
24: ; preds = %17
%25 = call i32 @fgetc(i32* %5)
%26 = shl i32 %18, 16
%27 = shl i32 %25, 8
%28 = or i32 %27, %26
%29 = call i32 @fgetc(i32* %5)
%30 = or i32 %28, %29
br label %31
31: ; preds = %24, %17
%32 = phi i32 [ %30, %24 ], [ %18, %17 ]
%33 = phi i32 [ 0, %24 ], [ %19, %17 ]
%34 = phi i32 [ 65535, %24 ], [ %20, %17 ]
%35 = load i32, i32* %3
%36 = call i32 @predict(i32 %35, i32 %34, i32 %33)
%37 = icmp sgt i32 %32, %36
br i1 %37, label %38, label %42
38: ; preds = %31
%39 = add nsw i32 %36, 1
%40 = shl nuw i32 1, %22
%41 = or i32 %40, %21
br label %46
42: ; preds = %31
%43 = shl nuw i32 1, %22
%44 = xor i32 %43, -1
%45 = and i32 %21, %44
br label %46
46: ; preds = %42, %38
%47 = phi i32 [ 0, %42 ], [ 1, %38 ]
%48 = phi i32 [ %33, %42 ], [ %39, %38 ]
%49 = phi i32 [ %36, %42 ], [ %34, %38 ]
%50 = phi i32 [ %45, %42 ], [ %41, %38 ]
%51 = call i32 @update(i32* %3, i32 %47)
%52 = add nuw nsw i32 %22, 1
%53 = icmp eq i32 %52, 8
br i1 %53, label %54, label %17
54: ; preds = %46
%55 = call i32 @fputc(i32 %50, i32* %6)
%56 = add nsw i32 %12, -1
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %11
58: ; preds = %54
%59 = call i32 @fclose(i32* %5)
%60 = call i32 @fclose(i32* %6)
ret void
}