#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*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned char *malloc(int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, unsigned char *, int);
int /*<<< orphan*/ write(int, unsigned char *, int);
int main(int argc, char *argv[]) {
  int dest_fd;
  int boot_fd;
  int i;
  unsigned char *dest_buf;
  unsigned char *boot_buf;
  if (argc < 3) {
    printf("Usage : %s destination_device bootsector_file\n", argv[0]);
    exit(0);
  } else {
    dest_fd = open(argv[1], O_RDWR, 0640);
    if (dest_fd < 0) {
      perror("open :");
      exit(-1);
    }
    boot_fd = open(argv[2], O_RDONLY, 0640);
    if (boot_fd < 0) {
      perror("open :");
      exit(-1);
    }
    dest_buf = malloc(512 * sizeof(*dest_buf));
    boot_buf = malloc(512 * sizeof(*boot_buf));
    read(dest_fd, dest_buf, 512);
    printf("dest: \n");
    for (i = 0; i < 512; i++) {
      printf("%.2X ", dest_buf[i]);
      if (!((i + 1) % 16))
        printf("\n");
    }
    read(boot_fd, boot_buf, 512);
    printf("boot: \n");
    for (i = 0; i < 512; i++) {
      printf("%.2X ", dest_buf[i]);
      if (!((i + 1) % 16))
        printf("\n");
    }
    memcpy(&boot_buf[446], &dest_buf[446], 64);
    lseek(dest_fd, 0, SEEK_SET);
    write(dest_fd, boot_buf, 512);
    free(dest_buf);
    free(boot_buf);
    close(dest_fd);
    close(boot_fd);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11ec <main+0x3c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    $0x1a0,%edx
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    122a <main+0x7a>
lea    0xe14(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    $0x1a0,%edx
call   10b0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1268 <main+0xb8>
lea    0xdd6(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    $0x200,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    $0x200,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x200,%edx
call   1080 <read@plt>
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x200,-0x1c(%rbp)
jge    1309 <main+0x159>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12f6 <main+0x146>
lea    0xd5b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12fb <main+0x14b>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12aa <main+0xfa>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x200,%edx
call   1080 <read@plt>
lea    0xd23(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x200,-0x1c(%rbp)
jge    138e <main+0x1de>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xcef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    137b <main+0x1cb>
lea    0xcd6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1380 <main+0x1d0>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    132f <main+0x17f>
mov    -0x30(%rbp),%rdi
add    $0x1be,%rdi
mov    -0x28(%rbp),%rsi
add    $0x1be,%rsi
mov    $0x40,%edx
call   1090 <memcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2c18(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jle    1385 <main+0x1b5>
mov    0x8(%rbx),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    $0x1a0,%edx
call   10d0 <open@plt>
test   %eax,%eax
js     136f <main+0x19f>
mov    %eax,%r15d
mov    0x10(%rbx),%rdi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    $0x1a0,%edx
call   10d0 <open@plt>
test   %eax,%eax
js     136f <main+0x19f>
mov    %eax,%r14d
mov    $0x200,%edi
call   10c0 <malloc@plt>
mov    %rax,%rbp
mov    $0x200,%edi
call   10c0 <malloc@plt>
mov    %rax,%r12
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   10a0 <read@plt>
lea    0xde0(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%ebx
lea    0xdc9(%rip),%r13
jmp    127d <main+0xad>
nop
add    $0x1,%rbx
cmp    $0x201,%rbx
je     129d <main+0xcd>
movzbl -0x1(%rbp,%rbx,1),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
test   $0xf,%bl
jne    1270 <main+0xa0>
mov    $0xa,%edi
call   1040 <putchar@plt>
jmp    1270 <main+0xa0>
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x200,%edx
call   10a0 <read@plt>
lea    0xd8f(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%ebx
lea    0xd71(%rip),%r13
jmp    12dd <main+0x10d>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    $0x201,%rbx
je     12fd <main+0x12d>
movzbl -0x1(%rbp,%rbx,1),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
test   $0xf,%bl
jne    12d0 <main+0x100>
mov    $0xa,%edi
call   1040 <putchar@plt>
jmp    12d0 <main+0x100>
mov    %r12,%rdi
add    $0x1be,%rdi
mov    %rbp,%rsi
add    $0x1be,%rsi
mov    $0x40,%edx
call   10b0 <memcpy@plt>
mov    0x2cae(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1080 <lseek@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x200,%edx
call   1060 <write@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15d,%edi
call   1090 <close@plt>
mov    %r14d,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb9(%rip),%rdi
call   10e0 <perror@plt>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>
mov    (%rbx),%rsi
lea    0xc71(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [47 x i8] c"Usage : %s destination_device bootsector_file\0A\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"open :\00"
@O_RDONLY = global i32 0
@.str.2 = [8 x i8] c"dest: \0A\00"
@.str.3 = [6 x i8] c"%.2X \00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [8 x i8] c"boot: \0A\00"
@SEEK_SET = global i32 0
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 i8*
%10 = alloca i8*
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, 3
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* %22, i32 %23, i32 416)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %19
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* %33, i32 %34, i32 416)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %30
%42 = call i8* @malloc(i32 512)
store i8* %42, i8** %9
%43 = call i8* @malloc(i32 512)
store i8* %43, i8** %10
%44 = load i32, i32* %6
%45 = load i8*, i8** %9
%46 = call i32 @read(i32 %44, i8* %45, i32 512)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %8
br label %48
48: ; preds = %66, %41
%49 = load i32, i32* %8
%50 = icmp slt i32 %49, 512
br i1 %50, label %51, label %69
51: ; preds = %48
%52 = load i8*, i8** %9
%53 = load i32, i32* %8
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %57)
%59 = load i32, i32* %8
%60 = add nsw i32 %59, 1
%61 = srem i32 %60, 16
%62 = icmp ne i32 %61, 0
br i1 %62, label %65, label %63
63: ; preds = %51
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %65
65: ; preds = %63, %51
br label %66
66: ; preds = %65
%67 = load i32, i32* %8
%68 = add nsw i32 %67, 1
store i32 %68, i32* %8
br label %48
69: ; preds = %48
%70 = load i32, i32* %7
%71 = load i8*, i8** %10
%72 = call i32 @read(i32 %70, i8* %71, i32 512)
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %8
br label %74
74: ; preds = %92, %69
%75 = load i32, i32* %8
%76 = icmp slt i32 %75, 512
br i1 %76, label %77, label %95
77: ; preds = %74
%78 = load i8*, i8** %9
%79 = load i32, i32* %8
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i8, i8* %78, i64 %80
%82 = load i8, i8* %81
%83 = zext i8 %82 to i32
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %83)
%85 = load i32, i32* %8
%86 = add nsw i32 %85, 1
%87 = srem i32 %86, 16
%88 = icmp ne i32 %87, 0
br i1 %88, label %91, label %89
89: ; preds = %77
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %91
91: ; preds = %89, %77
br label %92
92: ; preds = %91
%93 = load i32, i32* %8
%94 = add nsw i32 %93, 1
store i32 %94, i32* %8
br label %74
95: ; preds = %74
%96 = load i8*, i8** %10
%97 = getelementptr inbounds i8, i8* %96, i64 446
%98 = load i8*, i8** %9
%99 = getelementptr inbounds i8, i8* %98, i64 446
%100 = call i32 @memcpy(i8* %97, i8* %99, i32 64)
%101 = load i32, i32* %6
%102 = load i32, i32* @SEEK_SET
%103 = call i32 @lseek(i32 %101, i32 0, i32 %102)
%104 = load i32, i32* %6
%105 = load i8*, i8** %10
%106 = call i32 @write(i32 %104, i8* %105, i32 512)
%107 = load i8*, i8** %9
%108 = call i32 @free(i8* %107)
%109 = load i8*, i8** %10
%110 = call i32 @free(i8* %109)
%111 = load i32, i32* %6
%112 = call i32 @close(i32 %111)
%113 = load i32, i32* %7
%114 = call i32 @close(i32 %113)
br label %115
115: ; preds = %95
ret i32 0
}

LLVM IR (-O2):
@.str = [47 x i8] c"Usage : %s destination_device bootsector_file\0A\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"open :\00"
@O_RDONLY = global i32 0
@.str.3 = [6 x i8] c"%.2X \00"
@SEEK_SET = global i32 0
@str = [7 x i8] c"dest: \00"
@str.6 = [7 x i8] c"boot: \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* %10, i32 %11, i32 416)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %8
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDONLY
%21 = tail call i32 @open(i8* %19, i32 %20, i32 416)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call i32 @exit(i32 -1)
unreachable
26: ; preds = %17
%27 = tail call dereferenceable_or_null(512) i8* @malloc(i32 512)
%28 = tail call dereferenceable_or_null(512) i8* @malloc(i32 512)
%29 = tail call i32 @read(i32 %12, i8* %27, i32 512)
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
br label %31
31: ; preds = %26, %42
%32 = phi i64 [ 0, %26 ], [ %37, %42 ]
%33 = getelementptr inbounds i8, i8* %27, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = add nuw nsw i64 %32, 1
%38 = and i64 %37, 15
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %31
%41 = tail call i32 @putchar(i32 10)
br label %42
42: ; preds = %31, %40
%43 = icmp eq i64 %37, 512
br i1 %43, label %44, label %31
44: ; preds = %42
%45 = tail call i32 @read(i32 %21, i8* %28, i32 512)
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.6, i64 0, i64 0))
br label %47
47: ; preds = %44, %58
%48 = phi i64 [ 0, %44 ], [ %53, %58 ]
%49 = getelementptr inbounds i8, i8* %27, i64 %48
%50 = load i8, i8* %49
%51 = zext i8 %50 to i32
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %51)
%53 = add nuw nsw i64 %48, 1
%54 = and i64 %53, 15
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %47
%57 = tail call i32 @putchar(i32 10)
br label %58
58: ; preds = %47, %56
%59 = icmp eq i64 %53, 512
br i1 %59, label %60, label %47
60: ; preds = %58
%61 = getelementptr inbounds i8, i8* %28, i64 446
%62 = getelementptr inbounds i8, i8* %27, i64 446
%63 = tail call i32 @memcpy(i8* %61, i8* %62, i32 64)
%64 = load i32, i32* @SEEK_SET
%65 = tail call i32 @lseek(i32 %12, i32 0, i32 %64)
%66 = tail call i32 @write(i32 %12, i8* %28, i32 512)
%67 = tail call i32 @free(i8* %27)
%68 = tail call i32 @free(i8* %28)
%69 = tail call i32 @close(i32 %12)
%70 = tail call i32 @close(i32 %21)
ret i32 0
}