#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 size_t ssize_t;
typedef int /*<<< orphan*/ proc_path;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ log_access(char *, int);
size_t readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
void log_access_byfd(int fd, int mode) {
  char proc_path[32];
  char buf[PATH_MAX + 1];
  ssize_t n;
  snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", fd);
  n = readlink(proc_path, buf, sizeof(buf) - 1);
  if (n > 0) {
    buf[n] = '\0';
    log_access(buf, mode);
  }
}


x86 assembly (clang, -O0):
<log_access_byfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2eab(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x20,%esi
lea    0xe7c(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x58(%rbp),%rsi
mov    -0x50(%rbp),%rax
lea    -0x30(%rbp),%rdi
sub    $0x1,%rax
mov    %eax,%edx
call   1040 <readlink@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jbe    11c3 <log_access_byfd+0x93>
mov    -0x58(%rbp),%rdi
mov    -0x48(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
mov    -0x8(%rbp),%esi
call   1030 <log_access@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_access_byfd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r14d
mov    %edi,%ecx
mov    %rsp,%r13
mov    0x2ea0(%rip),%rax
mov    (%rax),%r15d
lea    0x1(%r15),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe91(%rip),%rdx
lea    -0x50(%rbp),%r12
mov    %r12,%rdi
mov    $0x20,%esi
call   1050 <snprintf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <readlink@plt>
test   %rax,%rax
je     11a2 <log_access_byfd+0x72>
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <log_access@plt>
mov    %r13,%rsp
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"
define void @log_access_byfd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [32 x i8]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* @PATH_MAX
%10 = add nsw i32 %9, 1
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%15 = load i32, i32* %3
%16 = call i32 @snprintf(i8* %14, i32 32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%18 = sub i64 %11, 1
%19 = trunc i64 %18 to i32
%20 = call i64 @readlink(i8* %17, i8* %13, i32 %19)
store i64 %20, i64* %8
%21 = load i64, i64* %8
%22 = icmp ugt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %2
%24 = load i64, i64* %8
%25 = getelementptr inbounds i8, i8* %13, i64 %24
store i8 0, i8* %25
%26 = load i32, i32* %4
%27 = call i32 @log_access(i8* %13, i32 %26)
br label %28
28: ; preds = %23, %2
%29 = load i8*, i8** %6
ret void
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
define void @log_access_byfd(i32 %0, i32 %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @PATH_MAX
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = call i32 @snprintf(i8* %4, i32 32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%11 = call i64 @readlink(i8* %4, i8* %9, i32 %5)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = getelementptr inbounds i8, i8* %9, i64 %11
store i8 0, i8* %14
%15 = call i32 @log_access(i8* %9, i32 %1)
br label %16
16: ; preds = %13, %2
ret void
}