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

/* Variables and functions */
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ getcwd(char *, int);
scalar_t__ isdigit(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
unsigned int countprocs(void) {
  unsigned int nr = 0;
  DIR *dirp;
  struct dirent *entp;
  char origdir[1024];
  getcwd(origdir, sizeof origdir);
  chdir("/proc");
  dirp = opendir(".");
  while ((entp = readdir(dirp))) {
    if (isdigit(entp->d_name[0]))
      nr++;
  }
  closedir(dirp);
  chdir(origdir);
  return nr;
}


x86 assembly (clang, -O0):
<countprocs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x420(%rbp),%rdi
mov    $0x400,%esi
call   1060 <getcwd@plt>
lea    0xe76(%rip),%rdi
call   1050 <chdir@plt>
lea    0xe70(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11dc <countprocs+0x7c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     11d7 <countprocs+0x77>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    119f <countprocs+0x3f>
mov    -0x10(%rbp),%rdi
call   1070 <closedir@plt>
lea    -0x420(%rbp),%rdi
call   1050 <chdir@plt>
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countprocs>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
mov    %rsp,%rdi
mov    $0x400,%esi
call   1060 <getcwd@plt>
lea    0xe83(%rip),%rdi
call   1050 <chdir@plt>
lea    0xe7d(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11be <countprocs+0x5e>
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    11a0 <countprocs+0x40>
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    %rsp,%rdi
call   1050 <chdir@plt>
mov    %ebp,%eax
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32* }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [2 x i8] c".\00"
define i32 @countprocs() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca [1024 x i8]
store i32 0, i32* %1
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = call i32 @getcwd(i8* %5, i32 1024)
%7 = call i32 @chdir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %2
br label %9
9: ; preds = %24, %0
%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 %25
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 %24
21: ; preds = %13
%22 = load i32, i32* %1
%23 = add i32 %22, 1
store i32 %23, i32* %1
br label %24
24: ; preds = %21, %13
br label %9
25: ; preds = %9
%26 = load i32*, i32** %2
%27 = call i32 @closedir(i32* %26)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%29 = call i32 @chdir(i8* %28)
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
%struct.dirent = type { i32* }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [2 x i8] c".\00"
define i32 @countprocs() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = call i32 @getcwd(i8* %2, i32 1024)
%4 = call i32 @chdir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%5 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = call %struct.dirent* @readdir(i32* %5)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %20, label %8
8: ; preds = %0, %8
%9 = phi %struct.dirent* [ %18, %8 ], [ %6, %0 ]
%10 = phi i32 [ %17, %8 ], [ 0, %0 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%12 = load i32*, i32** %11
%13 = load i32, i32* %12
%14 = call i64 @isdigit(i32 %13)
%15 = icmp ne i64 %14, 0
%16 = zext i1 %15 to i32
%17 = add i32 %10, %16
%18 = call %struct.dirent* @readdir(i32* %5)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %20, label %8
20: ; preds = %8, %0
%21 = phi i32 [ 0, %0 ], [ %17, %8 ]
%22 = call i32 @closedir(i32* %5)
%23 = call i32 @chdir(i8* %2)
ret i32 %21
}