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

/* Variables and functions */
int /*<<< orphan*/ atoi(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ isnumbers(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *);
int proc_procsinfo(int /*<<< orphan*/, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int getprocs(int details) {
  struct dirent *dent;
  DIR *procdir;
  int count = 0;
  if ((char *)(procdir = opendir("/proc")) == NULL) {
    printf("opendir(/proc) failed");
    return 0;
  }
  while ((char *)(dent = readdir(procdir)) != NULL) {
    if (dent->d_type == 4) {
      if (isnumbers(dent->d_name)) {
        if (details) {
          count = count + proc_procsinfo(atoi(dent->d_name), count);
        } else {
          count++;
        }
      }
    }
  }
  closedir(procdir);
  return count;
}


x86 assembly (clang, -O0):
<getprocs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe77(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b4 <getprocs+0x44>
lea    0xe63(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124f <getprocs+0xdf>
jmp    11b9 <getprocs+0x49>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1240 <getprocs+0xd0>
mov    -0x10(%rbp),%rax
cmpl   $0x4,(%rax)
jne    123b <getprocs+0xcb>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1090 <isnumbers@plt>
cmp    $0x0,%rax
je     1236 <getprocs+0xc6>
cmpl   $0x0,-0x8(%rbp)
je     1228 <getprocs+0xb8>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1080 <atoi@plt>
mov    %eax,%edi
mov    -0x1c(%rbp),%esi
call   1050 <proc_procsinfo@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1231 <getprocs+0xc1>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1236 <getprocs+0xc6>
jmp    123b <getprocs+0xcb>
jmp    11b9 <getprocs+0x49>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getprocs>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
lea    0xe7f(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11ef <getprocs+0x7f>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1200 <getprocs+0x90>
mov    %rax,%rbp
xor    %r15d,%r15d
jmp    11c0 <getprocs+0x50>
add    $0x1,%r15d
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1203 <getprocs+0x93>
cmpl   $0x4,0x0(%rbp)
jne    11b0 <getprocs+0x40>
mov    0x4(%rbp),%edi
call   1090 <isnumbers@plt>
test   %rax,%rax
je     11b0 <getprocs+0x40>
test   %r14d,%r14d
je     11a3 <getprocs+0x33>
mov    0x4(%rbp),%edi
call   1080 <atoi@plt>
mov    %eax,%edi
mov    %r15d,%esi
call   1050 <proc_procsinfo@plt>
add    %eax,%r15d
jmp    11b0 <getprocs+0x40>
lea    0xe10(%rip),%rdi
call   1040 <printf@plt>
xor    %r15d,%r15d
jmp    120b <getprocs+0x9b>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1060 <closedir@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i32, i32 }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [22 x i8] c"opendir(/proc) failed\00"
define i32 @getprocs(i32 %0) {
%2 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %9, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %34, label %7
7: ; preds = %4
%8 = icmp eq i32 %0, 0
br label %11
9: ; preds = %1
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %37
11: ; preds = %7, %30
%12 = phi %struct.dirent* [ %5, %7 ], [ %32, %30 ]
%13 = phi i32 [ 0, %7 ], [ %31, %30 ]
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%15 = load i32, i32* %14
%16 = icmp eq i32 %15, 4
br i1 %16, label %17, label %30
17: ; preds = %11
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 1
%19 = load i32, i32* %18
%20 = tail call i64 @isnumbers(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %17
br i1 %8, label %28, label %23
23: ; preds = %22
%24 = load i32, i32* %18
%25 = tail call i32 @atoi(i32 %24)
%26 = tail call i32 @proc_procsinfo(i32 %25, i32 %13)
%27 = add nsw i32 %26, %13
br label %30
28: ; preds = %22
%29 = add nsw i32 %13, 1
br label %30
30: ; preds = %17, %28, %23, %11
%31 = phi i32 [ %27, %23 ], [ %29, %28 ], [ %13, %17 ], [ %13, %11 ]
%32 = tail call %struct.dirent* @readdir(i32* %2)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %34, label %11
34: ; preds = %30, %4
%35 = phi i32 [ 0, %4 ], [ %31, %30 ]
%36 = tail call i32 @closedir(i32* %2)
br label %37
37: ; preds = %34, %9
%38 = phi i32 [ 0, %9 ], [ %35, %34 ]
ret i32 %38
}