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

/* Variables and functions */
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ debug(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ *realloc(scalar_t__ *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
scalar_t__ strcmp(char *, char const *);
char *strrchr(char *, char);
pid_t *pidlist(const char *target, size_t *retlen) {
  pid_t *plist = NULL;
  size_t plistlen = 0;
  pid_t pid;
  DIR *pdir;
  FILE *fp;
  struct dirent *d;
  char path[256];
  char buf[256];
  char *tmp;
  pdir = opendir("/proc");
  if (!pdir)
    goto out;
  while ((d = readdir(pdir)) != NULL) {
    pid = (pid_t)atoi(d->d_name);
    if (!pid)
      continue;
    snprintf(path, sizeof(path), "/proc/%s/cmdline", d->d_name);
    fp = fopen(path, "r");
    if (!fp)
      continue;
    tmp = fgets(buf, sizeof(buf), fp);
    fclose(fp);
    if (!tmp)
      continue;
    tmp = strrchr(buf, '/');
    if (tmp)
      tmp++;
    else
      tmp = buf;
    if (strcmp(tmp, target))
      continue;
    plistlen++;
    plist = realloc(plist, plistlen * sizeof(pid_t));
    if (!plist) {
      debug("realloc failed");
      plistlen = 0;
      plist = NULL;
      goto out;
    }
    plist[plistlen - 1] = pid;
  }
out:
  if (pdir)
    closedir(pdir);
  *retlen = plistlen;
  return plist;
}


x86 assembly (clang, -O0):
<pidlist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe16(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1203 <pidlist+0x43>
jmp    1394 <pidlist+0x1d4>
jmp    1208 <pidlist+0x48>
mov    -0x30(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     138f <pidlist+0x1cf>
mov    -0x40(%rbp),%rax
mov    (%rax),%rdi
call   10e0 <atoi@plt>
cltq
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1241 <pidlist+0x81>
jmp    1208 <pidlist+0x48>
lea    -0x140(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%rcx
mov    $0x100,%esi
lea    0xdab(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x140(%rbp),%rdi
lea    0xda9(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1287 <pidlist+0xc7>
jmp    1208 <pidlist+0x48>
lea    -0x240(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x100,%esi
call   1090 <fgets@plt>
mov    %rax,-0x248(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x248(%rbp)
jne    12bf <pidlist+0xff>
jmp    1208 <pidlist+0x48>
lea    -0x240(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x248(%rbp)
cmpq   $0x0,-0x248(%rbp)
je     12fc <pidlist+0x13c>
mov    -0x248(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x248(%rbp)
jmp    130a <pidlist+0x14a>
lea    -0x240(%rbp),%rax
mov    %rax,-0x248(%rbp)
mov    -0x248(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     1329 <pidlist+0x169>
jmp    1208 <pidlist+0x48>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
shl    $0x3,%rsi
call   10c0 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1376 <pidlist+0x1b6>
lea    0xcbd(%rip),%rdi
call   1050 <debug@plt>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
jmp    1394 <pidlist+0x1d4>
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    1208 <pidlist+0x48>
jmp    1394 <pidlist+0x1d4>
cmpq   $0x0,-0x30(%rbp)
je     13a8 <pidlist+0x1e8>
mov    -0x30(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidlist>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
mov    %rsi,%r12
mov    %rdi,0x18(%rsp)
lea    0xe20(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1315 <pidlist+0x155>
mov    %rax,%r15
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     131b <pidlist+0x15b>
xor    %ebp,%ebp
lea    0x120(%rsp),%r14
xor    %ebx,%ebx
mov    %r12,(%rsp)
mov    %rbx,0x8(%rsp)
mov    %rbp,0x10(%rsp)
jmp    1231 <pidlist+0x71>
xchg   %ax,%ax
mov    %r15,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1305 <pidlist+0x145>
mov    (%rax),%r13
mov    %r13,%rdi
call   10e0 <atoi@plt>
test   %eax,%eax
je     1220 <pidlist+0x60>
mov    %eax,%r12d
mov    %r14,%rdi
mov    $0x100,%esi
lea    0xdb4(%rip),%rdx
mov    %r13,%rcx
call   1060 <snprintf@plt>
mov    %r14,%rdi
lea    0xdb3(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1220 <pidlist+0x60>
mov    %rax,%rbp
lea    0x20(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1090 <fgets@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %rbx,%rbx
je     1220 <pidlist+0x60>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
lea    0x1(%rax),%rdi
test   %rax,%rax
cmove  %rbx,%rdi
mov    0x18(%rsp),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
jne    1220 <pidlist+0x60>
mov    0x8(%rsp),%rbx
lea    0x8(,%rbx,8),%rsi
mov    0x10(%rsp),%rdi
call   10c0 <realloc@plt>
test   %rax,%rax
je     1340 <pidlist+0x180>
mov    %rax,%rbp
movslq %r12d,%rax
mov    %rax,0x0(%rbp,%rbx,8)
add    $0x1,%rbx
mov    %r15,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
mov    (%rsp),%r12
jne    1212 <pidlist+0x52>
jmp    131f <pidlist+0x15f>
mov    (%rsp),%r12
mov    0x10(%rsp),%rbp
mov    0x8(%rsp),%rbx
jmp    131f <pidlist+0x15f>
xor    %ebp,%ebp
xor    %ebx,%ebx
jmp    1327 <pidlist+0x167>
xor    %ebx,%ebx
xor    %ebp,%ebp
mov    %r15,%rdi
call   1080 <closedir@plt>
mov    %rbx,(%r12)
mov    %rbp,%rax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd2(%rip),%rdi
call   1050 <debug@plt>
xor    %ebx,%ebx
xor    %ebp,%ebp
mov    (%rsp),%r12
jmp    131f <pidlist+0x15f>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [17 x i8] c"/proc/%s/cmdline\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [15 x i8] c"realloc failed\00"
define i64* @pidlist(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i64*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca %struct.dirent*
%11 = alloca [256 x i8]
%12 = alloca [256 x i8]
%13 = alloca i8*
store i8* %0, i8** %3
store i64* %1, i64** %4
store i64* null, i64** %5
store i64 0, i64* %6
%14 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %8
%15 = load i32*, i32** %8
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %2
br label %86
18: ; preds = %2
br label %19
19: ; preds = %79, %67, %51, %42, %31, %18
%20 = load i32*, i32** %8
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %10
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %85
23: ; preds = %19
%24 = load %struct.dirent*, %struct.dirent** %10
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
%28 = sext i32 %27 to i64
store i64 %28, i64* %7
%29 = load i64, i64* %7
%30 = icmp ne i64 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %23
br label %19
32: ; preds = %23
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%34 = load %struct.dirent*, %struct.dirent** %10
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @snprintf(i8* %33, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%39 = call i32* @fopen(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %39, i32** %9
%40 = load i32*, i32** %9
%41 = icmp ne i32* %40, null
br i1 %41, label %43, label %42
42: ; preds = %32
br label %19
43: ; preds = %32
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%45 = load i32*, i32** %9
%46 = call i8* @fgets(i8* %44, i32 256, i32* %45)
store i8* %46, i8** %13
%47 = load i32*, i32** %9
%48 = call i32 @fclose(i32* %47)
%49 = load i8*, i8** %13
%50 = icmp ne i8* %49, null
br i1 %50, label %52, label %51
51: ; preds = %43
br label %19
52: ; preds = %43
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%54 = call i8* @strrchr(i8* %53, i8 signext 47)
store i8* %54, i8** %13
%55 = load i8*, i8** %13
%56 = icmp ne i8* %55, null
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = load i8*, i8** %13
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %13
br label %62
60: ; preds = %52
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
store i8* %61, i8** %13
br label %62
62: ; preds = %60, %57
%63 = load i8*, i8** %13
%64 = load i8*, i8** %3
%65 = call i64 @strcmp(i8* %63, i8* %64)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %62
br label %19
68: ; preds = %62
%69 = load i64, i64* %6
%70 = add i64 %69, 1
store i64 %70, i64* %6
%71 = load i64*, i64** %5
%72 = load i64, i64* %6
%73 = mul i64 %72, 8
%74 = call i64* @realloc(i64* %71, i64 %73)
store i64* %74, i64** %5
%75 = load i64*, i64** %5
%76 = icmp ne i64* %75, null
br i1 %76, label %79, label %77
77: ; preds = %68
%78 = call i32 @debug(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i64 0, i64* %6
store i64* null, i64** %5
br label %86
79: ; preds = %68
%80 = load i64, i64* %7
%81 = load i64*, i64** %5
%82 = load i64, i64* %6
%83 = sub i64 %82, 1
%84 = getelementptr inbounds i64, i64* %81, i64 %83
store i64 %80, i64* %84
br label %19
85: ; preds = %19
br label %86
86: ; preds = %85, %77, %17
%87 = load i32*, i32** %8
%88 = icmp ne i32* %87, null
br i1 %88, label %89, label %92
89: ; preds = %86
%90 = load i32*, i32** %8
%91 = call i32 @closedir(i32* %90)
br label %92
92: ; preds = %89, %86
%93 = load i64, i64* %6
%94 = load i64*, i64** %4
store i64 %93, i64* %94
%95 = load i64*, i64** %5
ret i64* %95
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [17 x i8] c"/proc/%s/cmdline\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [15 x i8] c"realloc failed\00"
define noalias i64* @pidlist(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %56, label %9
9: ; preds = %2
%10 = call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %52, label %12
12: ; preds = %9, %47
%13 = phi %struct.dirent* [ %50, %47 ], [ %10, %9 ]
%14 = phi i64* [ %43, %47 ], [ null, %9 ]
%15 = phi i64 [ %41, %47 ], [ 0, %9 ]
br label %16
16: ; preds = %12, %22
%17 = phi %struct.dirent* [ %13, %12 ], [ %23, %22 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16, %25, %29, %33
%23 = call %struct.dirent* @readdir(i32* %7)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %52, label %16
25: ; preds = %16
%26 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%27 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %22, label %29
29: ; preds = %25
%30 = call i8* @fgets(i8* %6, i32 256, i32* %27)
%31 = call i32 @fclose(i32* %27)
%32 = icmp eq i8* %30, null
br i1 %32, label %22, label %33
33: ; preds = %29
%34 = call i8* @strrchr(i8* %6, i8 signext 47)
%35 = icmp eq i8* %34, null
%36 = getelementptr inbounds i8, i8* %34, i64 1
%37 = select i1 %35, i8* %6, i8* %36
%38 = call i64 @strcmp(i8* %37, i8* %0)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %22
40: ; preds = %33
%41 = add i64 %15, 1
%42 = shl i64 %41, 3
%43 = call i64* @realloc(i64* %14, i64 %42)
%44 = icmp eq i64* %43, null
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @debug(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %52
47: ; preds = %40
%48 = sext i32 %20 to i64
%49 = getelementptr inbounds i64, i64* %43, i64 %15
store i64 %48, i64* %49
%50 = call %struct.dirent* @readdir(i32* %7)
%51 = icmp eq %struct.dirent* %50, null
br i1 %51, label %52, label %12
52: ; preds = %47, %22, %45, %9
%53 = phi i64 [ 0, %45 ], [ 0, %9 ], [ %15, %22 ], [ %41, %47 ]
%54 = phi i64* [ null, %45 ], [ null, %9 ], [ %14, %22 ], [ %43, %47 ]
%55 = call i32 @closedir(i32* %7)
br label %56
56: ; preds = %2, %52
%57 = phi i64* [ %54, %52 ], [ null, %2 ]
%58 = phi i64 [ %53, %52 ], [ 0, %2 ]
store i64 %58, i64* %1
ret i64* %57
}