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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *, char *, char *);
int /*<<< orphan*/ isdigit(char);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
unsigned int get_run_queue_size() {
  int dummy_i;
  unsigned int length = 0;
  FILE *fp;
  DIR *dp;
  struct dirent *de;
  char txt[64], dummy_str[64];
  if ((dp = opendir("/proc")) == NULL)
    return (0);
  while ((de = readdir(dp)) != NULL) {
    if (!isdigit(de->d_name[0]))
      continue;
    sprintf(txt, "/proc/%s/stat", de->d_name);
    if ((fp = fopen(txt, "r")) == NULL)
      return (length);
    fscanf(fp, "%d %s %s", &dummy_i, dummy_str, txt);
    if (txt[0] == 'R')
      ++length;
    fclose(fp);
  }
  closedir(dp);
  return (length);
}


x86 assembly (clang, -O0):
<get_run_queue_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0xc(%rbp)
lea    0xe67(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11b8 <get_run_queue_size+0x38>
movl   $0x0,-0x4(%rbp)
jmp    1283 <get_run_queue_size+0x103>
jmp    11bd <get_run_queue_size+0x3d>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1274 <get_run_queue_size+0xf4>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    11f1 <get_run_queue_size+0x71>
jmp    11bd <get_run_queue_size+0x3d>
lea    -0x70(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rdx
lea    0xe03(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x70(%rbp),%rdi
lea    0xe01(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1231 <get_run_queue_size+0xb1>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1283 <get_run_queue_size+0x103>
mov    -0x18(%rbp),%rdi
lea    -0xb0(%rbp),%rcx
lea    -0x70(%rbp),%r8
lea    0xdcf(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1060 <fscanf@plt>
movsbl -0x70(%rbp),%eax
cmp    $0x52,%eax
jne    1266 <get_run_queue_size+0xe6>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
jmp    11bd <get_run_queue_size+0x3d>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_run_queue_size>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
lea    0xe68(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1246 <get_run_queue_size+0xc6>
mov    %rax,%rbx
xor    %r14d,%r14d
lea    0xe53(%rip),%r12
lea    0x10(%rsp),%r15
lea    0x50(%rsp),%r13
nopl   (%rax)
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     123c <get_run_queue_size+0xbc>
mov    %rax,%rbp
mov    (%rax),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     11c0 <get_run_queue_size+0x40>
mov    0x0(%rbp),%rdx
mov    %r15,%rdi
mov    %r12,%rbp
mov    %r12,%rsi
call   10a0 <sprintf@plt>
mov    %r15,%rdi
lea    0xe19(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1249 <get_run_queue_size+0xc9>
mov    %rax,%r12
mov    %rax,%rdi
lea    0xe04(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %r13,%rcx
mov    %r15,%r8
call   1060 <fscanf@plt>
xor    %eax,%eax
cmpb   $0x52,0x10(%rsp)
sete   %al
add    %eax,%r14d
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %rbp,%r12
jmp    11c0 <get_run_queue_size+0x40>
mov    %rbx,%rdi
call   1070 <closedir@plt>
jmp    1249 <get_run_queue_size+0xc9>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [14 x i8] c"/proc/%s/stat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [9 x i8] c"%d %s %s\00"
define i32 @get_run_queue_size() {
%1 = alloca i32
%2 = alloca [64 x i8]
%3 = alloca [64 x i8]
%4 = bitcast i32* %1 to i8*
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%7 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %35, label %9
9: ; preds = %0, %26
%10 = phi i32 [ %31, %26 ], [ 0, %0 ]
br label %11
11: ; preds = %9, %14
%12 = call %struct.dirent* @readdir(i32* %7)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %33, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = load i8, i8* %16
%18 = call i32 @isdigit(i8 signext %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %11, label %20
20: ; preds = %14
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %35, label %26
26: ; preds = %20
%27 = call i32 @fscanf(i32* %24, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* %1, i8* %6, i8* %5)
%28 = load i8, i8* %5
%29 = icmp eq i8 %28, 82
%30 = zext i1 %29 to i32
%31 = add i32 %10, %30
%32 = call i32 @fclose(i32* %24)
br label %9
33: ; preds = %11
%34 = call i32 @closedir(i32* %7)
br label %35
35: ; preds = %20, %0, %33
%36 = phi i32 [ %10, %33 ], [ 0, %0 ], [ %10, %20 ]
ret i32 %36
}