#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*/ error(char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  FILE *diskFile;
  FILE *bootFile;
  unsigned char sectorBuffer[512];
  if (argc != 3) {
    usage(argv[0]);
  }
  diskFile = fopen(argv[1], "r+b");
  if (diskFile == NULL) {
    error("cannot open disk file '%s'", argv[1]);
  }
  bootFile = fopen(argv[2], "rb");
  if (bootFile == NULL) {
    error("cannot open boot sector file '%s'", argv[2]);
  }
  if (fseek(bootFile, 0, SEEK_END) != 0) {
    error("cannot position to end of boot sector file");
  }
  if (ftell(bootFile) != 512) {
    error("'%s' is not a proper boot sector file (wrong length)", argv[2]);
  }
  if (fseek(bootFile, 0, SEEK_SET) != 0) {
    error("cannot position to start of boot sector file");
  }
  if (fread(sectorBuffer, 512, 1, bootFile) != 1) {
    error("cannot read data from boot sector file");
  }
  fclose(bootFile);
  if (sectorBuffer[512 - 2] != 0x55 || sectorBuffer[512 - 1] != 0xAA) {
    error("'%s' is not a proper boot sector file (no signature)", argv[2]);
  }
  if (fseek(diskFile, 0, SEEK_SET) != 0) {
    error("cannot position to start of disk file");
  }
  if (fwrite(sectorBuffer, 1, 512, diskFile) != 512) {
    error("cannot write boot sector to disk file");
  }
  fclose(diskFile);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11af <main+0x2f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe42(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e8 <main+0x68>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1080 <error@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe28(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1221 <main+0xa1>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1080 <error@plt>
mov    -0x20(%rbp),%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     124d <main+0xcd>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1080 <error@plt>
mov    -0x20(%rbp),%rdi
call   1050 <ftell@plt>
cmp    $0x200,%eax
je     1277 <main+0xf7>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1080 <error@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d56(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     12a3 <main+0x123>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1080 <error@plt>
lea    -0x220(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     12d4 <main+0x154>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1080 <error@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movzbl -0x22(%rbp),%eax
cmp    $0x55,%eax
jne    12f9 <main+0x179>
movzbl -0x21(%rbp),%eax
cmp    $0xaa,%eax
je     130f <main+0x18f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1080 <error@plt>
mov    -0x18(%rbp),%rdi
mov    0x2cbe(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     133b <main+0x1bb>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1080 <error@plt>
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   10a0 <fwrite@plt>
cmp    $0x200,%eax
je     136e <main+0x1ee>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1080 <error@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r15
cmp    $0x3,%edi
je     119e <main+0x1e>
mov    (%r15),%rdi
call   1070 <usage@plt>
mov    0x8(%r15),%rdi
lea    0xe57(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11c8 <main+0x48>
mov    0x8(%r15),%rsi
lea    0xe43(%rip),%rdi
xor    %eax,%eax
call   1080 <error@plt>
mov    0x10(%r15),%rdi
lea    0xe4c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f2 <main+0x72>
mov    0x10(%r15),%rsi
lea    0xe37(%rip),%rdi
xor    %eax,%eax
call   1080 <error@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
test   %rax,%rax
je     1218 <main+0x98>
lea    0xe33(%rip),%rdi
xor    %eax,%eax
call   1080 <error@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
cmp    $0x200,%eax
je     1239 <main+0xb9>
mov    0x10(%r15),%rsi
lea    0xe3d(%rip),%rdi
xor    %eax,%eax
call   1080 <error@plt>
mov    0x2d98(%rip),%r12
mov    (%r12),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
test   %rax,%rax
je     1261 <main+0xe1>
lea    0xe4a(%rip),%rdi
xor    %eax,%eax
call   1080 <error@plt>
mov    %rsp,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1289 <main+0x109>
lea    0xe4f(%rip),%rdi
xor    %eax,%eax
call   1080 <error@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmpb   $0x55,0x1fe(%rsp)
55
jne    12a5 <main+0x125>
cmpb   $0xaa,0x1ff(%rsp)
aa
je     12b7 <main+0x137>
mov    0x10(%r15),%rsi
lea    0xe48(%rip),%rdi
xor    %eax,%eax
call   1080 <error@plt>
mov    (%r12),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
test   %rax,%rax
je     12d8 <main+0x158>
lea    0xe5c(%rip),%rdi
xor    %eax,%eax
call   1080 <error@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r14,%rcx
call   10a0 <fwrite@plt>
cmp    $0x200,%eax
je     1302 <main+0x182>
lea    0xe58(%rip),%rdi
xor    %eax,%eax
call   1080 <error@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"r+b\00"
@.str.1 = [27 x i8] c"cannot open disk file '%s'\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [34 x i8] c"cannot open boot sector file '%s'\00"
@SEEK_END = global i32 0
@.str.4 = [43 x i8] c"cannot position to end of boot sector file\00"
@.str.5 = [53 x i8] c"'%s' is not a proper boot sector file (wrong length)\00"
@SEEK_SET = global i32 0
@.str.6 = [45 x i8] c"cannot position to start of boot sector file\00"
@.str.7 = [39 x i8] c"cannot read data from boot sector file\00"
@.str.8 = [53 x i8] c"'%s' is not a proper boot sector file (no signature)\00"
@.str.9 = [38 x i8] c"cannot position to start of disk file\00"
@.str.10 = [38 x i8] c"cannot write boot sector to disk file\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 [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @usage(i8* %14)
br label %16
16: ; preds = %11, %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %28
23: ; preds = %16
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %23, %16
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %32, i32** %7
%33 = load i32*, i32** %7
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %40
35: ; preds = %28
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %35, %28
%41 = load i32*, i32** %7
%42 = load i32, i32* @SEEK_END
%43 = call i64 @fseek(i32* %41, i32 0, i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
br label %47
47: ; preds = %45, %40
%48 = load i32*, i32** %7
%49 = call i32 @ftell(i32* %48)
%50 = icmp ne i32 %49, 512
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 2
%54 = load i8*, i8** %53
%55 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %51, %47
%57 = load i32*, i32** %7
%58 = load i32, i32* @SEEK_SET
%59 = call i64 @fseek(i32* %57, i32 0, i32 %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %56
%62 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
br label %63
63: ; preds = %61, %56
%64 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%65 = load i32*, i32** %7
%66 = call i32 @fread(i8* %64, i32 512, i32 1, i32* %65)
%67 = icmp ne i32 %66, 1
br i1 %67, label %68, label %70
68: ; preds = %63
%69 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
br label %70
70: ; preds = %68, %63
%71 = load i32*, i32** %7
%72 = call i32 @fclose(i32* %71)
%73 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 510
%74 = load i8, i8* %73
%75 = zext i8 %74 to i32
%76 = icmp ne i32 %75, 85
br i1 %76, label %82, label %77
77: ; preds = %70
%78 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 511
%79 = load i8, i8* %78
%80 = zext i8 %79 to i32
%81 = icmp ne i32 %80, 170
br i1 %81, label %82, label %87
82: ; preds = %77, %70
%83 = load i8**, i8*** %5
%84 = getelementptr inbounds i8*, i8** %83, i64 2
%85 = load i8*, i8** %84
%86 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.8, i64 0, i64 0), i8* %85)
br label %87
87: ; preds = %82, %77
%88 = load i32*, i32** %6
%89 = load i32, i32* @SEEK_SET
%90 = call i64 @fseek(i32* %88, i32 0, i32 %89)
%91 = icmp ne i64 %90, 0
br i1 %91, label %92, label %94
92: ; preds = %87
%93 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.9, i64 0, i64 0))
br label %94
94: ; preds = %92, %87
%95 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%96 = load i32*, i32** %6
%97 = call i32 @fwrite(i8* %95, i32 1, i32 512, i32* %96)
%98 = icmp ne i32 %97, 512
br i1 %98, label %99, label %101
99: ; preds = %94
%100 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.10, i64 0, i64 0))
br label %101
101: ; preds = %99, %94
%102 = load i32*, i32** %6
%103 = call i32 @fclose(i32* %102)
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"r+b\00"
@.str.1 = [27 x i8] c"cannot open disk file '%s'\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [34 x i8] c"cannot open boot sector file '%s'\00"
@SEEK_END = global i32 0
@.str.4 = [43 x i8] c"cannot position to end of boot sector file\00"
@.str.5 = [53 x i8] c"'%s' is not a proper boot sector file (wrong length)\00"
@SEEK_SET = global i32 0
@.str.6 = [45 x i8] c"cannot position to start of boot sector file\00"
@.str.7 = [39 x i8] c"cannot read data from boot sector file\00"
@.str.8 = [53 x i8] c"'%s' is not a proper boot sector file (no signature)\00"
@.str.9 = [38 x i8] c"cannot position to start of disk file\00"
@.str.10 = [38 x i8] c"cannot write boot sector to disk file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @usage(i8* %7)
br label %9
9: ; preds = %6, %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i8*, i8** %10
%16 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %9
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i8*, i8** %18
%24 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %17
%26 = load i32, i32* @SEEK_END
%27 = tail call i64 @fseek(i32* %20, i32 0, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
%32 = tail call i32 @ftell(i32* %20)
%33 = icmp eq i32 %32, 512
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = load i8*, i8** %18
%36 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %34, %31
%38 = load i32, i32* @SEEK_SET
%39 = tail call i64 @fseek(i32* %20, i32 0, i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %37
%42 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = call i32 @fread(i8* %4, i32 512, i32 1, i32* %20)
%45 = icmp eq i32 %44, 1
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
br label %48
48: ; preds = %46, %43
%49 = tail call i32 @fclose(i32* %20)
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 510
%51 = load i8, i8* %50
%52 = icmp ne i8 %51, 85
%53 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 511
%54 = load i8, i8* %53
%55 = icmp ne i8 %54, -86
%56 = select i1 %52, i1 true, i1 %55
br i1 %56, label %57, label %60
57: ; preds = %48
%58 = load i8*, i8** %18
%59 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.8, i64 0, i64 0), i8* %58)
br label %60
60: ; preds = %48, %57
%61 = load i32, i32* @SEEK_SET
%62 = tail call i64 @fseek(i32* %12, i32 0, i32 %61)
%63 = icmp eq i64 %62, 0
br i1 %63, label %66, label %64
64: ; preds = %60
%65 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.9, i64 0, i64 0))
br label %66
66: ; preds = %64, %60
%67 = call i32 @fwrite(i8* %4, i32 1, i32 512, i32* %12)
%68 = icmp eq i32 %67, 512
br i1 %68, label %71, label %69
69: ; preds = %66
%70 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.10, i64 0, i64 0))
br label %71
71: ; preds = %69, %66
%72 = tail call i32 @fclose(i32* %12)
ret i32 0
}