#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ fread(unsigned char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
unsigned int fwrite(unsigned char *, int, unsigned int, int /*<<< orphan*/ *);
scalar_t__ huffman_encode_memory(unsigned char *, unsigned int,
                                 unsigned char **, unsigned int *);
scalar_t__ realloc(unsigned char *, unsigned int);
int memory_encode_file(FILE *in, FILE *out) {
  unsigned char *buf = NULL, *bufout = NULL;
  unsigned int len = 0, cur = 0, inc = 1024, bufoutlen = 0;
  assert(in && out);
  while (!feof(in)) {
    unsigned char *tmp;
    len += inc;
    tmp = (unsigned char *)realloc(buf, len);
    if (!tmp) {
      if (buf)
        free(buf);
      return 1;
    }
    buf = tmp;
    cur += fread(buf + cur, 1, inc, in);
  }
  if (!buf)
    return 1;
  if (huffman_encode_memory(buf, cur, &bufout, &bufoutlen)) {
    free(buf);
    return 1;
  }
  free(buf);
  if (fwrite(bufout, 1, bufoutlen, out) != bufoutlen) {
    free(bufout);
    return 1;
  }
  free(bufout);
  return 0;
}


x86 assembly (clang, -O0):
<memory_encode_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x400,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x41(%rbp)
je     11c7 <memory_encode_file+0x57>
cmpq   $0x0,-0x18(%rbp)
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11f2 <memory_encode_file+0x82>
jmp    126a <memory_encode_file+0xfa>
mov    -0x34(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1080 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1236 <memory_encode_file+0xc6>
cmpq   $0x0,-0x20(%rbp)
je     122a <memory_encode_file+0xba>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <memory_encode_file+0x190>
mov    -0x40(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%eax
add    %rax,%rdi
mov    -0x34(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x30(%rbp)
jmp    11d4 <memory_encode_file+0x64>
cmpq   $0x0,-0x20(%rbp)
jne    1281 <memory_encode_file+0x111>
movl   $0x1,-0x4(%rbp)
jmp    1300 <memory_encode_file+0x190>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%esi
lea    -0x28(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1070 <huffman_encode_memory@plt>
cmp    $0x0,%rax
je     12b4 <memory_encode_file+0x144>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <memory_encode_file+0x190>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
cmp    -0x38(%rbp),%eax
je     12f0 <memory_encode_file+0x180>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <memory_encode_file+0x190>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memory_encode_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movq   $0x0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
test   %rdi,%rdi
setne  %al
test   %rsi,%rsi
setne  %cl
and    %al,%cl
movzbl %cl,%edi
call   1050 <assert@plt>
mov    %r15,%rdi
call   1060 <feof@plt>
mov    $0x1,%ebx
test   %eax,%eax
jne    1275 <memory_encode_file+0x105>
xor    %r13d,%r13d
mov    $0x400,%ebx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %ebx,%esi
call   1080 <realloc@plt>
test   %rax,%rax
je     1263 <memory_encode_file+0xf3>
mov    %rax,%r12
mov    %ebp,%edi
add    %rax,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
add    %eax,%ebp
mov    %r15,%rdi
call   1060 <feof@plt>
add    $0x400,%ebx
mov    %r12,%r13
test   %eax,%eax
je     11d0 <memory_encode_file+0x60>
lea    0x10(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %r12,%rdi
mov    %ebp,%esi
call   1070 <huffman_encode_memory@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1030 <free@plt>
mov    $0x1,%ebx
test   %rbp,%rbp
jne    1275 <memory_encode_file+0x105>
mov    0x10(%rsp),%rdi
mov    0xc(%rsp),%edx
mov    $0x1,%esi
mov    %r14,%rcx
call   1090 <fwrite@plt>
xor    %ebx,%ebx
cmp    0xc(%rsp),%eax
setne  %bl
mov    0x10(%rsp),%rdi
jmp    1270 <memory_encode_file+0x100>
mov    $0x1,%ebx
test   %r13,%r13
je     1275 <memory_encode_file+0x105>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @memory_encode_file(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32* %0, i32** %4
store i32* %1, i32** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 1024, i32* %10
store i32 0, i32* %11
%13 = load i32*, i32** %4
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32*, i32** %5
%17 = icmp ne i32* %16, null
br label %18
18: ; preds = %15, %2
%19 = phi i1 [ false, %2 ], [ %17, %15 ]
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
br label %22
22: ; preds = %44, %18
%23 = load i32*, i32** %4
%24 = call i32 @feof(i32* %23)
%25 = icmp ne i32 %24, 0
%26 = xor i1 %25, true
br i1 %26, label %27, label %57
27: ; preds = %22
%28 = load i32, i32* %10
%29 = load i32, i32* %8
%30 = add i32 %29, %28
store i32 %30, i32* %8
%31 = load i8*, i8** %6
%32 = load i32, i32* %8
%33 = call i64 @realloc(i8* %31, i32 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %12
%35 = load i8*, i8** %12
%36 = icmp ne i8* %35, null
br i1 %36, label %44, label %37
37: ; preds = %27
%38 = load i8*, i8** %6
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i8*, i8** %6
%42 = call i32 @free(i8* %41)
br label %43
43: ; preds = %40, %37
store i32 1, i32* %3
br label %84
44: ; preds = %27
%45 = load i8*, i8** %12
store i8* %45, i8** %6
%46 = load i8*, i8** %6
%47 = load i32, i32* %9
%48 = zext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = load i32, i32* %10
%51 = load i32*, i32** %4
%52 = call i64 @fread(i8* %49, i32 1, i32 %50, i32* %51)
%53 = load i32, i32* %9
%54 = zext i32 %53 to i64
%55 = add nsw i64 %54, %52
%56 = trunc i64 %55 to i32
store i32 %56, i32* %9
br label %22
57: ; preds = %22
%58 = load i8*, i8** %6
%59 = icmp ne i8* %58, null
br i1 %59, label %61, label %60
60: ; preds = %57
store i32 1, i32* %3
br label %84
61: ; preds = %57
%62 = load i8*, i8** %6
%63 = load i32, i32* %9
%64 = call i64 @huffman_encode_memory(i8* %62, i32 %63, i8** %7, i32* %11)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %61
%67 = load i8*, i8** %6
%68 = call i32 @free(i8* %67)
store i32 1, i32* %3
br label %84
69: ; preds = %61
%70 = load i8*, i8** %6
%71 = call i32 @free(i8* %70)
%72 = load i8*, i8** %7
%73 = load i32, i32* %11
%74 = load i32*, i32** %5
%75 = call i32 @fwrite(i8* %72, i32 1, i32 %73, i32* %74)
%76 = load i32, i32* %11
%77 = icmp ne i32 %75, %76
br i1 %77, label %78, label %81
78: ; preds = %69
%79 = load i8*, i8** %7
%80 = call i32 @free(i8* %79)
store i32 1, i32* %3
br label %84
81: ; preds = %69
%82 = load i8*, i8** %7
%83 = call i32 @free(i8* %82)
store i32 0, i32* %3
br label %84
84: ; preds = %81, %78, %66, %60, %43
%85 = load i32, i32* %3
ret i32 %85
}

LLVM IR (-O2):
define i32 @memory_encode_file(i32* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = icmp ne i32* %0, null
%8 = icmp ne i32* %1, null
%9 = and i1 %7, %8
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = tail call i32 @feof(i32* %0)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %48
14: ; preds = %2, %26
%15 = phi i8* [ %20, %26 ], [ null, %2 ]
%16 = phi i32 [ %31, %26 ], [ 0, %2 ]
%17 = phi i32 [ %18, %26 ], [ 0, %2 ]
%18 = add i32 %17, 1024
%19 = tail call i64 @realloc(i8* %15, i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %26
22: ; preds = %14
%23 = icmp eq i8* %15, null
br i1 %23, label %48, label %24
24: ; preds = %22
%25 = tail call i32 @free(i8* %15)
br label %48
26: ; preds = %14
%27 = zext i32 %16 to i64
%28 = getelementptr inbounds i8, i8* %20, i64 %27
%29 = tail call i64 @fread(i8* %28, i32 1, i32 1024, i32* %0)
%30 = trunc i64 %29 to i32
%31 = add i32 %16, %30
%32 = tail call i32 @feof(i32* %0)
%33 = icmp eq i32 %32, 0
br i1 %33, label %14, label %34
34: ; preds = %26
%35 = inttoptr i64 %19 to i8*
%36 = call i64 @huffman_encode_memory(i8* %35, i32 %31, i8** %3, i32* %4)
%37 = icmp eq i64 %36, 0
%38 = call i32 @free(i8* %35)
br i1 %37, label %39, label %48
39: ; preds = %34
%40 = load i8*, i8** %3
%41 = load i32, i32* %4
%42 = call i32 @fwrite(i8* %40, i32 1, i32 %41, i32* %1)
%43 = load i32, i32* %4
%44 = icmp ne i32 %42, %43
%45 = load i8*, i8** %3
%46 = call i32 @free(i8* %45)
%47 = zext i1 %44 to i32
br label %48
48: ; preds = %39, %34, %2, %24, %22
%49 = phi i32 [ 1, %22 ], [ 1, %24 ], [ 1, %2 ], [ 1, %34 ], [ %47, %39 ]
ret i32 %49
}