#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 */
struct stat {
  int /*<<< orphan*/ st_dev;
  int /*<<< orphan*/ st_ino;
};
typedef int ssize_t;
typedef int pid_t;
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int PATH_MAX;
scalar_t__ fstat(int const, struct stat *);
int major(int /*<<< orphan*/);
int minor(int /*<<< orphan*/);
int readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int const, ...);
char *strdup(char *);
char *fnotify_get_filename(const pid_t pid, const int fd) {
  char buf[256];
  char path[PATH_MAX];
  ssize_t len;
  char *filename;
  if (pid == -1)
    snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
  else
    snprintf(buf, sizeof(buf), "/proc/%d/fd/%d", pid, fd);
  len = readlink(buf, path, sizeof(path));
  if (len < 0) {
    struct stat statbuf;
    if (fstat(fd, &statbuf) < 0)
      filename = strdup("(unknown)");
    else {
      snprintf(buf, sizeof(buf), "dev: %i:%i inode %ld", major(statbuf.st_dev),
               minor(statbuf.st_dev), statbuf.st_ino);
      filename = strdup(buf);
    }
  } else {
    path[len >= PATH_MAX ? PATH_MAX - 1 : len] = '\0';
    filename = strdup(path);
  }
  return filename;
}


x86 assembly (clang, -O0):
<fnotify_get_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x148(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x118(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x140(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x120(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11d9 <fnotify_get_filename+0x79>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x100,%esi
lea    0xe33(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    11fa <fnotify_get_filename+0x9a>
lea    -0x110(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    -0x8(%rbp),%r8d
mov    $0x100,%esi
lea    0xe1e(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x140(%rbp),%rsi
mov    -0x148(%rbp),%rax
lea    -0x110(%rbp),%rdi
mov    %eax,%edx
call   1030 <readlink@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    12c6 <fnotify_get_filename+0x166>
mov    -0x8(%rbp),%edi
lea    -0x138(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
jge    125a <fnotify_get_filename+0xfa>
lea    0xdd7(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x130(%rbp)
jmp    12c1 <fnotify_get_filename+0x161>
lea    -0x110(%rbp),%rax
mov    %rax,-0x158(%rbp)
mov    -0x138(%rbp),%edi
call   1060 <major@plt>
mov    %eax,-0x14c(%rbp)
mov    -0x138(%rbp),%edi
call   1040 <minor@plt>
mov    -0x158(%rbp),%rdi
mov    -0x14c(%rbp),%ecx
mov    %eax,%r8d
mov    -0x134(%rbp),%r9d
mov    $0x100,%esi
lea    0xd83(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x130(%rbp)
jmp    131d <fnotify_get_filename+0x1bd>
mov    -0x124(%rbp),%eax
mov    0x2d1d(%rip),%rcx
cmp    (%rcx),%eax
jl     12f2 <fnotify_get_filename+0x192>
mov    0x2d0e(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
mov    %eax,-0x15c(%rbp)
jmp    12fe <fnotify_get_filename+0x19e>
mov    -0x124(%rbp),%eax
mov    %eax,-0x15c(%rbp)
mov    -0x140(%rbp),%rdi
mov    -0x15c(%rbp),%eax
cltq
movb   $0x0,(%rdi,%rax,1)
call   1070 <strdup@plt>
mov    %rax,-0x130(%rbp)
mov    -0x130(%rbp),%rax
mov    -0x118(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fnotify_get_filename>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %esi,%r14d
mov    %rsp,%r12
mov    0x2e6f(%rip),%r13
mov    0x0(%r13),%r15d
mov    %rsp,%rbx
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0xffffffff,%edi
je     11bf <fnotify_get_filename+0x5f>
mov    %edi,%ecx
lea    0xe6a(%rip),%rdx
lea    -0x130(%rbp),%rdi
mov    $0x100,%esi
mov    %r14d,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
jmp    11dc <fnotify_get_filename+0x7c>
lea    0xe3a(%rip),%rdx
lea    -0x130(%rbp),%rdi
mov    $0x100,%esi
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
lea    -0x130(%rbp),%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <readlink@plt>
test   %eax,%eax
js     1207 <fnotify_get_filename+0xa7>
mov    0x0(%r13),%ecx
lea    -0x1(%rcx),%edx
cmp    %ecx,%eax
cmovl  %eax,%edx
movslq %edx,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    1253 <fnotify_get_filename+0xf3>
lea    -0x30(%rbp),%rsi
mov    %r14d,%edi
call   1080 <fstat@plt>
test   %rax,%rax
js     126d <fnotify_get_filename+0x10d>
mov    -0x30(%rbp),%ebx
mov    %ebx,%edi
call   1060 <major@plt>
mov    %eax,%r14d
mov    %ebx,%edi
call   1040 <minor@plt>
mov    -0x2c(%rbp),%r9d
lea    0xdf3(%rip),%rdx
lea    -0x130(%rbp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r14d,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1070 <strdup@plt>
mov    %r12,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdac(%rip),%rdi
jmp    1256 <fnotify_get_filename+0xf6>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@PATH_MAX = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [15 x i8] c"/proc/%d/fd/%d\00"
@.str.2 = [10 x i8] c"(unknown)\00"
@.str.3 = [21 x i8] c"dev: %i:%i inode %ld\00"
define i8* @fnotify_get_filename(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = alloca %struct.stat
store i32 %0, i32* %3
store i32 %1, i32* %4
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%19 = load i32, i32* %4
%20 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %18, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %26
21: ; preds = %2
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%23 = load i32, i32* %3
%24 = load i32, i32* %4
%25 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %22, i32 256, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %24)
br label %26
26: ; preds = %21, %17
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%28 = trunc i64 %12 to i32
%29 = call i32 @readlink(i8* %27, i8* %14, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %52
32: ; preds = %26
%33 = load i32, i32* %4
%34 = call i64 @fstat(i32 %33, %struct.stat* %10)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i8* @strdup(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i8* %37, i8** %9
br label %51
38: ; preds = %32
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%40 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @major(i32 %41)
%43 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @minor(i32 %44)
%46 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%47 = load i32, i32* %46
%48 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %39, i32 256, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %42, i32 %45, i32 %47)
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%50 = call i8* @strdup(i8* %49)
store i8* %50, i8** %9
br label %51
51: ; preds = %38, %36
br label %66
52: ; preds = %26
%53 = load i32, i32* %8
%54 = load i32, i32* @PATH_MAX
%55 = icmp sge i32 %53, %54
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = load i32, i32* @PATH_MAX
%58 = sub nsw i32 %57, 1
br label %61
59: ; preds = %52
%60 = load i32, i32* %8
br label %61
61: ; preds = %59, %56
%62 = phi i32 [ %58, %56 ], [ %60, %59 ]
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %14, i64 %63
store i8 0, i8* %64
%65 = call i8* @strdup(i8* %14)
store i8* %65, i8** %9
br label %66
66: ; preds = %61, %51
%67 = load i8*, i8** %9
%68 = load i8*, i8** %6
ret i8* %67
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@PATH_MAX = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [15 x i8] c"/proc/%d/fd/%d\00"
@.str.2 = [10 x i8] c"(unknown)\00"
@.str.3 = [21 x i8] c"dev: %i:%i inode %ld\00"
define noalias i8* @fnotify_get_filename(i32 %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = icmp eq i32 %0, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %15
13: ; preds = %2
%14 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %1)
br label %15
15: ; preds = %13, %11
%16 = call i32 @readlink(i8* %5, i8* %9, i32 %6)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %35
18: ; preds = %15
%19 = bitcast %struct.stat* %4 to i8*
%20 = call i64 @fstat(i32 %1, %struct.stat* %4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call dereferenceable_or_null(10) i8* @strdup(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %33
24: ; preds = %18
%25 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @major(i32 %26)
%28 = call i32 @minor(i32 %26)
%29 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%30 = load i32, i32* %29
%31 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32 %28, i32 %30)
%32 = call i8* @strdup(i8* %5)
br label %33
33: ; preds = %24, %22
%34 = phi i8* [ %23, %22 ], [ %32, %24 ]
br label %43
35: ; preds = %15
%36 = load i32, i32* @PATH_MAX
%37 = icmp slt i32 %16, %36
%38 = add nsw i32 %36, -1
%39 = select i1 %37, i32 %16, i32 %38
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %9, i64 %40
store i8 0, i8* %41
%42 = call i8* @strdup(i8* %9)
br label %43
43: ; preds = %35, %33
%44 = phi i8* [ %34, %33 ], [ %42, %35 ]
ret i8* %44
}