#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*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ read(int, char **, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strstr(char *, char *);
int pgrep(char *p) {
  DIR *dirp;
  struct dirent *dp;
  int f;
  char cmd[4096];
  char path[256];
  dirp = opendir("/proc");
  while ((dp = readdir(dirp)) != NULL)
    if ((dp->d_name[0] >= '1') && (dp->d_name[0] <= '9')) {
      strcpy(path, "/proc/");
      strcat(path, dp->d_name);
      strcat(path, "/cmdline");
      f = open(path, O_RDONLY);
      read(f, &cmd, 4096);
      close(f);
      if (strstr(cmd, p)) {
        closedir(dirp);
        return (atoi(dp->d_name));
      }
    }
  closedir(dirp);
  return (0);
}


x86 assembly (clang, -O0):
<pgrep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1130,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe4a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12aa <pgrep+0x10a>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x31,%eax
jl     12a5 <pgrep+0x105>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jg     12a5 <pgrep+0x105>
lea    -0x1130(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x1130(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   10b0 <strcat@plt>
lea    -0x1130(%rbp),%rdi
lea    0xddd(%rip),%rsi
call   10b0 <strcat@plt>
lea    -0x1130(%rbp),%rdi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
lea    -0x1030(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
je     12a0 <pgrep+0x100>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x4(%rbp)
jmp    12ba <pgrep+0x11a>
jmp    12a5 <pgrep+0x105>
jmp    11bf <pgrep+0x1f>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgrep>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1108,%rsp
mov    %rdi,%r14
lea    0xe45(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1288 <pgrep+0xe8>
mov    %rax,%rbx
mov    %rsp,%r13
lea    0x100(%rsp),%r12
jmp    1204 <pgrep+0x64>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1288 <pgrep+0xe8>
mov    (%rbx),%rax
movzbl (%rax),%eax
add    $0xcf,%al
cmp    $0x8,%al
ja     11f0 <pgrep+0x50>
mov    %r13,%rdi
lea    0xdec(%rip),%rsi
call   1030 <strcpy@plt>
mov    (%rbx),%rsi
mov    %r13,%rdi
call   10b0 <strcat@plt>
mov    %r13,%rdi
lea    0xdd9(%rip),%rsi
call   10b0 <strcat@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1090 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   10c0 <strstr@plt>
test   %rax,%rax
je     11f0 <pgrep+0x50>
mov    %r15,%rdi
call   1060 <closedir@plt>
mov    (%rbx),%rdi
call   10a0 <atoi@plt>
jmp    1292 <pgrep+0xf2>
mov    %r15,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
add    $0x1108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [7 x i8] c"/proc/\00"
@.str.2 = [9 x i8] c"/cmdline\00"
@O_RDONLY = global i32 0
define i32 @pgrep(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca [256 x i8]
store i8* %0, i8** %3
%9 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
br label %10
10: ; preds = %60, %1
%11 = load i32*, i32** %4
%12 = call %struct.dirent* @readdir(i32* %11)
store %struct.dirent* %12, %struct.dirent** %5
%13 = icmp ne %struct.dirent* %12, null
br i1 %13, label %14, label %61
14: ; preds = %10
%15 = load %struct.dirent*, %struct.dirent** %5
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i32 0, i32 0
%17 = load i8*, i8** %16
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp sge i32 %20, 49
br i1 %21, label %22, label %60
22: ; preds = %14
%23 = load %struct.dirent*, %struct.dirent** %5
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp sle i32 %28, 57
br i1 %29, label %30, label %60
30: ; preds = %22
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%32 = call i32 @strcpy(i8* %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%34 = load %struct.dirent*, %struct.dirent** %5
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @strcat(i8* %33, i8* %36)
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%39 = call i32 @strcat(i8* %38, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%41 = load i32, i32* @O_RDONLY
%42 = call i32 @open(i8* %40, i32 %41)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = bitcast [4096 x i8]* %7 to i8**
%45 = call i32 @read(i32 %43, i8** %44, i32 4096)
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%49 = load i8*, i8** %3
%50 = call i64 @strstr(i8* %48, i8* %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %30
%53 = load i32*, i32** %4
%54 = call i32 @closedir(i32* %53)
%55 = load %struct.dirent*, %struct.dirent** %5
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = call i32 @atoi(i8* %57)
store i32 %58, i32* %2
br label %64
59: ; preds = %30
br label %60
60: ; preds = %59, %22, %14
br label %10
61: ; preds = %10
%62 = load i32*, i32** %4
%63 = call i32 @closedir(i32* %62)
store i32 0, i32* %2
br label %64
64: ; preds = %61, %52
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [7 x i8] c"/proc/\00"
@.str.2 = [9 x i8] c"/cmdline\00"
@O_RDONLY = global i32 0
define i32 @pgrep(i8* %0) {
%2 = alloca [4096 x i8]
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = call %struct.dirent* @readdir(i32* %6)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %36, label %9
9: ; preds = %1
%10 = bitcast [4096 x i8]* %2 to i8**
br label %11
11: ; preds = %9, %33
%12 = phi %struct.dirent* [ %7, %9 ], [ %34, %33 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = load i8, i8* %14
%16 = add i8 %15, -49
%17 = icmp ult i8 %16, 9
br i1 %17, label %18, label %33
18: ; preds = %11
%19 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = load i8*, i8** %13
%21 = call i32 @strcat(i8* %5, i8* %20)
%22 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %5, i32 %23)
%25 = call i32 @read(i32 %24, i8** %10, i32 4096)
%26 = call i32 @close(i32 %24)
%27 = call i64 @strstr(i8* %4, i8* %0)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %18
%30 = call i32 @closedir(i32* %6)
%31 = load i8*, i8** %13
%32 = call i32 @atoi(i8* %31)
br label %38
33: ; preds = %18, %11
%34 = call %struct.dirent* @readdir(i32* %6)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %36, label %11
36: ; preds = %33, %1
%37 = call i32 @closedir(i32* %6)
br label %38
38: ; preds = %36, %29
%39 = phi i32 [ %32, %29 ], [ 0, %36 ]
ret i32 %39
}