#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 st_size;
};

/* Variables and functions */
int MAP_ANONYMOUS;
int MAP_DENYWRITE;
void *MAP_FAILED;
int MAP_FIXED;
int MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ *memmem(void *, int, char const *, scalar_t__);
void *mmap(void *, int, int, int, int, int /*<<< orphan*/);
scalar_t__ munmap(void *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ strlen(char const *);
int main(int argc, char *argv[]) {
  int fd = open(argv[0], O_RDONLY);
  struct stat sb;
  int size;
  void *a;
  void *b;
  const char *str = "a string you'll only find in the program";
  if (fd == -1) {
    perror("open");
    abort();
  }
  if (fstat(fd, &sb) < 0) {
    perror("fstat");
    abort();
  }
  size = 8192;
  b = mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
           MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (b == MAP_FAILED)
    abort();
  if (munmap(b, size) != 0)
    abort();
  size = sb.st_size;
  a = mmap(b, size, PROT_READ | PROT_EXEC,
           MAP_DENYWRITE | MAP_FIXED | MAP_PRIVATE, fd, 0);
  if ((a == MAP_FAILED || memmem(a, size, str, strlen(str) + 1) == NULL))
    abort();
  printf("pass\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
lea    0xe2b(%rip),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f6 <main+0x56>
lea    0xe3f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10c0 <fstat@plt>
cmp    $0x0,%rax
jge    121f <main+0x7f>
lea    0xe1b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
movl   $0x2000,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
mov    0x2dc8(%rip),%rax
mov    (%rax),%edx
mov    0x2d7f(%rip),%rax
or     (%rax),%edx
mov    0x2d86(%rip),%rax
or     (%rax),%edx
mov    0x2d65(%rip),%rax
mov    (%rax),%ecx
mov    0x2d7c(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d29(%rip),%rcx
cmp    (%rcx),%rax
jne    1287 <main+0xe7>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1080 <munmap@plt>
cmp    $0x0,%rax
je     12a4 <main+0x104>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2d40(%rip),%rax
mov    (%rax),%edx
mov    0x2d07(%rip),%rax
or     (%rax),%edx
mov    0x2d0e(%rip),%rax
mov    (%rax),%ecx
mov    0x2cd5(%rip),%rax
or     (%rax),%ecx
mov    0x2cd4(%rip),%rax
or     (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2ca7(%rip),%rcx
cmp    (%rcx),%rax
je     1342 <main+0x1a2>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
add    $0x1,%rcx
call   1070 <memmem@plt>
cmp    $0x0,%rax
jne    1349 <main+0x1a9>
mov    $0x0,%al
call   1030 <abort@plt>
lea    0xce4(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    (%rsi),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    11ba <main+0x2a>
lea    0xe71(%rip),%rdi
jmp    11d2 <main+0x42>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
test   %rax,%rax
jns    11de <main+0x4e>
lea    0xe5c(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    0x2e13(%rip),%r15
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
or     (%r15),%edx
mov    0x2dd0(%rip),%r13
or     0x0(%r13),%edx
mov    0x2dad(%rip),%r12
mov    0x2dc6(%rip),%rax
mov    (%rax),%ecx
or     (%r12),%ecx
xor    %edi,%edi
mov    $0x2000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    0x2d74(%rip),%rbp
cmp    0x0(%rbp),%rax
je     129b <main+0x10b>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x2000,%esi
call   1070 <munmap@plt>
test   %rax,%rax
jne    129b <main+0x10b>
mov    0x0(%r13),%edx
or     (%r15),%edx
mov    (%rsp),%r15d
mov    0x2d7f(%rip),%rax
mov    0x2d48(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     (%r12),%ecx
mov    %r14,%rdi
mov    %r15d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    0x0(%rbp),%rax
je     129b <main+0x10b>
lea    0xd7a(%rip),%rdx
mov    $0x29,%ecx
mov    %rax,%rdi
mov    %r15d,%esi
call   1060 <memmem@plt>
test   %rax,%rax
jne    12a2 <main+0x112>
xor    %eax,%eax
call   1030 <abort@plt>
lea    0xd8b(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [41 x i8] c"a string you'll only find in the program\00"
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@MAP_DENYWRITE = global i32 0
@MAP_FIXED = global i32 0
@.str.3 = [6 x i8] c"pass\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 i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %6
store i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8** %11
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 (...) @abort()
unreachable
22: ; preds = %2
%23 = load i32, i32* %6
%24 = call i64 @fstat(i32 %23, %struct.stat* %7)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 (...) @abort()
unreachable
29: ; preds = %22
store i32 8192, i32* %8
%30 = load i32, i32* %8
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @PROT_WRITE
%33 = or i32 %31, %32
%34 = load i32, i32* @PROT_EXEC
%35 = or i32 %33, %34
%36 = load i32, i32* @MAP_PRIVATE
%37 = load i32, i32* @MAP_ANONYMOUS
%38 = or i32 %36, %37
%39 = call i8* @mmap(i8* null, i32 %30, i32 %35, i32 %38, i32 -1, i32 0)
store i8* %39, i8** %10
%40 = load i8*, i8** %10
%41 = load i8*, i8** @MAP_FAILED
%42 = icmp eq i8* %40, %41
br i1 %42, label %43, label %45
43: ; preds = %29
%44 = call i32 (...) @abort()
unreachable
45: ; preds = %29
%46 = load i8*, i8** %10
%47 = load i32, i32* %8
%48 = call i64 @munmap(i8* %46, i32 %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 (...) @abort()
unreachable
52: ; preds = %45
%53 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%54 = load i32, i32* %53
store i32 %54, i32* %8
%55 = load i8*, i8** %10
%56 = load i32, i32* %8
%57 = load i32, i32* @PROT_READ
%58 = load i32, i32* @PROT_EXEC
%59 = or i32 %57, %58
%60 = load i32, i32* @MAP_DENYWRITE
%61 = load i32, i32* @MAP_FIXED
%62 = or i32 %60, %61
%63 = load i32, i32* @MAP_PRIVATE
%64 = or i32 %62, %63
%65 = load i32, i32* %6
%66 = call i8* @mmap(i8* %55, i32 %56, i32 %59, i32 %64, i32 %65, i32 0)
store i8* %66, i8** %9
%67 = load i8*, i8** %9
%68 = load i8*, i8** @MAP_FAILED
%69 = icmp eq i8* %67, %68
br i1 %69, label %79, label %70
70: ; preds = %52
%71 = load i8*, i8** %9
%72 = load i32, i32* %8
%73 = load i8*, i8** %11
%74 = load i8*, i8** %11
%75 = call i64 @strlen(i8* %74)
%76 = add nsw i64 %75, 1
%77 = call i32* @memmem(i8* %71, i32 %72, i8* %73, i64 %76)
%78 = icmp eq i32* %77, null
br i1 %78, label %79, label %81
79: ; preds = %70, %52
%80 = call i32 (...) @abort()
unreachable
81: ; preds = %70
%82 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%83 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [41 x i8] c"a string you'll only find in the program\00"
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@MAP_DENYWRITE = global i32 0
@MAP_FIXED = global i32 0
@str = [5 x i8] c"pass\00"
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 = bitcast %struct.stat* %3 to i8*
%8 = icmp eq i32 %6, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 (...) @abort()
unreachable
12: ; preds = %2
%13 = call i64 @fstat(i32 %6, %struct.stat* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 (...) @abort()
unreachable
18: ; preds = %12
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %20, %19
%22 = load i32, i32* @PROT_EXEC
%23 = or i32 %21, %22
%24 = load i32, i32* @MAP_PRIVATE
%25 = load i32, i32* @MAP_ANONYMOUS
%26 = or i32 %25, %24
%27 = tail call i8* @mmap(i8* null, i32 8192, i32 %23, i32 %26, i32 -1, i32 0)
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = tail call i32 (...) @abort()
unreachable
32: ; preds = %18
%33 = tail call i64 @munmap(i8* %27, i32 8192)
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = tail call i32 (...) @abort()
unreachable
37: ; preds = %32
%38 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%39 = load i32, i32* %38
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @PROT_EXEC
%42 = or i32 %41, %40
%43 = load i32, i32* @MAP_DENYWRITE
%44 = load i32, i32* @MAP_FIXED
%45 = or i32 %44, %43
%46 = load i32, i32* @MAP_PRIVATE
%47 = or i32 %45, %46
%48 = tail call i8* @mmap(i8* %27, i32 %39, i32 %42, i32 %47, i32 %6, i32 0)
%49 = load i8*, i8** @MAP_FAILED
%50 = icmp eq i8* %48, %49
br i1 %50, label %54, label %51
51: ; preds = %37
%52 = tail call i32* @memmem(i8* %48, i32 %39, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i64 41)
%53 = icmp eq i32* %52, null
br i1 %53, label %54, label %56
54: ; preds = %51, %37
%55 = tail call i32 (...) @abort()
unreachable
56: ; preds = %51
%57 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%58 = tail call i32 @exit(i32 0)
unreachable
}