#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 atoi(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int find_task_pids(int pid, int *child_pids) {
  struct dirent *dp;
  DIR *dir;
  int child_pid = 0;
  int found_index = 0;
  char *proc_line;
  char dirname[255 + 1];
  sprintf(dirname, "/proc/%d/task", pid);
  dir = opendir(dirname);
  if (dir == NULL) {
    return -1;
  }
  while ((dp = readdir(dir)) != NULL && found_index < 255) {
    int child_pid = atoi(dp->d_name);
    if (child_pid > 0 && child_pid != pid) {
      child_pids[found_index++] = child_pid;
    }
  }
  closedir(dir);
  return found_index;
}


x86 assembly (clang, -O0):
<find_task_pids>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe7f(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x130(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ad <find_task_pids+0x5d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <find_task_pids+0x103>
jmp    11b2 <find_task_pids+0x62>
mov    -0x20(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x135(%rbp)
je     11e4 <find_task_pids+0x94>
cmpl   $0xff,-0x28(%rbp)
setl   %al
mov    %al,-0x135(%rbp)
mov    -0x135(%rbp),%al
test   $0x1,%al
jne    11f7 <find_task_pids+0xa7>
jmp    1244 <find_task_pids+0xf4>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <atoi@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x0,-0x134(%rbp)
jle    123f <find_task_pids+0xef>
mov    -0x134(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     123f <find_task_pids+0xef>
mov    -0x134(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    11b2 <find_task_pids+0x62>
mov    -0x20(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_task_pids>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xe96(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15d,%edx
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11d9 <find_task_pids+0x89>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11cf <find_task_pids+0x7f>
nopw   0x0(%rax,%rax,1)
mov    (%rax),%edi
call   1060 <atoi@plt>
test   %eax,%eax
jle    11ba <find_task_pids+0x6a>
cmp    %r15d,%eax
je     11ba <find_task_pids+0x6a>
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %eax,(%r14,%rcx,4)
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     11cf <find_task_pids+0x7f>
cmp    $0xff,%ebp
jl     11a0 <find_task_pids+0x50>
mov    %rbx,%rdi
call   1040 <closedir@plt>
jmp    11de <find_task_pids+0x8e>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [14 x i8] c"/proc/%d/task\00"
define i32 @find_task_pids(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) 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 %33, label %8
8: ; preds = %2
%9 = tail call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %30, label %11
11: ; preds = %8, %24
%12 = phi %struct.dirent* [ %26, %24 ], [ %9, %8 ]
%13 = phi i32 [ %25, %24 ], [ 0, %8 ]
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @atoi(i32 %15)
%17 = icmp slt i32 %16, 1
%18 = icmp eq i32 %16, %0
%19 = or i1 %17, %18
br i1 %19, label %24, label %20
20: ; preds = %11
%21 = add nsw i32 %13, 1
%22 = sext i32 %13 to i64
%23 = getelementptr inbounds i32, i32* %1, i64 %22
store i32 %16, i32* %23
br label %24
24: ; preds = %20, %11
%25 = phi i32 [ %21, %20 ], [ %13, %11 ]
%26 = tail call %struct.dirent* @readdir(i32* %6)
%27 = icmp ne %struct.dirent* %26, null
%28 = icmp slt i32 %25, 255
%29 = select i1 %27, i1 %28, i1 false
br i1 %29, label %11, label %30
30: ; preds = %24, %8
%31 = phi i32 [ 0, %8 ], [ %25, %24 ]
%32 = tail call i32 @closedir(i32* %6)
br label %33
33: ; preds = %2, %30
%34 = phi i32 [ %31, %30 ], [ -1, %2 ]
ret i32 %34
}