#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 /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int main(void) {
  DIR *dir;
  struct dirent *entry;
  FILE *fp;
  char path[128];
  char buf[1024];
  printf("NAME\t\tPID\n");
  if ((dir = opendir("/proc")) == NULL) {
    perror("fail to open dir");
    return -1;
  }
  while ((entry = readdir(dir)) != NULL) {
    if (entry->d_name[0] == '.')
      continue;
    if ((entry->d_name[0] <= '0') || (entry->d_name[0] >= '9'))
      continue;
    sprintf(path, "/proc/%s/task/%s/status", entry->d_name, entry->d_name);
    fp = fopen(path, "r");
    if (fp == NULL) {
      perror("fail to open");
      exit(1);
    }
    while (fgets(buf, 1024, fp) != NULL) {
      if (buf[0] == 'N' && buf[1] == 'a' && buf[2] == 'm' && buf[3] == 'e') {
        int i = 6;
        while (buf[i] != '\n') {
          printf("%c", buf[i]);
          i++;
        }
      }
      if (buf[0] == 'P' && buf[1] == 'i' && buf[2] == 'd') {
        printf("\t\t%s", &buf[5]);
        break;
      }
    }
    fclose(fp);
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4b0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe44(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11f2 <main+0x52>
lea    0xe30(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d7 <main+0x237>
jmp    11f7 <main+0x57>
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     13c7 <main+0x227>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1226 <main+0x86>
jmp    11f7 <main+0x57>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jle    124c <main+0xac>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jl     1251 <main+0xb1>
jmp    11f7 <main+0x57>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
lea    0xdb5(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xda0(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12aa <main+0x10a>
lea    0xd9f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    12af <main+0x10f>
lea    -0x4a0(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     13b9 <main+0x219>
movsbl -0x4a0(%rbp),%eax
cmp    $0x4e,%eax
jne    1366 <main+0x1c6>
movsbl -0x49f(%rbp),%eax
cmp    $0x61,%eax
jne    1366 <main+0x1c6>
movsbl -0x49e(%rbp),%eax
cmp    $0x6d,%eax
jne    1366 <main+0x1c6>
movsbl -0x49d(%rbp),%eax
cmp    $0x65,%eax
jne    1366 <main+0x1c6>
movl   $0x6,-0x4a4(%rbp)
movslq -0x4a4(%rbp),%rax
movsbl -0x4a0(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
je     1361 <main+0x1c1>
movslq -0x4a4(%rbp),%rax
movsbl -0x4a0(%rbp,%rax,1),%esi
ff
lea    0xd01(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4a4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4a4(%rbp)
jmp    1318 <main+0x178>
jmp    1366 <main+0x1c6>
movsbl -0x4a0(%rbp),%eax
cmp    $0x50,%eax
jne    13b4 <main+0x214>
movsbl -0x49f(%rbp),%eax
cmp    $0x69,%eax
jne    13b4 <main+0x214>
movsbl -0x49e(%rbp),%eax
cmp    $0x64,%eax
jne    13b4 <main+0x214>
lea    -0x4a0(%rbp),%rsi
add    $0x5,%rsi
lea    0xca2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13b9 <main+0x219>
jmp    12af <main+0x10f>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11f7 <main+0x57>
mov    -0x10(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x498,%rsp
lea    0xe69(%rip),%rdi
call   1040 <puts@plt>
lea    0xe1c(%rip),%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     1332 <main+0x172>
mov    %rax,%r13
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1316 <main+0x156>
lea    0x17(%rsp),%r15
lea    0x410(%rsp),%r12
lea    0x10(%rsp),%rbp
jmp    1239 <main+0x79>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1316 <main+0x156>
mov    (%rax),%rcx
mov    (%rcx),%al
add    $0xc7,%al
cmp    $0xf8,%al
jb     1228 <main+0x68>
mov    %r12,%rdi
lea    0xdc9(%rip),%rsi
mov    %rcx,%rdx
call   10d0 <sprintf@plt>
mov    %r12,%rdi
lea    0xdb5(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
je     1345 <main+0x185>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     1220 <main+0x60>
movdqa 0x10(%rsp),%xmm0
movd   %xmm0,%esi
mov    0x16(%rsp),%cl
movdqa %xmm0,(%rsp)
mov    (%rsp),%bl
mov    0x1(%rsp),%dl
mov    0x2(%rsp),%al
cmp    $0x656d614e,%esi
jne    12f0 <main+0x130>
cmp    $0xa,%cl
je     12f0 <main+0x130>
mov    %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl %cl,%edi
call   1030 <putchar@plt>
movzbl (%rbx),%ecx
add    $0x1,%rbx
cmp    $0xa,%cl
jne    12d0 <main+0x110>
mov    0x10(%rsp),%bl
mov    0x11(%rsp),%dl
mov    0x12(%rsp),%al
cmp    $0x50,%bl
jne    1280 <main+0xc0>
cmp    $0x69,%dl
jne    1280 <main+0xc0>
cmp    $0x64,%al
jne    1280 <main+0xc0>
lea    0xd37(%rip),%rdi
lea    0x15(%rsp),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1220 <main+0x60>
mov    %r13,%rdi
call   1080 <closedir@plt>
xor    %eax,%eax
add    $0x498,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xccd(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%eax
jmp    1320 <main+0x160>
lea    0xce3(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [11 x i8] c"NAME\09\09PID\0A\00"
@.str.1 = [6 x i8] c"/proc\00"
@.str.2 = [17 x i8] c"fail to open dir\00"
@.str.3 = [24 x i8] c"/proc/%s/task/%s/status\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [13 x i8] c"fail to open\00"
@.str.6 = [3 x i8] c"%c\00"
@.str.7 = [5 x i8] c"\09\09%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca i32*
%5 = alloca [128 x i8]
%6 = alloca [1024 x i8]
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %2
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %129
13: ; preds = %0
br label %14
14: ; preds = %123, %43, %26, %13
%15 = load i32*, i32** %2
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %3
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %126
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %3
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 46
br i1 %25, label %26, label %27
26: ; preds = %18
br label %14
27: ; preds = %18
%28 = load %struct.dirent*, %struct.dirent** %3
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = getelementptr inbounds i8, i8* %30, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp sle i32 %33, 48
br i1 %34, label %43, label %35
35: ; preds = %27
%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 = getelementptr inbounds i8, i8* %38, i64 0
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp sge i32 %41, 57
br i1 %42, label %43, label %44
43: ; preds = %35, %27
br label %14
44: ; preds = %35
%45 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%46 = load %struct.dirent*, %struct.dirent** %3
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = load %struct.dirent*, %struct.dirent** %3
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = call i32 @sprintf(i8* %45, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %48, i8* %51)
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%54 = call i32* @fopen(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %54, i32** %4
%55 = load i32*, i32** %4
%56 = icmp eq i32* %55, null
br i1 %56, label %57, label %60
57: ; preds = %44
%58 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %44
br label %61
61: ; preds = %122, %60
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%63 = load i32*, i32** %4
%64 = call i32* @fgets(i8* %62, i32 1024, i32* %63)
%65 = icmp ne i32* %64, null
br i1 %65, label %66, label %123
66: ; preds = %61
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp eq i32 %69, 78
br i1 %70, label %71, label %104
71: ; preds = %66
%72 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 1
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
%75 = icmp eq i32 %74, 97
br i1 %75, label %76, label %104
76: ; preds = %71
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 2
%78 = load i8, i8* %77
%79 = sext i8 %78 to i32
%80 = icmp eq i32 %79, 109
br i1 %80, label %81, label %104
81: ; preds = %76
%82 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 3
%83 = load i8, i8* %82
%84 = sext i8 %83 to i32
%85 = icmp eq i32 %84, 101
br i1 %85, label %86, label %104
86: ; preds = %81
store i32 6, i32* %7
br label %87
87: ; preds = %94, %86
%88 = load i32, i32* %7
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %89
%91 = load i8, i8* %90
%92 = sext i8 %91 to i32
%93 = icmp ne i32 %92, 10
br i1 %93, label %94, label %103
94: ; preds = %87
%95 = load i32, i32* %7
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %96
%98 = load i8, i8* %97
%99 = sext i8 %98 to i32
%100 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %99)
%101 = load i32, i32* %7
%102 = add nsw i32 %101, 1
store i32 %102, i32* %7
br label %87
103: ; preds = %87
br label %104
104: ; preds = %103, %81, %76, %71, %66
%105 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%106 = load i8, i8* %105
%107 = sext i8 %106 to i32
%108 = icmp eq i32 %107, 80
br i1 %108, label %109, label %122
109: ; preds = %104
%110 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 1
%111 = load i8, i8* %110
%112 = sext i8 %111 to i32
%113 = icmp eq i32 %112, 105
br i1 %113, label %114, label %122
114: ; preds = %109
%115 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 2
%116 = load i8, i8* %115
%117 = sext i8 %116 to i32
%118 = icmp eq i32 %117, 100
br i1 %118, label %119, label %122
119: ; preds = %114
%120 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 5
%121 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* %120)
br label %123
122: ; preds = %114, %109, %104
br label %61
123: ; preds = %119, %61
%124 = load i32*, i32** %4
%125 = call i32 @fclose(i32* %124)
br label %14
126: ; preds = %14
%127 = load i32*, i32** %2
%128 = call i32 @closedir(i32* %127)
store i32 0, i32* %1
br label %129
129: ; preds = %126, %11
%130 = load i32, i32* %1
ret i32 %130
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str.1 = [6 x i8] c"/proc\00"
@.str.2 = [17 x i8] c"fail to open dir\00"
@.str.3 = [24 x i8] c"/proc/%s/task/%s/status\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [13 x i8] c"fail to open\00"
@.str.7 = [5 x i8] c"\09\09%s\00"
@str = [10 x i8] c"NAME\09\09PID\00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %17, label %8
8: ; preds = %0
%9 = call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %76, label %11
11: ; preds = %8
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 1
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 2
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 6
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 5
%16 = bitcast [1024 x i8]* %2 to <4 x i8>*
br label %19
17: ; preds = %0
%18 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %78
19: ; preds = %11, %73
%20 = phi %struct.dirent* [ %9, %11 ], [ %74, %73 ]
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = load i8, i8* %22
%24 = add i8 %23, -57
%25 = icmp ult i8 %24, -8
br i1 %25, label %73, label %26
26: ; preds = %19
%27 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %22, i8* %22)
%28 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %26, %60
%34 = call i32* @fgets(i8* %4, i32 1024, i32* %28)
%35 = icmp eq i32* %34, null
br i1 %35, label %71, label %36
36: ; preds = %33
%37 = load <4 x i8>, <4 x i8>* %16
%38 = freeze <4 x i8> %37
%39 = bitcast <4 x i8> %38 to i32
%40 = icmp ne i32 %39, 1701667150
%41 = load i8, i8* %14
%42 = icmp eq i8 %41, 10
%43 = select i1 %40, i1 true, i1 %42
%44 = extractelement <4 x i8> %38, i64 0
%45 = extractelement <4 x i8> %38, i64 1
%46 = extractelement <4 x i8> %38, i64 2
br i1 %43, label %60, label %47
47: ; preds = %36, %47
%48 = phi i64 [ %52, %47 ], [ 6, %36 ]
%49 = phi i8 [ %54, %47 ], [ %41, %36 ]
%50 = sext i8 %49 to i32
%51 = call i32 @putchar(i32 %50)
%52 = add nuw i64 %48, 1
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %52
%54 = load i8, i8* %53
%55 = icmp eq i8 %54, 10
br i1 %55, label %56, label %47
56: ; preds = %47
%57 = load i8, i8* %4
%58 = load i8, i8* %12
%59 = load i8, i8* %13
br label %60
60: ; preds = %56, %36
%61 = phi i8 [ %59, %56 ], [ %46, %36 ]
%62 = phi i8 [ %58, %56 ], [ %45, %36 ]
%63 = phi i8 [ %57, %56 ], [ %44, %36 ]
%64 = icmp eq i8 %63, 80
%65 = icmp eq i8 %62, 105
%66 = select i1 %64, i1 %65, i1 false
%67 = icmp eq i8 %61, 100
%68 = select i1 %66, i1 %67, i1 false
br i1 %68, label %69, label %33
69: ; preds = %60
%70 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* %15)
br label %71
71: ; preds = %33, %69
%72 = call i32 @fclose(i32* %28)
br label %73
73: ; preds = %71, %19
%74 = call %struct.dirent* @readdir(i32* %6)
%75 = icmp eq %struct.dirent* %74, null
br i1 %75, label %76, label %19
76: ; preds = %73, %8
%77 = call i32 @closedir(i32* %6)
br label %78
78: ; preds = %76, %17
%79 = phi i32 [ -1, %17 ], [ 0, %76 ]
ret i32 %79
}