#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*/ EXIT_FAILURE;
int O_CREAT;
int O_RDONLY;
int O_RDWR;
int O_TRUNC;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fchmod(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ makerom_process(int, int);
int open(char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int ifd, ofd;
  if (argc != 3) {
    fprintf(stderr, "usage: %s image rom\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((ifd = open(argv[1], O_RDONLY)) == -1) {
    fprintf(stderr, "error opening image file %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  if ((ofd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC)) == -1) {
    fprintf(stderr, "error opening rom file %s\n", argv[2]);
    exit(EXIT_FAILURE);
  }
  makerom_process(ifd, ofd);
  fchmod(ofd, S_IRUSR | S_IWUSR | S_IROTH | S_IRGRP);
  close(ifd);
  close(ofd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x4(%rbp)
je     11a3 <main+0x43>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe70(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11f0 <main+0x90>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe38(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
mov    0x2ddf(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    124f <main+0xef>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xdf6(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <makerom_process@plt>
mov    -0x18(%rbp),%edi
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
mov    0x2d5b(%rip),%rax
or     (%rax),%esi
mov    0x2d32(%rip),%rax
or     (%rax),%esi
mov    0x2d31(%rip),%rax
or     (%rax),%esi
call   1060 <fchmod@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
cmp    $0x3,%edi
jne    1201 <main+0xa1>
mov    0x8(%r14),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1216 <main+0xb6>
mov    %eax,%ebx
mov    0x2e44(%rip),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%r14),%rdi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     122c <main+0xcc>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %eax,%esi
call   1030 <makerom_process@plt>
mov    0x2dd0(%rip),%rax
mov    0x2df9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dce(%rip),%rax
or     (%rax),%esi
mov    0x2dcd(%rip),%rax
or     (%rax),%esi
mov    %ebp,%edi
call   1060 <fchmod@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdec(%rip),%rsi
jmp    1240 <main+0xe0>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xdeb(%rip),%rsi
jmp    1240 <main+0xe0>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    0x10(%r14),%rdx
lea    0xdf2(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"usage: %s image rom\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [29 x i8] c"error opening image file %s\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [27 x i8] c"error opening rom file %s\0A\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IROTH = global i32 0
@S_IRGRP = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%7 = load i32, i32* %3
%8 = icmp ne i32 %7, 3
br i1 %8, label %9, label %17
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8**, i8*** %4
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %4
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %5
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %32
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load i8**, i8*** %4
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %17
%33 = load i8**, i8*** %4
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = load i32, i32* @O_RDWR
%37 = load i32, i32* @O_CREAT
%38 = or i32 %36, %37
%39 = load i32, i32* @O_TRUNC
%40 = or i32 %38, %39
%41 = call i32 @open(i8* %35, i32 %40)
store i32 %41, i32* %6
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %51
43: ; preds = %32
%44 = load i32, i32* @stderr
%45 = load i8**, i8*** %4
%46 = getelementptr inbounds i8*, i8** %45, i64 2
%47 = load i8*, i8** %46
%48 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %47)
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %32
%52 = load i32, i32* %5
%53 = load i32, i32* %6
%54 = call i32 @makerom_process(i32 %52, i32 %53)
%55 = load i32, i32* %6
%56 = load i32, i32* @S_IRUSR
%57 = load i32, i32* @S_IWUSR
%58 = or i32 %56, %57
%59 = load i32, i32* @S_IROTH
%60 = or i32 %58, %59
%61 = load i32, i32* @S_IRGRP
%62 = or i32 %60, %61
%63 = call i32 @fchmod(i32 %55, i32 %62)
%64 = load i32, i32* %5
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %6
%67 = call i32 @close(i32 %66)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"usage: %s image rom\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [29 x i8] c"error opening image file %s\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [27 x i8] c"error opening rom file %s\0A\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IROTH = global i32 0
@S_IRGRP = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %11
%19 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %10
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDWR
%26 = load i32, i32* @O_CREAT
%27 = or i32 %26, %25
%28 = load i32, i32* @O_TRUNC
%29 = or i32 %27, %28
%30 = tail call i32 @open(i8* %24, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %38
32: ; preds = %22
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %23
%35 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %22
%39 = tail call i32 @makerom_process(i32 %14, i32 %30)
%40 = load i32, i32* @S_IRUSR
%41 = load i32, i32* @S_IWUSR
%42 = or i32 %41, %40
%43 = load i32, i32* @S_IROTH
%44 = or i32 %42, %43
%45 = load i32, i32* @S_IRGRP
%46 = or i32 %44, %45
%47 = tail call i32 @fchmod(i32 %30, i32 %46)
%48 = tail call i32 @close(i32 %14)
%49 = tail call i32 @close(i32 %30)
ret i32 0
}