#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*/ va_list;
typedef int ssize_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ ENAMETOOLONG;
int PATH_MAX;
int /*<<< orphan*/ errno;
int intercepted_open(char const *, int, int /*<<< orphan*/);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int intercepted_openat(int dirfd, const char *pathname, int flags,
                       va_list args) {
  char procfile[128];
  char buffer[PATH_MAX];
  ssize_t n;
  if (pathname[0] == '/') {
    return intercepted_open(pathname, flags, args);
  }
  sprintf(procfile, "/proc/self/fd/%d", dirfd);
  n = readlink(procfile, buffer, sizeof(buffer));
  if (n < 0) {
    return n;
  }
  if (n + strlen(pathname) + 2 >= sizeof(buffer)) {
    errno = ENAMETOOLONG;
    return -1;
  }
  buffer[n] = '/';
  strcpy(buffer + n + 1, pathname);
  return intercepted_open(buffer, flags, args);
}


x86 assembly (clang, -O0):
<intercepted_openat>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0xc8(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0xa8(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xc0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0xb0(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11d5 <intercepted_openat+0x85>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1060 <intercepted_open@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0xb8(%rbp)
jmp    12d6 <intercepted_openat+0x186>
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe1a(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0xc0(%rbp),%rsi
mov    -0xc8(%rbp),%rax
lea    -0xa0(%rbp),%rdi
mov    %eax,%edx
call   1040 <readlink@plt>
mov    %eax,-0xb4(%rbp)
cmpl   $0x0,-0xb4(%rbp)
jge    1232 <intercepted_openat+0xe2>
mov    -0xb4(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0xb8(%rbp)
jmp    12d6 <intercepted_openat+0x186>
mov    -0xb4(%rbp),%eax
mov    %eax,-0xcc(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xc8(%rbp),%rcx
mov    %eax,%edx
mov    -0xcc(%rbp),%eax
add    %edx,%eax
add    $0x2,%eax
cltq
cmp    %rcx,%rax
jb     128e <intercepted_openat+0x13e>
mov    0x2d63(%rip),%rax
mov    (%rax),%ecx
mov    0x2d62(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0xb8(%rbp)
jmp    12d6 <intercepted_openat+0x186>
mov    -0xc0(%rbp),%rdi
movslq -0xb4(%rbp),%rax
movb   $0x2f,(%rdi,%rax,1)
movslq -0xb4(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0xc0(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1060 <intercepted_open@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0xb8(%rbp)
mov    -0xa8(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<intercepted_openat>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rsp,%r8
mov    0x2e7c(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmpb   $0x2f,(%rsi)
jne    11a8 <intercepted_openat+0x58>
mov    %r12,%rdi
mov    %r15d,%esi
mov    %ecx,%edx
mov    %r8,%rbx
call   1060 <intercepted_open@plt>
mov    %rbx,%rcx
mov    %eax,%r14d
jmp    1237 <intercepted_openat+0xe7>
mov    %edi,%edx
mov    %ecx,-0x34(%rbp)
mov    %r8,-0x30(%rbp)
lea    0xe48(%rip),%rsi
lea    -0xc0(%rbp),%r14
mov    %r14,%rdi
call   1070 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1040 <readlink@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1233 <intercepted_openat+0xe3>
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %r14d,%eax
add    $0x2,%eax
cltq
cmp    %r13,%rax
jae    121b <intercepted_openat+0xcb>
mov    %r14d,%eax
movb   $0x2f,(%rbx,%rax,1)
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    -0x34(%rbp),%edx
call   1060 <intercepted_open@plt>
mov    %eax,%r14d
jmp    1233 <intercepted_openat+0xe3>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    0x2dad(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r14d
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@ENAMETOOLONG = global i32 0
@errno = global i32 0
define i32 @intercepted_openat(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [128 x i8]
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %11
%18 = alloca i8, i64 %16
store i64 %16, i64* %12
%19 = load i8*, i8** %7
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 47
br i1 %23, label %24, label %29
24: ; preds = %4
%25 = load i8*, i8** %7
%26 = load i32, i32* %8
%27 = load i32, i32* %9
%28 = call i32 @intercepted_open(i8* %25, i32 %26, i32 %27)
store i32 %28, i32* %5
store i32 1, i32* %14
br label %63
29: ; preds = %4
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%31 = load i32, i32* %6
%32 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%34 = trunc i64 %16 to i32
%35 = call i32 @readlink(i8* %33, i8* %18, i32 %34)
store i32 %35, i32* %13
%36 = load i32, i32* %13
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %29
%39 = load i32, i32* %13
store i32 %39, i32* %5
store i32 1, i32* %14
br label %63
40: ; preds = %29
%41 = load i32, i32* %13
%42 = load i8*, i8** %7
%43 = call i32 @strlen(i8* %42)
%44 = add nsw i32 %41, %43
%45 = add nsw i32 %44, 2
%46 = sext i32 %45 to i64
%47 = icmp uge i64 %46, %16
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = load i32, i32* @ENAMETOOLONG
store i32 %49, i32* @errno
store i32 -1, i32* %5
store i32 1, i32* %14
br label %63
50: ; preds = %40
%51 = load i32, i32* %13
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %18, i64 %52
store i8 47, i8* %53
%54 = load i32, i32* %13
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %18, i64 %55
%57 = getelementptr inbounds i8, i8* %56, i64 1
%58 = load i8*, i8** %7
%59 = call i32 @strcpy(i8* %57, i8* %58)
%60 = load i32, i32* %8
%61 = load i32, i32* %9
%62 = call i32 @intercepted_open(i8* %18, i32 %60, i32 %61)
store i32 %62, i32* %5
store i32 1, i32* %14
br label %63
63: ; preds = %50, %48, %38, %24
%64 = load i8*, i8** %11
%65 = load i32, i32* %5
ret i32 %65
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@ENAMETOOLONG = global i32 0
@errno = global i32 0
define i32 @intercepted_openat(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca [128 x i8]
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = load i8, i8* %1
%12 = icmp eq i8 %11, 47
br i1 %12, label %13, label %15
13: ; preds = %4
%14 = tail call i32 @intercepted_open(i8* %1, i32 %2, i32 %3)
br label %33
15: ; preds = %4
%16 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%17 = call i32 @readlink(i8* %6, i8* %10, i32 %7)
%18 = icmp slt i32 %17, 0
br i1 %18, label %33, label %19
19: ; preds = %15
%20 = tail call i32 @strlen(i8* %1)
%21 = add nuw i32 %17, 2
%22 = add i32 %21, %20
%23 = sext i32 %22 to i64
%24 = icmp ult i64 %23, %8
br i1 %24, label %27, label %25
25: ; preds = %19
%26 = load i32, i32* @ENAMETOOLONG
store i32 %26, i32* @errno
br label %33
27: ; preds = %19
%28 = zext i32 %17 to i64
%29 = getelementptr inbounds i8, i8* %10, i64 %28
store i8 47, i8* %29
%30 = getelementptr inbounds i8, i8* %29, i64 1
%31 = call i32 @strcpy(i8* %30, i8* %1)
%32 = call i32 @intercepted_open(i8* %10, i32 %2, i32 %3)
br label %33
33: ; preds = %15, %27, %25, %13
%34 = phi i32 [ %14, %13 ], [ -1, %25 ], [ %32, %27 ], [ %17, %15 ]
ret i32 %34
}