#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
void copy_bootsect(char const *filename, char *buf) {
  int c, fd;
  if ((fd = open(filename, O_RDONLY, 0)) < 0)
    die("Unable to open 'bootsect'");
  for (c = 0; c < 1024; c++)
    buf[c] = 0;
  c = read(fd, buf, 1024);
  close(fd);
  fprintf(stderr, "'bootsect' is %d bytes.\n", c);
  if (c != 512)
    die("'bootsect' must be exactly 512 bytes");
  if ((*(unsigned short *)(buf + 510)) != 0xAA55)
    die("'bootsect' hasn't got boot flag (0xAA55)");
  c = write(1, buf, 512);
  if (c != 512)
    die("Write call failed");
}


x86 assembly (clang, -O0):
<copy_bootsect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    119c <copy_bootsect+0x3c>
lea    0xe69(%rip),%rdi
call   1080 <die@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x400,-0x14(%rbp)
jge    11ca <copy_bootsect+0x6a>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11a3 <copy_bootsect+0x43>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe21(%rip),%rsi
call   1060 <fprintf@plt>
cmpl   $0x200,-0x14(%rbp)
je     1217 <copy_bootsect+0xb7>
lea    0xe21(%rip),%rdi
call   1080 <die@plt>
mov    -0x10(%rbp),%rax
movzwl 0x1fe(%rax),%eax
cmp    $0xaa55,%eax
je     1239 <copy_bootsect+0xd9>
lea    0xe24(%rip),%rdi
call   1080 <die@plt>
mov    -0x10(%rbp),%rsi
mov    $0x1,%edi
mov    $0x200,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x200,-0x14(%rbp)
je     1268 <copy_bootsect+0x108>
lea    0xe1e(%rip),%rdi
call   1080 <die@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_bootsect>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1199 <copy_bootsect+0x29>
lea    0xe6c(%rip),%rdi
call   1090 <die@plt>
mov    $0x400,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
mov    %ebx,%edx
call   1070 <fprintf@plt>
cmp    $0x200,%ebx
je     11eb <copy_bootsect+0x7b>
lea    0xe4d(%rip),%rdi
call   1090 <die@plt>
movzwl 0x1fe(%r14),%eax
cmp    $0xaa55,%eax
je     1206 <copy_bootsect+0x96>
lea    0xe57(%rip),%rdi
call   1090 <die@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    $0x200,%edx
call   1030 <write@plt>
cmp    $0x200,%eax
jne    1224 <copy_bootsect+0xb4>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe56(%rip),%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1090 <die@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [26 x i8] c"Unable to open 'bootsect'\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"'bootsect' is %d bytes.\0A\00"
@.str.2 = [37 x i8] c"'bootsect' must be exactly 512 bytes\00"
@.str.3 = [41 x i8] c"'bootsect' hasn't got boot flag (0xAA55)\00"
@.str.4 = [18 x i8] c"Write call failed\00"
define void @copy_bootsect(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8, i32 0)
store i32 %9, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
store i32 0, i32* %5
br label %14
14: ; preds = %22, %13
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 1024
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = load i8*, i8** %4
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %17
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
br label %14
25: ; preds = %14
%26 = load i32, i32* %6
%27 = load i8*, i8** %4
%28 = call i32 @read(i32 %26, i8* %27, i32 1024)
store i32 %28, i32* %5
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* @stderr
%32 = load i32, i32* %5
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %5
%35 = icmp ne i32 %34, 512
br i1 %35, label %36, label %38
36: ; preds = %25
%37 = call i32 @die(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %25
%39 = load i8*, i8** %4
%40 = getelementptr inbounds i8, i8* %39, i64 510
%41 = bitcast i8* %40 to i16*
%42 = load i16, i16* %41
%43 = zext i16 %42 to i32
%44 = icmp ne i32 %43, 43605
br i1 %44, label %45, label %47
45: ; preds = %38
%46 = call i32 @die(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %38
%48 = load i8*, i8** %4
%49 = call i32 @write(i32 1, i8* %48, i32 512)
store i32 %49, i32* %5
%50 = load i32, i32* %5
%51 = icmp ne i32 %50, 512
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %52, %47
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [26 x i8] c"Unable to open 'bootsect'\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"'bootsect' is %d bytes.\0A\00"
@.str.2 = [37 x i8] c"'bootsect' must be exactly 512 bytes\00"
@.str.3 = [41 x i8] c"'bootsect' hasn't got boot flag (0xAA55)\00"
@.str.4 = [18 x i8] c"Write call failed\00"
define void @copy_bootsect(i8* nocapture %0, i8* nocapture %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = tail call i32 @read(i32 %4, i8* %1, i32 1024)
%10 = tail call i32 @close(i32 %4)
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%13 = icmp eq i32 %9, 512
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = tail call i32 @die(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %8
%17 = getelementptr inbounds i8, i8* %1, i64 510
%18 = bitcast i8* %17 to i16*
%19 = load i16, i16* %18
%20 = icmp eq i16 %19, -21931
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = tail call i32 @die(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = tail call i32 @write(i32 1, i8* %1, i32 512)
%25 = icmp eq i32 %24, 512
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
ret void
}