#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, unsigned int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, unsigned int, size_t, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int lzhs_pad_file(const char *filename, const char *outfilename) {
  int input_filesize;
  size_t n;
  char *ptr;
  FILE *infile, *outfile;
  infile = fopen(filename, "rb");
  if (infile) {
    outfile = fopen(outfilename, "wb");
    if (outfile) {
      fseek(infile, 0, SEEK_END);
      size_t filesize = ftell(infile);
      rewind(infile);
      ptr = malloc(sizeof(char) * filesize);
      int extrabytes = 0;
      for (input_filesize = 0;; input_filesize += n) {
        n = fread(ptr, 1u, 0x200u, infile);
        if (n <= 0)
          break;
        if (n % 16 != 0) {
          unsigned int x = (n / 8) * 8;
          if (x < n)
            x += 8;
          x = x - n;
          extrabytes += x;
        }
        fwrite(ptr, 1u, n, outfile);
      }
      printf("We need to fill extra %d bytes\n", extrabytes);
      int i;
      for (i = 1; i <= extrabytes; i++)
        putc(0xff, outfile);
      fclose(infile);
      fclose(outfile);
      return 0;
    } else {
      printf("Open file %s failed.\n", outfilename);
      return 1;
    }
  } else {
    printf("open file %s fail \n", filename);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<lzhs_pad_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1353 <lzhs_pad_file+0x1b3>
mov    -0x18(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1335 <lzhs_pad_file+0x195>
mov    -0x38(%rbp),%rdi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x38(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x48(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x4c(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
ja     1264 <lzhs_pad_file+0xc4>
jmp    12d7 <lzhs_pad_file+0x137>
mov    -0x28(%rbp),%rax
and    $0xf,%rax
cmp    $0x0,%rax
je     12ae <lzhs_pad_file+0x10e>
mov    -0x28(%rbp),%rax
shr    $0x3,%rax
shl    $0x3,%rax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x28(%rbp),%rax
jae    129b <lzhs_pad_file+0xfb>
mov    -0x50(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
sub    -0x28(%rbp),%rax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
add    -0x4c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    -0x28(%rbp),%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
jmp    1239 <lzhs_pad_file+0x99>
mov    -0x4c(%rbp),%esi
lea    0xd25(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jg     1317 <lzhs_pad_file+0x177>
mov    -0x40(%rbp),%rsi
mov    $0xff,%edi
call   1070 <putc@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    12ef <lzhs_pad_file+0x14f>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    136c <lzhs_pad_file+0x1cc>
mov    -0x18(%rbp),%rsi
lea    0xce6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136c <lzhs_pad_file+0x1cc>
mov    -0x10(%rbp),%rsi
lea    0xcde(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lzhs_pad_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xe4c(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1295 <lzhs_pad_file+0xf5>
mov    %rax,%r14
lea    0xe37(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12e6 <lzhs_pad_file+0x146>
mov    %rax,%r15
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r14,%rdi
call   1080 <ftell@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1060 <rewind@plt>
mov    %ebx,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    127d <lzhs_pad_file+0xdd>
lea    0xdd3(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12d2 <lzhs_pad_file+0x132>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r15,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     12a1 <lzhs_pad_file+0x101>
test   $0xf,%al
je     1250 <lzhs_pad_file+0xb0>
sub    %eax,%ebp
mov    %eax,%ecx
and    $0xfffffff8,%ecx
lea    0x8(%rcx),%edx
cmp    %rcx,%rax
cmovbe %ecx,%edx
add    %edx,%ebp
jmp    1250 <lzhs_pad_file+0xb0>
lea    0xda0(%rip),%rdi
mov    %rbp,%rsi
jmp    12f0 <lzhs_pad_file+0x150>
lea    0xd5e(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebp,%ebp
jle    12d2 <lzhs_pad_file+0x132>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0xff,%edi
mov    %r15,%rsi
call   1070 <putc@plt>
add    $0xffffffff,%ebp
jne    12c0 <lzhs_pad_file+0x120>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    12fc <lzhs_pad_file+0x15c>
lea    0xd39(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
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"
@SEEK_END = global i32 0
@.str.2 = [32 x i8] c"We need to fill extra %d bytes\0A\00"
@.str.3 = [22 x i8] c"Open file %s failed.\0A\00"
@.str.4 = [20 x i8] c"open file %s fail \0A\00"
define i32 @lzhs_pad_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%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** %9
%17 = load i32*, i32** %9
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %100
19: ; preds = %2
%20 = load i8*, i8** %5
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %10
%22 = load i32*, i32** %10
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %97
24: ; preds = %19
%25 = load i32*, i32** %9
%26 = load i32, i32* @SEEK_END
%27 = call i32 @fseek(i32* %25, i32 0, i32 %26)
%28 = load i32*, i32** %9
%29 = call i64 @ftell(i32* %28)
store i64 %29, i64* %11
%30 = load i32*, i32** %9
%31 = call i32 @rewind(i32* %30)
%32 = load i64, i64* %11
%33 = mul i64 1, %32
%34 = trunc i64 %33 to i32
%35 = call i8* @malloc(i32 %34)
store i8* %35, i8** %8
store i32 0, i32* %12
store i32 0, i32* %6
br label %36
36: ; preds = %73, %24
%37 = load i8*, i8** %8
%38 = load i32*, i32** %9
%39 = call i64 @fread(i8* %37, i32 1, i32 512, i32* %38)
store i64 %39, i64* %7
%40 = load i64, i64* %7
%41 = icmp ule i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
br label %79
43: ; preds = %36
%44 = load i64, i64* %7
%45 = urem i64 %44, 16
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %68
47: ; preds = %43
%48 = load i64, i64* %7
%49 = udiv i64 %48, 8
%50 = mul i64 %49, 8
%51 = trunc i64 %50 to i32
store i32 %51, i32* %13
%52 = load i32, i32* %13
%53 = zext i32 %52 to i64
%54 = load i64, i64* %7
%55 = icmp ult i64 %53, %54
br i1 %55, label %56, label %59
56: ; preds = %47
%57 = load i32, i32* %13
%58 = add i32 %57, 8
store i32 %58, i32* %13
br label %59
59: ; preds = %56, %47
%60 = load i32, i32* %13
%61 = zext i32 %60 to i64
%62 = load i64, i64* %7
%63 = sub i64 %61, %62
%64 = trunc i64 %63 to i32
store i32 %64, i32* %13
%65 = load i32, i32* %13
%66 = load i32, i32* %12
%67 = add i32 %66, %65
store i32 %67, i32* %12
br label %68
68: ; preds = %59, %43
%69 = load i8*, i8** %8
%70 = load i64, i64* %7
%71 = load i32*, i32** %10
%72 = call i32 @fwrite(i8* %69, i32 1, i64 %70, i32* %71)
br label %73
73: ; preds = %68
%74 = load i64, i64* %7
%75 = load i32, i32* %6
%76 = sext i32 %75 to i64
%77 = add i64 %76, %74
%78 = trunc i64 %77 to i32
store i32 %78, i32* %6
br label %36
79: ; preds = %42
%80 = load i32, i32* %12
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %80)
store i32 1, i32* %14
br label %82
82: ; preds = %89, %79
%83 = load i32, i32* %14
%84 = load i32, i32* %12
%85 = icmp sle i32 %83, %84
br i1 %85, label %86, label %92
86: ; preds = %82
%87 = load i32*, i32** %10
%88 = call i32 @putc(i32 255, i32* %87)
br label %89
89: ; preds = %86
%90 = load i32, i32* %14
%91 = add nsw i32 %90, 1
store i32 %91, i32* %14
br label %82
92: ; preds = %82
%93 = load i32*, i32** %9
%94 = call i32 @fclose(i32* %93)
%95 = load i32*, i32** %10
%96 = call i32 @fclose(i32* %95)
store i32 0, i32* %3
br label %103
97: ; preds = %19
%98 = load i8*, i8** %5
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %98)
store i32 1, i32* %3
br label %103
100: ; preds = %2
%101 = load i8*, i8** %4
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %101)
store i32 1, i32* %3
br label %103
103: ; preds = %100, %97, %92
%104 = load i32, i32* %3
ret i32 %104
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
@SEEK_END = global i32 0
@.str.2 = [32 x i8] c"We need to fill extra %d bytes\0A\00"
@.str.3 = [22 x i8] c"Open file %s failed.\0A\00"
@.str.4 = [20 x i8] c"open file %s fail \0A\00"
define i32 @lzhs_pad_file(i8* %0, i8* %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 %51, label %5
5: ; preds = %2
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %49, label %8
8: ; preds = %5
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %3, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %3)
%12 = tail call i32 @rewind(i32* %3)
%13 = trunc i64 %11 to i32
%14 = tail call i8* @malloc(i32 %13)
%15 = tail call i64 @fread(i8* %14, i32 1, i32 512, i32* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %8
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 0)
br label %46
19: ; preds = %8, %33
%20 = phi i64 [ %36, %33 ], [ %15, %8 ]
%21 = phi i32 [ %34, %33 ], [ 0, %8 ]
%22 = and i64 %20, 15
%23 = icmp eq i64 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %19
%25 = trunc i64 %20 to i32
%26 = and i32 %25, -8
%27 = zext i32 %26 to i64
%28 = icmp ugt i64 %20, %27
%29 = add i32 %26, 8
%30 = select i1 %28, i32 %29, i32 %26
%31 = sub i32 %21, %25
%32 = add i32 %31, %30
br label %33
33: ; preds = %24, %19
%34 = phi i32 [ %32, %24 ], [ %21, %19 ]
%35 = tail call i32 @fwrite(i8* %14, i32 1, i64 %20, i32* %6)
%36 = tail call i64 @fread(i8* %14, i32 1, i32 512, i32* %3)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %19
38: ; preds = %33
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%40 = icmp slt i32 %34, 1
br i1 %40, label %46, label %41
41: ; preds = %38, %41
%42 = phi i32 [ %44, %41 ], [ 1, %38 ]
%43 = tail call i32 @putc(i32 255, i32* %6)
%44 = add nuw i32 %42, 1
%45 = icmp eq i32 %42, %34
br i1 %45, label %46, label %41
46: ; preds = %41, %17, %38
%47 = tail call i32 @fclose(i32* %3)
%48 = tail call i32 @fclose(i32* %6)
br label %53
49: ; preds = %5
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %53
51: ; preds = %2
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %53
53: ; preds = %51, %49, %46
%54 = phi i32 [ 0, %46 ], [ 1, %49 ], [ 1, %51 ]
ret i32 %54
}