#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 stat {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ elfboot_main(void *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int fd;
  struct stat st;
  void *p;
  if (argc < 2) {
    fprintf(stderr, "Usage : %s (elf)\n", argv[0]);
    return 1;
  }
  fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    perror("open");
    return 1;
  }
  fstat(fd, &st);
  p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (p == MAP_FAILED) {
    perror("mmap");
    return 2;
  }
  printf("read result : %lx\n", elfboot_main(p));
  munmap(p, st.st_size);
  close(fd);
  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)
cmpl   $0x2,-0x8(%rbp)
jge    11d8 <main+0x48>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a6 <main+0x116>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1213 <main+0x83>
lea    0xe10(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a6 <main+0x116>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10b0 <fstat@plt>
mov    -0x18(%rbp),%esi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
mov    0x2d96(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d6d(%rip),%rcx
cmp    (%rcx),%rax
jne    1274 <main+0xe4>
lea    0xdb4(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12a6 <main+0x116>
mov    -0x20(%rbp),%rdi
call   1060 <elfboot_main@plt>
mov    %eax,%esi
lea    0xd96(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1080 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11bf <main+0x2f>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe51(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%r14d
jmp    1241 <main+0xb1>
mov    0x8(%rsi),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     124f <main+0xbf>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
mov    (%rsp),%r15d
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    0x2dd3(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %r15d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2dac(%rip),%rax
cmp    (%rax),%rbp
je     1263 <main+0xd3>
mov    %rbp,%rdi
call   1060 <elfboot_main@plt>
lea    0xdf4(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    %r15d,%esi
call   1080 <munmap@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbc(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%r14d
jmp    1241 <main+0xb1>
lea    0xdad(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x2,%r14d
jmp    1241 <main+0xb1>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Usage : %s (elf)\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [19 x i8] c"read result : %lx\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %51
17: ; preds = %2
%18 = load i8**, i8*** %5
%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* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %51
27: ; preds = %17
%28 = load i32, i32* %6
%29 = call i32 @fstat(i32 %28, %struct.stat* %7)
%30 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* @PROT_READ
%33 = load i32, i32* @MAP_PRIVATE
%34 = load i32, i32* %6
%35 = call i8* @mmap(i32* null, i32 %31, i32 %32, i32 %33, i32 %34, i32 0)
store i8* %35, i8** %8
%36 = load i8*, i8** %8
%37 = load i8*, i8** @MAP_FAILED
%38 = icmp eq i8* %36, %37
br i1 %38, label %39, label %41
39: ; preds = %27
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %3
br label %51
41: ; preds = %27
%42 = load i8*, i8** %8
%43 = call i32 @elfboot_main(i8* %42)
%44 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %43)
%45 = load i8*, i8** %8
%46 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @munmap(i8* %45, i32 %47)
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
store i32 0, i32* %3
br label %51
51: ; preds = %41, %39, %25, %11
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Usage : %s (elf)\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [19 x i8] c"read result : %lx\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %34
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 slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %34
18: ; preds = %10
%19 = call i32 @fstat(i32 %14, %struct.stat* %3)
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @MAP_PRIVATE
%24 = tail call i8* @mmap(i32* null, i32 %21, i32 %22, i32 %23, i32 %14, i32 0)
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %34
29: ; preds = %18
%30 = tail call i32 @elfboot_main(i8* %24)
%31 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = tail call i32 @munmap(i8* %24, i32 %21)
%33 = tail call i32 @close(i32 %14)
br label %34
34: ; preds = %29, %27, %16, %6
%35 = phi i32 [ 1, %6 ], [ 1, %16 ], [ 2, %27 ], [ 0, %29 ]
ret i32 %35
}