#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 */
struct MemImage {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ binoutClose();
int /*<<< orphan*/ binoutOpen(char *);
int /*<<< orphan*/ calcByteorder();
int /*<<< orphan*/ checkEntry(struct MemImage *);
int /*<<< orphan*/ exit(int);
struct MemImage *memImage();
int /*<<< orphan*/ outimage(struct MemImage *);
int /*<<< orphan*/ parseArg(struct MemImage *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ showMemImage(char *, struct MemImage *);
int main(int argc, char *argv[]) {
  calcByteorder();
  if (argc < 2) {
    printf("Usage: mimgmake imagefile [arg ...]\n");
    printf("where each arg is one of the following:\n");
    printf("  file               load ELF file\n");
    printf("  noload:file        reserve ELF file\n");
    printf("  zero:addr-addr     zero all addresses in specified range\n");
    printf("  bootdata:addr-addr store bootdata in specified range\n");
    printf("  reserved:addr-addr reserve all addresses in specified range\n");
    printf("  entry@addr         set explicit entry point\n");
    printf("  file@addr          load file at given address\n");
    printf("  file@next          load file at next address\n");
    printf("  file@page          load file at next page boundary\n");
    exit(1);
  } else {
    struct MemImage *mimg = memImage();
    int i = 2;
    for (; i < argc; i++) {
      parseArg(mimg, argv[i]);
    }
    checkEntry(mimg);
    showMemImage(argv[1], mimg);
    binoutOpen(argv[1]);
    outimage(mimg);
    binoutClose();
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10b0 <calcByteorder@plt>
cmpl   $0x2,-0x8(%rbp)
jge    1255 <main+0xb5>
lea    0xe32(%rip),%rdi
call   1060 <printf@plt>
lea    0xe4b(%rip),%rdi
call   1060 <printf@plt>
lea    0xe68(%rip),%rdi
call   1060 <printf@plt>
lea    0xe80(%rip),%rdi
call   1060 <printf@plt>
lea    0xe9b(%rip),%rdi
call   1060 <printf@plt>
lea    0xecb(%rip),%rdi
call   1060 <printf@plt>
lea    0xef7(%rip),%rdi
call   1060 <printf@plt>
lea    0xf2a(%rip),%rdi
call   1060 <printf@plt>
lea    0xf4d(%rip),%rdi
call   1060 <printf@plt>
lea    0xf72(%rip),%rdi
call   1060 <printf@plt>
lea    0xf96(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   10a0 <memImage@plt>
mov    %rax,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1296 <main+0xf6>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1070 <parseArg@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1267 <main+0xc7>
mov    -0x18(%rbp),%rdi
call   1030 <checkEntry@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <showMemImage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <binoutOpen@plt>
mov    -0x18(%rbp),%rdi
call   1050 <outimage@plt>
mov    $0x0,%al
call   1090 <binoutClose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   10b0 <calcByteorder@plt>
cmp    $0x1,%ebp
jle    121e <main+0x7e>
xor    %eax,%eax
call   10a0 <memImage@plt>
mov    %rax,%r15
cmp    $0x2,%ebp
je     11e5 <main+0x45>
mov    %ebp,%ebp
mov    $0x2,%ebx
xchg   %ax,%ax
mov    (%r14,%rbx,8),%rsi
mov    %r15,%rdi
call   1070 <parseArg@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11d0 <main+0x30>
mov    %r15,%rdi
call   1030 <checkEntry@plt>
mov    0x8(%r14),%rdi
mov    %r15,%rsi
call   1050 <showMemImage@plt>
mov    0x8(%r14),%rdi
call   1080 <binoutOpen@plt>
mov    %r15,%rdi
call   1060 <outimage@plt>
xor    %eax,%eax
call   1090 <binoutClose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xddb(%rip),%rdi
call   1040 <puts@plt>
lea    0xdf3(%rip),%rdi
call   1040 <puts@plt>
lea    0xe0f(%rip),%rdi
call   1040 <puts@plt>
lea    0xe26(%rip),%rdi
call   1040 <puts@plt>
lea    0xe40(%rip),%rdi
call   1040 <puts@plt>
lea    0xe6f(%rip),%rdi
call   1040 <puts@plt>
lea    0xe9a(%rip),%rdi
call   1040 <puts@plt>
lea    0xecc(%rip),%rdi
call   1040 <puts@plt>
lea    0xeee(%rip),%rdi
call   1040 <puts@plt>
lea    0xf12(%rip),%rdi
call   1040 <puts@plt>
lea    0xf35(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.MemImage = type { i32 }
@.str = [37 x i8] c"Usage: mimgmake imagefile [arg ...]\0A\00"
@.str.1 = [41 x i8] c"where each arg is one of the following:\0A\00"
@.str.2 = [36 x i8] c" file load ELF file\0A\00"
@.str.3 = [39 x i8] c" noload:file reserve ELF file\0A\00"
@.str.4 = [60 x i8] c" zero:addr-addr zero all addresses in specified range\0A\00"
@.str.5 = [56 x i8] c" bootdata:addr-addr store bootdata in specified range\0A\00"
@.str.6 = [63 x i8] c" reserved:addr-addr reserve all addresses in specified range\0A\00"
@.str.7 = [47 x i8] c" entry@addr set explicit entry point\0A\00"
@.str.8 = [49 x i8] c" file@addr load file at given address\0A\00"
@.str.9 = [48 x i8] c" file@next load file at next address\0A\00"
@.str.10 = [54 x i8] c" file@page load file at next page boundary\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.MemImage*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @calcByteorder()
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %24
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%16 = call i32 @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.4, i64 0, i64 0))
%17 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0))
%18 = call i32 @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.6, i64 0, i64 0))
%19 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.7, i64 0, i64 0))
%20 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.8, i64 0, i64 0))
%21 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.9, i64 0, i64 0))
%22 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.10, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = call %struct.MemImage* (...) @memImage()
store %struct.MemImage* %25, %struct.MemImage** %6
store i32 2, i32* %7
br label %26
26: ; preds = %38, %24
%27 = load i32, i32* %7
%28 = load i32, i32* %4
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %41
30: ; preds = %26
%31 = load %struct.MemImage*, %struct.MemImage** %6
%32 = load i8**, i8*** %5
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %32, i64 %34
%36 = load i8*, i8** %35
%37 = call i32 @parseArg(%struct.MemImage* %31, i8* %36)
br label %38
38: ; preds = %30
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %26
41: ; preds = %26
%42 = load %struct.MemImage*, %struct.MemImage** %6
%43 = call i32 @checkEntry(%struct.MemImage* %42)
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = load %struct.MemImage*, %struct.MemImage** %6
%48 = call i32 @showMemImage(i8* %46, %struct.MemImage* %47)
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = call i32 @binoutOpen(i8* %51)
%53 = load %struct.MemImage*, %struct.MemImage** %6
%54 = call i32 @outimage(%struct.MemImage* %53)
%55 = call i32 (...) @binoutClose()
br label %56
56: ; preds = %41
ret i32 0
}

LLVM IR (-O2):
%struct.MemImage = type { i32 }
@str = [36 x i8] c"Usage: mimgmake imagefile [arg ...]\00"
@str.11 = [40 x i8] c"where each arg is one of the following:\00"
@str.12 = [35 x i8] c" file load ELF file\00"
@str.13 = [38 x i8] c" noload:file reserve ELF file\00"
@str.14 = [59 x i8] c" zero:addr-addr zero all addresses in specified range\00"
@str.15 = [55 x i8] c" bootdata:addr-addr store bootdata in specified range\00"
@str.16 = [62 x i8] c" reserved:addr-addr reserve all addresses in specified range\00"
@str.17 = [46 x i8] c" entry@addr set explicit entry point\00"
@str.18 = [48 x i8] c" file@addr load file at given address\00"
@str.19 = [47 x i8] c" file@next load file at next address\00"
@str.20 = [53 x i8] c" file@page load file at next page boundary\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (...) @calcByteorder()
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %18
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.11, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.12, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.13, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @str.14, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.15, i64 0, i64 0))
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str.16, i64 0, i64 0))
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.17, i64 0, i64 0))
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.18, i64 0, i64 0))
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.19, i64 0, i64 0))
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.20, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = tail call %struct.MemImage* (...) @memImage()
%20 = icmp eq i32 %0, 2
br i1 %20, label %30, label %21
21: ; preds = %18
%22 = zext i32 %0 to i64
br label %23
23: ; preds = %21, %23
%24 = phi i64 [ 2, %21 ], [ %28, %23 ]
%25 = getelementptr inbounds i8*, i8** %1, i64 %24
%26 = load i8*, i8** %25
%27 = tail call i32 @parseArg(%struct.MemImage* %19, i8* %26)
%28 = add nuw nsw i64 %24, 1
%29 = icmp eq i64 %28, %22
br i1 %29, label %30, label %23
30: ; preds = %23, %18
%31 = tail call i32 @checkEntry(%struct.MemImage* %19)
%32 = getelementptr inbounds i8*, i8** %1, i64 1
%33 = load i8*, i8** %32
%34 = tail call i32 @showMemImage(i8* %33, %struct.MemImage* %19)
%35 = load i8*, i8** %32
%36 = tail call i32 @binoutOpen(i8* %35)
%37 = tail call i32 @outimage(%struct.MemImage* %19)
%38 = tail call i32 (...) @binoutClose()
ret i32 0
}