#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 {
  int /*<<< orphan*/ *d_name;
};
typedef int pid_t;
typedef int /*<<< orphan*/ cmdline;
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int EINVAL;
int EIO;
char *basename(char const *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ isdigit(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strcmp(char *, char *);
scalar_t__ strlen(char *);
int strtol(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int getPIDbyName(const char *process_name, pid_t pid_list[], int list_size) {
  DIR *dir;
  struct dirent *next;
  int count = 0;
  pid_t pid;
  FILE *fp;
  char *base_pname = NULL;
  char *base_fname = NULL;
  char cmdline[256];
  char path[256];
  if (process_name == NULL || pid_list == NULL)
    return -EINVAL;
  base_pname = basename(process_name);
  if (strlen(base_pname) <= 0)
    return -EINVAL;
  dir = opendir("/proc");
  if (!dir) {
    return -EIO;
  }
  while ((next = readdir(dir)) != NULL) {
    if (!isdigit(*next->d_name))
      continue;
    pid = strtol(next->d_name, NULL, 0);
    sprintf(path, "/proc/%u/cmdline", pid);
    fp = fopen(path, "r");
    if (fp == NULL)
      continue;
    memset(cmdline, 0, sizeof(cmdline));
    if (fread(cmdline, 256 - 1, 1, fp) < 0) {
      fclose(fp);
      continue;
    }
    fclose(fp);
    base_fname = basename(cmdline);
    if (strcmp(base_fname, base_pname) == 0) {
      if (count >= list_size) {
        break;
      } else {
        pid_list[count] = pid;
        count++;
      }
    }
  }
  closedir(dir);
  return count;
}


x86 assembly (clang, -O0):
<getPIDbyName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x34(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1213 <getPIDbyName+0x43>
cmpq   $0x0,-0x18(%rbp)
jne    1226 <getPIDbyName+0x56>
xor    %eax,%eax
mov    0x2dbc(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13c7 <getPIDbyName+0x1f7>
mov    -0x10(%rbp),%rdi
call   10b0 <basename@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
call   1070 <strlen@plt>
cmp    $0x0,%rax
jg     1259 <getPIDbyName+0x89>
xor    %eax,%eax
mov    0x2d89(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13c7 <getPIDbyName+0x1f7>
lea    0xda0(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1287 <getPIDbyName+0xb7>
xor    %eax,%eax
mov    0x2d63(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13c7 <getPIDbyName+0x1f7>
jmp    128c <getPIDbyName+0xbc>
mov    -0x28(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     13b8 <getPIDbyName+0x1e8>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    12bf <getPIDbyName+0xef>
jmp    128c <getPIDbyName+0xbc>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10c0 <strtol@plt>
mov    %eax,-0x38(%rbp)
lea    -0x250(%rbp),%rdi
mov    -0x38(%rbp),%edx
lea    0xd21(%rip),%rsi
call   10f0 <sprintf@plt>
lea    -0x250(%rbp),%rdi
lea    0xd1f(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1311 <getPIDbyName+0x141>
jmp    128c <getPIDbyName+0xbc>
lea    -0x150(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
lea    -0x150(%rbp),%rdi
mov    -0x40(%rbp),%rcx
mov    $0xff,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x0,%rax
jge    1356 <getPIDbyName+0x186>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
jmp    128c <getPIDbyName+0xbc>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x150(%rbp),%rdi
call   10b0 <basename@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    13b3 <getPIDbyName+0x1e3>
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     1397 <getPIDbyName+0x1c7>
jmp    13b8 <getPIDbyName+0x1e8>
mov    -0x38(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    13b3 <getPIDbyName+0x1e3>
jmp    128c <getPIDbyName+0xbc>
mov    -0x28(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getPIDbyName>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
test   %rdi,%rdi
je     1313 <getPIDbyName+0x153>
mov    %rsi,%rbp
test   %rsi,%rsi
je     1313 <getPIDbyName+0x153>
mov    %edx,%r14d
call   10a0 <basename@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <strlen@plt>
test   %rax,%rax
jle    1313 <getPIDbyName+0x153>
lea    0xdf7(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1335 <getPIDbyName+0x175>
mov    %rax,%rbx
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     133e <getPIDbyName+0x17e>
mov    %r15,0x18(%rsp)
mov    %rbp,0x10(%rsp)
mov    %r14d,0xc(%rsp)
xor    %r14d,%r14d
jmp    1251 <getPIDbyName+0x91>
nop
mov    %rbx,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     1341 <getPIDbyName+0x181>
mov    (%rax),%rdi
mov    (%rdi),%eax
add    $0xffffffd0,%eax
cmp    $0x9,%eax
ja     1240 <getPIDbyName+0x80>
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <strtol@plt>
mov    %eax,%r15d
lea    0x120(%rsp),%rbp
mov    %rbp,%rdi
lea    0xd8a(%rip),%rsi
mov    %eax,%edx
call   10e0 <sprintf@plt>
mov    %rbp,%rdi
lea    0xd8a(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1240 <getPIDbyName+0x80>
mov    %rax,%r13
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    %rbp,%rdi
mov    $0xff,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
mov    %rax,%r12
mov    %r13,%rdi
call   1040 <fclose@plt>
test   %r12,%r12
js     1240 <getPIDbyName+0x80>
lea    0x20(%rsp),%rdi
call   10a0 <basename@plt>
mov    %rax,%rdi
mov    0x18(%rsp),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1240 <getPIDbyName+0x80>
cmp    0xc(%rsp),%r14d
jge    1341 <getPIDbyName+0x181>
movslq %r14d,%rax
mov    0x10(%rsp),%rcx
mov    %r15d,(%rcx,%rax,4)
add    $0x1,%r14d
jmp    1240 <getPIDbyName+0x80>
mov    0x2cbe(%rip),%rax
xor    %r14d,%r14d
sub    (%rax),%r14d
mov    %r14d,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ca4(%rip),%rax
jmp    131a <getPIDbyName+0x15a>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1080 <closedir@plt>
jmp    1320 <getPIDbyName+0x160>

LLVM IR (-O0):
%struct.dirent = type { i32* }
@EINVAL = global i32 0
@.str = [6 x i8] c"/proc\00"
@EIO = global i32 0
@.str.1 = [17 x i8] c"/proc/%u/cmdline\00"
@.str.2 = [2 x i8] c"r\00"
define i32 @getPIDbyName(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca [256 x i8]
%16 = alloca [256 x i8]
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 0, i32* %10
store i8* null, i8** %13
store i8* null, i8** %14
%17 = load i8*, i8** %5
%18 = icmp eq i8* %17, null
br i1 %18, label %22, label %19
19: ; preds = %3
%20 = load i32*, i32** %6
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %19, %3
%23 = load i32, i32* @EINVAL
%24 = sub nsw i32 0, %23
store i32 %24, i32* %4
br label %105
25: ; preds = %19
%26 = load i8*, i8** %5
%27 = call i8* @basename(i8* %26)
store i8* %27, i8** %13
%28 = load i8*, i8** %13
%29 = call i64 @strlen(i8* %28)
%30 = icmp sle i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = load i32, i32* @EINVAL
%33 = sub nsw i32 0, %32
store i32 %33, i32* %4
br label %105
34: ; preds = %25
%35 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %35, i32** %8
%36 = load i32*, i32** %8
%37 = icmp ne i32* %36, null
br i1 %37, label %41, label %38
38: ; preds = %34
%39 = load i32, i32* @EIO
%40 = sub nsw i32 0, %39
store i32 %40, i32* %4
br label %105
41: ; preds = %34
br label %42
42: ; preds = %100, %74, %66, %53, %41
%43 = load i32*, i32** %8
%44 = call %struct.dirent* @readdir(i32* %43)
store %struct.dirent* %44, %struct.dirent** %9
%45 = icmp ne %struct.dirent* %44, null
br i1 %45, label %46, label %101
46: ; preds = %42
%47 = load %struct.dirent*, %struct.dirent** %9
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i32*, i32** %48
%50 = load i32, i32* %49
%51 = call i32 @isdigit(i32 %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %54, label %53
53: ; preds = %46
br label %42
54: ; preds = %46
%55 = load %struct.dirent*, %struct.dirent** %9
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i32*, i32** %56
%58 = call i32 @strtol(i32* %57, i32* null, i32 0)
store i32 %58, i32* %11
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%60 = load i32, i32* %11
%61 = call i32 @sprintf(i8* %59, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %60)
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%63 = call i32* @fopen(i8* %62, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %63, i32** %12
%64 = load i32*, i32** %12
%65 = icmp eq i32* %64, null
br i1 %65, label %66, label %67
66: ; preds = %54
br label %42
67: ; preds = %54
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%69 = call i32 @memset(i8* %68, i32 0, i32 256)
%70 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%71 = load i32*, i32** %12
%72 = call i64 @fread(i8* %70, i32 255, i32 1, i32* %71)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %67
%75 = load i32*, i32** %12
%76 = call i32 @fclose(i32* %75)
br label %42
77: ; preds = %67
%78 = load i32*, i32** %12
%79 = call i32 @fclose(i32* %78)
%80 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%81 = call i8* @basename(i8* %80)
store i8* %81, i8** %14
%82 = load i8*, i8** %14
%83 = load i8*, i8** %13
%84 = call i64 @strcmp(i8* %82, i8* %83)
%85 = icmp eq i64 %84, 0
br i1 %85, label %86, label %100
86: ; preds = %77
%87 = load i32, i32* %10
%88 = load i32, i32* %7
%89 = icmp sge i32 %87, %88
br i1 %89, label %90, label %91
90: ; preds = %86
br label %101
91: ; preds = %86
%92 = load i32, i32* %11
%93 = load i32*, i32** %6
%94 = load i32, i32* %10
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i32, i32* %93, i64 %95
store i32 %92, i32* %96
%97 = load i32, i32* %10
%98 = add nsw i32 %97, 1
store i32 %98, i32* %10
br label %99
99: ; preds = %91
br label %100
100: ; preds = %99, %77
br label %42
101: ; preds = %90, %42
%102 = load i32*, i32** %8
%103 = call i32 @closedir(i32* %102)
%104 = load i32, i32* %10
store i32 %104, i32* %4
br label %105
105: ; preds = %101, %38, %31, %22
%106 = load i32, i32* %4
ret i32 %106
}

LLVM IR (-O2):
%struct.dirent = type { i32* }
@EINVAL = global i32 0
@.str = [6 x i8] c"/proc\00"
@EIO = global i32 0
@.str.1 = [17 x i8] c"/proc/%u/cmdline\00"
@.str.2 = [2 x i8] c"r\00"
define i32 @getPIDbyName(i8* %0, i32* writeonly %1, i32 %2) {
%4 = alloca [256 x i8]
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%8 = icmp eq i8* %0, null
%9 = icmp eq i32* %1, null
%10 = or i1 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* @EINVAL
%13 = sub nsw i32 0, %12
br label %70
14: ; preds = %3
%15 = tail call i8* @basename(i8* %0)
%16 = tail call i64 @strlen(i8* dereferenceable(1) %15)
%17 = icmp slt i64 %16, 1
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* @EINVAL
%20 = sub nsw i32 0, %19
br label %70
21: ; preds = %14
%22 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = call %struct.dirent* @readdir(i32* %22)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %67, label %64
27: ; preds = %21
%28 = load i32, i32* @EIO
%29 = sub nsw i32 0, %28
br label %70
30: ; preds = %64, %47
%31 = phi %struct.dirent* [ %65, %64 ], [ %48, %47 ]
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i64 0, i32 0
%33 = load i32*, i32** %32
%34 = load i32, i32* %33
%35 = add i32 %34, -48
%36 = icmp ult i32 %35, 10
br i1 %36, label %37, label %47
37: ; preds = %30
%38 = call i32 @strtol(i32* nocapture %33, i32* null, i32 0)
%39 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%41 = icmp eq i32* %40, null
br i1 %41, label %47, label %42
42: ; preds = %37
%43 = call i32 @memset(i8* %6, i32 0, i32 256)
%44 = call i64 @fread(i8* %6, i32 255, i32 1, i32* %40)
%45 = icmp slt i64 %44, 0
%46 = call i32 @fclose(i32* %40)
br i1 %45, label %47, label %50
47: ; preds = %42, %30, %37
%48 = call %struct.dirent* @readdir(i32* %22)
%49 = icmp eq %struct.dirent* %48, null
br i1 %49, label %67, label %30
50: ; preds = %42
%51 = call i8* @basename(i8* %6)
%52 = call i64 @strcmp(i8* %51, i8* %15)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %50
%55 = icmp slt i32 %66, %2
br i1 %55, label %56, label %67
56: ; preds = %54
%57 = sext i32 %66 to i64
%58 = getelementptr inbounds i32, i32* %1, i64 %57
store i32 %38, i32* %58
%59 = add nsw i32 %66, 1
br label %60
60: ; preds = %56, %50
%61 = phi i32 [ %59, %56 ], [ %66, %50 ]
%62 = call %struct.dirent* @readdir(i32* %22)
%63 = icmp eq %struct.dirent* %62, null
br i1 %63, label %67, label %64
64: ; preds = %24, %60
%65 = phi %struct.dirent* [ %62, %60 ], [ %25, %24 ]
%66 = phi i32 [ %61, %60 ], [ 0, %24 ]
br label %30
67: ; preds = %54, %60, %47, %24
%68 = phi i32 [ 0, %24 ], [ %66, %47 ], [ %61, %60 ], [ %66, %54 ]
%69 = call i32 @closedir(i32* %22)
br label %70
70: ; preds = %67, %27, %18, %11
%71 = phi i32 [ %13, %11 ], [ %20, %18 ], [ %68, %67 ], [ %29, %27 ]
ret i32 %71
}