#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ print_process_command_line(int);
int /*<<< orphan*/ printf(char *, ...);
char *read_from_file(int, int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ sscanf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
char *strtok(char *, char *);
void print_process_tree(int *proc_list) {
  char filename[64], token1[64], token2[64], proc_name[64], *read_buf;
  int fd, i;
  int num_processes = proc_list[0];
  char *str;
  int ppid;
  printf("PID\t%-20s\tPPID\tCOMMAND\n", "NAME");
  printf("--------------------------------------------------\n");
  for (i = 1; i < num_processes; i++) {
    sprintf(filename, "/proc/%d/status", proc_list[i]);
    fd = open(filename, O_RDONLY);
    if (fd != -1) {
      read_buf = read_from_file(fd, 512);
      if (read_buf != NULL) {
        str = strtok(read_buf, "\n");
        while (str != NULL) {
          sscanf(str, "%s %s", token1, token2);
          if (strcmp(token1, "Name:") == 0) {
            sprintf(proc_name, "%s", token2);
          }
          if (strcmp(token1, "PPid:") == 0) {
            ppid = atoi(token2);
          }
          str = strtok(NULL, "\n");
        }
        free(read_buf);
      }
    }
    printf("%d\t%-20s\t%d\t", proc_list[i], proc_name, ppid);
    print_process_command_line(proc_list[i]);
  }
}


x86 assembly (clang, -O0):
<print_process_tree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x124(%rbp)
lea    0xe3e(%rip),%rdi
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x120(%rbp)
mov    -0x120(%rbp),%eax
cmp    -0x124(%rbp),%eax
jge    1392 <print_process_tree+0x1f2>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x120(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x11c(%rbp)
cmpl   $0xffffffff,-0x11c(%rbp)
je     1342 <print_process_tree+0x1a2>
mov    -0x11c(%rbp),%edi
mov    $0x200,%esi
call   1040 <read_from_file@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
je     133d <print_process_tree+0x19d>
mov    -0x118(%rbp),%rdi
lea    0xddd(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x130(%rbp)
cmpq   $0x0,-0x130(%rbp)
je     1331 <print_process_tree+0x191>
mov    -0x130(%rbp),%rdi
lea    -0x90(%rbp),%rdx
lea    -0xd0(%rbp),%rcx
lea    0xdb9(%rip),%rsi
call   1070 <sscanf@plt>
lea    -0x90(%rbp),%rdi
lea    0xdac(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12e6 <print_process_tree+0x146>
lea    -0x110(%rbp),%rdi
lea    -0xd0(%rbp),%rdx
lea    0xd85(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xd79(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1315 <print_process_tree+0x175>
lea    -0xd0(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x134(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd2f(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x130(%rbp)
jmp    127e <print_process_tree+0xde>
mov    -0x118(%rbp),%rdi
call   1030 <free@plt>
jmp    1342 <print_process_tree+0x1a2>
mov    -0x8(%rbp),%rax
movslq -0x120(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    -0x110(%rbp),%rdx
mov    -0x134(%rbp),%ecx
lea    0xd0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0x120(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   10c0 <print_process_command_line@plt>
mov    -0x120(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x120(%rbp)
jmp    11e8 <print_process_tree+0x48>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_process_tree>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %rdi,%rbx
mov    (%rdi),%ebp
lea    0xe23(%rip),%rdi
lea    0xe34(%rip),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe5a(%rip),%rdi
call   1060 <puts@plt>
mov    %rbp,0x18(%rsp)
cmp    $0x2,%ebp
jl     1348 <print_process_tree+0x188>
mov    $0x1,%r13d
lea    0xe04(%rip),%rbp
lea    0xa0(%rsp),%r12
lea    0x60(%rsp),%r15
mov    %rbx,0x8(%rsp)
jmp    1271 <print_process_tree+0xb1>
cs nopw 0x0(%rax,%rax,1)
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rbx
mov    (%rbx,%r13,4),%esi
lea    0xdf5(%rip),%rdi
lea    0x20(%rsp),%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1070 <printf@plt>
mov    (%rbx,%r13,4),%edi
call   10e0 <print_process_command_line@plt>
add    $0x1,%r13
cmp    0x18(%rsp),%r13
je     1348 <print_process_tree+0x188>
mov    (%rbx,%r13,4),%edx
lea    0xe0(%rsp),%rbx
mov    %rbx,%rdi
lea    0xd96(%rip),%rsi
xor    %eax,%eax
call   10d0 <sprintf@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     123a <print_process_tree+0x7a>
mov    %eax,%edi
mov    $0x200,%esi
call   1040 <read_from_file@plt>
test   %rax,%rax
je     123a <print_process_tree+0x7a>
mov    %rax,0x10(%rsp)
mov    %rax,%rdi
mov    %rbp,%rsi
call   10b0 <strtok@plt>
test   %rax,%rax
je     1230 <print_process_tree+0x70>
lea    0x20(%rsp),%rbx
jmp    12f3 <print_process_tree+0x133>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %edi,%edi
mov    %rbp,%rsi
call   10b0 <strtok@plt>
test   %rax,%rax
je     1230 <print_process_tree+0x70>
mov    %rax,%rdi
lea    0xd30(%rip),%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   1090 <sscanf@plt>
mov    %r12,%rdi
lea    0xd21(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    1327 <print_process_tree+0x167>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strcpy@plt>
mov    %r12,%rdi
lea    0xd08(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    12e0 <print_process_tree+0x120>
mov    %r15,%rdi
call   10c0 <atoi@plt>
mov    %eax,%r14d
jmp    12e0 <print_process_tree+0x120>
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"PID\09%-20s\09PPID\09COMMAND\0A\00"
@.str.1 = [5 x i8] c"NAME\00"
@.str.2 = [52 x i8] c"--------------------------------------------------\0A\00"
@.str.3 = [16 x i8] c"/proc/%d/status\00"
@O_RDONLY = global i32 0
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [6 x i8] c"%s %s\00"
@.str.6 = [6 x i8] c"Name:\00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [6 x i8] c"PPid:\00"
@.str.9 = [13 x i8] c"%d\09%-20s\09%d\09\00"
define void @print_process_tree(i32* %0) {
%2 = alloca i32*
%3 = alloca [64 x i8]
%4 = alloca [64 x i8]
%5 = alloca [64 x i8]
%6 = alloca [64 x i8]
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i32* %0, i32** %2
%13 = load i32*, i32** %2
%14 = getelementptr inbounds i32, i32* %13, i64 0
%15 = load i32, i32* %14
store i32 %15, i32* %10
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %9
br label %18
18: ; preds = %86, %1
%19 = load i32, i32* %9
%20 = load i32, i32* %10
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %89
22: ; preds = %18
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%24 = load i32*, i32** %2
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = call i32 (i8*, i8*, ...) @sprintf(i8* %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%31 = load i32, i32* @O_RDONLY
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp ne i32 %33, -1
br i1 %34, label %35, label %71
35: ; preds = %22
%36 = load i32, i32* %8
%37 = call i8* @read_from_file(i32 %36, i32 512)
store i8* %37, i8** %7
%38 = load i8*, i8** %7
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %70
40: ; preds = %35
%41 = load i8*, i8** %7
%42 = call i8* @strtok(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %42, i8** %11
br label %43
43: ; preds = %65, %40
%44 = load i8*, i8** %11
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %67
46: ; preds = %43
%47 = load i8*, i8** %11
%48 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%49 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%50 = call i32 @sscanf(i8* %47, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %48, i8* %49)
%51 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%52 = call i64 @strcmp(i8* %51, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %46
%55 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%56 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%57 = call i32 (i8*, i8*, ...) @sprintf(i8* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %56)
br label %58
58: ; preds = %54, %46
%59 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%60 = call i64 @strcmp(i8* %59, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %58
%63 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%64 = call i32 @atoi(i8* %63)
store i32 %64, i32* %12
br label %65
65: ; preds = %62, %58
%66 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %66, i8** %11
br label %43
67: ; preds = %43
%68 = load i8*, i8** %7
%69 = call i32 @free(i8* %68)
br label %70
70: ; preds = %67, %35
br label %71
71: ; preds = %70, %22
%72 = load i32*, i32** %2
%73 = load i32, i32* %9
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i32, i32* %72, i64 %74
%76 = load i32, i32* %75
%77 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%78 = load i32, i32* %12
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0), i32 %76, i8* %77, i32 %78)
%80 = load i32*, i32** %2
%81 = load i32, i32* %9
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i32, i32* %80, i64 %82
%84 = load i32, i32* %83
%85 = call i32 @print_process_command_line(i32 %84)
br label %86
86: ; preds = %71
%87 = load i32, i32* %9
%88 = add nsw i32 %87, 1
store i32 %88, i32* %9
br label %18
89: ; preds = %18
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"PID\09%-20s\09PPID\09COMMAND\0A\00"
@.str.1 = [5 x i8] c"NAME\00"
@.str.3 = [16 x i8] c"/proc/%d/status\00"
@O_RDONLY = global i32 0
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [6 x i8] c"%s %s\00"
@.str.6 = [6 x i8] c"Name:\00"
@.str.8 = [6 x i8] c"PPid:\00"
@.str.9 = [13 x i8] c"%d\09%-20s\09%d\09\00"
@str = [51 x i8] c"--------------------------------------------------\00"
define void @print_process_tree(i32* nocapture %0) {
%2 = alloca [64 x i8]
%3 = alloca [64 x i8]
%4 = alloca [64 x i8]
%5 = alloca [64 x i8]
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* %0
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
%13 = icmp sgt i32 %10, 1
br i1 %13, label %14, label %59
14: ; preds = %1
%15 = zext i32 %10 to i64
br label %16
16: ; preds = %14, %51
%17 = phi i64 [ 1, %14 ], [ %57, %51 ]
%18 = phi i32 [ undef, %14 ], [ %52, %51 ]
%19 = getelementptr inbounds i32, i32* %0, i64 %17
%20 = load i32, i32* %19
%21 = call i32 (i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %6, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %51, label %25
25: ; preds = %16
%26 = call i8* @read_from_file(i32 %23, i32 512)
%27 = icmp eq i8* %26, null
br i1 %27, label %51, label %28
28: ; preds = %25
%29 = call i8* @strtok(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%30 = icmp eq i8* %29, null
br i1 %30, label %48, label %31
31: ; preds = %28, %44
%32 = phi i32 [ %45, %44 ], [ %18, %28 ]
%33 = phi i8* [ %46, %44 ], [ %29, %28 ]
%34 = call i32 @sscanf(i8* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %7, i8* %8)
%35 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i8* @strcpy(i8* %9, i8* %8)
br label %39
39: ; preds = %37, %31
%40 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @atoi(i8* %8)
br label %44
44: ; preds = %42, %39
%45 = phi i32 [ %43, %42 ], [ %32, %39 ]
%46 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %31
48: ; preds = %44, %28
%49 = phi i32 [ %18, %28 ], [ %45, %44 ]
%50 = call i32 @free(i8* %26)
br label %51
51: ; preds = %25, %48, %16
%52 = phi i32 [ %49, %48 ], [ %18, %25 ], [ %18, %16 ]
%53 = load i32, i32* %19
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0), i32 %53, i8* %9, i32 %52)
%55 = load i32, i32* %19
%56 = call i32 @print_process_command_line(i32 %55)
%57 = add nuw nsw i64 %17, 1
%58 = icmp eq i64 %57, %15
br i1 %58, label %59, label %16
59: ; preds = %51, %1
ret void
}