#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 /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
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(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
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;
  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 = sb.st_size;
  if ((size & 8191) == 0)
    size--;
  a = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
  if (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    0x2e24(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
lea    0xe3b(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e6 <main+0x56>
lea    0xe4f(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
jge    120f <main+0x7f>
lea    0xe2b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x1fff,%eax
cmp    $0x0,%eax
jne    122f <main+0x9f>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
mov    0x2da6(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
add    $0x1,%rcx
call   1070 <memmem@plt>
cmp    $0x0,%rax
jne    129f <main+0x10f>
mov    $0x0,%al
call   1030 <abort@plt>
lea    0xd8e(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    (%rsi),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
jne    11a2 <main+0x22>
lea    0xe89(%rip),%rdi
jmp    11ba <main+0x3a>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
test   %rax,%rax
jns    11c6 <main+0x46>
lea    0xe74(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    (%rsp),%ebp
mov    %ebp,%eax
and    $0x1fff,%eax
cmp    $0x1,%eax
sbb    $0x0,%ebp
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    0x2e02(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
lea    0xe02(%rip),%rdx
mov    $0x29,%ecx
mov    %rax,%rdi
mov    %ebp,%esi
call   1060 <memmem@plt>
test   %rax,%rax
jne    1219 <main+0x99>
xor    %eax,%eax
call   1030 <abort@plt>
lea    0xe14(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1090 <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
@MAP_SHARED = 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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %6
store i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8** %10
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 (...) @abort()
unreachable
21: ; preds = %2
%22 = load i32, i32* %6
%23 = call i64 @fstat(i32 %22, %struct.stat* %7)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 (...) @abort()
unreachable
28: ; preds = %21
%29 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%30 = load i32, i32* %29
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = and i32 %31, 8191
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = load i32, i32* %8
%36 = add nsw i32 %35, -1
store i32 %36, i32* %8
br label %37
37: ; preds = %34, %28
%38 = load i32, i32* %8
%39 = load i32, i32* @PROT_READ
%40 = load i32, i32* @MAP_SHARED
%41 = load i32, i32* %6
%42 = call i8* @mmap(i32* null, i32 %38, i32 %39, i32 %40, i32 %41, i32 0)
store i8* %42, i8** %9
%43 = load i8*, i8** %9
%44 = load i32, i32* %8
%45 = load i8*, i8** %10
%46 = load i8*, i8** %10
%47 = call i64 @strlen(i8* %46)
%48 = add nsw i64 %47, 1
%49 = call i32* @memmem(i8* %43, i32 %44, i8* %45, i64 %48)
%50 = icmp eq i32* %49, null
br i1 %50, label %51, label %53
51: ; preds = %37
%52 = call i32 (...) @abort()
unreachable
53: ; preds = %37
%54 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%55 = 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
@MAP_SHARED = 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 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%20 = load i32, i32* %19
%21 = and i32 %20, 8191
%22 = icmp eq i32 %21, 0
%23 = sext i1 %22 to i32
%24 = add nsw i32 %20, %23
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @MAP_SHARED
%27 = tail call i8* @mmap(i32* null, i32 %24, i32 %25, i32 %26, i32 %6, i32 0)
%28 = tail call i32* @memmem(i8* %27, i32 %24, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i64 41)
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = tail call i32 (...) @abort()
unreachable
32: ; preds = %18
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%34 = tail call i32 @exit(i32 0)
unreachable
}