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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ psname(int /*<<< orphan*/, char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *realloc(int /*<<< orphan*/ *, int);
char *strchr(char const *, char);
scalar_t__ strcmp(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ strtol(int /*<<< orphan*/, char **, int);
int _pidof(const char *name, pid_t **pids) {
  const char *p;
  char *e;
  DIR *dir;
  struct dirent *de;
  pid_t i;
  int count;
  char buf[256];
  count = 0;
  *pids = NULL;
  if ((p = strchr(name, '/')) != NULL)
    name = p + 1;
  if ((dir = opendir("/proc")) != NULL) {
    while ((de = readdir(dir)) != NULL) {
      i = strtol(de->d_name, &e, 10);
      if (*e != 0)
        continue;
      if (strcmp(name, psname(i, buf, sizeof(buf))) == 0) {
        if ((*pids = realloc(*pids, sizeof(pid_t) * (count + 1))) == NULL) {
          return -1;
        }
        (*pids)[count++] = i;
      }
    }
  }
  closedir(dir);
  return count;
}


x86 assembly (clang, -O0):
<_pidof>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11cd <_pidof+0x4d>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
lea    0xe2c(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     12c2 <_pidof+0x142>
jmp    11ec <_pidof+0x6c>
mov    -0x30(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     12bd <_pidof+0x13d>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     122f <_pidof+0xaf>
jmp    11ec <_pidof+0x6c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x148(%rbp)
mov    -0x3c(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x100,%edx
call   1030 <psname@plt>
mov    -0x148(%rbp),%rdi
mov    %eax,%esi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12b8 <_pidof+0x138>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%esi
call   10a0 <realloc@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    129d <_pidof+0x11d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d1 <_pidof+0x151>
mov    -0x3c(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x40(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x40(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    11ec <_pidof+0x6c>
jmp    12c2 <_pidof+0x142>
mov    -0x30(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_pidof>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,(%rsi)
mov    $0x2f,%esi
call   1050 <strchr@plt>
lea    0x1(%rax),%r12
test   %rax,%rax
cmove  %rbx,%r12
lea    0xe46(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
xor    %r15d,%r15d
test   %rax,%rax
je     123c <_pidof+0xbc>
lea    0x8(%rsp),%r13
nop
mov    %rbx,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     123c <_pidof+0xbc>
mov    (%rax),%edi
mov    %r13,%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
jne    11d0 <_pidof+0x50>
mov    %ebp,%edi
lea    0x10(%rsp),%rsi
mov    $0x100,%edx
call   1030 <psname@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    11d0 <_pidof+0x50>
mov    (%r14),%rdi
lea    0x4(,%r15,4),%esi
call   10a0 <realloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1259 <_pidof+0xd9>
mov    %r15d,%ecx
add    $0x1,%r15d
mov    %ebp,(%rax,%rcx,4)
jmp    11d0 <_pidof+0x50>
mov    %rbx,%rdi
call   1060 <closedir@plt>
mov    %r15d,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%r15d
jmp    1244 <_pidof+0xc4>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/proc\00"
define i32 @_pidof(i8* %0, i32** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [256 x i8]
store i8* %0, i8** %4
store i32** %1, i32*** %5
store i32 0, i32* %11
%13 = load i32**, i32*** %5
store i32* null, i32** %13
%14 = load i8*, i8** %4
%15 = call i8* @strchr(i8* %14, i8 signext 47)
store i8* %15, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i64 1
store i8* %19, i8** %4
br label %20
20: ; preds = %17, %2
%21 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %8
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %67
23: ; preds = %20
br label %24
24: ; preds = %65, %37, %23
%25 = load i32*, i32** %8
%26 = call %struct.dirent* @readdir(i32* %25)
store %struct.dirent* %26, %struct.dirent** %9
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %66
28: ; preds = %24
%29 = load %struct.dirent*, %struct.dirent** %9
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @strtol(i32 %31, i8** %7, i32 10)
store i32 %32, i32* %10
%33 = load i8*, i8** %7
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %28
br label %24
38: ; preds = %28
%39 = load i8*, i8** %4
%40 = load i32, i32* %10
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%42 = call i32 @psname(i32 %40, i8* %41, i32 256)
%43 = call i64 @strcmp(i8* %39, i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %65
45: ; preds = %38
%46 = load i32**, i32*** %5
%47 = load i32*, i32** %46
%48 = load i32, i32* %11
%49 = add nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = mul i64 4, %50
%52 = trunc i64 %51 to i32
%53 = call i32* @realloc(i32* %47, i32 %52)
%54 = load i32**, i32*** %5
store i32* %53, i32** %54
%55 = icmp eq i32* %53, null
br i1 %55, label %56, label %57
56: ; preds = %45
store i32 -1, i32* %3
br label %71
57: ; preds = %45
%58 = load i32, i32* %10
%59 = load i32**, i32*** %5
%60 = load i32*, i32** %59
%61 = load i32, i32* %11
%62 = add nsw i32 %61, 1
store i32 %62, i32* %11
%63 = sext i32 %61 to i64
%64 = getelementptr inbounds i32, i32* %60, i64 %63
store i32 %58, i32* %64
br label %65
65: ; preds = %57, %38
br label %24
66: ; preds = %24
br label %67
67: ; preds = %66, %20
%68 = load i32*, i32** %8
%69 = call i32 @closedir(i32* %68)
%70 = load i32, i32* %11
store i32 %70, i32* %3
br label %71
71: ; preds = %67, %56
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/proc\00"
define i32 @_pidof(i8* %0, i32** nocapture %1) {
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = bitcast i8** %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
store i32* null, i32** %1
%7 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 47)
%8 = icmp eq i8* %7, null
%9 = getelementptr inbounds i8, i8* %7, i64 1
%10 = select i1 %8, i8* %0, i8* %9
%11 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %39, label %13
13: ; preds = %2, %36
%14 = phi i32 [ %32, %36 ], [ 0, %2 ]
br label %15
15: ; preds = %13, %26
br label %16
16: ; preds = %15, %19
%17 = call %struct.dirent* @readdir(i32* %11)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %39, label %19
19: ; preds = %16
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @strtol(i32 %21, i8** %3, i32 10)
%23 = load i8*, i8** %3
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %26, label %16
26: ; preds = %19
%27 = call i32 @psname(i32 %22, i8* %6, i32 256)
%28 = call i64 @strcmp(i8* %10, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %15
30: ; preds = %26
%31 = load i32*, i32** %1
%32 = add nuw nsw i32 %14, 1
%33 = shl i32 %32, 2
%34 = call i32* @realloc(i32* %31, i32 %33)
store i32* %34, i32** %1
%35 = icmp eq i32* %34, null
br i1 %35, label %42, label %36
36: ; preds = %30
%37 = zext i32 %14 to i64
%38 = getelementptr inbounds i32, i32* %34, i64 %37
store i32 %22, i32* %38
br label %13
39: ; preds = %16, %2
%40 = phi i32 [ 0, %2 ], [ %14, %16 ]
%41 = call i32 @closedir(i32* %11)
br label %42
42: ; preds = %30, %39
%43 = phi i32 [ %40, %39 ], [ -1, %30 ]
ret i32 %43
}