#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*/ O_RDONLY;
scalar_t__ check_openat(int, char *, int /*<<< orphan*/);
int errno;
int openat_(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, char *);
char *strerror(int);
int check_proc(void) {
  int root_dfd = openat_(AT_FDCWD, "/", O_RDONLY);
  int proc_dfd = openat_(AT_FDCWD, "/proc/self", O_RDONLY);
  int fail = 0;
  if (proc_dfd < 0) {
    printf("'/proc/self' unavailable (errno=%d '%s'), skipping\n", errno,
           strerror(errno));
    return 0;
  }
  fail += check_openat(proc_dfd, "root/etc/passwd", O_RDONLY);
  fail += check_openat(root_dfd, "proc/self/root/etc/passwd", O_RDONLY);
  return fail;
}


x86 assembly (clang, -O0):
<check_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%edx
lea    0xe9f(%rip),%rsi
call   1050 <openat_@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
lea    0xe80(%rip),%rsi
call   1050 <openat_@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11d3 <check_proc+0x93>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x14(%rbp),%esi
mov    %rax,%rdx
lea    0xe4b(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1223 <check_proc+0xe3>
mov    -0xc(%rbp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
lea    0xe65(%rip),%rsi
call   1040 <check_openat@plt>
mov    %rax,%rcx
movslq -0x10(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
lea    0xe36(%rip),%rsi
call   1040 <check_openat@plt>
mov    %rax,%rcx
movslq -0x10(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_proc>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e7d(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e94(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0xea2(%rip),%rsi
call   1050 <openat_@plt>
mov    %eax,%r14d
mov    (%rbx),%edi
mov    0x0(%rbp),%edx
lea    0xe90(%rip),%rsi
call   1050 <openat_@plt>
test   %eax,%eax
js     11a5 <check_proc+0x65>
mov    0x0(%rbp),%edx
lea    0xec6(%rip),%rsi
mov    %eax,%edi
call   1040 <check_openat@plt>
mov    %rax,%rbx
mov    0x0(%rbp),%edx
lea    0xea8(%rip),%rsi
mov    %r14d,%edi
call   1040 <check_openat@plt>
add    %ebx,%eax
jmp    11c8 <check_proc+0x88>
mov    0x2e2c(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1060 <strerror@plt>
lea    0xe51(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@AT_FDCWD = global i32 0
@.str = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"/proc/self\00"
@.str.2 = [52 x i8] c"'/proc/self' unavailable (errno=%d '%s'), skipping\0A\00"
@errno = global i32 0
@.str.3 = [16 x i8] c"root/etc/passwd\00"
@.str.4 = [26 x i8] c"proc/self/root/etc/passwd\00"
define i32 @check_proc() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @AT_FDCWD
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @openat_(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* @AT_FDCWD
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @openat_(i32 %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %9)
store i32 %10, i32* %3
store i32 0, i32* %4
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %0
%14 = load i32, i32* @errno
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %14, i8* %16)
store i32 0, i32* %1
br label %34
18: ; preds = %0
%19 = load i32, i32* %3
%20 = load i32, i32* @O_RDONLY
%21 = call i64 @check_openat(i32 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = add nsw i64 %23, %21
%25 = trunc i64 %24 to i32
store i32 %25, i32* %4
%26 = load i32, i32* %2
%27 = load i32, i32* @O_RDONLY
%28 = call i64 @check_openat(i32 %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = add nsw i64 %30, %28
%32 = trunc i64 %31 to i32
store i32 %32, i32* %4
%33 = load i32, i32* %4
store i32 %33, i32* %1
br label %34
34: ; preds = %18, %13
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@AT_FDCWD = global i32 0
@.str = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"/proc/self\00"
@.str.2 = [52 x i8] c"'/proc/self' unavailable (errno=%d '%s'), skipping\0A\00"
@errno = global i32 0
@.str.3 = [16 x i8] c"root/etc/passwd\00"
@.str.4 = [26 x i8] c"proc/self/root/etc/passwd\00"
define i32 @check_proc() {
%1 = load i32, i32* @AT_FDCWD
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @openat_(i32 %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = load i32, i32* @AT_FDCWD
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @openat_(i32 %4, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %9, i8* %10)
br label %19
12: ; preds = %0
%13 = load i32, i32* @O_RDONLY
%14 = tail call i64 @check_openat(i32 %6, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @O_RDONLY
%16 = tail call i64 @check_openat(i32 %3, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %15)
%17 = add i64 %16, %14
%18 = trunc i64 %17 to i32
br label %19
19: ; preds = %12, %8
%20 = phi i32 [ 0, %8 ], [ %18, %12 ]
ret i32 %20
}