#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 stat {
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int atoi(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ print_cmdline(int);
int /*<<< orphan*/ print_headers();
int /*<<< orphan*/ print_ppid(int);
int /*<<< orphan*/ print_state(int);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcpy(char *, char *);
int main() {
  DIR *dir = opendir("/proc");
  struct dirent *entry;
  struct stat buf;
  char filepath[15];
  int pid;
  if (dir != NULL) {
    print_headers();
    while ((entry = readdir(dir))) {
      strcpy(filepath, "/proc/");
      strcpy(filepath + 6, entry->d_name);
      if (lstat(filepath, &buf) == 0) {
        if (!S_ISDIR(buf.st_mode)) {
          continue;
        }
      }
      pid = atoi(entry->d_name);
      printf("%d\t", pid);
      print_ppid(pid);
      printf("\t");
      print_cmdline(pid);
      printf("\t");
      print_state(pid);
      printf("\n");
    }
    closedir(dir);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe2a(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12ce <main+0x10e>
mov    $0x0,%al
call   10b0 <print_headers@plt>
mov    -0x10(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12c5 <main+0x105>
lea    -0x2f(%rbp),%rdi
lea    0xdf3(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x2f(%rbp),%rdi
add    $0x6,%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
lea    -0x2f(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0x0,%rax
jne    125e <main+0x9e>
mov    -0x20(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1259 <main+0x99>
jmp    11f1 <main+0x31>
jmp    125e <main+0x9e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x34(%rbp),%edi
call   1030 <print_ppid@plt>
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x34(%rbp),%edi
call   1050 <print_cmdline@plt>
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x34(%rbp),%edi
call   10e0 <print_state@plt>
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    11f1 <main+0x31>
mov    -0x10(%rbp),%rdi
call   1090 <closedir@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe1b(%rip),%rdi
call   1070 <opendir@plt>
test   %rax,%rax
je     12d3 <main+0x103>
mov    %rax,%rbx
xor    %eax,%eax
call   10c0 <print_headers@plt>
mov    %rbx,%rdi
call   10d0 <readdir@plt>
test   %rax,%rax
je     12cb <main+0xfb>
mov    %rax,%rbp
lea    0xf(%rsp),%r14
lea    0xde9(%rip),%r15
lea    0x9(%rsp),%r12
mov    %rsp,%r13
jmp    128e <main+0xbe>
nopw   0x0(%rax,%rax,1)
mov    0x0(%rbp),%rdi
call   10e0 <atoi@plt>
mov    %eax,%ebp
lea    0xdcb(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebp,%edi
call   1030 <print_ppid@plt>
mov    $0x9,%edi
call   1040 <putchar@plt>
mov    %ebp,%edi
call   1060 <print_cmdline@plt>
mov    $0x9,%edi
call   1040 <putchar@plt>
mov    %ebp,%edi
call   10f0 <print_state@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %rbx,%rdi
call   10d0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12cb <main+0xfb>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <strcpy@plt>
mov    0x0(%rbp),%rsi
mov    %r14,%rdi
call   1050 <strcpy@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   10b0 <lstat@plt>
test   %rax,%rax
jne    1230 <main+0x60>
mov    (%rsp),%edi
call   1090 <S_ISDIR@plt>
test   %eax,%eax
jne    1230 <main+0x60>
jmp    127e <main+0xae>
mov    %rbx,%rdi
call   10a0 <closedir@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [7 x i8] c"/proc/\00"
@.str.2 = [4 x i8] c"%d\09\00"
@.str.3 = [2 x i8] c"\09\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca %struct.stat
%5 = alloca [15 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %54
10: ; preds = %0
%11 = call i32 (...) @print_headers()
br label %12
12: ; preds = %35, %33, %10
%13 = load i32*, i32** %2
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %3
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %51
16: ; preds = %12
%17 = getelementptr inbounds [15 x i8], [15 x i8]* %5, i64 0, i64 0
%18 = call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%19 = getelementptr inbounds [15 x i8], [15 x i8]* %5, i64 0, i64 0
%20 = getelementptr inbounds i8, i8* %19, i64 6
%21 = load %struct.dirent*, %struct.dirent** %3
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i32 @strcpy(i8* %20, i8* %23)
%25 = getelementptr inbounds [15 x i8], [15 x i8]* %5, i64 0, i64 0
%26 = call i64 @lstat(i8* %25, %struct.stat* %4)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %16
%29 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @S_ISDIR(i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %28
br label %12
34: ; preds = %28
br label %35
35: ; preds = %34, %16
%36 = load %struct.dirent*, %struct.dirent** %3
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i32 @atoi(i8* %38)
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %6
%43 = call i32 @print_ppid(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32, i32* %6
%46 = call i32 @print_cmdline(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32, i32* %6
%49 = call i32 @print_state(i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %12
51: ; preds = %12
%52 = load i32*, i32** %2
%53 = call i32 @closedir(i32* %52)
br label %54
54: ; preds = %51, %0
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [7 x i8] c"/proc/\00"
@.str.2 = [4 x i8] c"%d\09\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = alloca [15 x i8]
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%4 = bitcast %struct.stat* %1 to i8*
%5 = getelementptr inbounds [15 x i8], [15 x i8]* %2, i64 0, i64 0
%6 = icmp eq i32* %3, null
br i1 %6, label %41, label %7
7: ; preds = %0
%8 = tail call i32 (...) @print_headers()
%9 = call %struct.dirent* @readdir(i32* %3)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %39, label %11
11: ; preds = %7
%12 = getelementptr inbounds [15 x i8], [15 x i8]* %2, i64 0, i64 6
%13 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
br label %14
14: ; preds = %11, %36
%15 = phi %struct.dirent* [ %9, %11 ], [ %37, %36 ]
%16 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = call i32 @strcpy(i8* %12, i8* %18)
%20 = call i64 @lstat(i8* %5, %struct.stat* %1)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %14
%23 = load i32, i32* %13
%24 = call i32 @S_ISDIR(i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %36, label %26
26: ; preds = %22, %14
%27 = load i8*, i8** %17
%28 = call i32 @atoi(i8* %27)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = call i32 @print_ppid(i32 %28)
%31 = call i32 @putchar(i32 9)
%32 = call i32 @print_cmdline(i32 %28)
%33 = call i32 @putchar(i32 9)
%34 = call i32 @print_state(i32 %28)
%35 = call i32 @putchar(i32 10)
br label %36
36: ; preds = %26, %22
%37 = call %struct.dirent* @readdir(i32* %3)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %39, label %14
39: ; preds = %36, %7
%40 = call i32 @closedir(i32* %3)
br label %41
41: ; preds = %39, %0
ret i32 0
}