#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 */
long atol(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc < 4) {
    fprintf(stderr, "Usage: %s <input file> <output file> <offset>\n", argv[0]);
    return 1;
  }
  long offset = atol(argv[3]);
  FILE *fin = fopen(argv[1], "rb");
  if (fin == NULL) {
    fprintf(stderr, "ERROR: Cannot open input file %s\n", argv[1]);
    return 1;
  }
  char buf[32769];
  memset(buf, 0, 32769);
  int r = fread(buf, 1, 32768, fin);
  if (r < 32768) {
    fprintf(stderr, "ERROR: ROM is smaller than 32kB\n");
    return 1;
  }
  fclose(fin);
  FILE *fout = fopen(argv[2], "wb");
  if (fout == NULL) {
    fprintf(stderr, "ERROR: Cannot create output file %s\n", argv[2]);
    return 1;
  }
  if (fwrite(buf + offset, 1, 8192, fout) < 8192) {
    fclose(fout);
    fprintf(stderr, "ERROR: Failed to write the entire 8kB ROM\n");
    return 1;
  }
  fclose(fout);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11bd <main+0x4d>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134d <main+0x1dd>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1070 <atol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe52(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121c <main+0xac>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134d <main+0x1dd>
lea    -0x8030(%rbp),%rdi
xor    %esi,%esi
mov    $0x8001,%edx
call   1050 <memset@plt>
lea    -0x8030(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8000,%edx
call   1030 <fread@plt>
mov    %eax,-0x8034(%rbp)
cmpl   $0x8000,-0x8034(%rbp)
jge    1282 <main+0x112>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134d <main+0x1dd>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xddb(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8040(%rbp)
cmpq   $0x0,-0x8040(%rbp)
jne    12df <main+0x16f>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134d <main+0x1dd>
lea    -0x8030(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x8040(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2000,%edx
call   1090 <fwrite@plt>
cmp    $0x2000,%eax
jge    133a <main+0x1ca>
mov    -0x8040(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134d <main+0x1dd>
mov    -0x8040(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x8040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x8010,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jg     119f <main+0x2f>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe66(%rip),%rsi
jmp    129f <main+0x12f>
mov    0x18(%rbx),%rdi
call   1070 <atol@plt>
mov    %rax,%r14
mov    0x8(%rbx),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1212 <main+0xa2>
mov    %rax,%r15
mov    %rsp,%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8001,%edx
call   1050 <memset@plt>
mov    $0x1,%ebp
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x8000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    $0x7fff,%eax
jg     1228 <main+0xb8>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12ab <main+0x13b>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe0c(%rip),%rsi
jmp    129f <main+0x12f>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0x10(%rbx),%rdi
lea    0xe3a(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     128b <main+0x11b>
mov    %rax,%rbp
lea    (%rsp,%r14,1),%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
cmp    $0x1fff,%ebx
jg     12ab <main+0x13b>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12a6 <main+0x136>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rbx),%rdx
lea    0xdd9(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8010,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [47 x i8] c"Usage: %s <input file> <output file> <offset>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [34 x i8] c"ERROR: Cannot open input file %s\0A\00"
@.str.3 = [33 x i8] c"ERROR: ROM is smaller than 32kB\0A\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [37 x i8] c"ERROR: Cannot create output file %s\0A\00"
@.str.6 = [43 x i8] c"ERROR: Failed to write the entire 8kB ROM\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32*
%8 = alloca [32769 x i8]
%9 = alloca i32
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 4
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %77
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 3
%22 = load i8*, i8** %21
%23 = call i64 @atol(i8* %22)
store i64 %23, i64* %6
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %7
%28 = load i32*, i32** %7
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %36
30: ; preds = %19
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %34)
store i32 1, i32* %3
br label %77
36: ; preds = %19
%37 = getelementptr inbounds [32769 x i8], [32769 x i8]* %8, i64 0, i64 0
%38 = call i32 @memset(i8* %37, i32 0, i32 32769)
%39 = getelementptr inbounds [32769 x i8], [32769 x i8]* %8, i64 0, i64 0
%40 = load i32*, i32** %7
%41 = call i32 @fread(i8* %39, i32 1, i32 32768, i32* %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp slt i32 %42, 32768
br i1 %43, label %44, label %47
44: ; preds = %36
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %77
47: ; preds = %36
%48 = load i32*, i32** %7
%49 = call i32 @fclose(i32* %48)
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 2
%52 = load i8*, i8** %51
%53 = call i32* @fopen(i8* %52, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %53, i32** %10
%54 = load i32*, i32** %10
%55 = icmp eq i32* %54, null
br i1 %55, label %56, label %62
56: ; preds = %47
%57 = load i32, i32* @stderr
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 2
%60 = load i8*, i8** %59
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %60)
store i32 1, i32* %3
br label %77
62: ; preds = %47
%63 = getelementptr inbounds [32769 x i8], [32769 x i8]* %8, i64 0, i64 0
%64 = load i64, i64* %6
%65 = getelementptr inbounds i8, i8* %63, i64 %64
%66 = load i32*, i32** %10
%67 = call i32 @fwrite(i8* %65, i32 1, i32 8192, i32* %66)
%68 = icmp slt i32 %67, 8192
br i1 %68, label %69, label %74
69: ; preds = %62
%70 = load i32*, i32** %10
%71 = call i32 @fclose(i32* %70)
%72 = load i32, i32* @stderr
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %77
74: ; preds = %62
%75 = load i32*, i32** %10
%76 = call i32 @fclose(i32* %75)
store i32 0, i32* %3
br label %77
77: ; preds = %74, %69, %56, %44, %30, %13
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [47 x i8] c"Usage: %s <input file> <output file> <offset>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [34 x i8] c"ERROR: Cannot open input file %s\0A\00"
@.str.3 = [33 x i8] c"ERROR: ROM is smaller than 32kB\0A\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [37 x i8] c"ERROR: Cannot create output file %s\0A\00"
@.str.6 = [43 x i8] c"ERROR: Failed to write the entire 8kB ROM\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [32769 x i8]
%4 = icmp slt i32 %0, 4
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %49
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 3
%11 = load i8*, i8** %10
%12 = tail call i64 @atol(i8* %11)
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %9
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %13
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %49
21: ; preds = %9
%22 = getelementptr inbounds [32769 x i8], [32769 x i8]* %3, i64 0, i64 0
%23 = call i32 @memset(i8* %22, i32 0, i32 32769)
%24 = call i32 @fread(i8* %22, i32 1, i32 32768, i32* %15)
%25 = icmp slt i32 %24, 32768
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %47
29: ; preds = %21
%30 = call i32 @fclose(i32* %15)
%31 = getelementptr inbounds i8*, i8** %1, i64 2
%32 = load i8*, i8** %31
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %39
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %31
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %37)
br label %47
39: ; preds = %29
%40 = getelementptr inbounds [32769 x i8], [32769 x i8]* %3, i64 0, i64 %12
%41 = call i32 @fwrite(i8* %40, i32 1, i32 8192, i32* %33)
%42 = icmp slt i32 %41, 8192
%43 = call i32 @fclose(i32* %33)
br i1 %42, label %44, label %47
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0))
br label %47
47: ; preds = %39, %35, %44, %26
%48 = phi i32 [ 1, %26 ], [ 1, %35 ], [ 1, %44 ], [ 0, %39 ]
br label %49
49: ; preds = %17, %47, %5
%50 = phi i32 [ 1, %5 ], [ 1, %17 ], [ %48, %47 ]
ret i32 %50
}