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

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
int atoi(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int readlink(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int strlen(char const *);
int /*<<< orphan*/ strncmp(char const *, char *, int);
char *strrchr(char *, char);
int get_pids_by_name(const char *procname, int foundpid[]) {
  DIR *dir;
  struct dirent *d;
  int pid, i;
  char *s;
  int pnlen;
  i = 0;
  foundpid[0] = 0;
  pnlen = strlen(procname);
  dir = opendir("/proc");
  if (!dir) {
    printf("cannot open /proc");
    return -1;
  }
  while ((d = readdir(dir)) != NULL) {
    char exe[PATH_MAX + 1];
    char path[PATH_MAX + 1];
    int len;
    int namelen;
    if ((pid = atoi(d->d_name)) == 0)
      continue;
    snprintf(exe, sizeof(exe), "/proc/%s/exe", d->d_name);
    if ((len = readlink(exe, path, PATH_MAX)) < 0)
      continue;
    path[len] = '\0';
    s = strrchr(path, '/');
    if (s == NULL)
      continue;
    s++;
    namelen = strlen(s);
    if (namelen < pnlen)
      continue;
    if (!strncmp(procname, s, pnlen)) {
      if (s[pnlen] == ' ' || s[pnlen] == '\0') {
        foundpid[i] = pid;
        i++;
      }
    }
  }
  foundpid[i] = 0;
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<get_pids_by_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x3c(%rbp)
lea    0xe35(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1203 <get_pids_by_name+0x63>
lea    0xe0e(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e9 <get_pids_by_name+0x249>
jmp    1208 <get_pids_by_name+0x68>
mov    -0x20(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     13ca <get_pids_by_name+0x22a>
mov    0x2dca(%rip),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %rcx,-0x80(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x78(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x50(%rbp)
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jne    1297 <get_pids_by_name+0xf7>
movl   $0x2,-0x64(%rbp)
jmp    13a9 <get_pids_by_name+0x209>
mov    -0x78(%rbp),%rdi
mov    -0x80(%rbp),%rax
mov    %eax,%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xd63(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x78(%rbp),%rdi
mov    -0x70(%rbp),%rsi
mov    0x2d2d(%rip),%rax
mov    (%rax),%edx
call   1040 <readlink@plt>
mov    %eax,-0x5c(%rbp)
cmp    $0x0,%eax
jge    12e2 <get_pids_by_name+0x142>
movl   $0x2,-0x64(%rbp)
jmp    13a9 <get_pids_by_name+0x209>
mov    -0x70(%rbp),%rdi
movslq -0x5c(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
mov    $0x2f,%esi
call   1090 <strrchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1313 <get_pids_by_name+0x173>
movl   $0x2,-0x64(%rbp)
jmp    13a9 <get_pids_by_name+0x209>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x60(%rbp)
mov    -0x60(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    1343 <get_pids_by_name+0x1a3>
movl   $0x2,-0x64(%rbp)
jmp    13a9 <get_pids_by_name+0x209>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    13a2 <get_pids_by_name+0x202>
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
je     1386 <get_pids_by_name+0x1e6>
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    139d <get_pids_by_name+0x1fd>
mov    -0x2c(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    13a2 <get_pids_by_name+0x202>
movl   $0x0,-0x64(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x64(%rbp),%eax
test   %eax,%eax
je     13c5 <get_pids_by_name+0x225>
jmp    13c0 <get_pids_by_name+0x220>
jmp    1208 <get_pids_by_name+0x68>
jmp    1208 <get_pids_by_name+0x68>
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x20(%rbp),%rdi
call   10a0 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pids_by_name>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,-0x58(%rbp)
movl   $0x0,(%rsi)
mov    %rdi,-0x68(%rbp)
call   1060 <strlen@plt>
mov    %eax,-0x3c(%rbp)
lea    0xe3e(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     133f <get_pids_by_name+0x19f>
mov    %rax,%rbx
mov    %rax,%rdi
call   10b0 <readdir@plt>
mov    $0x0,%ecx
mov    %rcx,-0x48(%rbp)
test   %rax,%rax
je     1324 <get_pids_by_name+0x184>
movslq -0x3c(%rbp),%rcx
mov    %rcx,-0x60(%rbp)
xor    %ecx,%ecx
mov    %rcx,-0x48(%rbp)
mov    0x2de2(%rip),%r14
mov    %rbx,-0x30(%rbp)
jmp    123c <get_pids_by_name+0x9c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    -0x30(%rbp),%rbx
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1324 <get_pids_by_name+0x184>
mov    %rsp,-0x38(%rbp)
mov    (%r14),%r12d
add    $0x1,%r12d
mov    %rsp,%r15
lea    0xf(%r12),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r15
mov    %r15,%rsp
mov    (%r14),%ecx
add    $0x1,%ecx
mov    %rsp,%r13
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r13
mov    %r13,%rsp
mov    (%rax),%rbx
mov    %rbx,%rdi
call   10c0 <atoi@plt>
test   %eax,%eax
je     1220 <get_pids_by_name+0x80>
mov    %eax,-0x4c(%rbp)
mov    %r15,%rdi
mov    %r12d,%esi
lea    0xd83(%rip),%rdx
mov    %rbx,%rcx
call   1080 <snprintf@plt>
mov    (%r14),%edx
mov    %r15,%rdi
mov    %r13,%rsi
call   1040 <readlink@plt>
test   %eax,%eax
js     1220 <get_pids_by_name+0x80>
mov    %eax,%eax
movb   $0x0,0x0(%r13,%rax,1)
mov    %r13,%rdi
mov    $0x2f,%esi
call   1090 <strrchr@plt>
test   %rax,%rax
je     1220 <get_pids_by_name+0x80>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    -0x3c(%rbp),%edx
cmp    %edx,%eax
jl     1220 <get_pids_by_name+0x80>
mov    -0x68(%rbp),%rdi
mov    %rbx,%rsi
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1220 <get_pids_by_name+0x80>
mov    -0x60(%rbp),%rax
testb  $0xdf,(%rbx,%rax,1)
jne    1220 <get_pids_by_name+0x80>
mov    -0x48(%rbp),%rcx
movslq %ecx,%rcx
mov    -0x58(%rbp),%rax
mov    -0x4c(%rbp),%edx
mov    %edx,(%rax,%rcx,4)
add    $0x1,%ecx
mov    %rcx,-0x48(%rbp)
jmp    1220 <get_pids_by_name+0x80>
movslq -0x48(%rbp),%rax
mov    -0x58(%rbp),%rcx
movl   $0x0,(%rcx,%rax,4)
mov    %rbx,%rdi
call   10a0 <closedir@plt>
xor    %eax,%eax
jmp    1350 <get_pids_by_name+0x1b0>
lea    0xcba(%rip),%rdi
call   1070 <printf@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%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 = [18 x i8] c"cannot open /proc\00"
@PATH_MAX = global i32 0
@.str.2 = [13 x i8] c"/proc/%s/exe\00"
define i32 @get_pids_by_name(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 0, i32* %9
%18 = load i32*, i32** %5
%19 = getelementptr inbounds i32, i32* %18, i64 0
store i32 0, i32* %19
%20 = load i8*, i8** %4
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %11
%22 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp ne i32* %23, null
br i1 %24, label %27, label %25
25: ; preds = %2
%26 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %118
27: ; preds = %2
br label %28
28: ; preds = %110, %107, %27
%29 = load i32*, i32** %6
%30 = call %struct.dirent* @readdir(i32* %29)
store %struct.dirent* %30, %struct.dirent** %7
%31 = icmp ne %struct.dirent* %30, null
br i1 %31, label %32, label %111
32: ; preds = %28
%33 = load i32, i32* @PATH_MAX
%34 = add nsw i32 %33, 1
%35 = zext i32 %34 to i64
%36 = call i8* @llvm.stacksave()
store i8* %36, i8** %12
%37 = alloca i8, i64 %35
store i64 %35, i64* %13
%38 = load i32, i32* @PATH_MAX
%39 = add nsw i32 %38, 1
%40 = zext i32 %39 to i64
%41 = alloca i8, i64 %40
store i64 %40, i64* %14
%42 = load %struct.dirent*, %struct.dirent** %7
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i32 @atoi(i8* %44)
store i32 %45, i32* %8
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %32
store i32 2, i32* %17
br label %107
48: ; preds = %32
%49 = trunc i64 %35 to i32
%50 = load %struct.dirent*, %struct.dirent** %7
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 0
%52 = load i8*, i8** %51
%53 = call i32 @snprintf(i8* %37, i32 %49, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %52)
%54 = load i32, i32* @PATH_MAX
%55 = call i32 @readlink(i8* %37, i8* %41, i32 %54)
store i32 %55, i32* %15
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %48
store i32 2, i32* %17
br label %107
58: ; preds = %48
%59 = load i32, i32* %15
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %41, i64 %60
store i8 0, i8* %61
%62 = call i8* @strrchr(i8* %41, i8 signext 47)
store i8* %62, i8** %10
%63 = load i8*, i8** %10
%64 = icmp eq i8* %63, null
br i1 %64, label %65, label %66
65: ; preds = %58
store i32 2, i32* %17
br label %107
66: ; preds = %58
%67 = load i8*, i8** %10
%68 = getelementptr inbounds i8, i8* %67, i32 1
store i8* %68, i8** %10
%69 = load i8*, i8** %10
%70 = call i32 @strlen(i8* %69)
store i32 %70, i32* %16
%71 = load i32, i32* %16
%72 = load i32, i32* %11
%73 = icmp slt i32 %71, %72
br i1 %73, label %74, label %75
74: ; preds = %66
store i32 2, i32* %17
br label %107
75: ; preds = %66
%76 = load i8*, i8** %4
%77 = load i8*, i8** %10
%78 = load i32, i32* %11
%79 = call i32 @strncmp(i8* %76, i8* %77, i32 %78)
%80 = icmp ne i32 %79, 0
br i1 %80, label %106, label %81
81: ; preds = %75
%82 = load i8*, i8** %10
%83 = load i32, i32* %11
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %82, i64 %84
%86 = load i8, i8* %85
%87 = sext i8 %86 to i32
%88 = icmp eq i32 %87, 32
br i1 %88, label %97, label %89
89: ; preds = %81
%90 = load i8*, i8** %10
%91 = load i32, i32* %11
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %90, i64 %92
%94 = load i8, i8* %93
%95 = sext i8 %94 to i32
%96 = icmp eq i32 %95, 0
br i1 %96, label %97, label %105
97: ; preds = %89, %81
%98 = load i32, i32* %8
%99 = load i32*, i32** %5
%100 = load i32, i32* %9
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds i32, i32* %99, i64 %101
store i32 %98, i32* %102
%103 = load i32, i32* %9
%104 = add nsw i32 %103, 1
store i32 %104, i32* %9
br label %105
105: ; preds = %97, %89
br label %106
106: ; preds = %105, %75
store i32 0, i32* %17
br label %107
107: ; preds = %106, %74, %65, %57, %47
%108 = load i8*, i8** %12
%109 = load i32, i32* %17
switch i32 %109, label %120 [
i32 0, label %110
i32 2, label %28
]
110: ; preds = %107
br label %28
111: ; preds = %28
%112 = load i32*, i32** %5
%113 = load i32, i32* %9
%114 = sext i32 %113 to i64
%115 = getelementptr inbounds i32, i32* %112, i64 %114
store i32 0, i32* %115
%116 = load i32*, i32** %6
%117 = call i32 @closedir(i32* %116)
store i32 0, i32* %3
br label %118
118: ; preds = %111, %25
%119 = load i32, i32* %3
ret i32 %119
120: ; preds = %107
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [18 x i8] c"cannot open /proc\00"
@PATH_MAX = global i32 0
@.str.2 = [13 x i8] c"/proc/%s/exe\00"
define i32 @get_pids_by_name(i8* %0, i32* nocapture writeonly %1) {
store i32 0, i32* %1
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %57, label %9
9: ; preds = %6
%10 = sext i32 %3 to i64
br label %13
11: ; preds = %2
%12 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %62
13: ; preds = %9, %53
%14 = phi %struct.dirent* [ %7, %9 ], [ %55, %53 ]
%15 = phi i32 [ 0, %9 ], [ %54, %53 ]
%16 = load i32, i32* @PATH_MAX
%17 = add nsw i32 %16, 1
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
%20 = alloca i8, i64 %18
%21 = load i32, i32* @PATH_MAX
%22 = add nsw i32 %21, 1
%23 = zext i32 %22 to i64
%24 = alloca i8, i64 %23
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %53, label %29
29: ; preds = %13
%30 = call i32 @snprintf(i8* %20, i32 %17, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%31 = load i32, i32* @PATH_MAX
%32 = call i32 @readlink(i8* %20, i8* %24, i32 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %53, label %34
34: ; preds = %29
%35 = zext i32 %32 to i64
%36 = getelementptr inbounds i8, i8* %24, i64 %35
store i8 0, i8* %36
%37 = call i8* @strrchr(i8* %24, i8 signext 47)
%38 = icmp eq i8* %37, null
br i1 %38, label %53, label %39
39: ; preds = %34
%40 = getelementptr inbounds i8, i8* %37, i64 1
%41 = call i32 @strlen(i8* %40)
%42 = icmp slt i32 %41, %3
br i1 %42, label %53, label %43
43: ; preds = %39
%44 = call i32 @strncmp(i8* %0, i8* %40, i32 %3)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %43
%47 = getelementptr inbounds i8, i8* %40, i64 %10
%48 = load i8, i8* %47
switch i8 %48, label %53 [
i8 32, label %49
i8 0, label %49
]
49: ; preds = %46, %46
%50 = sext i32 %15 to i64
%51 = getelementptr inbounds i32, i32* %1, i64 %50
store i32 %27, i32* %51
%52 = add nsw i32 %15, 1
br label %53
53: ; preds = %43, %49, %46, %39, %34, %29, %13
%54 = phi i32 [ %15, %13 ], [ %15, %29 ], [ %15, %34 ], [ %15, %39 ], [ %15, %43 ], [ %52, %49 ], [ %15, %46 ]
%55 = call %struct.dirent* @readdir(i32* %4)
%56 = icmp eq %struct.dirent* %55, null
br i1 %56, label %57, label %13
57: ; preds = %53, %6
%58 = phi i32 [ 0, %6 ], [ %54, %53 ]
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %1, i64 %59
store i32 0, i32* %60
%61 = call i32 @closedir(i32* %4)
br label %62
62: ; preds = %57, %11
%63 = phi i32 [ 0, %57 ], [ -1, %11 ]
ret i32 %63
}