#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 {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ regmatch_t;
typedef int /*<<< orphan*/ regex_t;
typedef int /*<<< orphan*/ buff;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int REG_EXTENDED;
int REG_NEWLINE;
int REG_NOMATCH;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ is_parent(int /*<<< orphan*/, char const *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ regcomp(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ regerror(int, int /*<<< orphan*/ *, char *, int);
int regexec(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
            int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ regfree(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, const char **argv) {
  if (argc < 2) {
    fprintf(stderr, "Incorrect ppid\n");
    return 1;
  }
  const char *ppid = argv[1];
  regex_t process_dir_regex;
  if (regcomp(&process_dir_regex, "^[0-9]+$", REG_EXTENDED | REG_NEWLINE)) {
    fprintf(stderr, "Could not compile process dir regex\n");
    return 1;
  }
  size_t number_of_pmatches = 2;
  regmatch_t pmatch[number_of_pmatches];
  int reti;
  char buff[255];
  const char *proc_dir_name = "/proc/";
  DIR *proc_dir = opendir(proc_dir_name);
  struct dirent *process_dir;
  int children_counter = 0;
  while (NULL != (process_dir = readdir(proc_dir))) {
    reti = regexec(&process_dir_regex, process_dir->d_name, 0, NULL, 0);
    if (0 == reti) {
      children_counter += is_parent(process_dir->d_name, ppid);
    } else if (reti == REG_NOMATCH) {
      continue;
    } else {
      regerror(reti, &process_dir_regex, buff, sizeof(buff));
      fprintf(stderr, "Process dir name regex match failed: %s\n", buff);
      closedir(proc_dir);
      regfree(&process_dir_regex);
      return 1;
    }
  }
  printf("%d\n", children_counter);
  closedir(proc_dir);
  regfree(&process_dir_regex);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11e6 <main+0x46>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b4 <main+0x214>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
mov    0x2de6(%rip),%rax
or     (%rax),%edx
lea    -0x1c(%rbp),%rdi
lea    0xe01(%rip),%rsi
call   1080 <regcomp@plt>
cmp    $0x0,%rax
je     1241 <main+0xa1>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b4 <main+0x214>
movq   $0x2,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,-0x38(%rbp)
lea    0xddf(%rip),%rax
mov    %rax,-0x148(%rbp)
mov    -0x148(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x150(%rbp)
movl   $0x0,-0x15c(%rbp)
mov    -0x150(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,%rcx
mov    %rcx,-0x158(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     1375 <main+0x1d5>
mov    -0x158(%rbp),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdi
xor    %r8d,%r8d
xor    %eax,%eax
mov    %eax,%ecx
mov    %r8d,%edx
call   10c0 <regexec@plt>
mov    %eax,-0x3c(%rbp)
xor    %eax,%eax
cmp    -0x3c(%rbp),%eax
jne    12f8 <main+0x158>
mov    -0x158(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
call   1060 <is_parent@plt>
mov    %rax,%rcx
movslq -0x15c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x15c(%rbp)
jmp    1370 <main+0x1d0>
mov    -0x3c(%rbp),%eax
mov    0x2cde(%rip),%rcx
cmp    (%rcx),%eax
jne    130f <main+0x16f>
jmp    1283 <main+0xe3>
mov    -0x3c(%rbp),%edi
lea    -0x140(%rbp),%rdx
lea    -0x1c(%rbp),%rsi
mov    $0xff,%ecx
call   10a0 <regerror@plt>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
lea    -0x140(%rbp),%rdx
lea    0xd07(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x150(%rbp),%rdi
call   1070 <closedir@plt>
lea    -0x1c(%rbp),%rdi
call   1040 <regfree@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    13ad <main+0x20d>
jmp    1283 <main+0xe3>
mov    -0x15c(%rbp),%esi
lea    0xcec(%rip),%rdi
call   1050 <printf@plt>
mov    -0x150(%rbp),%rdi
call   1070 <closedir@plt>
lea    -0x1c(%rbp),%rdi
call   1040 <regfree@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
cmp    $0x1,%edi
jg     11c8 <main+0x28>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
jmp    1204 <main+0x64>
mov    0x8(%rsi),%r14
mov    0x2df5(%rip),%rax
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe2b(%rip),%rsi
lea    0x4(%rsp),%rdi
call   1080 <regcomp@plt>
test   %rax,%rax
je     1224 <main+0x84>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,0x8(%rsp)
lea    0xe0e(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
xor    %r15d,%r15d
lea    0x4(%rsp),%r12
mov    0x2d99(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     128d <main+0xed>
mov    %rax,%rbp
mov    (%rax),%esi
mov    %r12,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10c0 <regexec@plt>
test   %eax,%eax
je     127d <main+0xdd>
cmp    0x0(%r13),%eax
je     1250 <main+0xb0>
jmp    12a0 <main+0x100>
mov    0x0(%rbp),%edi
mov    %r14,%rsi
call   1060 <is_parent@plt>
add    %eax,%r15d
jmp    1250 <main+0xb0>
lea    0xdda(%rip),%rdi
mov    %r15d,%esi
call   1050 <printf@plt>
xor    %ebp,%ebp
jmp    12d8 <main+0x138>
lea    0x4(%rsp),%rsi
lea    0x10(%rsp),%rbp
mov    %eax,%edi
mov    %rbp,%rdx
mov    $0xff,%ecx
call   10a0 <regerror@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%ebp
mov    0x8(%rsp),%r14
mov    %rbx,%rdi
call   1070 <closedir@plt>
lea    0x4(%rsp),%rdi
call   1040 <regfree@plt>
mov    %r14,%rsp
jmp    1210 <main+0x70>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [16 x i8] c"Incorrect ppid\0A\00"
@.str.1 = [9 x i8] c"^[0-9]+$\00"
@REG_EXTENDED = global i32 0
@REG_NEWLINE = global i32 0
@.str.2 = [37 x i8] c"Could not compile process dir regex\0A\00"
@.str.3 = [7 x i8] c"/proc/\00"
@REG_NOMATCH = global i32 0
@.str.4 = [41 x i8] c"Process dir name regex match failed: %s\0A\00"
@.str.5 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca [255 x i8]
%13 = alloca i8*
%14 = alloca i32*
%15 = alloca %struct.dirent*
%16 = alloca i32
%17 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 2
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %86
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
store i8* %26, i8** %6
%27 = load i32, i32* @REG_EXTENDED
%28 = load i32, i32* @REG_NEWLINE
%29 = or i32 %27, %28
%30 = call i64 @regcomp(i32* %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %86
35: ; preds = %23
store i64 2, i64* %8
%36 = load i64, i64* %8
%37 = call i8* @llvm.stacksave()
store i8* %37, i8** %9
%38 = alloca i32, i64 %36
store i64 %36, i64* %10
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %13
%39 = load i8*, i8** %13
%40 = call i32* @opendir(i8* %39)
store i32* %40, i32** %14
store i32 0, i32* %16
br label %41
41: ; preds = %77, %66, %35
%42 = load i32*, i32** %14
%43 = call %struct.dirent* @readdir(i32* %42)
store %struct.dirent* %43, %struct.dirent** %15
%44 = icmp ne %struct.dirent* null, %43
br i1 %44, label %45, label %78
45: ; preds = %41
%46 = load %struct.dirent*, %struct.dirent** %15
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @regexec(i32* %7, i32 %48, i32 0, i32* null, i32 0)
store i32 %49, i32* %11
%50 = load i32, i32* %11
%51 = icmp eq i32 0, %50
br i1 %51, label %52, label %62
52: ; preds = %45
%53 = load %struct.dirent*, %struct.dirent** %15
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = load i8*, i8** %6
%57 = call i64 @is_parent(i32 %55, i8* %56)
%58 = load i32, i32* %16
%59 = sext i32 %58 to i64
%60 = add nsw i64 %59, %57
%61 = trunc i64 %60 to i32
store i32 %61, i32* %16
br label %77
62: ; preds = %45
%63 = load i32, i32* %11
%64 = load i32, i32* @REG_NOMATCH
%65 = icmp eq i32 %63, %64
br i1 %65, label %66, label %67
66: ; preds = %62
br label %41
67: ; preds = %62
%68 = load i32, i32* %11
%69 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%70 = call i32 @regerror(i32 %68, i32* %7, i8* %69, i32 255)
%71 = load i32, i32* @stderr
%72 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %72)
%74 = load i32*, i32** %14
%75 = call i32 @closedir(i32* %74)
%76 = call i32 @regfree(i32* %7)
store i32 1, i32* %3
store i32 1, i32* %17
br label %84
77: ; preds = %52
br label %41
78: ; preds = %41
%79 = load i32, i32* %16
%80 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %79)
%81 = load i32*, i32** %14
%82 = call i32 @closedir(i32* %81)
%83 = call i32 @regfree(i32* %7)
store i32 0, i32* %3
store i32 1, i32* %17
br label %84
84: ; preds = %78, %67
%85 = load i8*, i8** %9
br label %86
86: ; preds = %84, %32, %20
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [16 x i8] c"Incorrect ppid\0A\00"
@.str.1 = [9 x i8] c"^[0-9]+$\00"
@REG_EXTENDED = global i32 0
@REG_NEWLINE = global i32 0
@.str.2 = [37 x i8] c"Could not compile process dir regex\0A\00"
@.str.3 = [7 x i8] c"/proc/\00"
@REG_NOMATCH = global i32 0
@.str.4 = [41 x i8] c"Process dir name regex match failed: %s\0A\00"
@.str.5 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [255 x i8]
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %56
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = bitcast i32* %3 to i8*
%13 = load i32, i32* @REG_EXTENDED
%14 = load i32, i32* @REG_NEWLINE
%15 = or i32 %14, %13
%16 = call i64 @regcomp(i32* %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %9
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %54
21: ; preds = %9
%22 = call i8* @llvm.stacksave()
%23 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%24 = call i32* @opendir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %35, %21
%26 = phi i32 [ %40, %35 ], [ 0, %21 ]
br label %27
27: ; preds = %25, %41
%28 = call %struct.dirent* @readdir(i32* %24)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %48, label %30
30: ; preds = %27
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @regexec(i32* %3, i32 %32, i32 0, i32* null, i32 0)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i64 0, i32 0
%37 = load i32, i32* %36
%38 = call i64 @is_parent(i32 %37, i8* %11)
%39 = trunc i64 %38 to i32
%40 = add i32 %26, %39
br label %25
41: ; preds = %30
%42 = load i32, i32* @REG_NOMATCH
%43 = icmp eq i32 %33, %42
br i1 %43, label %27, label %44
44: ; preds = %41
%45 = call i32 @regerror(i32 %33, i32* %3, i8* %23, i32 255)
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %23)
br label %50
48: ; preds = %27
%49 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %26)
br label %50
50: ; preds = %48, %44
%51 = phi i32 [ 1, %44 ], [ 0, %48 ]
%52 = call i32 @closedir(i32* %24)
%53 = call i32 @regfree(i32* %3)
br label %54
54: ; preds = %50, %18
%55 = phi i32 [ 1, %18 ], [ %51, %50 ]
br label %56
56: ; preds = %54, %6
%57 = phi i32 [ 1, %6 ], [ %55, %54 ]
ret i32 %57
}