#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_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ hex_to_image(char *, int, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd = 0, deviceFd = 0, readSize = 99, bytesRead, forkId = 1;
  char buf[readSize];
  char *fileName;
  if (forkId == 0) {
    fileName = "/boot/initramfs-4.14.0-1.el6.elrepo.x86_64.img";
    return 0;
  } else {
    fileName = "/bin/vi";
  }
  if ((deviceFd = open("/dev/ISSIMAGEDRV", O_RDWR)) >= 0) {
    if ((fd = open(fileName, O_RDWR)) >= 0) {
      bytesRead = read(fd, buf, readSize);
      write(deviceFd, buf, bytesRead);
      close(fd);
    } else {
      perror("File couldn't be opened!!!!");
      return 1;
    }
    int bufferSize = 99;
    char readBuffer[bufferSize];
    int readSize;
    readSize = read(deviceFd, readBuffer, bufferSize);
    hex_to_image(forkId == 0 ? "child_image.ppd" : "parent_image.ppd", readSize,
                 readBuffer);
    close(deviceFd);
  } else {
    perror("open(/dev/ISSIMAGEDRV) failed");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x63,-0x1c(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11e0 <main+0x80>
lea    0xe37(%rip),%rax
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1317 <main+0x1b7>
lea    0xe48(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
lea    0xe3c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jl     12ea <main+0x18a>
mov    -0x40(%rbp),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jl     1258 <main+0xf8>
mov    -0x68(%rbp),%rsi
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%edx
call   1060 <read@plt>
mov    -0x68(%rbp),%rsi
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    1277 <main+0x117>
lea    0xde9(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1317 <main+0x1b7>
movl   $0x63,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x70(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x48(%rbp),%edx
call   1060 <read@plt>
mov    -0x70(%rbp),%rdx
mov    %eax,-0x5c(%rbp)
mov    -0x24(%rbp),%ecx
lea    0xdb4(%rip),%rdi
lea    0xd9d(%rip),%rax
cmp    $0x0,%ecx
cmove  %rax,%rdi
mov    -0x5c(%rbp),%esi
call   1040 <hex_to_image@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x50(%rbp),%rax
mov    %rax,%rsp
jmp    1309 <main+0x1a9>
lea    0xd94(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1317 <main+0x1b7>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    0x2e74(%rip),%rbx
mov    (%rbx),%esi
lea    0xe8b(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     120c <main+0xac>
mov    %eax,%r14d
mov    (%rbx),%esi
lea    0xe6a(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1215 <main+0xb5>
mov    %eax,%ebx
lea    -0x80(%rbp),%r15
mov    %eax,%edi
mov    %r15,%rsi
mov    $0x63,%edx
call   1060 <read@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %rsp,%r15
lea    -0x70(%rsp),%rbx
mov    %rbx,%rsp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x63,%edx
call   1060 <read@plt>
lea    0xe4b(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1040 <hex_to_image@plt>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
mov    %r15,%rsp
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe33(%rip),%rdi
jmp    121c <main+0xbc>
lea    0xdfd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    1201 <main+0xa1>

LLVM IR (-O0):
@.str = [47 x i8] c"/boot/initramfs-4.14.0-1.el6.elrepo.x86_64.img\00"
@.str.1 = [8 x i8] c"/bin/vi\00"
@.str.2 = [17 x i8] c"/dev/ISSIMAGEDRV\00"
@O_RDWR = global i32 0
@.str.3 = [28 x i8] c"File couldn't be opened!!!!\00"
@.str.4 = [16 x i8] c"child_image.ppd\00"
@.str.5 = [17 x i8] c"parent_image.ppd\00"
@.str.6 = [30 x i8] c"open(/dev/ISSIMAGEDRV) failed\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
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i64
%18 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 99, i32* %8
store i32 1, i32* %10
%19 = load i32, i32* %8
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %11
%22 = alloca i8, i64 %20
store i64 %20, i64* %12
%23 = load i32, i32* %10
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %2
store i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8** %13
store i32 0, i32* %3
store i32 1, i32* %14
br label %67
26: ; preds = %2
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %13
br label %27
27: ; preds = %26
%28 = load i32, i32* @O_RDWR
%29 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %28)
store i32 %29, i32* %7
%30 = icmp sge i32 %29, 0
br i1 %30, label %31, label %64
31: ; preds = %27
%32 = load i8*, i8** %13
%33 = load i32, i32* @O_RDWR
%34 = call i32 @open(i8* %32, i32 %33)
store i32 %34, i32* %6
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %45
36: ; preds = %31
%37 = load i32, i32* %6
%38 = load i32, i32* %8
%39 = call i32 @read(i32 %37, i8* %22, i32 %38)
store i32 %39, i32* %9
%40 = load i32, i32* %7
%41 = load i32, i32* %9
%42 = call i32 @write(i32 %40, i8* %22, i32 %41)
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
br label %47
45: ; preds = %31
%46 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %14
br label %67
47: ; preds = %36
store i32 99, i32* %15
%48 = load i32, i32* %15
%49 = zext i32 %48 to i64
%50 = call i8* @llvm.stacksave()
store i8* %50, i8** %16
%51 = alloca i8, i64 %49
store i64 %49, i64* %17
%52 = load i32, i32* %7
%53 = load i32, i32* %15
%54 = call i32 @read(i32 %52, i8* %51, i32 %53)
store i32 %54, i32* %18
%55 = load i32, i32* %10
%56 = icmp eq i32 %55, 0
%57 = zext i1 %56 to i64
%58 = select i1 %56, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0)
%59 = load i32, i32* %18
%60 = call i32 @hex_to_image(i8* %58, i32 %59, i8* %51)
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
%63 = load i8*, i8** %16
br label %66
64: ; preds = %27
%65 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %14
br label %67
66: ; preds = %47
store i32 0, i32* %3
store i32 1, i32* %14
br label %67
67: ; preds = %66, %64, %45, %25
%68 = load i8*, i8** %11
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"/bin/vi\00"
@.str.2 = [17 x i8] c"/dev/ISSIMAGEDRV\00"
@O_RDWR = global i32 0
@.str.3 = [28 x i8] c"File couldn't be opened!!!!\00"
@.str.5 = [17 x i8] c"parent_image.ppd\00"
@.str.6 = [30 x i8] c"open(/dev/ISSIMAGEDRV) failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [99 x i8]
%4 = getelementptr inbounds [99 x i8], [99 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %5)
%7 = icmp sgt i32 %6, -1
br i1 %7, label %8, label %24
8: ; preds = %2
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %12, label %22
12: ; preds = %8
%13 = call i32 @read(i32 %10, i8* %4, i32 99)
%14 = call i32 @write(i32 %6, i8* %4, i32 %13)
%15 = tail call i32 @close(i32 %10)
%16 = tail call i8* @llvm.stacksave()
%17 = alloca [99 x i8]
%18 = getelementptr inbounds [99 x i8], [99 x i8]* %17, i64 0, i64 0
%19 = call i32 @read(i32 %6, i8* %18, i32 99)
%20 = call i32 @hex_to_image(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %19, i8* %18)
%21 = call i32 @close(i32 %6)
br label %26
22: ; preds = %8
%23 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %26
24: ; preds = %2
%25 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
br label %26
26: ; preds = %12, %24, %22
%27 = phi i32 [ 0, %12 ], [ 1, %22 ], [ 1, %24 ]
ret i32 %27
}