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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int atoi(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ is_number(int /*<<< orphan*/);
int /*<<< orphan*/ lstat(int /*<<< orphan*/, struct stat *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ parse_proc_status_val_by_tag(int, char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strcmp(char *, char *);
void get_child_pids(pid_t parent_pid, pid_t pids[], int *p_pids_len) {
  DIR *dp;
  struct dirent *entry;
  struct stat stat_buf;
  pid_t pid;
  char buf[512];
  char parent_pid_str[512];
  int pids_len = 0;
  *p_pids_len = -1;
  sprintf(parent_pid_str, "%d", parent_pid);
  if ((dp = opendir("/proc/")) == NULL) {
    return;
  }
  chdir("/proc/");
  while ((entry = readdir(dp)) != NULL) {
    lstat(entry->d_name, &stat_buf);
    if (S_ISDIR(stat_buf.st_mode)) {
      if (!is_number(entry->d_name)) {
        continue;
      }
      pid = atoi(entry->d_name);
      if (pid > 0 && pid <= 32767) {
        parse_proc_status_val_by_tag(pid, "PPid", buf);
        if (strcmp(buf, "error_result") != 0 &&
            strcmp(parent_pid_str, buf) == 0) {
          pids[pids_len++] = pid;
        }
      }
    }
  }
  *p_pids_len = pids_len;
}


x86 assembly (clang, -O0):
<get_child_pids>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x444(%rbp)
mov    -0x18(%rbp),%rax
movl   $0xffffffff,(%rax)
lea    -0x440(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe25(%rip),%rsi
call   10c0 <sprintf@plt>
lea    0xe1c(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ff <get_child_pids+0x5f>
jmp    130a <get_child_pids+0x16a>
lea    0xdfd(%rip),%rdi
call   1060 <chdir@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12fe <get_child_pids+0x15e>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1080 <lstat@plt>
mov    -0x30(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12f9 <get_child_pids+0x159>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1030 <is_number@plt>
cmp    $0x0,%eax
jne    125c <get_child_pids+0xbc>
jmp    120b <get_child_pids+0x6b>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   10b0 <atoi@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    12f4 <get_child_pids+0x154>
cmpl   $0x7fff,-0x34(%rbp)
jg     12f4 <get_child_pids+0x154>
mov    -0x34(%rbp),%edi
lea    -0x240(%rbp),%rdx
lea    0xd78(%rip),%rsi
call   1040 <parse_proc_status_val_by_tag@plt>
lea    -0x240(%rbp),%rdi
lea    0xd6a(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     12ef <get_child_pids+0x14f>
lea    -0x440(%rbp),%rdi
lea    -0x240(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    12ef <get_child_pids+0x14f>
mov    -0x34(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    -0x444(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x444(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    12f4 <get_child_pids+0x154>
jmp    12f9 <get_child_pids+0x159>
jmp    120b <get_child_pids+0x6b>
mov    -0x444(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_child_pids>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %edi,%edx
movl   $0xffffffff,(%r15)
lea    0xe39(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10c0 <sprintf@plt>
lea    0xe2b(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     12ae <get_child_pids+0x10e>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
lea    0xe0e(%rip),%rdi
call   1060 <chdir@plt>
xor    %r14d,%r14d
mov    %rsp,%r12
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12ab <get_child_pids+0x10b>
mov    %rax,%rbp
mov    (%rax),%edi
mov    %r12,%rsi
call   1080 <lstat@plt>
mov    (%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
je     1200 <get_child_pids+0x60>
mov    0x0(%rbp),%edi
call   1030 <is_number@plt>
test   %eax,%eax
je     1200 <get_child_pids+0x60>
mov    0x0(%rbp),%edi
call   10b0 <atoi@plt>
mov    %eax,%r13d
lea    -0x1(%r13),%eax
cmp    $0x7ffe,%eax
ja     1200 <get_child_pids+0x60>
mov    %r13d,%edi
lea    0xdb3(%rip),%rsi
lea    0x210(%rsp),%rbp
mov    %rbp,%rdx
call   1040 <parse_proc_status_val_by_tag@plt>
mov    %rbp,%rdi
lea    0xd9e(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1200 <get_child_pids+0x60>
lea    0x10(%rsp),%rdi
lea    0x210(%rsp),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1200 <get_child_pids+0x60>
mov    %r14d,%eax
add    $0x1,%r14d
mov    0x8(%rsp),%rcx
mov    %r13d,(%rcx,%rax,4)
jmp    1200 <get_child_pids+0x60>
mov    %r14d,(%r15)
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
%struct.stat = type { i32 }
@.str = [3 x i8] c"%d\00"
@.str.1 = [7 x i8] c"/proc/\00"
@.str.2 = [5 x i8] c"PPid\00"
@.str.3 = [13 x i8] c"error_result\00"
define void @get_child_pids(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca [512 x i8]
%12 = alloca [512 x i8]
%13 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32* %2, i32** %6
store i32 0, i32* %13
%14 = load i32*, i32** %6
store i32 -1, i32* %14
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%16 = load i32, i32* %4
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = call i32* @opendir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %7
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %3
br label %78
21: ; preds = %3
%22 = call i32 @chdir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %74, %42, %21
%24 = load i32*, i32** %7
%25 = call %struct.dirent* @readdir(i32* %24)
store %struct.dirent* %25, %struct.dirent** %8
%26 = icmp ne %struct.dirent* %25, null
br i1 %26, label %27, label %75
27: ; preds = %23
%28 = load %struct.dirent*, %struct.dirent** %8
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @lstat(i32 %30, %struct.stat* %9)
%32 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i64 @S_ISDIR(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %74
36: ; preds = %27
%37 = load %struct.dirent*, %struct.dirent** %8
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @is_number(i32 %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %43, label %42
42: ; preds = %36
br label %23
43: ; preds = %36
%44 = load %struct.dirent*, %struct.dirent** %8
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @atoi(i32 %46)
store i32 %47, i32* %10
%48 = load i32, i32* %10
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %73
50: ; preds = %43
%51 = load i32, i32* %10
%52 = icmp sle i32 %51, 32767
br i1 %52, label %53, label %73
53: ; preds = %50
%54 = load i32, i32* %10
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%56 = call i32 @parse_proc_status_val_by_tag(i32 %54, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %55)
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%58 = call i64 @strcmp(i8* %57, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %72
60: ; preds = %53
%61 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%63 = call i64 @strcmp(i8* %61, i8* %62)
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %72
65: ; preds = %60
%66 = load i32, i32* %10
%67 = load i32*, i32** %5
%68 = load i32, i32* %13
%69 = add nsw i32 %68, 1
store i32 %69, i32* %13
%70 = sext i32 %68 to i64
%71 = getelementptr inbounds i32, i32* %67, i64 %70
store i32 %66, i32* %71
br label %72
72: ; preds = %65, %60, %53
br label %73
73: ; preds = %72, %50, %43
br label %74
74: ; preds = %73, %27
br label %23
75: ; preds = %23
%76 = load i32, i32* %13
%77 = load i32*, i32** %6
store i32 %76, i32* %77
br label %78
78: ; preds = %75, %20
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@.str = [3 x i8] c"%d\00"
@.str.1 = [7 x i8] c"/proc/\00"
@.str.2 = [5 x i8] c"PPid\00"
@.str.3 = [13 x i8] c"error_result\00"
define void @get_child_pids(i32 %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = alloca [512 x i8]
%6 = alloca [512 x i8]
%7 = bitcast %struct.stat* %4 to i8*
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
store i32 -1, i32* %2
%10 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%11 = tail call i32* @opendir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %51, label %13
13: ; preds = %3
%14 = tail call i32 @chdir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %16
16: ; preds = %46, %13
%17 = phi i32 [ %47, %46 ], [ 0, %13 ]
br label %18
18: ; preds = %38, %16
br label %19
19: ; preds = %18, %29
%20 = call %struct.dirent* @readdir(i32* %11)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %50, label %22
22: ; preds = %19
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @lstat(i32 %24, %struct.stat* %4)
%26 = load i32, i32* %15
%27 = call i64 @S_ISDIR(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %38, label %29
29: ; preds = %22
%30 = load i32, i32* %23
%31 = call i32 @is_number(i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %19, label %33
33: ; preds = %29
%34 = load i32, i32* %23
%35 = call i32 @atoi(i32 %34)
%36 = add i32 %35, -1
%37 = icmp ult i32 %36, 32767
br i1 %37, label %39, label %38
38: ; preds = %22, %33, %43, %39
br label %18
39: ; preds = %33
%40 = call i32 @parse_proc_status_val_by_tag(i32 %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%41 = call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
br i1 %42, label %38, label %43
43: ; preds = %39
%44 = call i64 @strcmp(i8* %9, i8* %8)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %38
46: ; preds = %43
%47 = add nuw nsw i32 %17, 1
%48 = zext i32 %17 to i64
%49 = getelementptr inbounds i32, i32* %1, i64 %48
store i32 %35, i32* %49
br label %16
50: ; preds = %19
store i32 %17, i32* %2
br label %51
51: ; preds = %3, %50
ret void
}