#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ PR_SET_SECCOMP;
int /*<<< orphan*/ SECCOMP_MODE_STRICT;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ prctl(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ write(int, char const *, scalar_t__);
int main(int argc, char **argv) {
  int output = open("output.txt", O_WRONLY);
  const char *val = "test";
  printf("Calling prctl() to set seccomp strict mode...\n");
  prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
  printf("Writing to an already open file...\n");
  write(output, val, strlen(val) + 1);
  printf("Trying to open file for reading...\n");
  int input = open("output.txt", O_RDONLY);
  printf("You will not see this message--the process will be killed first\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
lea    0xe8d(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe87(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
call   1060 <prctl@plt>
lea    0xe93(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
lea    0xe81(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
lea    0xe09(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
lea    0xe81(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rbx
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
lea    0xeac(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1050 <prctl@plt>
lea    0xeb7(%rip),%rdi
call   1030 <puts@plt>
lea    0xe78(%rip),%rsi
mov    $0x5,%edx
mov    %ebp,%edi
call   1040 <write@plt>
lea    0xebb(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
lea    0xec1(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"output.txt\00"
@O_WRONLY = global i32 0
@.str.1 = [5 x i8] c"test\00"
@.str.2 = [47 x i8] c"Calling prctl() to set seccomp strict mode...\0A\00"
@PR_SET_SECCOMP = global i32 0
@SECCOMP_MODE_STRICT = global i32 0
@.str.3 = [36 x i8] c"Writing to an already open file...\0A\00"
@.str.4 = [36 x i8] c"Trying to open file for reading...\0A\00"
@O_RDONLY = global i32 0
@.str.5 = [65 x i8] c"You will not see this message--the process will be killed first\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = load i32, i32* @O_WRONLY
%9 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %5
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %6
%10 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
%11 = load i32, i32* @PR_SET_SECCOMP
%12 = load i32, i32* @SECCOMP_MODE_STRICT
%13 = call i32 @prctl(i32 %11, i32 %12)
%14 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i8*, i8** %6
%18 = call i64 @strlen(i8* %17)
%19 = add nsw i64 %18, 1
%20 = call i32 @write(i32 %15, i8* %16, i64 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 %23, i32* %7
%24 = call i32 @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"output.txt\00"
@O_WRONLY = global i32 0
@.str.1 = [5 x i8] c"test\00"
@PR_SET_SECCOMP = global i32 0
@SECCOMP_MODE_STRICT = global i32 0
@O_RDONLY = global i32 0
@str = [46 x i8] c"Calling prctl() to set seccomp strict mode...\00"
@str.6 = [35 x i8] c"Writing to an already open file...\00"
@str.7 = [35 x i8] c"Trying to open file for reading...\00"
@str.8 = [64 x i8] c"You will not see this message--the process will be killed first\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%6 = load i32, i32* @PR_SET_SECCOMP
%7 = load i32, i32* @SECCOMP_MODE_STRICT
%8 = tail call i32 @prctl(i32 %6, i32 %7)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.6, i64 0, i64 0))
%10 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 5)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.7, i64 0, i64 0))
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @str.8, i64 0, i64 0))
ret i32 0
}