#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
unsigned char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(unsigned char, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  FILE *in, *out;
  unsigned char dummyb;
  unsigned int counter;
  if (argc != 3) {
    printf("Syntax: fill16k.exe <bin file> <rom file>\n");
    return (1);
  }
  in = fopen(argv[1], "rb");
  if (!in) {
    printf("Error opening input file %s\n", argv[1]);
    return (1);
  }
  out = fopen(argv[2], "wb");
  if (!out) {
    printf("Error opening output file %s\n", argv[2]);
    return (1);
  }
  counter = 0;
  while (!feof(in)) {
    dummyb = fgetc(in);
    if (!feof(in)) {
      fputc(dummyb, out);
      counter++;
    }
  }
  while (counter < 16384) {
    fputc(0, out);
    counter++;
  }
  fclose(in);
  fclose(out);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     119a <main+0x3a>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c8 <main+0x168>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe82(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11df <main+0x7f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c8 <main+0x168>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe5d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1224 <main+0xc4>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c8 <main+0x168>
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1249 <main+0xe9>
jmp    1284 <main+0x124>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x21(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
jne    127f <main+0x11f>
mov    -0x21(%rbp),%al
mov    -0x20(%rbp),%rsi
movzbl %al,%edi
call   1060 <fputc@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    122b <main+0xcb>
jmp    1289 <main+0x129>
cmpl   $0x4000,-0x28(%rbp)
jae    12af <main+0x14f>
mov    -0x20(%rbp),%rsi
xor    %edi,%edi
call   1060 <fputc@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1289 <main+0x129>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x3,%edi
jne    1202 <main+0x92>
mov    %rsi,%r15
mov    0x8(%rsi),%rdi
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1215 <main+0xa5>
mov    %rax,%r12
mov    0x10(%r15),%rdi
lea    0xe75(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1222 <main+0xb2>
mov    %rax,%r14
mov    %r12,%rdi
call   1080 <feof@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    1243 <main+0xd3>
xor    %ebp,%ebp
jmp    11dc <main+0x6c>
xchg   %ax,%ax
mov    %r12,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    123b <main+0xcb>
mov    %r12,%rdi
call   1060 <fgetc@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    11d0 <main+0x60>
movzbl %bl,%edi
mov    %r14,%rsi
call   1070 <fputc@plt>
add    $0x1,%ebp
jmp    11d0 <main+0x60>
lea    0xe38(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    1270 <main+0x100>
mov    0x8(%r15),%rsi
lea    0xde3(%rip),%rdi
jmp    122d <main+0xbd>
mov    0x10(%r15),%rsi
lea    0xdf6(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    1270 <main+0x100>
cmp    $0x3fff,%ebp
ja     125e <main+0xee>
add    $0xffffc000,%ebp
nopl   0x0(%rax)
xor    %edi,%edi
mov    %r14,%rsi
call   1070 <fputc@plt>
inc    %ebp
jne    1250 <main+0xe0>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"Syntax: fill16k.exe <bin file> <rom file>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [29 x i8] c"Error opening input file %s\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [30 x i8] c"Error opening output file %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %71
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %6
%19 = load i32*, i32** %6
%20 = icmp ne i32* %19, null
br i1 %20, label %26, label %21
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 1, i32* %3
br label %71
26: ; preds = %14
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %30, i32** %7
%31 = load i32*, i32** %7
%32 = icmp ne i32* %31, null
br i1 %32, label %38, label %33
33: ; preds = %26
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %36)
store i32 1, i32* %3
br label %71
38: ; preds = %26
store i32 0, i32* %9
br label %39
39: ; preds = %56, %38
%40 = load i32*, i32** %6
%41 = call i32 @feof(i32* %40)
%42 = icmp ne i32 %41, 0
%43 = xor i1 %42, true
br i1 %43, label %44, label %57
44: ; preds = %39
%45 = load i32*, i32** %6
%46 = call zeroext i8 @fgetc(i32* %45)
store i8 %46, i8* %8
%47 = load i32*, i32** %6
%48 = call i32 @feof(i32* %47)
%49 = icmp ne i32 %48, 0
br i1 %49, label %56, label %50
50: ; preds = %44
%51 = load i8, i8* %8
%52 = load i32*, i32** %7
%53 = call i32 @fputc(i8 zeroext %51, i32* %52)
%54 = load i32, i32* %9
%55 = add i32 %54, 1
store i32 %55, i32* %9
br label %56
56: ; preds = %50, %44
br label %39
57: ; preds = %39
br label %58
58: ; preds = %61, %57
%59 = load i32, i32* %9
%60 = icmp ult i32 %59, 16384
br i1 %60, label %61, label %66
61: ; preds = %58
%62 = load i32*, i32** %7
%63 = call i32 @fputc(i8 zeroext 0, i32* %62)
%64 = load i32, i32* %9
%65 = add i32 %64, 1
store i32 %65, i32* %9
br label %58
66: ; preds = %58
%67 = load i32*, i32** %6
%68 = call i32 @fclose(i32* %67)
%69 = load i32*, i32** %7
%70 = call i32 @fclose(i32* %69)
store i32 0, i32* %3
br label %71
71: ; preds = %66, %33, %21, %12
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [29 x i8] c"Error opening input file %s\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [30 x i8] c"Error opening output file %s\0A\00"
@str = [42 x i8] c"Syntax: fill16k.exe <bin file> <rom file>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
br label %49
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i8*, i8** %7
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %12)
br label %49
14: ; preds = %6
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = tail call i32 @feof(i32* %9)
%21 = icmp eq i32 %20, 0
br i1 %21, label %29, label %27
22: ; preds = %14
%23 = load i8*, i8** %15
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %23)
br label %49
25: ; preds = %37
%26 = icmp ult i32 %38, 16384
br i1 %26, label %27, label %46
27: ; preds = %19, %25
%28 = phi i32 [ 0, %19 ], [ %38, %25 ]
br label %41
29: ; preds = %19, %37
%30 = phi i32 [ %38, %37 ], [ 0, %19 ]
%31 = tail call zeroext i8 @fgetc(i32* %9)
%32 = tail call i32 @feof(i32* %9)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = tail call i32 @fputc(i8 zeroext %31, i32* %17)
%36 = add i32 %30, 1
br label %37
37: ; preds = %34, %29
%38 = phi i32 [ %30, %29 ], [ %36, %34 ]
%39 = tail call i32 @feof(i32* %9)
%40 = icmp eq i32 %39, 0
br i1 %40, label %29, label %25
41: ; preds = %27, %41
%42 = phi i32 [ %44, %41 ], [ %28, %27 ]
%43 = tail call i32 @fputc(i8 zeroext 0, i32* %17)
%44 = add i32 %42, 1
%45 = icmp eq i32 %44, 16384
br i1 %45, label %46, label %41
46: ; preds = %41, %25
%47 = tail call i32 @fclose(i32* %9)
%48 = tail call i32 @fclose(i32* %17)
br label %49
49: ; preds = %46, %22, %11, %4
%50 = phi i32 [ 1, %4 ], [ 0, %46 ], [ 1, %22 ], [ 1, %11 ]
ret i32 %50
}