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

/* Variables and functions */
scalar_t__ DT_DIR;
scalar_t__ closedir(int /*<<< orphan*/ *);
char *create_full_path(char const *, int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
char *get_fd_dir_path(char const *);
scalar_t__ is_numeric_string(char const *);
int match_proc_files(char const *, char *, int *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ print_proc_info(char const *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int match_all_proc_files(const char *path) {
  int ret = 0;
  DIR *dir = NULL;
  struct dirent *entry = NULL;
  char *absolute_path = NULL;
  if ((absolute_path = create_full_path(path, NULL)) == NULL) {
    ret = -1;
    goto out;
  }
  if ((dir = opendir("/proc")) == NULL) {
    ret = -1;
    goto out;
  }
  printf("%s", "name   PID    PPID\n");
  while ((entry = readdir(dir)) != NULL) {
    const char *dir_name = entry->d_name;
    if (entry->d_type == DT_DIR && is_numeric_string(dir_name)) {
      char *dir_path = NULL;
      bool matches = false;
      if ((dir_path = get_fd_dir_path(dir_name)) == NULL) {
        ret = -1;
        goto out;
      }
      if ((ret = match_proc_files(path, dir_path, &matches)) != 0) {
        errno = 0;
      }
      if (matches) {
        print_proc_info(dir_name);
      }
      free(dir_path);
    }
  }
  if (errno != 0) {
    ret = -1;
    goto out;
  }
out:
  if (dir && closedir(dir) != 0) {
    ret = -1;
  }
  if (absolute_path) {
    free(absolute_path);
  }
  return ret;
}


x86 assembly (clang, -O0):
<match_all_proc_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <create_full_path@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11f2 <match_all_proc_files+0x52>
movl   $0xffffffff,-0xc(%rbp)
jmp    1318 <match_all_proc_files+0x178>
lea    0xe07(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1218 <match_all_proc_files+0x78>
movl   $0xffffffff,-0xc(%rbp)
jmp    1318 <match_all_proc_files+0x178>
lea    0xde7(%rip),%rdi
lea    0xde3(%rip),%rsi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12f6 <match_all_proc_files+0x156>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d84(%rip),%rcx
cmp    (%rcx),%rax
jne    12f1 <match_all_proc_files+0x151>
mov    -0x30(%rbp),%rdi
call   1040 <is_numeric_string@plt>
cmp    $0x0,%rax
je     12f1 <match_all_proc_files+0x151>
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <get_fd_dir_path@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    12aa <match_all_proc_files+0x10a>
movl   $0xffffffff,-0xc(%rbp)
jmp    1318 <match_all_proc_files+0x178>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10b0 <match_proc_files@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     12d5 <match_all_proc_files+0x135>
mov    0x2d0a(%rip),%rax
movq   $0x0,(%rax)
cmpl   $0x0,-0x3c(%rbp)
je     12e8 <match_all_proc_files+0x148>
mov    -0x30(%rbp),%rdi
call   10c0 <print_proc_info@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    122b <match_all_proc_files+0x8b>
mov    0x2cdb(%rip),%rax
cmpq   $0x0,(%rax)
je     1313 <match_all_proc_files+0x173>
movl   $0xffffffff,-0xc(%rbp)
jmp    1318 <match_all_proc_files+0x178>
jmp    1318 <match_all_proc_files+0x178>
cmpq   $0x0,-0x18(%rbp)
je     133d <match_all_proc_files+0x19d>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
cmp    $0x0,%rax
je     133d <match_all_proc_files+0x19d>
movl   $0xffffffff,-0xc(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1351 <match_all_proc_files+0x1b1>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<match_all_proc_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
xor    %esi,%esi
call   10a0 <create_full_path@plt>
test   %rax,%rax
je     12a0 <match_all_proc_files+0x100>
mov    %rax,%rbp
lea    0xe35(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     12ce <match_all_proc_files+0x12e>
mov    %rax,%rbx
mov    %rbp,0x10(%rsp)
lea    0xe1e(%rip),%rdi
lea    0xe1a(%rip),%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1090 <readdir@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     12a7 <match_all_proc_files+0x107>
mov    0x2dd1(%rip),%r14
jmp    1235 <match_all_proc_files+0x95>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12a7 <match_all_proc_files+0x107>
mov    0x8(%rax),%rcx
cmp    (%r14),%rcx
jne    1228 <match_all_proc_files+0x88>
mov    (%rax),%rbp
mov    %rbp,%rdi
call   1040 <is_numeric_string@plt>
test   %rax,%rax
je     1228 <match_all_proc_files+0x88>
movl   $0x0,0xc(%rsp)
mov    %rbp,%rdi
call   1080 <get_fd_dir_path@plt>
test   %rax,%rax
je     12ec <match_all_proc_files+0x14c>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rax,%rsi
lea    0xc(%rsp),%rdx
call   10b0 <match_proc_files@plt>
mov    %eax,%r13d
test   %eax,%eax
je     128f <match_all_proc_files+0xef>
mov    0x2d50(%rip),%rax
movq   $0x0,(%rax)
cmpl   $0x0,0xc(%rsp)
je     1220 <match_all_proc_files+0x80>
mov    %rbp,%rdi
call   10c0 <print_proc_info@plt>
jmp    1220 <match_all_proc_files+0x80>
mov    $0xffffffff,%ebx
jmp    12db <match_all_proc_files+0x13b>
mov    0x2d2a(%rip),%rax
xor    %ecx,%ecx
cmp    (%rax),%rcx
sbb    %ebp,%ebp
or     %r13d,%ebp
mov    %rbx,%rdi
call   1070 <closedir@plt>
neg    %rax
sbb    %ebx,%ebx
or     %ebp,%ebx
mov    0x10(%rsp),%rbp
jmp    12d3 <match_all_proc_files+0x133>
mov    $0xffffffff,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebp
jmp    12b8 <match_all_proc_files+0x118>

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [20 x i8] c"name PID PPID\0A\00"
@DT_DIR = global i64 0
@errno = global i64 0
define i32 @match_all_proc_files(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
store i32* null, i32** %4
store %struct.dirent* null, %struct.dirent** %5
store i8* null, i8** %6
%10 = load i8*, i8** %2
%11 = call i8* @create_full_path(i8* %10, i32* null)
store i8* %11, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %3
br label %63
14: ; preds = %1
%15 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %4
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %14
store i32 -1, i32* %3
br label %63
18: ; preds = %14
%19 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %57, %18
%21 = load i32*, i32** %4
%22 = call %struct.dirent* @readdir(i32* %21)
store %struct.dirent* %22, %struct.dirent** %5
%23 = icmp ne %struct.dirent* %22, null
br i1 %23, label %24, label %58
24: ; preds = %20
%25 = load %struct.dirent*, %struct.dirent** %5
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
store i8* %27, i8** %7
%28 = load %struct.dirent*, %struct.dirent** %5
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 1
%30 = load i64, i64* %29
%31 = load i64, i64* @DT_DIR
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %57
33: ; preds = %24
%34 = load i8*, i8** %7
%35 = call i64 @is_numeric_string(i8* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %57
37: ; preds = %33
store i8* null, i8** %8
store i32 0, i32* %9
%38 = load i8*, i8** %7
%39 = call i8* @get_fd_dir_path(i8* %38)
store i8* %39, i8** %8
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %42
41: ; preds = %37
store i32 -1, i32* %3
br label %63
42: ; preds = %37
%43 = load i8*, i8** %2
%44 = load i8*, i8** %8
%45 = call i32 @match_proc_files(i8* %43, i8* %44, i32* %9)
store i32 %45, i32* %3
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %42
store i64 0, i64* @errno
br label %48
48: ; preds = %47, %42
%49 = load i32, i32* %9
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i8*, i8** %7
%53 = call i32 @print_proc_info(i8* %52)
br label %54
54: ; preds = %51, %48
%55 = load i8*, i8** %8
%56 = call i32 @free(i8* %55)
br label %57
57: ; preds = %54, %33, %24
br label %20
58: ; preds = %20
%59 = load i64, i64* @errno
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %58
store i32 -1, i32* %3
br label %63
62: ; preds = %58
br label %63
63: ; preds = %62, %61, %41, %17, %13
%64 = load i32*, i32** %4
%65 = icmp ne i32* %64, null
br i1 %65, label %66, label %71
66: ; preds = %63
%67 = load i32*, i32** %4
%68 = call i64 @closedir(i32* %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %66
store i32 -1, i32* %3
br label %71
71: ; preds = %70, %66, %63
%72 = load i8*, i8** %6
%73 = icmp ne i8* %72, null
br i1 %73, label %74, label %77
74: ; preds = %71
%75 = load i8*, i8** %6
%76 = call i32 @free(i8* %75)
br label %77
77: ; preds = %74, %71
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [20 x i8] c"name PID PPID\0A\00"
@DT_DIR = global i64 0
@errno = global i64 0
define i32 @match_all_proc_files(i8* %0) {
%2 = alloca i32
%3 = tail call i8* @create_full_path(i8* %0, i32* null)
%4 = icmp eq i8* %3, null
br i1 %4, label %58, label %5
5: ; preds = %1
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %55, label %8
8: ; preds = %5
%9 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%10 = call %struct.dirent* @readdir(i32* %6)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %45, label %12
12: ; preds = %8
%13 = bitcast i32* %2 to i8*
br label %14
14: ; preds = %12, %41
%15 = phi %struct.dirent* [ %10, %12 ], [ %43, %41 ]
%16 = phi i32 [ 0, %12 ], [ %42, %41 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 1
%20 = load i64, i64* %19
%21 = load i64, i64* @DT_DIR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %41
23: ; preds = %14
%24 = call i64 @is_numeric_string(i8* %18)
%25 = icmp eq i64 %24, 0
br i1 %25, label %41, label %26
26: ; preds = %23
store i32 0, i32* %2
%27 = call i8* @get_fd_dir_path(i8* %18)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %26
br label %50
30: ; preds = %26
%31 = call i32 @match_proc_files(i8* %0, i8* %27, i32* %2)
%32 = icmp eq i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %30
store i64 0, i64* @errno
br label %34
34: ; preds = %33, %30
%35 = load i32, i32* %2
%36 = icmp eq i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = call i32 @print_proc_info(i8* %18)
br label %39
39: ; preds = %34, %37
%40 = call i32 @free(i8* %27)
br label %41
41: ; preds = %14, %23, %39
%42 = phi i32 [ %31, %39 ], [ %16, %23 ], [ %16, %14 ]
%43 = call %struct.dirent* @readdir(i32* %6)
%44 = icmp eq %struct.dirent* %43, null
br i1 %44, label %45, label %14
45: ; preds = %41, %8
%46 = phi i32 [ 0, %8 ], [ %42, %41 ]
%47 = load i64, i64* @errno
%48 = icmp eq i64 %47, 0
%49 = select i1 %48, i32 %46, i32 -1
br label %50
50: ; preds = %45, %29
%51 = phi i32 [ %49, %45 ], [ -1, %29 ]
%52 = call i64 @closedir(i32* %6)
%53 = icmp eq i64 %52, 0
%54 = select i1 %53, i32 %51, i32 -1
br label %55
55: ; preds = %50, %5
%56 = phi i32 [ %54, %50 ], [ -1, %5 ]
%57 = call i32 @free(i8* %3)
br label %58
58: ; preds = %1, %55
%59 = phi i32 [ %56, %55 ], [ -1, %1 ]
ret i32 %59
}