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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
scalar_t__ isdigit(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ proc_info_add(int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strtol(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int proc_info_load(void) {
  DIR *dir;
  struct dirent *dirent;
  if ((dir = opendir("/proc")) == NULL)
    return -1;
  while ((dirent = readdir(dir))) {
    if (isdigit(dirent->d_name[0])) {
      errno = 0;
      pid_t pid = (pid_t)strtol(dirent->d_name, NULL, 10);
      if (!errno)
        proc_info_add(pid);
    }
  }
  (void)closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<proc_info_load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    118e <proc_info_load+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <proc_info_load+0xbb>
jmp    1193 <proc_info_load+0x33>
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     120b <proc_info_load+0xab>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     1206 <proc_info_load+0xa6>
mov    0x2e17(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2df1(%rip),%rax
cmpq   $0x0,(%rax)
jne    1201 <proc_info_load+0xa1>
mov    -0x1c(%rbp),%edi
call   1080 <proc_info_add@plt>
jmp    1206 <proc_info_load+0xa6>
jmp    1193 <proc_info_load+0x33>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_info_load>:
push   %r15
push   %r14
push   %rbx
lea    0xe94(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11f0 <proc_info_load+0x90>
mov    %rax,%r14
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11e4 <proc_info_load+0x84>
mov    %rax,%rbx
mov    0x2e50(%rip),%r15
jmp    11b0 <proc_info_load+0x50>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11e4 <proc_info_load+0x84>
mov    (%rbx),%rax
mov    (%rax),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     11a0 <proc_info_load+0x40>
movq   $0x0,(%r15)
mov    (%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
cmpq   $0x0,(%r15)
jne    11a0 <proc_info_load+0x40>
mov    %eax,%edi
call   1080 <proc_info_add@plt>
jmp    11a0 <proc_info_load+0x40>
mov    %r14,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
jmp    11f5 <proc_info_load+0x95>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i32* }
@.str = [6 x i8] c"/proc\00"
@errno = global i64 0
define i32 @proc_info_load() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca i32
%5 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 -1, i32* %1
br label %36
8: ; preds = %0
br label %9
9: ; preds = %32, %8
%10 = load i32*, i32** %2
%11 = call %struct.dirent* @readdir(i32* %10)
store %struct.dirent* %11, %struct.dirent** %3
%12 = icmp ne %struct.dirent* %11, null
br i1 %12, label %13, label %33
13: ; preds = %9
%14 = load %struct.dirent*, %struct.dirent** %3
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i32 0, i32 0
%16 = load i32*, i32** %15
%17 = getelementptr inbounds i32, i32* %16, i64 0
%18 = load i32, i32* %17
%19 = call i64 @isdigit(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %13
store i64 0, i64* @errno
%22 = load %struct.dirent*, %struct.dirent** %3
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i32*, i32** %23
%25 = call i32 @strtol(i32* %24, i32* null, i32 10)
store i32 %25, i32* %4
%26 = load i64, i64* @errno
%27 = icmp ne i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %21
%29 = load i32, i32* %4
%30 = call i32 @proc_info_add(i32 %29)
br label %31
31: ; preds = %28, %21
br label %32
32: ; preds = %31, %13
br label %9
33: ; preds = %9
%34 = load i32*, i32** %2
%35 = call i32 @closedir(i32* %34)
store i32 0, i32* %1
br label %36
36: ; preds = %33, %7
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
%struct.dirent = type { i32* }
@.str = [6 x i8] c"/proc\00"
@errno = global i64 0
define i32 @proc_info_load() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %25, label %3
3: ; preds = %0
%4 = tail call %struct.dirent* @readdir(i32* %1)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %23, label %6
6: ; preds = %3, %20
%7 = phi %struct.dirent* [ %21, %20 ], [ %4, %3 ]
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 0
%9 = load i32*, i32** %8
%10 = load i32, i32* %9
%11 = tail call i64 @isdigit(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %6
store i64 0, i64* @errno
%14 = load i32*, i32** %8
%15 = tail call i32 @strtol(i32* nocapture %14, i32* null, i32 10)
%16 = load i64, i64* @errno
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @proc_info_add(i32 %15)
br label %20
20: ; preds = %13, %18, %6
%21 = tail call %struct.dirent* @readdir(i32* %1)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %23, label %6
23: ; preds = %20, %3
%24 = tail call i32 @closedir(i32* %1)
br label %25
25: ; preds = %0, %23
%26 = phi i32 [ 0, %23 ], [ -1, %0 ]
ret i32 %26
}