#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 dirent {
  char *d_name;
};
typedef int pid_t;
typedef int /*<<< orphan*/ fdpath;
typedef int /*<<< orphan*/ dirpath;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ numeric_name(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
size_t readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int const, ...);
int /*<<< orphan*/ strcmp(char const *, char *);
int check_pid(const pid_t pid, const char *dev_name,
              const char *short_dev_name) {
  char dirpath[64], fdpath[64], linkpath[1024];
  DIR *dirp;
  struct dirent *direntry;
  size_t len;
  int r = 0;
  snprintf(dirpath, sizeof(dirpath), "/proc/%d/fd", pid);
  if (!(dirp = opendir(dirpath)))
    return r;
  while ((direntry = readdir(dirp))) {
    if (!numeric_name(direntry->d_name))
      continue;
    snprintf(fdpath, sizeof(fdpath), "/proc/%d/fd/%s", pid, direntry->d_name);
    if ((len = readlink(fdpath, linkpath, 1024 - 1)) < 0)
      break;
    linkpath[len] = '\0';
    if (!strcmp(dev_name, linkpath)) {
      r = 1;
      break;
    }
    if (!strcmp(short_dev_name, linkpath)) {
      r = 2;
      break;
    }
  }
  closedir(dirp);
  return r;
}


x86 assembly (clang, -O0):
<check_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4c0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x4bc(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe5d(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x60(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x4a8(%rbp)
cmp    $0x0,%rax
jne    11d2 <check_pid+0x62>
mov    -0x4bc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12df <check_pid+0x16f>
jmp    11d7 <check_pid+0x67>
mov    -0x4a8(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x4b0(%rbp)
cmp    $0x0,%rax
je     12ca <check_pid+0x15a>
mov    -0x4b0(%rbp),%rax
mov    (%rax),%rdi
call   1060 <numeric_name@plt>
cmp    $0x0,%eax
jne    1211 <check_pid+0xa1>
jmp    11d7 <check_pid+0x67>
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0x4b0(%rbp),%rax
mov    (%rax),%r8
mov    $0x40,%esi
lea    0xddb(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    -0x4a0(%rbp),%rsi
mov    $0x3ff,%edx
call   1030 <readlink@plt>
mov    %rax,-0x4b8(%rbp)
cmp    $0x0,%rax
jae    1266 <check_pid+0xf6>
jmp    12ca <check_pid+0x15a>
mov    -0x4b8(%rbp),%rax
movb   $0x0,-0x4a0(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
lea    -0x4a0(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    129d <check_pid+0x12d>
movl   $0x1,-0x4bc(%rbp)
jmp    12ca <check_pid+0x15a>
mov    -0x18(%rbp),%rdi
lea    -0x4a0(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    12c5 <check_pid+0x155>
movl   $0x2,-0x4bc(%rbp)
jmp    12ca <check_pid+0x15a>
jmp    11d7 <check_pid+0x67>
mov    -0x4a8(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x4bc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_pid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x498,%rsp
mov    %rdx,0x8(%rsp)
mov    %rsi,%r15
mov    %edi,%ebp
lea    0xe6e(%rip),%rdx
xor    %r12d,%r12d
lea    0x50(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     127a <check_pid+0x10a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1267 <check_pid+0xf7>
mov    %rax,%r14
lea    0x10(%rsp),%r13
lea    0x90(%rsp),%r12
jmp    1200 <check_pid+0x90>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1267 <check_pid+0xf7>
mov    (%r14),%rdi
call   1060 <numeric_name@plt>
test   %eax,%eax
je     11f0 <check_pid+0x80>
mov    (%r14),%r8
mov    %r13,%rdi
mov    $0x40,%esi
lea    0xdee(%rip),%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0x3ff,%edx
call   1030 <readlink@plt>
movb   $0x0,0x90(%rsp,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     126c <check_pid+0xfc>
mov    0x8(%rsp),%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
jne    11f0 <check_pid+0x80>
mov    $0x2,%r12d
jmp    1272 <check_pid+0x102>
xor    %r12d,%r12d
jmp    1272 <check_pid+0x102>
mov    $0x1,%r12d
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    %r12d,%eax
add    $0x498,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [12 x i8] c"/proc/%d/fd\00"
@.str.1 = [15 x i8] c"/proc/%d/fd/%s\00"
define i32 @check_pid(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [64 x i8]
%9 = alloca [64 x i8]
%10 = alloca [1024 x i8]
%11 = alloca i32*
%12 = alloca %struct.dirent*
%13 = alloca i64
%14 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 0, i32* %14
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%16 = load i32, i32* %5
%17 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %15, i32 64, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%19 = call i32* @opendir(i8* %18)
store i32* %19, i32** %11
%20 = icmp ne i32* %19, null
br i1 %20, label %23, label %21
21: ; preds = %3
%22 = load i32, i32* %14
store i32 %22, i32* %4
br label %66
23: ; preds = %3
br label %24
24: ; preds = %61, %34, %23
%25 = load i32*, i32** %11
%26 = call %struct.dirent* @readdir(i32* %25)
store %struct.dirent* %26, %struct.dirent** %12
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %62
28: ; preds = %24
%29 = load %struct.dirent*, %struct.dirent** %12
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = call i32 @numeric_name(i8* %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %28
br label %24
35: ; preds = %28
%36 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%37 = load i32, i32* %5
%38 = load %struct.dirent*, %struct.dirent** %12
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %36, i32 64, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %37, i8* %40)
%42 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%44 = call i64 @readlink(i8* %42, i8* %43, i32 1023)
store i64 %44, i64* %13
%45 = icmp ult i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %35
br label %62
47: ; preds = %35
%48 = load i64, i64* %13
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %6
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%52 = call i32 @strcmp(i8* %50, i8* %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %55, label %54
54: ; preds = %47
store i32 1, i32* %14
br label %62
55: ; preds = %47
%56 = load i8*, i8** %7
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%58 = call i32 @strcmp(i8* %56, i8* %57)
%59 = icmp ne i32 %58, 0
br i1 %59, label %61, label %60
60: ; preds = %55
store i32 2, i32* %14
br label %62
61: ; preds = %55
br label %24
62: ; preds = %60, %54, %46, %24
%63 = load i32*, i32** %11
%64 = call i32 @closedir(i32* %63)
%65 = load i32, i32* %14
store i32 %65, i32* %4
br label %66
66: ; preds = %62, %21
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [12 x i8] c"/proc/%d/fd\00"
@.str.1 = [15 x i8] c"/proc/%d/fd/%s\00"
define i32 @check_pid(i32 %0, i8* nocapture %1, i8* nocapture %2) {
%4 = alloca [64 x i8]
%5 = alloca [64 x i8]
%6 = alloca [1024 x i8]
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%10 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %7, i32 64, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0)
%11 = call i32* @opendir(i8* %7)
%12 = icmp eq i32* %11, null
br i1 %12, label %38, label %13
13: ; preds = %3
%14 = call %struct.dirent* @readdir(i32* %11)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %35, label %16
16: ; preds = %13, %22
%17 = phi %struct.dirent* [ %23, %22 ], [ %14, %13 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = call i32 @numeric_name(i8* %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16, %32
%23 = call %struct.dirent* @readdir(i32* %11)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %35, label %16
25: ; preds = %16
%26 = load i8*, i8** %18
%27 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %8, i32 64, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8* %26)
%28 = call i64 @readlink(i8* %8, i8* %9, i32 1023)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %28
store i8 0, i8* %29
%30 = call i32 @strcmp(i8* dereferenceable(1) %1, i8* %9)
%31 = icmp eq i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %25
%33 = call i32 @strcmp(i8* dereferenceable(1) %2, i8* %9)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %22
35: ; preds = %22, %25, %32, %13
%36 = phi i32 [ 0, %13 ], [ 2, %32 ], [ 1, %25 ], [ 0, %22 ]
%37 = call i32 @closedir(i32* %11)
br label %38
38: ; preds = %3, %35
%39 = phi i32 [ %36, %35 ], [ 0, %3 ]
ret i32 %39
}