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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ cons_printf(char *, int, char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *, char *, char *,
                          int *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ pids_cmdline(int, char *);
scalar_t__ readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int pids_sons_of_r(int pid, int recursive, int limit) {
  int p;
  int n = 0;
  int mola;
  char buf[128];
  int tmp;
  char tmp2[1024];
  char tmp3[8];
  struct dirent *file;
  FILE *fd;
  DIR *dh = opendir("/proc/");
  if (pid == 0 || dh == NULL)
    return 0;
  while ((file = (struct dirent *)readdir(dh))) {
    p = atoi(file->d_name);
    if (p) {
      sprintf(buf, "/proc/%s/stat", file->d_name);
      fd = fopen(buf, "r");
      if (fd) {
        mola = 0;
        fscanf(fd, "%d %s %s %d", &tmp, tmp2, tmp3, &mola);
        if (mola == pid) {
          pids_cmdline(p, tmp2);
          cons_printf(" `- %d : %s (%s)\n", p, tmp2,
                      (tmp3[0] == 'S')   ? "sleeping"
                      : (tmp3[0] == 'T') ? "stopped"
                                         : "running");
          n++;
          if (recursive < limit)
            n += pids_sons_of_r(p, recursive + 1, limit);
        }
      }
      fclose(fd);
    }
  }
  return n;
}


x86 assembly (clang, -O0):
<pids_sons_of_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4f0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
lea    0xe3e(%rip),%rdi
call   1070 <opendir@plt>
mov    %rax,-0x4d0(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11e6 <pids_sons_of_r+0x46>
cmpq   $0x0,-0x4d0(%rbp)
jne    11f2 <pids_sons_of_r+0x52>
movl   $0x0,-0x4(%rbp)
jmp    138d <pids_sons_of_r+0x1ed>
jmp    11f7 <pids_sons_of_r+0x57>
mov    -0x4d0(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x4c0(%rbp)
cmp    $0x0,%rax
je     1387 <pids_sons_of_r+0x1e7>
mov    -0x4c0(%rbp),%rax
mov    (%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1382 <pids_sons_of_r+0x1e2>
lea    -0xa0(%rbp),%rdi
mov    -0x4c0(%rbp),%rax
mov    (%rax),%rdx
lea    0xdbf(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xdba(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x4c8(%rbp)
cmpq   $0x0,-0x4c8(%rbp)
je     1376 <pids_sons_of_r+0x1d6>
movl   $0x0,-0x1c(%rbp)
mov    -0x4c8(%rbp),%rdi
lea    -0x4b0(%rbp),%rcx
lea    -0x4b8(%rbp),%r8
lea    0xd7f(%rip),%rsi
lea    -0xa4(%rbp),%rdx
lea    -0x1c(%rbp),%r9
call   1080 <fscanf@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1371 <pids_sons_of_r+0x1d1>
mov    -0x14(%rbp),%edi
lea    -0x4b0(%rbp),%rsi
call   1030 <pids_cmdline@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4dc(%rbp)
lea    -0x4b0(%rbp),%rax
mov    %rax,-0x4d8(%rbp)
movsbl -0x4b8(%rbp),%eax
cmp    $0x53,%eax
jne    12fd <pids_sons_of_r+0x15d>
lea    0xd44(%rip),%rax
mov    %rax,-0x4e8(%rbp)
jmp    1320 <pids_sons_of_r+0x180>
movsbl -0x4b8(%rbp),%edx
lea    0xd3b(%rip),%rax
lea    0xd2c(%rip),%rcx
cmp    $0x54,%edx
cmove  %rcx,%rax
mov    %rax,-0x4e8(%rbp)
mov    -0x4d8(%rbp),%rdx
mov    -0x4dc(%rbp),%esi
mov    -0x4e8(%rbp),%rcx
lea    0xce8(%rip),%rdi
call   1060 <cons_printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    136c <pids_sons_of_r+0x1cc>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%edx
call   1040 <pids_sons_of_r@plt>
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1371 <pids_sons_of_r+0x1d1>
jmp    1376 <pids_sons_of_r+0x1d6>
mov    -0x4c8(%rbp),%rdi
call   1050 <fclose@plt>
jmp    11f7 <pids_sons_of_r+0x57>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4f0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pids_sons_of_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4b8,%rsp
mov    %edx,0x10(%rsp)
mov    %rsi,0x18(%rsp)
mov    %edi,%ebx
lea    0xe3d(%rip),%rdi
call   1070 <opendir@plt>
mov    %ebx,0x14(%rsp)
test   %ebx,%ebx
je     1316 <pids_sons_of_r+0x176>
mov    %rax,%r12
movl   $0x0,0x4(%rsp)
test   %rax,%rax
je     131e <pids_sons_of_r+0x17e>
mov    %r12,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     131e <pids_sons_of_r+0x17e>
mov    0x18(%rsp),%rcx
add    $0x1,%ecx
mov    %ecx,0xc(%rsp)
movl   $0x0,0x4(%rsp)
lea    0xb0(%rsp),%r14
jmp    1234 <pids_sons_of_r+0x94>
mov    %ebp,0x4(%rsp)
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     131e <pids_sons_of_r+0x17e>
mov    (%rax),%rbx
mov    %rbx,%rdi
call   10b0 <atoi@plt>
test   %eax,%eax
je     1223 <pids_sons_of_r+0x83>
mov    %eax,%r13d
lea    0x30(%rsp),%rbp
mov    %rbp,%rdi
lea    0xdb2(%rip),%rsi
mov    %rbx,%rdx
call   10c0 <sprintf@plt>
mov    %rbp,%rdi
lea    0xdae(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
je     121b <pids_sons_of_r+0x7b>
movl   $0x0,0x8(%rsp)
mov    %r15,%rdi
lea    0xd91(%rip),%rsi
lea    0x24(%rsp),%rdx
mov    %r14,%rcx
lea    0x28(%rsp),%r8
lea    0x8(%rsp),%r9
call   1080 <fscanf@plt>
mov    0x14(%rsp),%eax
cmp    %eax,0x8(%rsp)
jne    121b <pids_sons_of_r+0x7b>
mov    %r13d,%edi
mov    %r14,%rsi
call   1030 <pids_cmdline@plt>
movzbl 0x28(%rsp),%eax
cmp    $0x54,%al
lea    0xd82(%rip),%rcx
lea    0xd73(%rip),%rdx
cmove  %rdx,%rcx
cmp    $0x53,%al
lea    0xd5d(%rip),%rax
cmove  %rax,%rcx
lea    0xd40(%rip),%rdi
mov    %r13d,%esi
mov    %r14,%rdx
call   1060 <cons_printf@plt>
mov    0x4(%rsp),%ebp
add    $0x1,%ebp
mov    0x10(%rsp),%edx
cmp    %edx,0x18(%rsp)
jge    1217 <pids_sons_of_r+0x77>
mov    %r13d,%edi
mov    0xc(%rsp),%esi
call   1040 <pids_sons_of_r@plt>
add    %eax,%ebp
jmp    1217 <pids_sons_of_r+0x77>
movl   $0x0,0x4(%rsp)
mov    0x4(%rsp),%eax
add    $0x4b8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [7 x i8] c"/proc/\00"
@.str.1 = [14 x i8] c"/proc/%s/stat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [12 x i8] c"%d %s %s %d\00"
@.str.4 = [18 x i8] c" `- %d : %s (%s)\0A\00"
@.str.5 = [9 x i8] c"sleeping\00"
@.str.6 = [8 x i8] c"stopped\00"
@.str.7 = [8 x i8] c"running\00"
define i32 @pids_sons_of_r(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca [8 x i8]
%9 = bitcast i32* %4 to i8*
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%11 = bitcast i32* %6 to i8*
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%13 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%14 = tail call i32* @opendir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i32 %0, 0
%16 = icmp eq i32* %14, null
%17 = or i1 %15, %16
br i1 %17, label %59, label %18
18: ; preds = %3
%19 = call i64 @readdir(i32* %14)
%20 = icmp eq i64 %19, 0
br i1 %20, label %59, label %21
21: ; preds = %18
%22 = icmp slt i32 %1, %2
%23 = add nsw i32 %1, 1
br label %24
24: ; preds = %21, %55
%25 = phi i64 [ %19, %21 ], [ %57, %55 ]
%26 = phi i32 [ 0, %21 ], [ %56, %55 ]
%27 = inttoptr i64 %25 to %struct.dirent*
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %55, label %32
32: ; preds = %24
%33 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%34 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%35 = icmp eq i32* %34, null
br i1 %35, label %52, label %36
36: ; preds = %32
store i32 0, i32* %4
%37 = call i32 @fscanf(i32* %34, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* %6, i8* %12, i8* %13, i32* %4)
%38 = load i32, i32* %4
%39 = icmp eq i32 %38, %0
br i1 %39, label %40, label %52
40: ; preds = %36
%41 = call i32 @pids_cmdline(i32 %30, i8* %12)
%42 = load i8, i8* %13
%43 = icmp eq i8 %42, 83
%44 = icmp eq i8 %42, 84
%45 = select i1 %44, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0)
%46 = select i1 %43, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %45
%47 = call i32 @cons_printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %30, i8* %12, i8* %46)
%48 = add nsw i32 %26, 1
br i1 %22, label %49, label %52
49: ; preds = %40
%50 = call i32 @pids_sons_of_r(i32 %30, i32 %23, i32 %2)
%51 = add nsw i32 %50, %48
br label %52
52: ; preds = %36, %49, %40, %32
%53 = phi i32 [ %51, %49 ], [ %48, %40 ], [ %26, %36 ], [ %26, %32 ]
%54 = call i32 @fclose(i32* %34)
br label %55
55: ; preds = %52, %24
%56 = phi i32 [ %53, %52 ], [ %26, %24 ]
%57 = call i64 @readdir(i32* %14)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %24
59: ; preds = %55, %18, %3
%60 = phi i32 [ 0, %3 ], [ 0, %18 ], [ %56, %55 ]
ret i32 %60
}