#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 {
  scalar_t__ st_ino;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ pathname;
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ basename(char *);
int /*<<< orphan*/ err_quit(char *, int /*<<< orphan*/);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, ...);
int /*<<< orphan*/ sockq(unsigned long long);
scalar_t__ stat(char *, struct stat *);
char *strchr(char *, char);
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  DIR *dp;
  char *ch, buf[256], filename[256], pathname[256];
  struct dirent *dirp;
  int n;
  unsigned long long ino;
  struct stat statbuf;
  if (argc != 2)
    err_quit("Usage: %s <PID>", basename(argv[0]));
  snprintf(buf, sizeof(buf), "/proc/%s/fd", argv[1]);
  if ((dp = opendir(buf)) == NULL)
    err_sys("opendir error");
  while ((dirp = readdir(dp)) != NULL) {
    if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
      continue;
    snprintf(filename, sizeof(filename), "%s/%s", buf, dirp->d_name);
    if ((n = readlink(filename, pathname, sizeof(pathname) - 1)) == -1)
      err_sys("readlink");
    pathname[n] = '\0';
    if (*pathname == '/')
      continue;
    if ((ch = strchr(pathname, ':'))) {
      *ch = '\0';
      if (strcmp(pathname, "socket") == 0) {
        if (stat(filename, &statbuf) < 0)
          err_sys("stat error");
        ino = (unsigned long long)statbuf.st_ino;
        sockq(ino);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ed <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10b0 <basename@plt>
mov    %eax,%esi
lea    0xe18(%rip),%rdi
call   1090 <err_quit@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    $0x100,%esi
lea    0xe08(%rip),%rdx
mov    $0x0,%al
call   1080 <snprintf@plt>
lea    -0x120(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1235 <main+0x85>
lea    0xdec(%rip),%rdi
call   1040 <err_sys@plt>
jmp    123a <main+0x8a>
mov    -0x18(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x328(%rbp)
cmp    $0x0,%rax
je     13ab <main+0x1fb>
mov    -0x328(%rbp),%rax
mov    (%rax),%rdi
lea    0xdc6(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     1294 <main+0xe4>
mov    -0x328(%rbp),%rax
mov    (%rax),%rdi
lea    0xda5(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    1299 <main+0xe9>
jmp    123a <main+0x8a>
lea    -0x220(%rbp),%rdi
lea    -0x120(%rbp),%rcx
mov    -0x328(%rbp),%rax
mov    (%rax),%r8
mov    $0x100,%esi
lea    0xd70(%rip),%rdx
mov    $0x0,%al
call   1080 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    -0x320(%rbp),%rsi
mov    $0xff,%edx
call   1050 <readlink@plt>
mov    %eax,-0x32c(%rbp)
cmp    $0xffffffff,%eax
jne    12f7 <main+0x147>
lea    0xd41(%rip),%rdi
call   1040 <err_sys@plt>
movslq -0x32c(%rbp),%rax
movb   $0x0,-0x320(%rbp,%rax,1)
movsbl -0x320(%rbp),%eax
cmp    $0x2f,%eax
jne    131b <main+0x16b>
jmp    123a <main+0x8a>
lea    -0x320(%rbp),%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     13a6 <main+0x1f6>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x320(%rbp),%rdi
lea    0xced(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    13a1 <main+0x1f1>
lea    -0x220(%rbp),%rdi
lea    -0x340(%rbp),%rsi
call   10c0 <stat@plt>
cmp    $0x0,%rax
jge    1387 <main+0x1d7>
lea    0xcc1(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x340(%rbp),%rax
mov    %rax,-0x338(%rbp)
mov    -0x338(%rbp),%rdi
call   1030 <sockq@plt>
jmp    13a6 <main+0x1f6>
jmp    123a <main+0x8a>
xor    %eax,%eax
add    $0x340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x318,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11df <main+0x2f>
mov    (%rbx),%rdi
call   10b0 <basename@plt>
lea    0xe28(%rip),%rdi
mov    %eax,%esi
call   1090 <err_quit@plt>
mov    0x8(%rbx),%rcx
lea    0xe26(%rip),%rdx
lea    0x110(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
xor    %eax,%eax
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1060 <opendir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    121d <main+0x6d>
lea    0xe04(%rip),%rdi
call   1040 <err_sys@plt>
mov    %rbx,%rdi
call   10d0 <readdir@plt>
test   %rax,%rax
je     1346 <main+0x196>
mov    %rax,%rbp
lea    0xdf3(%rip),%r14
lea    0xdeb(%rip),%r15
lea    0x210(%rsp),%r13
lea    0x10(%rsp),%r12
jmp    1274 <main+0xc4>
mov    0x8(%rsp),%rdi
call   1030 <sockq@plt>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10d0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1346 <main+0x196>
mov    0x0(%rbp),%rdi
mov    %r14,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1260 <main+0xb0>
mov    0x0(%rbp),%rdi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1260 <main+0xb0>
mov    0x0(%rbp),%r8
mov    %r13,%rdi
mov    $0x100,%esi
lea    0xd84(%rip),%rdx
lea    0x110(%rsp),%rcx
xor    %eax,%eax
call   1080 <snprintf@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0xff,%edx
call   1050 <readlink@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    12db <main+0x12b>
lea    0xd5d(%rip),%rdi
call   1040 <err_sys@plt>
movslq %ebp,%rax
movb   $0x0,0x10(%rsp,%rax,1)
cmpb   $0x2f,0x10(%rsp)
je     1260 <main+0xb0>
mov    %r12,%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     1260 <main+0xb0>
movb   $0x0,(%rax)
mov    %r12,%rdi
lea    0xd2b(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
jne    1260 <main+0xb0>
mov    %r13,%rdi
lea    0x8(%rsp),%rsi
call   10c0 <stat@plt>
test   %rax,%rax
jns    124e <main+0x9e>
lea    0xd07(%rip),%rdi
call   1040 <err_sys@plt>
jmp    124e <main+0x9e>
xor    %eax,%eax
add    $0x318,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i64 }
@.str = [16 x i8] c"Usage: %s <PID>\00"
@.str.1 = [12 x i8] c"/proc/%s/fd\00"
@.str.2 = [14 x i8] c"opendir error\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [6 x i8] c"%s/%s\00"
@.str.6 = [9 x i8] c"readlink\00"
@.str.7 = [7 x i8] c"socket\00"
@.str.8 = [11 x i8] c"stat error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca [256 x i8]
%9 = alloca [256 x i8]
%10 = alloca [256 x i8]
%11 = alloca %struct.dirent*
%12 = alloca i32
%13 = alloca i64
%14 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 2
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @basename(i8* %20)
%22 = call i32 @err_quit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %17, %2
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %24, i32 256, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%30 = call i32* @opendir(i8* %29)
store i32* %30, i32** %6
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %34
32: ; preds = %23
%33 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %23
br label %35
35: ; preds = %95, %73, %51, %34
%36 = load i32*, i32** %6
%37 = call %struct.dirent* @readdir(i32* %36)
store %struct.dirent* %37, %struct.dirent** %11
%38 = icmp ne %struct.dirent* %37, null
br i1 %38, label %39, label %96
39: ; preds = %35
%40 = load %struct.dirent*, %struct.dirent** %11
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = call i64 @strcmp(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
br i1 %44, label %51, label %45
45: ; preds = %39
%46 = load %struct.dirent*, %struct.dirent** %11
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i64 @strcmp(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %45, %39
br label %35
52: ; preds = %45
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%55 = load %struct.dirent*, %struct.dirent** %11
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %53, i32 256, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %54, i8* %57)
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%60 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%61 = call i32 @readlink(i8* %59, i8* %60, i32 255)
store i32 %61, i32* %12
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %52
%64 = call i32 @err_sys(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %65
65: ; preds = %63, %52
%66 = load i32, i32* %12
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 %67
store i8 0, i8* %68
%69 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%70 = load i8, i8* %69
%71 = sext i8 %70 to i32
%72 = icmp eq i32 %71, 47
br i1 %72, label %73, label %74
73: ; preds = %65
br label %35
74: ; preds = %65
%75 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%76 = call i8* @strchr(i8* %75, i8 signext 58)
store i8* %76, i8** %7
%77 = icmp ne i8* %76, null
br i1 %77, label %78, label %95
78: ; preds = %74
%79 = load i8*, i8** %7
store i8 0, i8* %79
%80 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%81 = call i64 @strcmp(i8* %80, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%82 = icmp eq i64 %81, 0
br i1 %82, label %83, label %94
83: ; preds = %78
%84 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%85 = call i64 @stat(i8* %84, %struct.stat* %14)
%86 = icmp slt i64 %85, 0
br i1 %86, label %87, label %89
87: ; preds = %83
%88 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
br label %89
89: ; preds = %87, %83
%90 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%91 = load i64, i64* %90
store i64 %91, i64* %13
%92 = load i64, i64* %13
%93 = call i32 @sockq(i64 %92)
br label %94
94: ; preds = %89, %78
br label %95
95: ; preds = %94, %74
br label %35
96: ; preds = %35
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i64 }
%struct.dirent = type { i8* }
@.str = [16 x i8] c"Usage: %s <PID>\00"
@.str.1 = [12 x i8] c"/proc/%s/fd\00"
@.str.2 = [14 x i8] c"opendir error\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [6 x i8] c"%s/%s\00"
@.str.6 = [9 x i8] c"readlink\00"
@.str.7 = [7 x i8] c"socket\00"
@.str.8 = [11 x i8] c"stat error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = alloca [256 x i8]
%6 = alloca %struct.stat
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%10 = bitcast %struct.stat* %6 to i8*
%11 = icmp eq i32 %0, 2
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i8*, i8** %1
%14 = tail call i32 @basename(i8* %13)
%15 = tail call i32 @err_quit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %12, %2
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %7, i32 256, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = call i32* @opendir(i8* %7)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = call %struct.dirent* @readdir(i32* %20)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %68, label %27
27: ; preds = %24
%28 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
br label %29
29: ; preds = %27, %39
%30 = phi %struct.dirent* [ %25, %27 ], [ %40, %39 ]
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i64 0, i32 0
%32 = load i8*, i8** %31
%33 = call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %29
%36 = load i8*, i8** %31
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %54, %65, %57, %29, %35, %49
%40 = call %struct.dirent* @readdir(i32* %20)
%41 = icmp eq %struct.dirent* %40, null
br i1 %41, label %68, label %29
42: ; preds = %35
%43 = load i8*, i8** %31
%44 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %8, i32 256, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %7, i8* %43)
%45 = call i32 @readlink(i8* %8, i8* %9, i32 255)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 @err_sys(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %49
49: ; preds = %47, %42
%50 = sext i32 %45 to i64
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %50
store i8 0, i8* %51
%52 = load i8, i8* %9
%53 = icmp eq i8 %52, 47
br i1 %53, label %39, label %54
54: ; preds = %49
%55 = call i8* @strchr(i8* %9, i8 signext 58)
%56 = icmp eq i8* %55, null
br i1 %56, label %39, label %57
57: ; preds = %54
store i8 0, i8* %55
%58 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %39
60: ; preds = %57
%61 = call i64 @stat(i8* %8, %struct.stat* %6)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
br label %65
65: ; preds = %63, %60
%66 = load i64, i64* %28
%67 = call i32 @sockq(i64 %66)
br label %39
68: ; preds = %39, %24
ret i32 0
}