#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 */
struct stat {
  int st_size;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  FILE *img = NULL;
  FILE *out = NULL;
  int size = 0;
  int ch = 0;
  int osize = 1474560;
  struct stat stbuf = {0};
  if (argc != 3) {
    fprintf(stderr, "syntax:: mkboot image outimage\n");
    return 1;
  }
  img = fopen(argv[1], "r");
  if (!img) {
    fprintf(stderr, "error opening image file [%s]\n", argv[1]);
    return 2;
  }
  out = fopen(argv[2], "w");
  if (!out) {
    fprintf(stderr, "error opening output file [%s]\n", argv[2]);
    return 2;
  }
  stat(argv[1], &stbuf);
  size = stbuf.st_size;
  while (size--) {
    ch = fgetc(img);
    fputc(ch, out);
  }
  size = osize - stbuf.st_size;
  ch = 0x0;
  while (size--) {
    fputc(ch, out);
  }
  fclose(img);
  fclose(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x168000,-0x2c(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
cmpl   $0x3,-0x8(%rbp)
je     11e8 <main+0x78>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131a <main+0x1aa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe29(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1236 <main+0xc6>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    131a <main+0x1aa>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdfc(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1284 <main+0x114>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xdd2(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    131a <main+0x1aa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <stat@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x24(%rbp)
cmp    $0x0,%eax
je     12cc <main+0x15c>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1060 <fputc@plt>
jmp    129b <main+0x12b>
mov    -0x2c(%rbp),%eax
sub    -0x38(%rbp),%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x24(%rbp)
cmp    $0x0,%eax
je     1301 <main+0x191>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1060 <fputc@plt>
jmp    12dc <main+0x16c>
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    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
cmp    $0x3,%edi
jne    1232 <main+0xd2>
mov    %rsi,%r15
mov    0x8(%rsi),%rdi
lea    0xe94(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1250 <main+0xf0>
mov    %rax,%r14
mov    0x10(%r15),%rdi
lea    0xe99(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1266 <main+0x106>
mov    %rax,%r12
mov    0x8(%r15),%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
mov    0x8(%rsp),%ebx
test   %ebx,%ebx
je     11ff <main+0x9f>
mov    %ebx,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    %eax,%edi
mov    %r12,%rsi
call   1050 <fputc@plt>
add    $0xffffffff,%ebp
jne    11e0 <main+0x80>
cmp    $0x168000,%ebx
je     121e <main+0xbe>
add    $0xffe98000,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %edi,%edi
mov    %r12,%rsi
call   1050 <fputc@plt>
inc    %ebx
jne    1210 <main+0xb0>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1286 <main+0x126>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    1286 <main+0x126>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r15),%rdx
lea    0xdbe(%rip),%rsi
jmp    127a <main+0x11a>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
mov    0x10(%r15),%rdx
lea    0xdc9(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@stderr = global i32 0
@.str = [32 x i8] c"syntax:: mkboot image outimage\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"error opening image file [%s]\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [32 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 i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %6
store i32* null, i32** %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 1474560, i32* %10
%12 = bitcast %struct.stat* %11 to i8*
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %79
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp ne i32* %23, null
br i1 %24, label %31, label %25
25: ; preds = %18
%26 = load i32, i32* @stderr
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %29)
store i32 2, i32* %3
br label %79
31: ; preds = %18
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %35, i32** %7
%36 = load i32*, i32** %7
%37 = icmp ne i32* %36, null
br i1 %37, label %44, label %38
38: ; preds = %31
%39 = load i32, i32* @stderr
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %42)
store i32 2, i32* %3
br label %79
44: ; preds = %31
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = call i32 @stat(i8* %47, %struct.stat* %11)
%49 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%50 = load i32, i32* %49
store i32 %50, i32* %8
br label %51
51: ; preds = %55, %44
%52 = load i32, i32* %8
%53 = add nsw i32 %52, -1
store i32 %53, i32* %8
%54 = icmp ne i32 %52, 0
br i1 %54, label %55, label %61
55: ; preds = %51
%56 = load i32*, i32** %6
%57 = call i32 @fgetc(i32* %56)
store i32 %57, i32* %9
%58 = load i32, i32* %9
%59 = load i32*, i32** %7
%60 = call i32 @fputc(i32 %58, i32* %59)
br label %51
61: ; preds = %51
%62 = load i32, i32* %10
%63 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%64 = load i32, i32* %63
%65 = sub nsw i32 %62, %64
store i32 %65, i32* %8
store i32 0, i32* %9
br label %66
66: ; preds = %70, %61
%67 = load i32, i32* %8
%68 = add nsw i32 %67, -1
store i32 %68, i32* %8
%69 = icmp ne i32 %67, 0
br i1 %69, label %70, label %74
70: ; preds = %66
%71 = load i32, i32* %9
%72 = load i32*, i32** %7
%73 = call i32 @fputc(i32 %71, i32* %72)
br label %66
74: ; preds = %66
%75 = load i32*, i32** %6
%76 = call i32 @fclose(i32* %75)
%77 = load i32*, i32** %7
%78 = call i32 @fclose(i32* %77)
store i32 0, i32* %3
br label %79
79: ; preds = %74, %38, %25, %15
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@stderr = global i32 0
@.str = [32 x i8] c"syntax:: mkboot image outimage\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"error opening image file [%s]\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [32 x i8] c"error opening output file [%s]\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.stat*
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%6 = icmp eq i32 %0, 3
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %52
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %11
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %17)
br label %52
19: ; preds = %10
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = tail call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %20
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %26)
br label %52
28: ; preds = %19
%29 = load i8*, i8** %11
%30 = call i32 @stat(i8* %29, %struct.stat* %4)
%31 = bitcast i64* %3 to i32*
%32 = load i32, i32* %31
%33 = icmp eq i32 %32, 0
br i1 %33, label %42, label %34
34: ; preds = %28, %34
%35 = phi i32 [ %36, %34 ], [ %32, %28 ]
%36 = add nsw i32 %35, -1
%37 = tail call i32 @fgetc(i32* %13)
%38 = tail call i32 @fputc(i32 %37, i32* %22)
%39 = icmp eq i32 %36, 0
br i1 %39, label %40, label %34
40: ; preds = %34
%41 = icmp eq i32 %32, 1474560
br i1 %41, label %49, label %42
42: ; preds = %28, %40
%43 = sub nsw i32 1474560, %32
br label %44
44: ; preds = %42, %44
%45 = phi i32 [ %46, %44 ], [ %43, %42 ]
%46 = add nsw i32 %45, -1
%47 = tail call i32 @fputc(i32 0, i32* %22)
%48 = icmp eq i32 %46, 0
br i1 %48, label %49, label %44
49: ; preds = %44, %40
%50 = tail call i32 @fclose(i32* %13)
%51 = tail call i32 @fclose(i32* %22)
br label %52
52: ; preds = %49, %24, %15, %7
%53 = phi i32 [ 1, %7 ], [ 0, %49 ], [ 2, %24 ], [ 2, %15 ]
ret i32 %53
}