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

/* Variables and functions */
int atoi(char *);
scalar_t__ check_if_number(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
int *malloc(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int *realloc(int *, int);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int *pidof(char *pname) {
  DIR *dirp;
  FILE *fp;
  struct dirent *entry;
  int *pidlist, pidlist_index = 0, pidlist_realloc_count = 1;
  char path[1024], read_buf[1024];
  dirp = opendir("/proc/");
  if (dirp == NULL) {
    perror("Fail");
    return NULL;
  }
  pidlist = malloc(sizeof(int) * 32);
  if (pidlist == NULL) {
    return NULL;
  }
  while ((entry = readdir(dirp)) != NULL) {
    if (check_if_number(entry->d_name)) {
      strcpy(path, "/proc/");
      strcat(path, entry->d_name);
      strcat(path, "/comm");
      fp = fopen(path, "r");
      if (fp != NULL) {
        fscanf(fp, "%s", read_buf);
        if (strcmp(read_buf, pname) == 0) {
          pidlist[pidlist_index++] = atoi(entry->d_name);
          if (pidlist_index == 32 * pidlist_realloc_count) {
            pidlist_realloc_count++;
            pidlist =
                realloc(pidlist, sizeof(int) * 32 * pidlist_realloc_count);
            if (pidlist == NULL) {
              return NULL;
            }
          }
        }
        fclose(fp);
      }
    }
  }
  closedir(dirp);
  pidlist[pidlist_index] = -1;
  return pidlist;
}


x86 assembly (clang, -O0):
<pidof>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x1,-0x38(%rbp)
lea    0xdfc(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1231 <pidof+0x51>
lea    0xde8(%rip),%rdi
call   10e0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    13be <pidof+0x1de>
mov    $0x80,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1257 <pidof+0x77>
movq   $0x0,-0x8(%rbp)
jmp    13be <pidof+0x1de>
jmp    125c <pidof+0x7c>
mov    -0x18(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     139e <pidof+0x1be>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1080 <check_if_number@plt>
cmp    $0x0,%rax
je     1399 <pidof+0x1b9>
lea    -0x440(%rbp),%rdi
lea    0xd69(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x440(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
call   1100 <strcat@plt>
lea    -0x440(%rbp),%rdi
lea    0xd4f(%rip),%rsi
call   1100 <strcat@plt>
lea    -0x440(%rbp),%rdi
lea    0xd42(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1394 <pidof+0x1b4>
mov    -0x20(%rbp),%rdi
lea    -0x840(%rbp),%rdx
lea    0xd1e(%rip),%rsi
call   1060 <fscanf@plt>
lea    -0x840(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    138b <pidof+0x1ab>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,%edx
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x34(%rbp),%eax
mov    -0x38(%rbp),%ecx
shl    $0x5,%ecx
cmp    %ecx,%eax
jne    1386 <pidof+0x1a6>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
movslq -0x38(%rbp),%rax
shl    $0x7,%rax
mov    %eax,%esi
call   10c0 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1381 <pidof+0x1a1>
movq   $0x0,-0x8(%rbp)
jmp    13be <pidof+0x1de>
jmp    1386 <pidof+0x1a6>
jmp    138b <pidof+0x1ab>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1399 <pidof+0x1b9>
jmp    125c <pidof+0x7c>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidof>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %rdi,0x18(%rsp)
lea    0xe03(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     137a <pidof+0x19a>
mov    %rax,%r13
mov    $0x80,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1344 <pidof+0x164>
mov    %rax,%r14
mov    %r13,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1349 <pidof+0x169>
mov    %rax,%r12
movl   $0x1,0xc(%rsp)
lea    0x420(%rsp),%rbx
xor    %eax,%eax
mov    %rax,0x10(%rsp)
jmp    127c <pidof+0x9c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r13,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     1350 <pidof+0x170>
mov    (%r12),%rdi
call   1080 <check_if_number@plt>
test   %rax,%rax
je     1268 <pidof+0x88>
mov    %rbx,%rdi
lea    0xd6c(%rip),%rsi
call   1030 <strcpy@plt>
mov    (%r12),%rsi
mov    %rbx,%rdi
call   1100 <strcat@plt>
mov    %rbx,%rdi
lea    0xd5d(%rip),%rsi
call   1100 <strcat@plt>
mov    %rbx,%rdi
lea    0xd54(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1268 <pidof+0x88>
mov    %rax,%r15
mov    %rax,%rdi
lea    0xd3f(%rip),%rsi
lea    0x20(%rsp),%rbp
mov    %rbp,%rdx
call   1060 <fscanf@plt>
mov    %rbp,%rdi
mov    0x18(%rsp),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1260 <pidof+0x80>
mov    (%r12),%rdi
call   10f0 <atoi@plt>
mov    0x10(%rsp),%rdx
movslq %edx,%rcx
add    $0x1,%edx
mov    %eax,(%r14,%rcx,4)
mov    0xc(%rsp),%esi
mov    %esi,%eax
shl    $0x5,%eax
mov    %rdx,0x10(%rsp)
cmp    %eax,%edx
jne    1260 <pidof+0x80>
add    $0x1,%esi
mov    %esi,0xc(%rsp)
shl    $0x7,%esi
mov    %r14,%rdi
call   10c0 <realloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1260 <pidof+0x80>
xor    %r14d,%r14d
jmp    1365 <pidof+0x185>
xor    %eax,%eax
mov    %rax,0x10(%rsp)
mov    %r13,%rdi
call   1070 <closedir@plt>
movslq 0x10(%rsp),%rax
movl   $0xffffffff,(%r14,%rax,4)
ff
mov    %r14,%rax
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc86(%rip),%rdi
call   10e0 <perror@plt>
xor    %r14d,%r14d
jmp    1365 <pidof+0x185>

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [7 x i8] c"/proc/\00"
@.str.1 = [5 x i8] c"Fail\00"
@.str.2 = [6 x i8] c"/comm\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%s\00"
define i32* @pidof(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %66
10: ; preds = %1
%11 = tail call i32* @malloc(i32 128)
%12 = icmp eq i32* %11, null
br i1 %12, label %66, label %13
13: ; preds = %10
%14 = call %struct.dirent* @readdir(i32* %6)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %60, label %16
16: ; preds = %13, %54
%17 = phi %struct.dirent* [ %58, %54 ], [ %14, %13 ]
%18 = phi i32 [ %57, %54 ], [ 1, %13 ]
%19 = phi i32 [ %56, %54 ], [ 0, %13 ]
%20 = phi i32* [ %55, %54 ], [ %11, %13 ]
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = call i64 @check_if_number(i8* %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %54, label %25
25: ; preds = %16
%26 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%27 = load i8*, i8** %21
%28 = call i32 @strcat(i8* %4, i8* %27)
%29 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %54, label %32
32: ; preds = %25
%33 = call i32 @fscanf(i32* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %5)
%34 = call i64 @strcmp(i8* %5, i8* %0)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %49
36: ; preds = %32
%37 = load i8*, i8** %21
%38 = call i32 @atoi(i8* %37)
%39 = add nsw i32 %19, 1
%40 = sext i32 %19 to i64
%41 = getelementptr inbounds i32, i32* %20, i64 %40
store i32 %38, i32* %41
%42 = shl nsw i32 %18, 5
%43 = icmp eq i32 %39, %42
br i1 %43, label %44, label %49
44: ; preds = %36
%45 = add nsw i32 %18, 1
%46 = shl i32 %45, 7
%47 = call i32* @realloc(i32* %20, i32 %46)
%48 = icmp eq i32* %47, null
br i1 %48, label %66, label %49
49: ; preds = %36, %44, %32
%50 = phi i32* [ %47, %44 ], [ %20, %36 ], [ %20, %32 ]
%51 = phi i32 [ %39, %44 ], [ %39, %36 ], [ %19, %32 ]
%52 = phi i32 [ %45, %44 ], [ %18, %36 ], [ %18, %32 ]
%53 = call i32 @fclose(i32* %30)
br label %54
54: ; preds = %25, %49, %16
%55 = phi i32* [ %50, %49 ], [ %20, %25 ], [ %20, %16 ]
%56 = phi i32 [ %51, %49 ], [ %19, %25 ], [ %19, %16 ]
%57 = phi i32 [ %52, %49 ], [ %18, %25 ], [ %18, %16 ]
%58 = call %struct.dirent* @readdir(i32* %6)
%59 = icmp eq %struct.dirent* %58, null
br i1 %59, label %60, label %16
60: ; preds = %54, %13
%61 = phi i32* [ %11, %13 ], [ %55, %54 ]
%62 = phi i32 [ 0, %13 ], [ %56, %54 ]
%63 = call i32 @closedir(i32* %6)
%64 = sext i32 %62 to i64
%65 = getelementptr inbounds i32, i32* %61, i64 %64
store i32 -1, i32* %65
br label %66
66: ; preds = %44, %10, %60, %8
%67 = phi i32* [ null, %8 ], [ %61, %60 ], [ null, %10 ], [ null, %44 ]
ret i32* %67
}