#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 mode_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SOCK_STREAM;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void test_sandbox() {
  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  char *file1 = "file1";
  char *file2 = "file2";
  int fd = open(file2, O_WRONLY | O_CREAT | O_TRUNC, mode);
  if (fd == -1) {
    fprintf(stderr, "process can't open files\n");
  } else {
    fprintf(stderr, "process can open files\n");
    close(fd);
  }
  int tmp = fork();
  if (tmp == -1) {
    fprintf(stderr, "process can't fork\n");
  }
  if (tmp == 0) {
    _exit(0);
  }
  if (tmp > 0) {
    fprintf(stderr, "process can fork \n");
  }
  int listenfd = socket(AF_INET, SOCK_STREAM, 0);
  if (listenfd == -1) {
    fprintf(stderr, "process can't open socket\n");
  } else {
    fprintf(stderr, "process can open socket\n");
    close(listenfd);
  }
  fprintf(stderr, "\n\n");
}


x86 assembly (clang, -O0):
<test_sandbox>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    0x2e58(%rip),%rcx
or     (%rcx),%eax
mov    0x2e2f(%rip),%rcx
or     (%rcx),%eax
mov    0x2e1e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x4(%rbp)
lea    0xe6a(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe65(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    0x2e3f(%rip),%rax
or     (%rax),%esi
mov    0x2e2e(%rip),%rax
or     (%rax),%esi
mov    -0x4(%rbp),%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f3 <test_sandbox+0x93>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1210 <test_sandbox+0xb0>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1239 <test_sandbox+0xd9>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
call   1050 <fprintf@plt>
cmpl   $0x0,-0x20(%rbp)
jne    124a <test_sandbox+0xea>
xor    %edi,%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x20(%rbp)
jle    1269 <test_sandbox+0x109>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
mov    0x2d3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12a9 <test_sandbox+0x149>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12c6 <test_sandbox+0x166>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
call   1050 <fprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_sandbox>:
push   %r14
push   %rbx
push   %rax
mov    0x2e35(%rip),%rax
mov    0x2e5e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e33(%rip),%rax
or     (%rax),%edx
mov    0x2e22(%rip),%rax
or     (%rax),%edx
mov    0x2e01(%rip),%rax
mov    0x2e62(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4f(%rip),%rax
or     (%rax),%esi
lea    0xe56(%rip),%rdi
call   1060 <open@plt>
mov    0x2e0a(%rip),%r14
mov    (%r14),%edi
cmp    $0xffffffff,%eax
je     11d5 <test_sandbox+0x75>
mov    %eax,%ebx
lea    0xe59(%rip),%rsi
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11e1 <test_sandbox+0x81>
lea    0xe2a(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
cmp    $0xffffffff,%eax
je     11fc <test_sandbox+0x9c>
test   %eax,%eax
je     1265 <test_sandbox+0x105>
jle    120b <test_sandbox+0xab>
lea    0xe52(%rip),%rsi
jmp    1203 <test_sandbox+0xa3>
lea    0xe35(%rip),%rsi
mov    (%r14),%edi
call   1050 <fprintf@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    (%r14),%edi
cmp    $0xffffffff,%eax
je     1243 <test_sandbox+0xe3>
mov    %eax,%ebx
lea    0xe45(%rip),%rsi
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    124f <test_sandbox+0xef>
lea    0xe15(%rip),%rsi
call   1050 <fprintf@plt>
mov    (%r14),%edi
lea    0xe3a(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <fprintf@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [6 x i8] c"file1\00"
@.str.1 = [6 x i8] c"file2\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str.2 = [26 x i8] c"process can't open files\0A\00"
@.str.3 = [24 x i8] c"process can open files\0A\00"
@.str.4 = [20 x i8] c"process can't fork\0A\00"
@.str.5 = [19 x i8] c"process can fork \0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.6 = [27 x i8] c"process can't open socket\0A\00"
@.str.7 = [25 x i8] c"process can open socket\0A\00"
@.str.8 = [3 x i8] c"\0A\0A\00"
define void @test_sandbox() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IROTH
%13 = or i32 %11, %12
store i32 %13, i32* %1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %3
%14 = load i8*, i8** %3
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = load i32, i32* %1
%21 = call i32 @open(i8* %14, i32 %19, i32 %20)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %0
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %32
27: ; preds = %0
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %27, %24
%33 = call i32 (...) @fork()
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %36, %32
%40 = load i32, i32* %5
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @_exit(i32 0)
unreachable
44: ; preds = %39
%45 = load i32, i32* %5
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %50
50: ; preds = %47, %44
%51 = load i32, i32* @AF_INET
%52 = load i32, i32* @SOCK_STREAM
%53 = call i32 @socket(i32 %51, i32 %52, i32 0)
store i32 %53, i32* %6
%54 = load i32, i32* %6
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %59
56: ; preds = %50
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %64
59: ; preds = %50
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %59, %56
%65 = load i32, i32* @stderr
%66 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [6 x i8] c"file2\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str.2 = [26 x i8] c"process can't open files\0A\00"
@.str.3 = [24 x i8] c"process can open files\0A\00"
@.str.4 = [20 x i8] c"process can't fork\0A\00"
@.str.5 = [19 x i8] c"process can fork \0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.6 = [27 x i8] c"process can't open socket\0A\00"
@.str.7 = [25 x i8] c"process can open socket\0A\00"
@.str.8 = [3 x i8] c"\0A\0A\00"
define void @test_sandbox() {
%1 = load i32, i32* @S_IRUSR
%2 = load i32, i32* @S_IWUSR
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRGRP
%5 = or i32 %3, %4
%6 = load i32, i32* @S_IROTH
%7 = or i32 %5, %6
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %7)
%14 = icmp eq i32 %13, -1
%15 = load i32, i32* @stderr
br i1 %14, label %16, label %18
16: ; preds = %0
%17 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %21
18: ; preds = %0
%19 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @close(i32 %13)
br label %21
21: ; preds = %18, %16
%22 = tail call i32 (...) @fork()
switch i32 %22, label %25 [
i32 -1, label %27
i32 0, label %23
]
23: ; preds = %21
%24 = tail call i32 @_exit(i32 0)
unreachable
25: ; preds = %21
%26 = icmp sgt i32 %22, 0
br i1 %26, label %27, label %31
27: ; preds = %25, %21
%28 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), %21 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), %25 ]
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* %28)
br label %31
31: ; preds = %27, %25
%32 = load i32, i32* @AF_INET
%33 = load i32, i32* @SOCK_STREAM
%34 = tail call i32 @socket(i32 %32, i32 %33, i32 0)
%35 = icmp eq i32 %34, -1
%36 = load i32, i32* @stderr
br i1 %35, label %37, label %39
37: ; preds = %31
%38 = tail call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %42
39: ; preds = %31
%40 = tail call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
%41 = tail call i32 @close(i32 %34)
br label %42
42: ; preds = %39, %37
%43 = load i32, i32* @stderr
%44 = tail call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
ret void
}