#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 AdjustInSectorSize(int, int);
int CopyFile(int, int);
int O_CREAT;
int O_RDONLY;
int O_RDWR;
int O_TRUNC;
int S_IREAD;
int S_IWRITE;
int /*<<< orphan*/ WriteKernelInformation(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int iSourceFd;
  int iTargetFd;
  int iBootLoaderSize;
  int iKernel32SectorCount;
  int iSourceSize;
  if (argc < 3) {
    fprintf(stderr, "[ERROR] ImageMaker.exe BootLoader.bin Kernel32.bin\n");
    exit(-1);
  }
  if ((iTargetFd = open("Disk.img", O_RDWR | O_CREAT | O_TRUNC | 0,
                        S_IREAD | S_IWRITE)) == -1) {
    fprintf(stderr, "[ERROR] Disk.img open fail.\n");
    exit(-1);
  }
  printf("[INFO] Copy boot loader to image file\n");
  if ((iSourceFd = open(argv[1], O_RDONLY | 0)) == -1) {
    fprintf(stderr, "[ERROR] %s open fail\n", argv[1]);
    exit(-1);
  }
  iSourceSize = CopyFile(iSourceFd, iTargetFd);
  close(iSourceFd);
  iBootLoaderSize = AdjustInSectorSize(iTargetFd, iSourceSize);
  printf("[INFO] %s size = [%d] and sector count = [%d]\n", argv[1],
         iSourceSize, iBootLoaderSize);
  printf("[INFO] Copy protected mode kernel to image file\n");
  if ((iSourceFd = open(argv[2], O_RDONLY | 0)) == -1) {
    fprintf(stderr, "[ERROR] %s open fail\n", argv[2]);
    exit(-1);
  }
  iSourceSize = CopyFile(iSourceFd, iTargetFd);
  close(iSourceFd);
  iKernel32SectorCount = AdjustInSectorSize(iTargetFd, iSourceSize);
  printf("[INFO] %s size = [%d] and sector count = [%d]\n", argv[2],
         iSourceSize, iKernel32SectorCount);
  printf("[INFO] Start to write kernel information\n");
  WriteKernelInformation(iTargetFd, iKernel32SectorCount);
  printf("[INFO] Image file create complete\n");
  close(iTargetFd);
  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    11c1 <main+0x41>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
or     (%rax),%esi
mov    0x2e16(%rip),%rax
or     (%rax),%esi
or     $0x0,%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
mov    0x2dd9(%rip),%rax
or     (%rax),%edx
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    122c <main+0xac>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    128a <main+0x10a>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1090 <CopyFile@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1050 <AdjustInSectorSize@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x24(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2ce9(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1328 <main+0x1a8>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xd6a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1090 <CopyFile@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1050 <AdjustInSectorSize@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xd36(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1070 <WriteKernelInformation@plt>
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x2,%edi
jle    12db <main+0x14b>
mov    %rsi,%r14
mov    0x2e2d(%rip),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e33(%rip),%rax
or     (%rax),%esi
mov    0x2dea(%rip),%rax
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe5c(%rip),%rdi
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12ed <main+0x15d>
mov    %eax,%r15d
lea    0xead(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%r14),%rdi
mov    0x2dce(%rip),%r12
mov    (%r12),%esi
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     130e <main+0x17e>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15d,%esi
call   10a0 <CopyFile@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r15d,%edi
mov    %ebp,%esi
call   1060 <AdjustInSectorSize@plt>
mov    0x8(%r14),%rsi
lea    0xe30(%rip),%rdi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
mov    0x10(%r14),%rdi
mov    (%r12),%esi
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     131d <main+0x18d>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15d,%esi
call   10a0 <CopyFile@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r15d,%edi
mov    %ebp,%esi
call   1060 <AdjustInSectorSize@plt>
mov    %eax,%ebx
mov    0x10(%r14),%rsi
lea    0xdd5(%rip),%rdi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe48(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
mov    %ebx,%esi
call   1080 <WriteKernelInformation@plt>
lea    0xe5b(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
lea    0xd15(%rip),%rsi
jmp    12fd <main+0x16d>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
lea    0xd40(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
jmp    132a <main+0x19a>
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
mov    0x10(%r14),%rdx
lea    0xd29(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [52 x i8] c"[ERROR] ImageMaker.exe BootLoader.bin Kernel32.bin\0A\00"
@.str.1 = [9 x i8] c"Disk.img\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IREAD = global i32 0
@S_IWRITE = global i32 0
@.str.2 = [29 x i8] c"[ERROR] Disk.img open fail.\0A\00"
@.str.3 = [39 x i8] c"[INFO] Copy boot loader to image file\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [22 x i8] c"[ERROR] %s open fail\0A\00"
@.str.5 = [47 x i8] c"[INFO] %s size = [%d] and sector count = [%d]\0A\00"
@.str.6 = [49 x i8] c"[INFO] Copy protected mode kernel to image file\0A\00"
@.str.7 = [42 x i8] c"[INFO] Start to write kernel information\0A\00"
@.str.8 = [35 x i8] c"[INFO] Image file create complete\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 i32
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 %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = or i32 %22, 0
%24 = load i32, i32* @S_IREAD
%25 = load i32, i32* @S_IWRITE
%26 = or i32 %24, %25
%27 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %26)
store i32 %27, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %17
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %17
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = load i32, i32* @O_RDONLY
%39 = or i32 %38, 0
%40 = call i32 (i8*, i32, ...) @open(i8* %37, i32 %39)
store i32 %40, i32* %6
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %49
42: ; preds = %33
%43 = load i32, i32* @stderr
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %46)
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %33
%50 = load i32, i32* %6
%51 = load i32, i32* %7
%52 = call i32 @CopyFile(i32 %50, i32 %51)
store i32 %52, i32* %10
%53 = load i32, i32* %6
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* %7
%56 = load i32, i32* %10
%57 = call i32 @AdjustInSectorSize(i32 %55, i32 %56)
store i32 %57, i32* %8
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 1
%60 = load i8*, i8** %59
%61 = load i32, i32* %10
%62 = load i32, i32* %8
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %60, i32 %61, i32 %62)
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.6, i64 0, i64 0))
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i64 2
%67 = load i8*, i8** %66
%68 = load i32, i32* @O_RDONLY
%69 = or i32 %68, 0
%70 = call i32 (i8*, i32, ...) @open(i8* %67, i32 %69)
store i32 %70, i32* %6
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %79
72: ; preds = %49
%73 = load i32, i32* @stderr
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 2
%76 = load i8*, i8** %75
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %76)
%78 = call i32 @exit(i32 -1)
unreachable
79: ; preds = %49
%80 = load i32, i32* %6
%81 = load i32, i32* %7
%82 = call i32 @CopyFile(i32 %80, i32 %81)
store i32 %82, i32* %10
%83 = load i32, i32* %6
%84 = call i32 @close(i32 %83)
%85 = load i32, i32* %7
%86 = load i32, i32* %10
%87 = call i32 @AdjustInSectorSize(i32 %85, i32 %86)
store i32 %87, i32* %9
%88 = load i8**, i8*** %5
%89 = getelementptr inbounds i8*, i8** %88, i64 2
%90 = load i8*, i8** %89
%91 = load i32, i32* %10
%92 = load i32, i32* %9
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %90, i32 %91, i32 %92)
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.7, i64 0, i64 0))
%95 = load i32, i32* %7
%96 = load i32, i32* %9
%97 = call i32 @WriteKernelInformation(i32 %95, i32 %96)
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0))
%99 = load i32, i32* %7
%100 = call i32 @close(i32 %99)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [52 x i8] c"[ERROR] ImageMaker.exe BootLoader.bin Kernel32.bin\0A\00"
@.str.1 = [9 x i8] c"Disk.img\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IREAD = global i32 0
@S_IWRITE = global i32 0
@.str.2 = [29 x i8] c"[ERROR] Disk.img open fail.\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [22 x i8] c"[ERROR] %s open fail\0A\00"
@.str.5 = [47 x i8] c"[INFO] %s size = [%d] and sector count = [%d]\0A\00"
@str = [38 x i8] c"[INFO] Copy boot loader to image file\00"
@str.9 = [48 x i8] c"[INFO] Copy protected mode kernel to image file\00"
@str.10 = [41 x i8] c"[INFO] Start to write kernel information\00"
@str.11 = [34 x i8] c"[INFO] Image file create complete\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 i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %2
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IREAD
%15 = load i32, i32* @S_IWRITE
%16 = or i32 %15, %14
%17 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %8
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %8
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDONLY
%28 = tail call i32 (i8*, i32, ...) @open(i8* %26, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %23
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %25
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%34 = tail call i32 @exit(i32 -1)
unreachable
35: ; preds = %23
%36 = tail call i32 @CopyFile(i32 %28, i32 %17)
%37 = tail call i32 @close(i32 %28)
%38 = tail call i32 @AdjustInSectorSize(i32 %17, i32 %36)
%39 = load i8*, i8** %25
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %39, i32 %36, i32 %38)
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.9, i64 0, i64 0))
%42 = getelementptr inbounds i8*, i8** %1, i64 2
%43 = load i8*, i8** %42
%44 = load i32, i32* @O_RDONLY
%45 = tail call i32 (i8*, i32, ...) @open(i8* %43, i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %35
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %42
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %49)
%51 = tail call i32 @exit(i32 -1)
unreachable
52: ; preds = %35
%53 = tail call i32 @CopyFile(i32 %45, i32 %17)
%54 = tail call i32 @close(i32 %45)
%55 = tail call i32 @AdjustInSectorSize(i32 %17, i32 %53)
%56 = load i8*, i8** %42
%57 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %56, i32 %53, i32 %55)
%58 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.10, i64 0, i64 0))
%59 = tail call i32 @WriteKernelInformation(i32 %17, i32 %55)
%60 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.11, i64 0, i64 0))
%61 = tail call i32 @close(i32 %17)
ret i32 0
}