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

/* Variables and functions */
scalar_t__ EOF;
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int cnt_childs(pid_t ppid) {
  int result = 0;
  char cmd[32];
  sprintf(cmd, "ps -o pid= --ppid %d", ppid);
  FILE *f = popen(cmd, "r");
  pid_t pid;
  while (fscanf(f, "%d", &pid) != EOF) {
    result += 1 + cnt_childs(pid);
  }
  pclose(f);
  return result;
}


x86 assembly (clang, -O0):
<cnt_childs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe90(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x30(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe7e(%rip),%rsi
lea    -0x3c(%rbp),%rdx
call   1040 <fscanf@plt>
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
je     11c3 <cnt_childs+0x73>
mov    -0x3c(%rbp),%edi
call   1050 <cnt_childs@plt>
add    $0x1,%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    1189 <cnt_childs+0x39>
mov    -0x38(%rbp),%rdi
call   1030 <pclose@plt>
mov    -0x8(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cnt_childs>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %edi,%edx
lea    0xe9a(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xe9b(%rip),%rsi
mov    %rbx,%rdi
call   1060 <popen@plt>
mov    %rax,%r14
lea    0xe86(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    0x2e48(%rip),%r13
xor    %ebx,%ebx
cmp    0x0(%r13),%rax
je     11e2 <cnt_childs+0x92>
xor    %ebx,%ebx
lea    0xe61(%rip),%r15
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    0xc(%rsp),%edi
call   1050 <cnt_childs@plt>
add    %eax,%ebx
add    $0x1,%ebx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <fscanf@plt>
cmp    0x0(%r13),%rax
jne    11c0 <cnt_childs+0x70>
mov    %r14,%rdi
call   1030 <pclose@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"ps -o pid= --ppid %d\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@EOF = global i64 0
define i32 @cnt_childs(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [32 x i8]
%5 = alloca i32*
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* %2
%9 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%11 = call i32* @popen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %5
br label %12
12: ; preds = %17, %1
%13 = load i32*, i32** %5
%14 = call i64 @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %6)
%15 = load i64, i64* @EOF
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %23
17: ; preds = %12
%18 = load i32, i32* %6
%19 = call i32 @cnt_childs(i32 %18)
%20 = add nsw i32 1, %19
%21 = load i32, i32* %3
%22 = add nsw i32 %21, %20
store i32 %22, i32* %3
br label %12
23: ; preds = %12
%24 = load i32*, i32** %5
%25 = call i32 @pclose(i32* %24)
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [21 x i8] c"ps -o pid= --ppid %d\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@EOF = global i64 0
define i32 @cnt_childs(i32 %0) {
%2 = alloca [32 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = bitcast i32* %3 to i8*
%8 = call i64 @fscanf(i32* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%9 = load i64, i64* @EOF
%10 = icmp eq i64 %8, %9
br i1 %10, label %20, label %11
11: ; preds = %1, %11
%12 = phi i32 [ %16, %11 ], [ 0, %1 ]
%13 = load i32, i32* %3
%14 = call i32 @cnt_childs(i32 %13)
%15 = add i32 %12, 1
%16 = add i32 %15, %14
%17 = call i64 @fscanf(i32* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%18 = load i64, i64* @EOF
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %11
20: ; preds = %11, %1
%21 = phi i32 [ 0, %1 ], [ %16, %11 ]
%22 = call i32 @pclose(i32* %6)
ret i32 %21
}