#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 */
typedef int uint32_t;
struct dirent {
  char *d_name;
};
typedef int pid_t;
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int const);
uint32_t get_proc_cpu_tasks(const pid_t pid) {
  char path[PATH_MAX];
  DIR *dir;
  struct dirent *d;
  uint32_t n = 0;
  (void)snprintf(path, sizeof(path), "/proc/%d/task", pid);
  dir = opendir(path);
  if (!dir)
    return 1;
  while ((d = readdir(dir)) != NULL) {
    if (d->d_name[0] != '.')
      n++;
  }
  (void)closedir(dir);
  return (n < 1) ? 1 : n;
}


x86 assembly (clang, -O0):
<get_proc_cpu_tasks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%ecx
lea    0xe76(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ba <get_proc_cpu_tasks+0x7a>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    122a <get_proc_cpu_tasks+0xea>
jmp    11bf <get_proc_cpu_tasks+0x7f>
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11f7 <get_proc_cpu_tasks+0xb7>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     11f2 <get_proc_cpu_tasks+0xb2>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11bf <get_proc_cpu_tasks+0x7f>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
cmpl   $0x1,-0x2c(%rbp)
jge    1217 <get_proc_cpu_tasks+0xd7>
mov    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    121d <get_proc_cpu_tasks+0xdd>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_proc_cpu_tasks>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ecx
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe90(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <opendir@plt>
mov    $0x1,%r14d
test   %rax,%rax
je     11d8 <get_proc_cpu_tasks+0x98>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11d0 <get_proc_cpu_tasks+0x90>
xor    %ebx,%ebx
nopl   (%rax)
mov    (%rax),%rax
xor    %ecx,%ecx
cmpb   $0x2e,(%rax)
setne  %cl
add    %ecx,%ebx
mov    %r15,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    11a0 <get_proc_cpu_tasks+0x60>
mov    %r15,%rdi
call   1050 <closedir@plt>
test   %ebx,%ebx
mov    $0x1,%r14d
cmovne %ebx,%r14d
jmp    11d8 <get_proc_cpu_tasks+0x98>
mov    %r15,%rdi
call   1050 <closedir@plt>
mov    %r14d,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/proc/%d/task\00"
define i32 @get_proc_cpu_tasks(i32 %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @opendir(i8* %4)
%7 = icmp eq i32* %6, null
br i1 %7, label %28, label %8
8: ; preds = %1
%9 = call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @closedir(i32* %6)
br label %28
13: ; preds = %8, %13
%14 = phi %struct.dirent* [ %22, %13 ], [ %9, %8 ]
%15 = phi i32 [ %21, %13 ], [ 0, %8 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = load i8, i8* %17
%19 = icmp ne i8 %18, 46
%20 = zext i1 %19 to i32
%21 = add nuw nsw i32 %15, %20
%22 = call %struct.dirent* @readdir(i32* %6)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %24, label %13
24: ; preds = %13
%25 = call i32 @closedir(i32* %6)
%26 = icmp ugt i32 %21, 1
%27 = select i1 %26, i32 %21, i32 1
br label %28
28: ; preds = %24, %11, %1
%29 = phi i32 [ 1, %1 ], [ 1, %11 ], [ %27, %24 ]
ret i32 %29
}