#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*/ AT_FDCWD;
int /*<<< orphan*/ AT_SYMLINK_NOFOLLOW;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SYS_faccessat;
int /*<<< orphan*/ X_OK;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ mktemp(char *);
char *strdup(char *);
int symlink(char *, char *);
int syscall(int /*<<< orphan*/, int /*<<< orphan*/, char *, int /*<<< orphan*/,
            int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  int result;
  int status;
  char *path;
  int fd;
  path = strdup("/tmp/proot-test-iiiiiiii-XXXXXX");
  if (path == NULL) {
    result = 125;
    goto end;
  }
  mktemp(path);
  if (path[0] == '\0') {
    result = 125;
    goto end;
  }
  status = symlink("/this_shall_not_exist_outside_proot", path);
  if (status < 0) {
    result = 125;
    goto end;
  }
  status = syscall(SYS_faccessat, AT_FDCWD, path, X_OK, AT_SYMLINK_NOFOLLOW);
  result = (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
end:
  (void)unlink(path);
  exit(result);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1196 <main+0x36>
movl   $0x7d,-0x8(%rbp)
jmp    1242 <main+0xe2>
mov    -0x18(%rbp),%rdi
call   1080 <mktemp@plt>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11bb <main+0x5b>
movl   $0x7d,-0x8(%rbp)
jmp    1242 <main+0xe2>
mov    -0x18(%rbp),%rsi
lea    0xe5a(%rip),%rdi
call   1040 <symlink@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e4 <main+0x84>
movl   $0x7d,-0x8(%rbp)
jmp    1242 <main+0xe2>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
mov    0x2ddf(%rip),%rax
mov    (%rax),%ecx
mov    0x2dbe(%rip),%rax
mov    (%rax),%r8d
call   1050 <syscall@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1230 <main+0xd0>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    123c <main+0xdc>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xe96(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
mov    $0x7d,%ebp
test   %rax,%rax
je     11df <main+0x7f>
mov    %rbx,%rdi
call   1080 <mktemp@plt>
cmpb   $0x0,(%rbx)
je     11df <main+0x7f>
lea    0xe90(%rip),%rdi
mov    %rbx,%rsi
call   1040 <symlink@plt>
test   %eax,%eax
js     11df <main+0x7f>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    0x2e2b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0a(%rip),%rax
mov    (%rax),%r8d
mov    %rbx,%rdx
call   1050 <syscall@plt>
test   %eax,%eax
jne    11d6 <main+0x76>
mov    0x2dec(%rip),%rax
jmp    11dd <main+0x7d>
mov    0x2df3(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %ebp,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"/tmp/proot-test-iiiiiiii-XXXXXX\00"
@.str.1 = [36 x i8] c"/this_shall_not_exist_outside_proot\00"
@SYS_faccessat = global i32 0
@AT_FDCWD = global i32 0
@X_OK = global i32 0
@AT_SYMLINK_NOFOLLOW = global i32 0
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 0, i32* %1
%6 = call i8* @strdup(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 125, i32* %2
br label %40
10: ; preds = %0
%11 = load i8*, i8** %4
%12 = call i32 @mktemp(i8* %11)
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %10
store i32 125, i32* %2
br label %40
19: ; preds = %10
%20 = load i8*, i8** %4
%21 = call i32 @symlink(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %20)
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 125, i32* %2
br label %40
25: ; preds = %19
%26 = load i32, i32* @SYS_faccessat
%27 = load i32, i32* @AT_FDCWD
%28 = load i8*, i8** %4
%29 = load i32, i32* @X_OK
%30 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%31 = call i32 @syscall(i32 %26, i32 %27, i8* %28, i32 %29, i32 %30)
store i32 %31, i32* %3
%32 = load i32, i32* %3
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = load i32, i32* @EXIT_SUCCESS
br label %38
36: ; preds = %25
%37 = load i32, i32* @EXIT_FAILURE
br label %38
38: ; preds = %36, %34
%39 = phi i32 [ %35, %34 ], [ %37, %36 ]
store i32 %39, i32* %2
br label %40
40: ; preds = %38, %24, %18, %9
%41 = load i8*, i8** %4
%42 = call i32 @unlink(i8* %41)
%43 = load i32, i32* %2
%44 = call i32 @exit(i32 %43)
unreachable
}

LLVM IR (-O2):
@.str = [32 x i8] c"/tmp/proot-test-iiiiiiii-XXXXXX\00"
@.str.1 = [36 x i8] c"/this_shall_not_exist_outside_proot\00"
@SYS_faccessat = global i32 0
@AT_FDCWD = global i32 0
@X_OK = global i32 0
@AT_SYMLINK_NOFOLLOW = global i32 0
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = tail call dereferenceable_or_null(32) i8* @strdup(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %20, label %3
3: ; preds = %0
%4 = tail call i32 @mktemp(i8* %1)
%5 = load i8, i8* %1
%6 = icmp eq i8 %5, 0
br i1 %6, label %20, label %7
7: ; preds = %3
%8 = tail call i32 @symlink(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%9 = icmp slt i32 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %7
%11 = load i32, i32* @SYS_faccessat
%12 = load i32, i32* @AT_FDCWD
%13 = load i32, i32* @X_OK
%14 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%15 = tail call i32 @syscall(i32 %11, i32 %12, i8* %1, i32 %13, i32 %14)
%16 = icmp eq i32 %15, 0
%17 = load i32, i32* @EXIT_SUCCESS
%18 = load i32, i32* @EXIT_FAILURE
%19 = select i1 %16, i32 %17, i32 %18
br label %20
20: ; preds = %7, %3, %0, %10
%21 = phi i32 [ %19, %10 ], [ 125, %0 ], [ 125, %3 ], [ 125, %7 ]
%22 = tail call i32 @unlink(i8* %1)
%23 = tail call i32 @exit(i32 %21)
unreachable
}