#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 /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int count_open_fds(void) {
  DIR *dir;
  struct dirent *ent;
  int count = 0;
  dir = opendir("/proc/self/fd");
  assert(dir && "opening /proc/self/fd failed.");
  errno = 0;
  while ((ent = readdir(dir))) {
    const char *s = ent->d_name;
    if (s[0] == '.' && (s[1] == 0 || (s[1] == '.' && s[2] == 0)))
      continue;
    count++;
  }
  assert(errno == 0 && "reading /proc/self/fd failed.");
  closedir(dir);
  return count;
}


x86 assembly (clang, -O0):
<count_open_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x14(%rbp)
lea    0xeaa(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0x21(%rbp)
je     1179 <count_open_fds+0x39>
mov    $0x1,%al
mov    %al,-0x21(%rbp)
jmp    1179 <count_open_fds+0x39>
mov    -0x21(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    0x2e53(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     120c <count_open_fds+0xcc>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11fe <count_open_fds+0xbe>
mov    -0x20(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     11f9 <count_open_fds+0xb9>
mov    -0x20(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2e,%eax
jne    11fe <count_open_fds+0xbe>
mov    -0x20(%rbp),%rax
movsbl 0x2(%rax),%eax
cmp    $0x0,%eax
jne    11fe <count_open_fds+0xbe>
jmp    1194 <count_open_fds+0x54>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1194 <count_open_fds+0x54>
xor    %eax,%eax
mov    0x2dcb(%rip),%rcx
cmpq   $0x0,(%rcx)
mov    %al,-0x22(%rbp)
jne    122c <count_open_fds+0xec>
mov    $0x1,%al
mov    %al,-0x22(%rbp)
jmp    122c <count_open_fds+0xec>
mov    -0x22(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_open_fds>:
push   %rbp
push   %r14
push   %rbx
lea    0xeb5(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
mov    0x2e78(%rip),%r14
movq   $0x0,(%r14)
mov    %rbx,%rdi
call   1060 <readdir@plt>
xor    %ebp,%ebp
jmp    118b <count_open_fds+0x4b>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11ad <count_open_fds+0x6d>
mov    (%rax),%rax
cmpb   $0x2e,(%rax)
jne    1180 <count_open_fds+0x40>
movzbl 0x1(%rax),%ecx
test   %cl,%cl
je     1183 <count_open_fds+0x43>
cmp    $0x2e,%cl
jne    1180 <count_open_fds+0x40>
cmpb   $0x0,0x2(%rax)
jne    1180 <count_open_fds+0x40>
jmp    1183 <count_open_fds+0x43>
xor    %edi,%edi
cmpq   $0x0,(%r14)
sete   %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1040 <closedir@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [14 x i8] c"/proc/self/fd\00"
@.str.1 = [30 x i8] c"opening /proc/self/fd failed.\00"
@errno = global i64 0
@.str.2 = [30 x i8] c"reading /proc/self/fd failed.\00"
define i32 @count_open_fds() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %3
%5 = call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %1
%6 = load i32*, i32** %1
%7 = icmp ne i32* %6, null
br i1 %7, label %8, label %9
8: ; preds = %0
br label %9
9: ; preds = %8, %0
%10 = phi i1 [ false, %0 ], [ true, %8 ]
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
store i64 0, i64* @errno
br label %13
13: ; preds = %45, %44, %9
%14 = load i32*, i32** %1
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %2
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %48
17: ; preds = %13
%18 = load %struct.dirent*, %struct.dirent** %2
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i8*, i8** %19
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 46
br i1 %25, label %26, label %45
26: ; preds = %17
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 0
br i1 %31, label %44, label %32
32: ; preds = %26
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 46
br i1 %37, label %38, label %45
38: ; preds = %32
%39 = load i8*, i8** %4
%40 = getelementptr inbounds i8, i8* %39, i64 2
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %38, %26
br label %13
45: ; preds = %38, %32, %17
%46 = load i32, i32* %3
%47 = add nsw i32 %46, 1
store i32 %47, i32* %3
br label %13
48: ; preds = %13
%49 = load i64, i64* @errno
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %48
br label %52
52: ; preds = %51, %48
%53 = phi i1 [ false, %48 ], [ true, %51 ]
%54 = zext i1 %53 to i32
%55 = call i32 @assert(i32 %54)
%56 = load i32*, i32** %1
%57 = call i32 @closedir(i32* %56)
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [14 x i8] c"/proc/self/fd\00"
@errno = global i64 0
define i32 @count_open_fds() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%2 = icmp ne i32* %1, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
store i64 0, i64* @errno
%5 = tail call %struct.dirent* @readdir(i32* %1)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %27, label %7
7: ; preds = %0, %23
%8 = phi %struct.dirent* [ %25, %23 ], [ %5, %0 ]
%9 = phi i32 [ %24, %23 ], [ 0, %0 ]
%10 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%11 = load i8*, i8** %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 46
br i1 %13, label %14, label %21
14: ; preds = %7
%15 = getelementptr inbounds i8, i8* %11, i64 1
%16 = load i8, i8* %15
switch i8 %16, label %21 [
i8 0, label %23
i8 46, label %17
]
17: ; preds = %14
%18 = getelementptr inbounds i8, i8* %11, i64 2
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %14, %17, %7
%22 = add nsw i32 %9, 1
br label %23
23: ; preds = %17, %14, %21
%24 = phi i32 [ %22, %21 ], [ %9, %14 ], [ %9, %17 ]
%25 = tail call %struct.dirent* @readdir(i32* %1)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %27, label %7
27: ; preds = %23, %0
%28 = phi i32 [ 0, %0 ], [ %24, %23 ]
%29 = load i64, i64* @errno
%30 = icmp eq i64 %29, 0
%31 = zext i1 %30 to i32
%32 = tail call i32 @assert(i32 %31)
%33 = tail call i32 @closedir(i32* %1)
ret i32 %28
}