#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ fstat(int, struct stat *);
int getpid();
char *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ stderr;
int main(void) {
  int fd;
  struct stat status;
  char *ptr;
  fd = open("/etc/passwd", O_RDONLY);
  if (fd < 0)
    exit(1);
  if (fstat(fd, &status) != 0)
    exit(2);
  ptr = mmap(NULL, status.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (ptr == MAP_FAILED)
    exit(3);
  fprintf(stderr, "[%d] Projection Ok\n", getpid());
  pause();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ab <main+0x3b>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
je     11cb <main+0x5b>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%esi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    0x2e0a(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dc1(%rip),%rcx
cmp    (%rcx),%rax
jne    1212 <main+0xa2>
mov    $0x3,%edi
call   1080 <exit@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xddb(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1060 <pause@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11ff <main+0x8f>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
jne    1209 <main+0x99>
mov    0x8(%rsp),%esi
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
mov    0x2e33(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%rax
je     1213 <main+0xa3>
mov    0x2df1(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe25(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1060 <pause@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    $0x3,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str.1 = [20 x i8] c"[%d] Projection Ok\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.stat
%4 = alloca i8*
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i64 @fstat(i32 %12, %struct.stat* %3)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @exit(i32 2)
unreachable
17: ; preds = %11
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @MAP_SHARED
%22 = load i32, i32* %2
%23 = call i8* @mmap(i32* null, i32 %19, i32 %20, i32 %21, i32 %22, i32 0)
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = call i32 @exit(i32 3)
unreachable
29: ; preds = %17
%30 = load i32, i32* @stderr
%31 = call i32 (...) @getpid()
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = call i32 (...) @pause()
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str.1 = [20 x i8] c"[%d] Projection Ok\0A\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = call i64 @fstat(i32 %4, %struct.stat* %1)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @exit(i32 2)
unreachable
13: ; preds = %8
%14 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @MAP_SHARED
%18 = tail call i8* @mmap(i32* null, i32 %15, i32 %16, i32 %17, i32 %4, i32 0)
%19 = load i8*, i8** @MAP_FAILED
%20 = icmp eq i8* %18, %19
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = tail call i32 @exit(i32 3)
unreachable
23: ; preds = %13
%24 = load i32, i32* @stderr
%25 = tail call i32 (...) @getpid()
%26 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = tail call i32 (...) @pause()
ret i32 0
}