#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*/ S_IRUSR;
int close(int);
int creat(char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, int);
int write(int, unsigned char *, int);
int main(int argc, char *argv[]) {
  int i, h1, h2, h3;
  unsigned char c[512], c2[512], c3[512];
  if (argc != 4) {
    printf("usage: patchboot source-bootfile new-bootfile result-bootfile\n");
    return 1;
  }
  if ((h1 = open(argv[1], 0)) <= 0) {
    printf("can't open %s\n", argv[1]);
    return 2;
  }
  if (read(h1, c, 512) != 512) {
    printf("can't read %s\n", argv[1]);
    return 3;
  }
  if ((h2 = open(argv[2], 0)) <= 0) {
    printf("can't open %s\n", argv[2]);
    return 4;
  }
  i = read(h2, c2, 512);
  if (i == -1) {
    printf("can't read %s\n", argv[2]);
    return 5;
  }
  for (i = 0; i < 3 + 8; i++)
    c[i] = c2[i];
  for (i = 0x26; i < 510; i++)
    c[i] = c2[i];
  c[510] = 0x55;
  c[511] = 0xaa;
  if ((h3 = creat(argv[3], S_IRUSR)) <= 0) {
    printf("can't open %s\n", argv[3]);
    return 5;
  }
  if (write(h3, c, 512) != 512) {
    printf("can't write %s\n", argv[3]);
    return 6;
  }
  if (close(h3) == -1) {
    printf("can't close %s\n", argv[3]);
    return 7;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x620,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     119d <main+0x3d>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13cf <main+0x26f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     11da <main+0x7a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13cf <main+0x26f>
mov    -0x18(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
cmp    $0x200,%eax
je     121b <main+0xbb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    13cf <main+0x26f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jg     1258 <main+0xf8>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x4,-0x4(%rbp)
jmp    13cf <main+0x26f>
mov    -0x1c(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    129b <main+0x13b>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    13cf <main+0x26f>
movl   $0x0,-0x14(%rbp)
cmpl   $0xb,-0x14(%rbp)
jge    12d0 <main+0x170>
movslq -0x14(%rbp),%rax
mov    -0x420(%rbp,%rax,1),%cl
movslq -0x14(%rbp),%rax
mov    %cl,-0x220(%rbp,%rax,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12a2 <main+0x142>
movl   $0x26,-0x14(%rbp)
cmpl   $0x1fe,-0x14(%rbp)
jge    1308 <main+0x1a8>
movslq -0x14(%rbp),%rax
mov    -0x420(%rbp,%rax,1),%cl
movslq -0x14(%rbp),%rax
mov    %cl,-0x220(%rbp,%rax,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12d7 <main+0x177>
movb   $0x55,-0x22(%rbp)
movb   $0xaa,-0x21(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    0x2cc1(%rip),%rax
mov    (%rax),%esi
call   1080 <creat@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jg     1354 <main+0x1f4>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
lea    0xcfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    13cf <main+0x26f>
mov    -0x20(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1030 <write@plt>
cmp    $0x200,%eax
je     1395 <main+0x235>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
lea    0xcdb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x6,-0x4(%rbp)
jmp    13cf <main+0x26f>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    13c8 <main+0x268>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
lea    0xcb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x7,-0x4(%rbp)
jmp    13cf <main+0x26f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x620,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
cmp    $0x4,%edi
jne    1290 <main+0x110>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
xor    %esi,%esi
call   1090 <open@plt>
test   %eax,%eax
jle    12a6 <main+0x126>
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1070 <read@plt>
cmp    $0x200,%eax
jne    12bf <main+0x13f>
mov    0x10(%rbx),%rdi
xor    %esi,%esi
call   1090 <open@plt>
test   %eax,%eax
jle    12d8 <main+0x158>
lea    0x200(%rsp),%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     12f1 <main+0x171>
mov    0x207(%rsp),%eax
mov    %eax,0x7(%rsp)
mov    0x200(%rsp),%rax
mov    %rax,(%rsp)
lea    0x26(%rsp),%rdi
lea    0x226(%rsp),%rsi
mov    $0x1d8,%edx
call   1080 <memcpy@plt>
movw   $0xaa55,0x1fe(%rsp)
mov    0x18(%rbx),%rdi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
call   10a0 <creat@plt>
test   %eax,%eax
jle    12fe <main+0x17e>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1040 <write@plt>
cmp    $0x200,%eax
jne    1317 <main+0x197>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    132e <main+0x1ae>
mov    0x18(%rbx),%rsi
lea    0xdaf(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x7,%eax
jmp    132e <main+0x1ae>
lea    0xda7(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    132e <main+0x1ae>
mov    0x8(%rbx),%rsi
lea    0xd4f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%eax
jmp    132e <main+0x1ae>
mov    0x8(%rbx),%rsi
lea    0xd45(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%eax
jmp    132e <main+0x1ae>
mov    0x10(%rbx),%rsi
lea    0xd1d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x4,%eax
jmp    132e <main+0x1ae>
mov    0x10(%rbx),%rsi
lea    0xd13(%rip),%rdi
jmp    1309 <main+0x189>
mov    0x18(%rbx),%rsi
lea    0xcf7(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%eax
jmp    132e <main+0x1ae>
mov    0x18(%rbx),%rsi
lea    0xcfc(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x6,%eax
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [63 x i8] c"usage: patchboot source-bootfile new-bootfile result-bootfile\0A\00"
@.str.1 = [15 x i8] c"can't open %s\0A\00"
@.str.2 = [15 x i8] c"can't read %s\0A\00"
@S_IRUSR = global i32 0
@.str.3 = [16 x i8] c"can't write %s\0A\00"
@.str.4 = [16 x i8] c"can't close %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 [512 x i8]
%11 = alloca [512 x i8]
%12 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 4
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %124
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @open(i8* %20, i32 0)
store i32 %21, i32* %7
%22 = icmp sle i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i32 2, i32* %3
br label %124
28: ; preds = %17
%29 = load i32, i32* %7
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 512)
%32 = icmp ne i32 %31, 512
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %36)
store i32 3, i32* %3
br label %124
38: ; preds = %28
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 2
%41 = load i8*, i8** %40
%42 = call i32 @open(i8* %41, i32 0)
store i32 %42, i32* %8
%43 = icmp sle i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %38
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 2
%47 = load i8*, i8** %46
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %47)
store i32 4, i32* %3
br label %124
49: ; preds = %38
%50 = load i32, i32* %8
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%52 = call i32 @read(i32 %50, i8* %51, i32 512)
store i32 %52, i32* %6
%53 = load i32, i32* %6
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %60
55: ; preds = %49
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 2
%58 = load i8*, i8** %57
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %58)
store i32 5, i32* %3
br label %124
60: ; preds = %49
store i32 0, i32* %6
br label %61
61: ; preds = %72, %60
%62 = load i32, i32* %6
%63 = icmp slt i32 %62, 11
br i1 %63, label %64, label %75
64: ; preds = %61
%65 = load i32, i32* %6
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 %66
%68 = load i8, i8* %67
%69 = load i32, i32* %6
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 %70
store i8 %68, i8* %71
br label %72
72: ; preds = %64
%73 = load i32, i32* %6
%74 = add nsw i32 %73, 1
store i32 %74, i32* %6
br label %61
75: ; preds = %61
store i32 38, i32* %6
br label %76
76: ; preds = %87, %75
%77 = load i32, i32* %6
%78 = icmp slt i32 %77, 510
br i1 %78, label %79, label %90
79: ; preds = %76
%80 = load i32, i32* %6
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 %81
%83 = load i8, i8* %82
%84 = load i32, i32* %6
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 %85
store i8 %83, i8* %86
br label %87
87: ; preds = %79
%88 = load i32, i32* %6
%89 = add nsw i32 %88, 1
store i32 %89, i32* %6
br label %76
90: ; preds = %76
%91 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 510
store i8 85, i8* %91
%92 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 511
store i8 -86, i8* %92
%93 = load i8**, i8*** %5
%94 = getelementptr inbounds i8*, i8** %93, i64 3
%95 = load i8*, i8** %94
%96 = load i32, i32* @S_IRUSR
%97 = call i32 @creat(i8* %95, i32 %96)
store i32 %97, i32* %9
%98 = icmp sle i32 %97, 0
br i1 %98, label %99, label %104
99: ; preds = %90
%100 = load i8**, i8*** %5
%101 = getelementptr inbounds i8*, i8** %100, i64 3
%102 = load i8*, i8** %101
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %102)
store i32 5, i32* %3
br label %124
104: ; preds = %90
%105 = load i32, i32* %9
%106 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%107 = call i32 @write(i32 %105, i8* %106, i32 512)
%108 = icmp ne i32 %107, 512
br i1 %108, label %109, label %114
109: ; preds = %104
%110 = load i8**, i8*** %5
%111 = getelementptr inbounds i8*, i8** %110, i64 3
%112 = load i8*, i8** %111
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %112)
store i32 6, i32* %3
br label %124
114: ; preds = %104
%115 = load i32, i32* %9
%116 = call i32 @close(i32 %115)
%117 = icmp eq i32 %116, -1
br i1 %117, label %118, label %123
118: ; preds = %114
%119 = load i8**, i8*** %5
%120 = getelementptr inbounds i8*, i8** %119, i64 3
%121 = load i8*, i8** %120
%122 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %121)
store i32 7, i32* %3
br label %124
123: ; preds = %114
store i32 0, i32* %3
br label %124
124: ; preds = %123, %118, %109, %99, %55, %44, %33, %23, %15
%125 = load i32, i32* %3
ret i32 %125
}

LLVM IR (-O2):
@.str.1 = [15 x i8] c"can't open %s\0A\00"
@.str.2 = [15 x i8] c"can't read %s\0A\00"
@S_IRUSR = global i32 0
@.str.3 = [16 x i8] c"can't write %s\0A\00"
@.str.4 = [16 x i8] c"can't close %s\0A\00"
@str = [62 x i8] c"usage: patchboot source-bootfile new-bootfile result-bootfile\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 = alloca [512 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 4
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str, i64 0, i64 0))
br label %65
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @open(i8* %14, i32 0)
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i8*, i8** %13
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %65
20: ; preds = %12
%21 = call i32 @read(i32 %15, i8* %7, i32 512)
%22 = icmp eq i32 %21, 512
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i8*, i8** %13
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %24)
br label %65
26: ; preds = %20
%27 = getelementptr inbounds i8*, i8** %1, i64 2
%28 = load i8*, i8** %27
%29 = tail call i32 @open(i8* %28, i32 0)
%30 = icmp slt i32 %29, 1
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i8*, i8** %27
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %32)
br label %65
34: ; preds = %26
%35 = call i32 @read(i32 %29, i8* %8, i32 512)
%36 = icmp eq i32 %35, -1
br i1 %36, label %47, label %37
37: ; preds = %34
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 38
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 38
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 510
store i8 85, i8* %40
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 511
store i8 -86, i8* %41
%42 = getelementptr inbounds i8*, i8** %1, i64 3
%43 = load i8*, i8** %42
%44 = load i32, i32* @S_IRUSR
%45 = tail call i32 @creat(i8* %43, i32 %44)
%46 = icmp slt i32 %45, 1
br i1 %46, label %50, label %53
47: ; preds = %34
%48 = load i8*, i8** %27
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %48)
br label %65
50: ; preds = %37
%51 = load i8*, i8** %42
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %51)
br label %65
53: ; preds = %37
%54 = call i32 @write(i32 %45, i8* %7, i32 512)
%55 = icmp eq i32 %54, 512
br i1 %55, label %59, label %56
56: ; preds = %53
%57 = load i8*, i8** %42
%58 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %57)
br label %65
59: ; preds = %53
%60 = tail call i32 @close(i32 %45)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = load i8*, i8** %42
%64 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %63)
br label %65
65: ; preds = %59, %62, %56, %50, %47, %31, %23, %17, %10
%66 = phi i32 [ 1, %10 ], [ 2, %17 ], [ 3, %23 ], [ 4, %31 ], [ 5, %47 ], [ 5, %50 ], [ 6, %56 ], [ 7, %62 ], [ 0, %59 ]
ret i32 %66
}