#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 {
  int dummy;
};
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int INT_MAX;
int /*<<< orphan*/ errno;
scalar_t__ eslib_string_sprintf(char *, int, int /*<<< orphan*/ *, char *,
                                int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *strerror(int /*<<< orphan*/);
int eslib_proc_numfds(pid_t pid) {
  char path[256];
  int count;
  struct dirent *dent;
  DIR *dir;
  if (pid <= 0)
    return -1;
  if (eslib_string_sprintf(path, sizeof(path), NULL, "/proc/%d/fd", pid))
    return -1;
  dir = opendir(path);
  if (dir == NULL) {
    printf("error opening %s: %s\n", path, strerror(errno));
    return -1;
  }
  count = 0;
  while (1) {
    dent = readdir(dir);
    if (dent == NULL)
      break;
    ++count;
    if (count >= INT_MAX)
      break;
  }
  return count;
}


x86 assembly (clang, -O0):
<eslib_proc_numfds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     1174 <eslib_proc_numfds+0x24>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <eslib_proc_numfds+0x120>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    $0x100,%esi
xor    %eax,%eax
mov    %eax,%edx
lea    0xe71(%rip),%rcx
call   1030 <eslib_string_sprintf@plt>
cmp    $0x0,%rax
je     11aa <eslib_proc_numfds+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <eslib_proc_numfds+0x120>
lea    -0x110(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    1209 <eslib_proc_numfds+0xb9>
lea    -0x110(%rbp),%rax
mov    %rax,-0x130(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x130(%rbp),%rsi
mov    %rax,%rdx
lea    0xe14(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <eslib_proc_numfds+0x120>
movl   $0x0,-0x114(%rbp)
mov    -0x128(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
jne    1239 <eslib_proc_numfds+0xe9>
jmp    1267 <eslib_proc_numfds+0x117>
mov    -0x114(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%eax
mov    0x2d93(%rip),%rcx
cmp    (%rcx),%eax
jl     1262 <eslib_proc_numfds+0x112>
jmp    1267 <eslib_proc_numfds+0x117>
jmp    1213 <eslib_proc_numfds+0xc3>
mov    -0x114(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eslib_proc_numfds>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    $0xffffffff,%ebx
test   %edi,%edi
jle    1183 <eslib_proc_numfds+0x33>
mov    %edi,%r8d
lea    0xe91(%rip),%rcx
mov    %rsp,%rdi
mov    $0x100,%esi
xor    %edx,%edx
call   1030 <eslib_string_sprintf@plt>
test   %rax,%rax
je     1192 <eslib_proc_numfds+0x42>
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rsp,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11c7 <eslib_proc_numfds+0x77>
mov    %rax,%r14
xor    %ebx,%ebx
mov    0x2e3d(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1183 <eslib_proc_numfds+0x33>
add    $0x1,%ebx
cmp    (%r15),%ebx
jl     11b0 <eslib_proc_numfds+0x60>
jmp    1183 <eslib_proc_numfds+0x33>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe30(%rip),%rdi
mov    %rsp,%rsi
mov    %rax,%rdx
call   1050 <printf@plt>
jmp    1183 <eslib_proc_numfds+0x33>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [12 x i8] c"/proc/%d/fd\00"
@.str.1 = [22 x i8] c"error opening %s: %s\0A\00"
@errno = global i32 0
@INT_MAX = global i32 0
define i32 @eslib_proc_numfds(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = alloca %struct.dirent*
%7 = alloca i32*
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = icmp sle i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %44
11: ; preds = %1
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%13 = load i32, i32* %3
%14 = call i64 @eslib_string_sprintf(i8* %12, i32 256, i32* null, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 -1, i32* %2
br label %44
17: ; preds = %11
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%19 = call i32* @opendir(i8* %18)
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %23, i8* %25)
store i32 -1, i32* %2
br label %44
27: ; preds = %17
store i32 0, i32* %5
br label %28
28: ; preds = %27, %41
%29 = load i32*, i32** %7
%30 = call %struct.dirent* @readdir(i32* %29)
store %struct.dirent* %30, %struct.dirent** %6
%31 = load %struct.dirent*, %struct.dirent** %6
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %33, label %34
33: ; preds = %28
br label %42
34: ; preds = %28
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
%37 = load i32, i32* %5
%38 = load i32, i32* @INT_MAX
%39 = icmp sge i32 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %34
br label %42
41: ; preds = %34
br label %28
42: ; preds = %40, %33
%43 = load i32, i32* %5
store i32 %43, i32* %2
br label %44
44: ; preds = %42, %22, %16, %10
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [12 x i8] c"/proc/%d/fd\00"
@.str.1 = [22 x i8] c"error opening %s: %s\0A\00"
@errno = global i32 0
@INT_MAX = global i32 0
define i32 @eslib_proc_numfds(i32 %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = icmp slt i32 %0, 1
br i1 %4, label %23, label %5
5: ; preds = %1
%6 = call i64 @eslib_string_sprintf(i8* %3, i32 256, i32* null, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %23
8: ; preds = %5
%9 = call i32* @opendir(i8* %3)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %3, i8* %13)
br label %23
15: ; preds = %8, %19
%16 = phi i32 [ %20, %19 ], [ 0, %8 ]
%17 = call %struct.dirent* @readdir(i32* %9)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = add nuw nsw i32 %16, 1
%21 = load i32, i32* @INT_MAX
%22 = icmp slt i32 %20, %21
br i1 %22, label %15, label %23
23: ; preds = %15, %19, %5, %1, %11
%24 = phi i32 [ -1, %11 ], [ -1, %1 ], [ -1, %5 ], [ %20, %19 ], [ %16, %15 ]
ret i32 %24
}