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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int, char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *, int *);
scalar_t__ is_numeric(char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
pid_t *get_pid_tree(pid_t target_pid, pid_t *pidlist) {
  FILE *fp;
  DIR *dp;
  struct dirent *dir_entry;
  int errno_bak, pidlist_count;
  const char base_path[] = "/proc/";
  char stat_file_path[BUFSIZ];
  pid_t pid, ppid;
  dp = opendir(base_path);
  if (dp == NULL) {
    error(0, errno, "Error: %s", base_path);
    return NULL;
  }
  for (pidlist_count = 0; pidlist_count < 32768; pidlist_count++) {
    pidlist[pidlist_count] = -1;
  }
  pidlist_count = 0;
  errno_bak = errno;
  errno = 0;
  while ((dir_entry = readdir(dp)) != NULL) {
    if (is_numeric(dir_entry->d_name)) {
      strcpy(stat_file_path, base_path);
      strcat(stat_file_path, dir_entry->d_name);
      strcat(stat_file_path, "/stat");
      fp = fopen(stat_file_path, "r");
      if (fp == NULL) {
        error(0, errno, "Error: %s", stat_file_path);
        continue;
      }
      fscanf(fp, " %d %*s %*s %d", &pid, &ppid);
      if (ppid == target_pid) {
        pidlist[pidlist_count++] = pid;
      }
      fclose(fp);
    }
  }
  if (errno != 0) {
    error(0, errno, "Error");
  }
  errno = errno_bak;
  closedir(dp);
  return pidlist;
}


x86 assembly (clang, -O0):
<get_pid_tree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x2f636f,-0x3d(%rbp)
movl   $0x6f72702f,-0x40(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1232 <get_pid_tree+0x92>
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rcx
xor    %edi,%edi
lea    0xdf0(%rip),%rdx
mov    $0x0,%al
call   10a0 <error@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    13b5 <get_pid_tree+0x215>
movl   $0x0,-0x38(%rbp)
cmpl   $0x8000,-0x38(%rbp)
jge    1263 <get_pid_tree+0xc3>
mov    -0x18(%rbp),%rax
movslq -0x38(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1239 <get_pid_tree+0x99>
movl   $0x0,-0x38(%rbp)
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d5b(%rip),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1368 <get_pid_tree+0x1c8>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1060 <is_numeric@plt>
cmp    $0x0,%rax
je     1363 <get_pid_tree+0x1c3>
mov    -0x68(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x68(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   10c0 <strcat@plt>
mov    -0x68(%rbp),%rdi
lea    0xd39(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x68(%rbp),%rdi
lea    0xd42(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    131e <get_pid_tree+0x17e>
mov    -0x68(%rbp),%rcx
mov    0x2cd1(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xcf5(%rip),%rdx
mov    $0x0,%al
call   10a0 <error@plt>
jmp    1283 <get_pid_tree+0xe3>
mov    -0x20(%rbp),%rdi
lea    0xcee(%rip),%rsi
lea    -0x54(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1070 <fscanf@plt>
mov    -0x58(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    135a <get_pid_tree+0x1ba>
mov    -0x54(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x38(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1283 <get_pid_tree+0xe3>
mov    0x2c69(%rip),%rax
cmpl   $0x0,(%rax)
je     1391 <get_pid_tree+0x1f1>
mov    0x2c59(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xc9c(%rip),%rdx
mov    $0x0,%al
call   10a0 <error@plt>
mov    -0x34(%rbp),%ecx
mov    0x2c3d(%rip),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pid_tree>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r15
mov    %edi,-0x3c(%rbp)
mov    %rsp,%rbx
movl   $0x2f636f,-0x31(%rbp)
movl   $0x6f72702f,-0x34(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    -0x34(%rbp),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1350 <get_pid_tree+0x1a0>
mov    %rax,%r12
mov    %rbx,-0x58(%rbp)
mov    $0x20000,%edx
mov    %r15,-0x50(%rbp)
mov    %r15,%rdi
mov    $0xff,%esi
call   1070 <memset@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,-0x38(%rbp)
movl   $0x0,(%rax)
mov    %r12,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1315 <get_pid_tree+0x165>
mov    %rax,%r13
lea    0xddc(%rip),%r15
movl   $0x0,-0x2c(%rbp)
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%r13),%rdi
call   1060 <is_numeric@plt>
test   %rax,%rax
je     12c1 <get_pid_tree+0x111>
mov    %r14,%rdi
lea    -0x34(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x0(%r13),%rsi
mov    %r14,%rdi
call   10d0 <strcat@plt>
mov    %r14,%rdi
lea    0xd81(%rip),%rsi
call   10d0 <strcat@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
jne    12d3 <get_pid_tree+0x123>
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xd50(%rip),%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   10b0 <error@plt>
mov    %r12,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1260 <get_pid_tree+0xb0>
jmp    1315 <get_pid_tree+0x165>
mov    %rax,%rbx
mov    %rax,%rdi
lea    0xd37(%rip),%rsi
lea    -0x44(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1080 <fscanf@plt>
mov    -0x3c(%rbp),%eax
cmp    %eax,-0x40(%rbp)
jne    130b <get_pid_tree+0x15b>
mov    -0x44(%rbp),%eax
mov    -0x2c(%rbp),%edx
movslq %edx,%rcx
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
mov    -0x50(%rbp),%rdx
mov    %eax,(%rdx,%rcx,4)
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    12c1 <get_pid_tree+0x111>
mov    0x2cbc(%rip),%rax
mov    (%rax),%esi
test   %esi,%esi
je     1332 <get_pid_tree+0x182>
lea    0xcfd(%rip),%rdx
xor    %edi,%edi
xor    %eax,%eax
call   10b0 <error@plt>
mov    0x2c9f(%rip),%rax
mov    -0x38(%rbp),%ecx
mov    %ecx,(%rax)
mov    %r12,%rdi
call   1090 <closedir@plt>
mov    -0x50(%rbp),%r14
mov    -0x58(%rbp),%rbx
jmp    1370 <get_pid_tree+0x1c0>
mov    0x2c81(%rip),%rax
mov    (%rax),%esi
lea    0xca7(%rip),%rdx
xor    %r14d,%r14d
lea    -0x34(%rbp),%rcx
xor    %edi,%edi
xor    %eax,%eax
call   10b0 <error@plt>
mov    %rbx,%rsp
mov    %r14,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@__const.get_pid_tree.base_path = [7 x i8] c"/proc/\00"
@BUFSIZ = global i32 0
@errno = global i32 0
@.str = [10 x i8] c"Error: %s\00"
@.str.1 = [6 x i8] c"/stat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [15 x i8] c" %d %*s %*s %d\00"
@.str.4 = [6 x i8] c"Error\00"
define i32* @get_pid_tree(i32 %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [7 x i8]
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%17 = bitcast [7 x i8]* %11 to i8*
%18 = load i32, i32* @BUFSIZ
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %12
%21 = alloca i8, i64 %19
store i64 %19, i64* %13
%22 = getelementptr inbounds [7 x i8], [7 x i8]* %11, i64 0, i64 0
%23 = call i32* @opendir(i8* %22)
store i32* %23, i32** %7
%24 = load i32*, i32** %7
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %30
26: ; preds = %2
%27 = load i32, i32* @errno
%28 = getelementptr inbounds [7 x i8], [7 x i8]* %11, i64 0, i64 0
%29 = call i32 (i32, i32, i8*, ...) @error(i32 0, i32 %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %28)
store i32* null, i32** %3
store i32 1, i32* %16
br label %96
30: ; preds = %2
store i32 0, i32* %10
br label %31
31: ; preds = %39, %30
%32 = load i32, i32* %10
%33 = icmp slt i32 %32, 32768
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = load i32*, i32** %5
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
store i32 -1, i32* %38
br label %39
39: ; preds = %34
%40 = load i32, i32* %10
%41 = add nsw i32 %40, 1
store i32 %41, i32* %10
br label %31
42: ; preds = %31
store i32 0, i32* %10
%43 = load i32, i32* @errno
store i32 %43, i32* %9
store i32 0, i32* @errno
br label %44
44: ; preds = %84, %65, %42
%45 = load i32*, i32** %7
%46 = call %struct.dirent* @readdir(i32* %45)
store %struct.dirent* %46, %struct.dirent** %8
%47 = icmp ne %struct.dirent* %46, null
br i1 %47, label %48, label %85
48: ; preds = %44
%49 = load %struct.dirent*, %struct.dirent** %8
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = call i64 @is_numeric(i8* %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %84
54: ; preds = %48
%55 = getelementptr inbounds [7 x i8], [7 x i8]* %11, i64 0, i64 0
%56 = call i32 @strcpy(i8* %21, i8* %55)
%57 = load %struct.dirent*, %struct.dirent** %8
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i32 0, i32 0
%59 = load i8*, i8** %58
%60 = call i32 @strcat(i8* %21, i8* %59)
%61 = call i32 @strcat(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%62 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %62, i32** %6
%63 = load i32*, i32** %6
%64 = icmp eq i32* %63, null
br i1 %64, label %65, label %68
65: ; preds = %54
%66 = load i32, i32* @errno
%67 = call i32 (i32, i32, i8*, ...) @error(i32 0, i32 %66, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %44
68: ; preds = %54
%69 = load i32*, i32** %6
%70 = call i32 @fscanf(i32* %69, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32* %14, i32* %15)
%71 = load i32, i32* %15
%72 = load i32, i32* %4
%73 = icmp eq i32 %71, %72
br i1 %73, label %74, label %81
74: ; preds = %68
%75 = load i32, i32* %14
%76 = load i32*, i32** %5
%77 = load i32, i32* %10
%78 = add nsw i32 %77, 1
store i32 %78, i32* %10
%79 = sext i32 %77 to i64
%80 = getelementptr inbounds i32, i32* %76, i64 %79
store i32 %75, i32* %80
br label %81
81: ; preds = %74, %68
%82 = load i32*, i32** %6
%83 = call i32 @fclose(i32* %82)
br label %84
84: ; preds = %81, %48
br label %44
85: ; preds = %44
%86 = load i32, i32* @errno
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %91
88: ; preds = %85
%89 = load i32, i32* @errno
%90 = call i32 (i32, i32, i8*, ...) @error(i32 0, i32 %89, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %91
91: ; preds = %88, %85
%92 = load i32, i32* %9
store i32 %92, i32* @errno
%93 = load i32*, i32** %7
%94 = call i32 @closedir(i32* %93)
%95 = load i32*, i32** %5
store i32* %95, i32** %3
store i32 1, i32* %16
br label %96
96: ; preds = %91, %26
%97 = load i8*, i8** %12
%98 = load i32*, i32** %3
ret i32* %98
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@__const.get_pid_tree.base_path = [7 x i8] c"/proc/\00"
@BUFSIZ = global i32 0
@errno = global i32 0
@.str = [10 x i8] c"Error: %s\00"
@.str.1 = [6 x i8] c"/stat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [15 x i8] c" %d %*s %*s %d\00"
@.str.4 = [6 x i8] c"Error\00"
define i32* @get_pid_tree(i32 %0, i32* writeonly %1) {
%3 = alloca [7 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @BUFSIZ
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = bitcast i32* %4 to i8*
%12 = bitcast i32* %5 to i8*
%13 = call i32* @opendir(i8* %6)
%14 = icmp eq i32* %13, null
br i1 %14, label %20, label %15
15: ; preds = %2
%16 = bitcast i32* %1 to i8*
%17 = load i32, i32* @errno
store i32 0, i32* @errno
%18 = call %struct.dirent* @readdir(i32* %13)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %60, label %23
20: ; preds = %2
%21 = load i32, i32* @errno
%22 = call i32 (i32, i32, i8*, ...) @error(i32 0, i32 %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %67
23: ; preds = %15, %56
%24 = phi %struct.dirent* [ %58, %56 ], [ %18, %15 ]
%25 = phi i32 [ %57, %56 ], [ 0, %15 ]
br label %26
26: ; preds = %23, %39
%27 = phi %struct.dirent* [ %24, %23 ], [ %42, %39 ]
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%29 = load i8*, i8** %28
%30 = call i64 @is_numeric(i8* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %56, label %32
32: ; preds = %26
%33 = call i32 @strcpy(i8* %10, i8* %6)
%34 = load i8*, i8** %28
%35 = call i32 @strcat(i8* %10, i8* %34)
%36 = call i32 @strcat(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %44
39: ; preds = %32
%40 = load i32, i32* @errno
%41 = call i32 (i32, i32, i8*, ...) @error(i32 0, i32 %40, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %10)
%42 = call %struct.dirent* @readdir(i32* %13)
%43 = icmp eq %struct.dirent* %42, null
br i1 %43, label %60, label %26
44: ; preds = %32
%45 = call i32 @fscanf(i32* %37, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32* %4, i32* %5)
%46 = load i32, i32* %5
%47 = icmp eq i32 %46, %0
br i1 %47, label %48, label %53
48: ; preds = %44
%49 = load i32, i32* %4
%50 = add nsw i32 %25, 1
%51 = sext i32 %25 to i64
%52 = getelementptr inbounds i32, i32* %1, i64 %51
store i32 %49, i32* %52
br label %53
53: ; preds = %48, %44
%54 = phi i32 [ %50, %48 ], [ %25, %44 ]
%55 = call i32 @fclose(i32* %37)
br label %56
56: ; preds = %26, %53
%57 = phi i32 [ %54, %53 ], [ %25, %26 ]
%58 = call %struct.dirent* @readdir(i32* %13)
%59 = icmp eq %struct.dirent* %58, null
br i1 %59, label %60, label %23
60: ; preds = %56, %39, %15
%61 = load i32, i32* @errno
%62 = icmp eq i32 %61, 0
br i1 %62, label %65, label %63
63: ; preds = %60
%64 = call i32 (i32, i32, i8*, ...) @error(i32 0, i32 %61, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %65
65: ; preds = %63, %60
store i32 %17, i32* @errno
%66 = call i32 @closedir(i32* %13)
br label %67
67: ; preds = %65, %20
%68 = phi i32* [ null, %20 ], [ %1, %65 ]
ret i32* %68
}