#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_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fstat(int, struct stat *);
void *mmap64(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
             int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd = open(argv[0], O_RDONLY);
  if (fd == -1)
    return 0;
  struct stat fs;
  fstat(fd, &fs);
  void *ptr = mmap64(NULL, fs.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (ptr != MAP_FAILED) {
    munmap(ptr, fs.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)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1194 <main+0x44>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <main+0xa8>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <fstat@plt>
mov    -0x18(%rbp),%esi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
mov    0x2e35(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap64@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%rax
je     11e9 <main+0x99>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1050 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    (%rsi),%rdi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11b6 <main+0x66>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
mov    (%rsp),%ebp
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
mov    0x2e60(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap64@plt>
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%rax
je     11af <main+0x5f>
mov    %rax,%rdi
mov    %ebp,%esi
call   1050 <munmap@plt>
mov    %ebx,%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
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 i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 0
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 0, i32* %3
br label %37
17: ; preds = %2
%18 = load i32, i32* %6
%19 = call i32 @fstat(i32 %18, %struct.stat* %7)
%20 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @MAP_SHARED
%24 = load i32, i32* %6
%25 = call i8* @mmap64(i32* null, i32 %21, i32 %22, i32 %23, i32 %24, i32 0)
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = load i8*, i8** @MAP_FAILED
%28 = icmp ne i8* %26, %27
br i1 %28, label %29, label %34
29: ; preds = %17
%30 = load i8*, i8** %8
%31 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @munmap(i8* %30, i32 %32)
br label %34
34: ; preds = %29, %17
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
store i32 0, i32* %3
br label %37
37: ; preds = %34, %16
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = load i8*, i8** %1
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %4, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %22, label %8
8: ; preds = %2
%9 = bitcast %struct.stat* %3 to i8*
%10 = call i32 @fstat(i32 %6, %struct.stat* %3)
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%12 = load i32, i32* %11
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i8* @mmap64(i32* null, i32 %12, i32 %13, i32 %14, i32 %6, i32 0)
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %20, label %18
18: ; preds = %8
%19 = tail call i32 @munmap(i8* %15, i32 %12)
br label %20
20: ; preds = %18, %8
%21 = tail call i32 @close(i32 %6)
br label %22
22: ; preds = %2, %20
ret i32 0
}