#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 */
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
scalar_t__ EFAULT;
scalar_t__ ENAMETOOLONG;
scalar_t__ EPERM;
int MAP_ANONYMOUS;
int MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SYS_open;
int /*<<< orphan*/ SYS_socket;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ abort();
scalar_t__ errno;
scalar_t__ mmap(int /*<<< orphan*/ *, int const, int, int, int,
                int /*<<< orphan*/);
scalar_t__ sigfillset(int /*<<< orphan*/ *);
scalar_t__ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *);
int syscall(int /*<<< orphan*/, char *, int /*<<< orphan*/, ...);
int sysconf(int /*<<< orphan*/);
int main() {
  const int page_size = sysconf(_SC_PAGESIZE);
  char *ptr = (char *)mmap(NULL, page_size, PROT_READ | PROT_WRITE,
                           MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  for (int i = 0; i < page_size; ++i)
    ptr[i] = 'x';
  char *fake_name = ptr + page_size - 10;
  if (syscall(SYS_open, fake_name, O_RDONLY) != -1 || errno != ENAMETOOLONG)
    abort();
  if (syscall(SYS_open, NULL, O_RDONLY) != -1 || errno != EFAULT)
    abort();
  if (syscall(SYS_open, "exec", O_RDONLY) != -1 || errno != EPERM)
    abort();
  sigset_t set;
  if (sigfillset(&set))
    abort();
  if (sigprocmask(SIG_BLOCK, &set, NULL))
    abort();
  syscall(SYS_socket, 0, 0, 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    0x2e66(%rip),%rax
mov    (%rax),%edx
mov    0x2e0d(%rip),%rax
or     (%rax),%edx
mov    0x2dec(%rip),%rax
mov    (%rax),%ecx
mov    0x2e23(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11ea <main+0x8a>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x78,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c4 <main+0x64>
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
add    %rcx,%rax
add    $0xfffffffffffffff6,%rax
mov    %rax,-0x20(%rbp)
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1060 <syscall@plt>
cmp    $0xffffffff,%eax
jne    123d <main+0xdd>
mov    0x2d86(%rip),%rax
mov    (%rax),%rax
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%rax
je     1244 <main+0xe4>
mov    $0x0,%al
call   1040 <abort@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
mov    $0x0,%al
call   1060 <syscall@plt>
cmp    $0xffffffff,%eax
jne    1284 <main+0x124>
mov    0x2d3f(%rip),%rax
mov    (%rax),%rax
mov    0x2d7d(%rip),%rcx
cmp    (%rcx),%rax
je     128b <main+0x12b>
mov    $0x0,%al
call   1040 <abort@plt>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
mov    0x2d3d(%rip),%rax
mov    (%rax),%edx
lea    0xd5c(%rip),%rsi
mov    $0x0,%al
call   1060 <syscall@plt>
cmp    $0xffffffff,%eax
jne    12ce <main+0x16e>
mov    0x2cf5(%rip),%rax
mov    (%rax),%rax
mov    0x2cb3(%rip),%rcx
cmp    (%rcx),%rax
je     12d5 <main+0x175>
mov    $0x0,%al
call   1040 <abort@plt>
lea    -0x24(%rbp),%rdi
call   1070 <sigfillset@plt>
cmp    $0x0,%rax
je     12ef <main+0x18f>
mov    $0x0,%al
call   1040 <abort@plt>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
cmp    $0x0,%rax
je     1316 <main+0x1b6>
mov    $0x0,%al
call   1040 <abort@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
mov    %ecx,%edx
mov    $0x0,%al
call   1060 <syscall@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,%ebx
mov    0x2e60(%rip),%rax
mov    0x2e09(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2de6(%rip),%rax
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%r14
test   %ebx,%ebx
jle    11d5 <main+0x65>
mov    %ebx,%edx
mov    %r14,%rdi
mov    $0x78,%esi
call   1060 <memset@plt>
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
add    $0xfffffffffffffff6,%rsi
mov    0x2db1(%rip),%r14
mov    (%r14),%edi
mov    0x2de7(%rip),%rbx
mov    (%rbx),%edx
xor    %eax,%eax
call   1070 <syscall@plt>
cmp    $0xffffffff,%eax
jne    12b5 <main+0x145>
mov    0x2da6(%rip),%r15
mov    (%r15),%rax
mov    0x2d7c(%rip),%rcx
cmp    (%rcx),%rax
jne    12b5 <main+0x145>
mov    (%r14),%edi
mov    (%rbx),%edx
xor    %esi,%esi
xor    %eax,%eax
call   1070 <syscall@plt>
cmp    $0xffffffff,%eax
jne    12b5 <main+0x145>
mov    (%r15),%rax
mov    0x2dba(%rip),%rcx
cmp    (%rcx),%rax
jne    12b5 <main+0x145>
mov    (%r14),%edi
mov    (%rbx),%edx
lea    0xdb1(%rip),%rsi
xor    %eax,%eax
call   1070 <syscall@plt>
cmp    $0xffffffff,%eax
jne    12b5 <main+0x145>
mov    (%r15),%rax
mov    0x2d13(%rip),%rcx
cmp    (%rcx),%rax
jne    12b5 <main+0x145>
lea    0xc(%rsp),%rdi
call   1080 <sigfillset@plt>
test   %rax,%rax
jne    12b5 <main+0x145>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
test   %rax,%rax
jne    12b5 <main+0x145>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1070 <syscall@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@SYS_open = global i32 0
@O_RDONLY = global i32 0
@errno = global i64 0
@ENAMETOOLONG = global i64 0
@EFAULT = global i64 0
@.str = [5 x i8] c"exec\00"
@EPERM = global i64 0
@SIG_BLOCK = global i32 0
@SYS_socket = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @_SC_PAGESIZE
%8 = call i32 @sysconf(i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
%13 = load i32, i32* @MAP_PRIVATE
%14 = load i32, i32* @MAP_ANONYMOUS
%15 = or i32 %13, %14
%16 = call i64 @mmap(i32* null, i32 %9, i32 %12, i32 %15, i32 -1, i32 0)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %3
store i32 0, i32* %4
br label %18
18: ; preds = %27, %0
%19 = load i32, i32* %4
%20 = load i32, i32* %2
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %30
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 120, i8* %26
br label %27
27: ; preds = %22
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %18
30: ; preds = %18
%31 = load i8*, i8** %3
%32 = load i32, i32* %2
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = getelementptr inbounds i8, i8* %34, i64 -10
store i8* %35, i8** %5
%36 = load i32, i32* @SYS_open
%37 = load i8*, i8** %5
%38 = load i32, i32* @O_RDONLY
%39 = call i32 (i32, i8*, i32, ...) @syscall(i32 %36, i8* %37, i32 %38)
%40 = icmp ne i32 %39, -1
br i1 %40, label %45, label %41
41: ; preds = %30
%42 = load i64, i64* @errno
%43 = load i64, i64* @ENAMETOOLONG
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %47
45: ; preds = %41, %30
%46 = call i32 (...) @abort()
unreachable
47: ; preds = %41
%48 = load i32, i32* @SYS_open
%49 = load i32, i32* @O_RDONLY
%50 = call i32 (i32, i8*, i32, ...) @syscall(i32 %48, i8* null, i32 %49)
%51 = icmp ne i32 %50, -1
br i1 %51, label %56, label %52
52: ; preds = %47
%53 = load i64, i64* @errno
%54 = load i64, i64* @EFAULT
%55 = icmp ne i64 %53, %54
br i1 %55, label %56, label %58
56: ; preds = %52, %47
%57 = call i32 (...) @abort()
unreachable
58: ; preds = %52
%59 = load i32, i32* @SYS_open
%60 = load i32, i32* @O_RDONLY
%61 = call i32 (i32, i8*, i32, ...) @syscall(i32 %59, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %60)
%62 = icmp ne i32 %61, -1
br i1 %62, label %67, label %63
63: ; preds = %58
%64 = load i64, i64* @errno
%65 = load i64, i64* @EPERM
%66 = icmp ne i64 %64, %65
br i1 %66, label %67, label %69
67: ; preds = %63, %58
%68 = call i32 (...) @abort()
unreachable
69: ; preds = %63
%70 = call i64 @sigfillset(i32* %6)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %69
%73 = call i32 (...) @abort()
unreachable
74: ; preds = %69
%75 = load i32, i32* @SIG_BLOCK
%76 = call i64 @sigprocmask(i32 %75, i32* %6, i32* null)
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %74
%79 = call i32 (...) @abort()
unreachable
80: ; preds = %74
%81 = load i32, i32* @SYS_socket
%82 = call i32 (i32, i8*, i32, ...) @syscall(i32 %81, i8* null, i32 0, i32 0)
ret i32 0
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@SYS_open = global i32 0
@O_RDONLY = global i32 0
@errno = global i64 0
@ENAMETOOLONG = global i64 0
@EFAULT = global i64 0
@.str = [5 x i8] c"exec\00"
@EPERM = global i64 0
@SIG_BLOCK = global i32 0
@SYS_socket = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = load i32, i32* @_SC_PAGESIZE
%3 = tail call i32 @sysconf(i32 %2)
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %5, %4
%7 = load i32, i32* @MAP_PRIVATE
%8 = load i32, i32* @MAP_ANONYMOUS
%9 = or i32 %8, %7
%10 = tail call i64 @mmap(i32* null, i32 %3, i32 %6, i32 %9, i32 -1, i32 0)
%11 = inttoptr i64 %10 to i8*
%12 = icmp sgt i32 %3, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = zext i32 %3 to i64
br label %15
15: ; preds = %13, %0
%16 = sext i32 %3 to i64
%17 = getelementptr inbounds i8, i8* %11, i64 %16
%18 = getelementptr inbounds i8, i8* %17, i64 -10
%19 = load i32, i32* @SYS_open
%20 = load i32, i32* @O_RDONLY
%21 = tail call i32 (i32, i8*, i32, ...) @syscall(i32 %19, i8* %18, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %15
%24 = load i64, i64* @errno
%25 = load i64, i64* @ENAMETOOLONG
%26 = icmp eq i64 %24, %25
br i1 %26, label %29, label %27
27: ; preds = %23, %15
%28 = tail call i32 (...) @abort()
unreachable
29: ; preds = %23
%30 = load i32, i32* @SYS_open
%31 = load i32, i32* @O_RDONLY
%32 = tail call i32 (i32, i8*, i32, ...) @syscall(i32 %30, i8* null, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i64, i64* @errno
%36 = load i64, i64* @EFAULT
%37 = icmp eq i64 %35, %36
br i1 %37, label %40, label %38
38: ; preds = %34, %29
%39 = tail call i32 (...) @abort()
unreachable
40: ; preds = %34
%41 = load i32, i32* @SYS_open
%42 = load i32, i32* @O_RDONLY
%43 = tail call i32 (i32, i8*, i32, ...) @syscall(i32 %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %49
45: ; preds = %40
%46 = load i64, i64* @errno
%47 = load i64, i64* @EPERM
%48 = icmp eq i64 %46, %47
br i1 %48, label %51, label %49
49: ; preds = %45, %40
%50 = tail call i32 (...) @abort()
unreachable
51: ; preds = %45
%52 = bitcast i32* %1 to i8*
%53 = call i64 @sigfillset(i32* %1)
%54 = icmp eq i64 %53, 0
br i1 %54, label %57, label %55
55: ; preds = %51
%56 = call i32 (...) @abort()
unreachable
57: ; preds = %51
%58 = load i32, i32* @SIG_BLOCK
%59 = call i64 @sigprocmask(i32 %58, i32* %1, i32* null)
%60 = icmp eq i64 %59, 0
br i1 %60, label %63, label %61
61: ; preds = %57
%62 = call i32 (...) @abort()
unreachable
63: ; preds = %57
%64 = load i32, i32* @SYS_socket
%65 = call i32 (i32, i8*, i32, ...) @syscall(i32 %64, i8* null, i32 0, i32 0)
ret i32 0
}