#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 /*<<< 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)) == -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)) == -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)) == -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("\n%d , %d", iTargetFd, 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    0x2e19(%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
lea    0xe51(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1215 <main+0x95>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    126e <main+0xee>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe24(%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    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1307 <main+0x187>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xd8b(%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    0xd57(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1070 <WriteKernelInformation@plt>
lea    0xdaf(%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    12d6 <main+0x146>
mov    %rsi,%r14
mov    0x2e2d(%rip),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e33(%rip),%rax
or     (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12e8 <main+0x158>
mov    %eax,%r15d
lea    0xeca(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%r14),%rdi
mov    0x2de2(%rip),%r12
mov    (%r12),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1309 <main+0x179>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15d,%esi
call   10a0 <CopyFile@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r15d,%edi
mov    %ebx,%esi
call   1060 <AdjustInSectorSize@plt>
mov    0x8(%r14),%rsi
lea    0xe46(%rip),%rdi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
mov    0x10(%r14),%rdi
mov    (%r12),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1318 <main+0x188>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15d,%esi
call   10a0 <CopyFile@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r15d,%edi
mov    %ebx,%esi
call   1060 <AdjustInSectorSize@plt>
mov    %eax,%ebp
mov    0x10(%r14),%rsi
lea    0xded(%rip),%rdi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe0a(%rip),%rdi
mov    %r15d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe56(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
mov    %ebp,%esi
call   1080 <WriteKernelInformation@plt>
lea    0xe69(%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    0x2ce3(%rip),%rax
mov    (%rax),%edi
lea    0xd1a(%rip),%rsi
jmp    12f8 <main+0x168>
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
lea    0xd45(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
jmp    1325 <main+0x195>
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
mov    0x10(%r14),%rdx
lea    0xd2e(%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
@.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 = [9 x i8] c"\0A%d , %d\00"
@.str.8 = [42 x i8] c"[INFO] Start to write kernel information\0A\00"
@.str.9 = [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 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %22)
store i32 %23, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %17
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %6
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %44
37: ; preds = %29
%38 = load i32, i32* @stderr
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %41)
%43 = call i32 @exit(i32 -1)
unreachable
44: ; preds = %29
%45 = load i32, i32* %6
%46 = load i32, i32* %7
%47 = call i32 @CopyFile(i32 %45, i32 %46)
store i32 %47, i32* %10
%48 = load i32, i32* %6
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %7
%51 = load i32, i32* %10
%52 = call i32 @AdjustInSectorSize(i32 %50, i32 %51)
store i32 %52, i32* %8
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
%56 = load i32, i32* %10
%57 = load i32, i32* %8
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %55, i32 %56, i32 %57)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.6, i64 0, i64 0))
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 2
%62 = load i8*, i8** %61
%63 = load i32, i32* @O_RDONLY
%64 = call i32 @open(i8* %62, i32 %63)
store i32 %64, i32* %6
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %73
66: ; preds = %44
%67 = load i32, i32* @stderr
%68 = load i8**, i8*** %5
%69 = getelementptr inbounds i8*, i8** %68, i64 2
%70 = load i8*, i8** %69
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %70)
%72 = call i32 @exit(i32 -1)
unreachable
73: ; preds = %44
%74 = load i32, i32* %6
%75 = load i32, i32* %7
%76 = call i32 @CopyFile(i32 %74, i32 %75)
store i32 %76, i32* %10
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
%79 = load i32, i32* %7
%80 = load i32, i32* %10
%81 = call i32 @AdjustInSectorSize(i32 %79, i32 %80)
store i32 %81, i32* %9
%82 = load i8**, i8*** %5
%83 = getelementptr inbounds i8*, i8** %82, i64 2
%84 = load i8*, i8** %83
%85 = load i32, i32* %10
%86 = load i32, i32* %9
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %84, i32 %85, i32 %86)
%88 = load i32, i32* %7
%89 = load i32, i32* %9
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %88, i32 %89)
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.8, i64 0, i64 0))
%92 = load i32, i32* %7
%93 = load i32, i32* %9
%94 = call i32 @WriteKernelInformation(i32 %92, i32 %93)
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0))
%96 = load i32, i32* %7
%97 = call i32 @close(i32 %96)
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
@.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.7 = [9 x i8] c"\0A%d , %d\00"
@str = [38 x i8] c"[INFO] Copy boot loader to image file\00"
@str.10 = [48 x i8] c"[INFO] Copy protected mode kernel to image file\00"
@str.11 = [41 x i8] c"[INFO] Start to write kernel information\00"
@str.12 = [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 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %8
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %8
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDONLY
%25 = tail call i32 @open(i8* %23, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %22
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = tail call i32 @exit(i32 -1)
unreachable
32: ; preds = %20
%33 = tail call i32 @CopyFile(i32 %25, i32 %14)
%34 = tail call i32 @close(i32 %25)
%35 = tail call i32 @AdjustInSectorSize(i32 %14, i32 %33)
%36 = load i8*, i8** %22
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %36, i32 %33, i32 %35)
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.10, i64 0, i64 0))
%39 = getelementptr inbounds i8*, i8** %1, i64 2
%40 = load i8*, i8** %39
%41 = load i32, i32* @O_RDONLY
%42 = tail call i32 @open(i8* %40, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %32
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %39
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %46)
%48 = tail call i32 @exit(i32 -1)
unreachable
49: ; preds = %32
%50 = tail call i32 @CopyFile(i32 %42, i32 %14)
%51 = tail call i32 @close(i32 %42)
%52 = tail call i32 @AdjustInSectorSize(i32 %14, i32 %50)
%53 = load i8*, i8** %39
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0), i8* %53, i32 %50, i32 %52)
%55 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %14, i32 %52)
%56 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.11, i64 0, i64 0))
%57 = tail call i32 @WriteKernelInformation(i32 %14, i32 %52)
%58 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.12, i64 0, i64 0))
%59 = tail call i32 @close(i32 %14)
ret i32 0
}