#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 records) {
  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 (records != 0) {
          count = count + proc_procsinfo(atoi(dent->d_name), count);
          if (count == records) {
            break;
          }
        } 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    1260 <getprocs+0xf0>
jmp    11b9 <getprocs+0x49>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1251 <getprocs+0xe1>
mov    -0x10(%rbp),%rax
cmpl   $0x4,(%rax)
jne    124c <getprocs+0xdc>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1090 <isnumbers@plt>
cmp    $0x0,%rax
je     1247 <getprocs+0xd7>
cmpl   $0x0,-0x8(%rbp)
je     1239 <getprocs+0xc9>
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)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1234 <getprocs+0xc4>
jmp    1251 <getprocs+0xe1>
jmp    1242 <getprocs+0xd2>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1247 <getprocs+0xd7>
jmp    124c <getprocs+0xdc>
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     11f4 <getprocs+0x84>
mov    %rax,%r15
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1204 <getprocs+0x94>
mov    %rax,%rbx
xor    %ebp,%ebp
jmp    11c0 <getprocs+0x50>
add    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1206 <getprocs+0x96>
cmpl   $0x4,(%rbx)
jne    11b0 <getprocs+0x40>
mov    0x4(%rbx),%edi
call   1090 <isnumbers@plt>
test   %rax,%rax
je     11b0 <getprocs+0x40>
test   %r14d,%r14d
je     11a2 <getprocs+0x32>
mov    0x4(%rbx),%edi
call   1080 <atoi@plt>
mov    %eax,%edi
mov    %ebp,%esi
call   1050 <proc_procsinfo@plt>
add    %eax,%ebp
cmp    %r14d,%ebp
jne    11b0 <getprocs+0x40>
mov    %r14d,%ebp
jmp    1206 <getprocs+0x96>
lea    0xe0b(%rip),%rdi
call   1040 <printf@plt>
xor    %ebp,%ebp
jmp    120e <getprocs+0x9e>
xor    %ebp,%ebp
mov    %r15,%rdi
call   1060 <closedir@plt>
mov    %ebp,%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 %56
12: ; preds = %1
br label %13
13: ; preds = %51, %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 %52
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 %51
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 %50
29: ; preds = %23
%30 = load i32, i32* %3
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %46
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
%41 = load i32, i32* %6
%42 = load i32, i32* %3
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %32
br label %52
45: ; preds = %32
br label %49
46: ; preds = %29
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
store i32 %48, i32* %6
br label %49
49: ; preds = %46, %45
br label %50
50: ; preds = %49, %23
br label %51
51: ; preds = %50, %18
br label %13
52: ; preds = %44, %13
%53 = load i32*, i32** %5
%54 = call i32 @closedir(i32* %53)
%55 = load i32, i32* %6
store i32 %55, i32* %2
br label %56
56: ; preds = %52, %10
%57 = load i32, i32* %2
ret i32 %57
}

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 %35, 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 %38
11: ; preds = %7, %31
%12 = phi %struct.dirent* [ %5, %7 ], [ %33, %31 ]
%13 = phi i32 [ 0, %7 ], [ %32, %31 ]
%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 %31
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 %31, label %22
22: ; preds = %17
br i1 %8, label %29, 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
%28 = icmp eq i32 %27, %0
br i1 %28, label %35, label %31
29: ; preds = %22
%30 = add nsw i32 %13, 1
br label %31
31: ; preds = %17, %23, %29, %11
%32 = phi i32 [ %27, %23 ], [ %30, %29 ], [ %13, %17 ], [ %13, %11 ]
%33 = tail call %struct.dirent* @readdir(i32* %2)
%34 = icmp eq %struct.dirent* %33, null
br i1 %34, label %35, label %11
35: ; preds = %31, %23, %4
%36 = phi i32 [ 0, %4 ], [ %0, %23 ], [ %32, %31 ]
%37 = tail call i32 @closedir(i32* %2)
br label %38
38: ; preds = %35, %9
%39 = phi i32 [ 0, %9 ], [ %36, %35 ]
ret i32 %39
}