#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  char *buf[512];
  FILE *img, *boot;
  if (argc < 3) {
    printf("Usage: bootwrite.exe floppyimage bootsect\n");
    return 3;
  }
  if (boot = fopen(argv[2], "rb")) {
    if (!fread(buf, 512, 1, boot)) {
      printf("'%s' ReadError!\n", argv[1]);
      return 2;
    }
    fclose(boot);
    if (img = fopen(argv[1], "r+b+")) {
      if (!fwrite(buf, 512, 1, img)) {
        printf("'%s' WriteError!\n", argv[2]);
        return 2;
      }
      fclose(img);
    } else {
      printf("'%s' WriteError!\n", argv[2]);
      return 1;
    }
  } else {
    printf("'%s' ReadError!\n", argv[1]);
    return 1;
  }
  printf("Boot sector is successfully written!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    118d <main+0x3d>
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    12e2 <main+0x192>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe8f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x1020(%rbp)
cmp    $0x0,%rax
je     12ab <main+0x15b>
lea    -0x1010(%rbp),%rdi
mov    -0x1020(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%eax
jne    11fa <main+0xaa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e2 <main+0x192>
mov    -0x1020(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe2a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
je     1284 <main+0x134>
lea    -0x1010(%rbp),%rdi
mov    -0x1018(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
cmp    $0x0,%eax
jne    1273 <main+0x123>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e2 <main+0x192>
mov    -0x1018(%rbp),%rdi
call   1040 <fclose@plt>
jmp    12a6 <main+0x156>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e2 <main+0x192>
jmp    12cd <main+0x17d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e2 <main+0x192>
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
cmp    $0x2,%edi
jg     1185 <main+0x25>
lea    0xeda(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%eax
jmp    1254 <main+0xf4>
mov    %rsi,%r14
mov    0x10(%rsi),%rdi
lea    0xe6d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     120a <main+0xaa>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %eax,%eax
je     1217 <main+0xb7>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x8(%r14),%rdi
lea    0xe48(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1224 <main+0xc4>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
test   %eax,%eax
je     123d <main+0xdd>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe2a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1254 <main+0xf4>
mov    0x8(%r14),%rsi
lea    0xdee(%rip),%rdi
jmp    122f <main+0xcf>
mov    0x8(%r14),%rsi
lea    0xde1(%rip),%rdi
jmp    1248 <main+0xe8>
mov    0x10(%r14),%rsi
lea    0xdea(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%eax
jmp    1254 <main+0xf4>
mov    0x10(%r14),%rsi
lea    0xdd1(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x2,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [43 x i8] c"Usage: bootwrite.exe floppyimage bootsect\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [17 x i8] c"'%s' ReadError!\0A\00"
@.str.3 = [5 x i8] c"r+b+\00"
@.str.4 = [18 x i8] c"'%s' WriteError!\0A\00"
@.str.5 = [38 x i8] c"Boot sector is successfully written!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [512 x i8*]
%7 = alloca i32*
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i32 3, i32* %3
br label %63
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 2
%16 = load i8*, i8** %15
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %8
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %56
19: ; preds = %13
%20 = getelementptr inbounds [512 x i8*], [512 x i8*]* %6, i64 0, i64 0
%21 = load i32*, i32** %8
%22 = call i32 @fread(i8** %20, i32 512, i32 1, i32* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %19
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %27)
store i32 2, i32* %3
br label %63
29: ; preds = %19
%30 = load i32*, i32** %8
%31 = call i32 @fclose(i32* %30)
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32* %35, i32** %7
%36 = icmp ne i32* %35, null
br i1 %36, label %37, label %50
37: ; preds = %29
%38 = getelementptr inbounds [512 x i8*], [512 x i8*]* %6, i64 0, i64 0
%39 = load i32*, i32** %7
%40 = call i32 @fwrite(i8** %38, i32 512, i32 1, i32* %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %37
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 2
%45 = load i8*, i8** %44
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %45)
store i32 2, i32* %3
br label %63
47: ; preds = %37
%48 = load i32*, i32** %7
%49 = call i32 @fclose(i32* %48)
br label %55
50: ; preds = %29
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 2
%53 = load i8*, i8** %52
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %53)
store i32 1, i32* %3
br label %63
55: ; preds = %47
br label %61
56: ; preds = %13
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 1
%59 = load i8*, i8** %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %59)
store i32 1, i32* %3
br label %63
61: ; preds = %55
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
br label %63
63: ; preds = %61, %56, %50, %42, %24, %11
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [17 x i8] c"'%s' ReadError!\0A\00"
@.str.3 = [5 x i8] c"r+b+\00"
@.str.4 = [18 x i8] c"'%s' WriteError!\0A\00"
@str = [37 x i8] c"Boot sector is successfully written!\00"
@str.6 = [42 x i8] c"Usage: bootwrite.exe floppyimage bootsect\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8*]
%4 = bitcast [512 x i8*]* %3 to i8*
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.6, i64 0, i64 0))
br label %43
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %39, label %13
13: ; preds = %8
%14 = getelementptr inbounds [512 x i8*], [512 x i8*]* %3, i64 0, i64 0
%15 = call i32 @fread(i8** %14, i32 512, i32 1, i32* %11)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %43
21: ; preds = %13
%22 = tail call i32 @fclose(i32* %11)
%23 = getelementptr inbounds i8*, i8** %1, i64 1
%24 = load i8*, i8** %23
%25 = tail call i32* @fopen(i8* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %36, label %27
27: ; preds = %21
%28 = call i32 @fwrite(i8** %14, i32 512, i32 1, i32* %25)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i8*, i8** %9
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %31)
br label %43
33: ; preds = %27
%34 = tail call i32 @fclose(i32* %25)
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
br label %43
36: ; preds = %21
%37 = load i8*, i8** %9
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %37)
br label %43
39: ; preds = %8
%40 = getelementptr inbounds i8*, i8** %1, i64 1
%41 = load i8*, i8** %40
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %33, %39, %36, %30, %17, %6
%44 = phi i32 [ 3, %6 ], [ 0, %33 ], [ 2, %30 ], [ 1, %36 ], [ 2, %17 ], [ 1, %39 ]
ret i32 %44
}