#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
unsigned char *malloc(int);
int mio0_encode(unsigned char *, size_t, unsigned char *);
int mio0_encode_file(const char *in_file, const char *out_file) {
  FILE *in;
  FILE *out;
  unsigned char *in_buf = NULL;
  unsigned char *out_buf = NULL;
  size_t file_size;
  size_t bytes_read;
  int bytes_encoded;
  int bytes_written;
  int ret_val = 0;
  in = fopen(in_file, "rb");
  if (in == NULL) {
    return 1;
  }
  fseek(in, 0, SEEK_END);
  file_size = ftell(in);
  fseek(in, 0, SEEK_SET);
  in_buf = malloc(file_size);
  bytes_read = fread(in_buf, 1, file_size, in);
  if (bytes_read != file_size) {
    ret_val = 2;
    goto free_all;
  }
  out_buf = malloc(16 + ((file_size + 7) / 8) + file_size);
  bytes_encoded = mio0_encode(in_buf, file_size, out_buf);
  out = fopen(out_file, "wb");
  if (out == NULL) {
    ret_val = 4;
    goto free_all;
  }
  bytes_written = fwrite(out_buf, 1, bytes_encoded, out);
  if (bytes_written != bytes_encoded) {
    ret_val = 5;
  }
  fclose(out);
free_all:
  if (out_buf) {
    free(out_buf);
  }
  if (in_buf) {
    free(in_buf);
  }
  fclose(in);
  return ret_val;
}


x86 assembly (clang, -O0):
<mio0_encode_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e2 <mio0_encode_file+0x52>
movl   $0x1,-0x4(%rbp)
jmp    1323 <mio0_encode_file+0x193>
mov    -0x20(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x40(%rbp),%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x40(%rbp),%rax
je     125a <mio0_encode_file+0xca>
movl   $0x2,-0x54(%rbp)
jmp    12ec <mio0_encode_file+0x15c>
mov    -0x40(%rbp),%rax
add    $0x7,%rax
shr    $0x3,%rax
add    $0x10,%rax
add    -0x40(%rbp),%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   10a0 <mio0_encode@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xd6b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12b8 <mio0_encode_file+0x128>
movl   $0x4,-0x54(%rbp)
jmp    12ec <mio0_encode_file+0x15c>
mov    -0x38(%rbp),%rdi
mov    -0x4c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x4c(%rbp),%eax
je     12e3 <mio0_encode_file+0x153>
movl   $0x5,-0x54(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x38(%rbp)
je     1300 <mio0_encode_file+0x170>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x30(%rbp)
je     1314 <mio0_encode_file+0x184>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mio0_encode_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
lea    0xe5b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1296 <mio0_encode_file+0x106>
mov    %rax,%r13
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r13,%rdi
call   1060 <ftell@plt>
mov    %rax,%rbx
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1040 <fread@plt>
mov    $0x2,%ebp
cmp    %rbx,%rax
jne    127f <mio0_encode_file+0xef>
mov    %rbx,%rax
add    $0x7,%rax
shr    $0x3,%rax
lea    (%rbx,%rax,1),%edi
add    $0x10,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   10a0 <mio0_encode@plt>
mov    %eax,%ebp
lea    0xdc5(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12ac <mio0_encode_file+0x11c>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
xor    %ecx,%ecx
cmp    %ebp,%eax
setne  %cl
lea    (%rcx,%rcx,4),%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %r12,%r12
je     127f <mio0_encode_file+0xef>
mov    %r12,%rdi
call   1030 <free@plt>
test   %r14,%r14
je     128c <mio0_encode_file+0xfc>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
jmp    129b <mio0_encode_file+0x10b>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x4,%ebp
test   %r12,%r12
jne    1277 <mio0_encode_file+0xe7>
jmp    127f <mio0_encode_file+0xef>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [3 x i8] c"wb\00"
define i32 @mio0_encode_file(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %42, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %3, i32 0, i32 %6)
%8 = tail call i64 @ftell(i32* %3)
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %3, i32 0, i32 %9)
%11 = trunc i64 %8 to i32
%12 = tail call i8* @malloc(i32 %11)
%13 = tail call i64 @fread(i8* %12, i32 1, i64 %8, i32* %3)
%14 = icmp eq i64 %13, %8
br i1 %14, label %15, label %35
15: ; preds = %5
%16 = add i64 %8, 7
%17 = lshr i64 %16, 3
%18 = add i64 %8, 16
%19 = add i64 %18, %17
%20 = trunc i64 %19 to i32
%21 = tail call i8* @malloc(i32 %20)
%22 = tail call i32 @mio0_encode(i8* %12, i64 %8, i8* %21)
%23 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %30, label %25
25: ; preds = %15
%26 = tail call i32 @fwrite(i8* %21, i32 1, i32 %22, i32* %23)
%27 = icmp eq i32 %26, %22
%28 = select i1 %27, i32 0, i32 5
%29 = tail call i32 @fclose(i32* %23)
br label %30
30: ; preds = %15, %25
%31 = phi i32 [ %28, %25 ], [ 4, %15 ]
%32 = icmp eq i8* %21, null
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = tail call i32 @free(i8* %21)
br label %35
35: ; preds = %5, %33, %30
%36 = phi i32 [ %31, %33 ], [ %31, %30 ], [ 2, %5 ]
%37 = icmp eq i8* %12, null
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = tail call i32 @free(i8* %12)
br label %40
40: ; preds = %38, %35
%41 = tail call i32 @fclose(i32* %3)
br label %42
42: ; preds = %2, %40
%43 = phi i32 [ %36, %40 ], [ 1, %2 ]
ret i32 %43
}