#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 {
  scalar_t__ d_type;
  char *d_name;
};
typedef int ssize_t;
typedef int pid_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
int PATH_MAX;
int atoi(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
pid_t findProcessByName(char *processName) {
  if (processName == NULL) {
    return -1;
  }
  struct dirent *procDirs;
  DIR *directory = opendir("/proc/");
  if (directory) {
    while ((procDirs = readdir(directory)) != NULL) {
      if (procDirs->d_type != DT_DIR)
        continue;
      pid_t pid = atoi(procDirs->d_name);
      int exePathLen = 10 + strlen(procDirs->d_name) + 1;
      char *exePath = malloc(exePathLen * sizeof(char));
      if (exePath == NULL) {
        continue;
      }
      sprintf(exePath, "/proc/%s/exe", procDirs->d_name);
      exePath[exePathLen - 1] = '\0';
      char *exeBuf = malloc(PATH_MAX * sizeof(char));
      if (exeBuf == NULL) {
        free(exePath);
        continue;
      }
      ssize_t len = readlink(exePath, exeBuf, PATH_MAX - 1);
      if (len == -1) {
        free(exePath);
        free(exeBuf);
        continue;
      }
      exeBuf[len] = '\0';
      char *exeName = NULL;
      char *exeToken = strtok(exeBuf, "/");
      while (exeToken) {
        exeName = exeToken;
        exeToken = strtok(NULL, "/");
      }
      if (strcmp(exeName, processName) == 0) {
        free(exePath);
        free(exeBuf);
        closedir(directory);
        return pid;
      }
      free(exePath);
      free(exeBuf);
    }
    closedir(directory);
  }
  return -1;
}


x86 assembly (clang, -O0):
<findProcessByName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d3 <findProcessByName+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bf <findProcessByName+0x20f>
lea    0xe26(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     13b8 <findProcessByName+0x208>
jmp    11f3 <findProcessByName+0x43>
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     13af <findProcessByName+0x1ff>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2dc0(%rip),%rcx
cmp    (%rcx),%rax
je     1226 <findProcessByName+0x76>
jmp    11f3 <findProcessByName+0x43>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
add    $0xa,%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
movslq -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    126f <findProcessByName+0xbf>
jmp    11f3 <findProcessByName+0x43>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd85(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2d51(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12ca <findProcessByName+0x11a>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    11f3 <findProcessByName+0x43>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2d17(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1040 <readlink@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1307 <findProcessByName+0x157>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    11f3 <findProcessByName+0x43>
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movq   $0x0,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xcdf(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     135b <findProcessByName+0x1ab>
mov    -0x50(%rbp),%rax
mov    %rax,-0x48(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xcb8(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x50(%rbp)
jmp    132f <findProcessByName+0x17f>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1398 <findProcessByName+0x1e8>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13bf <findProcessByName+0x20f>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    11f3 <findProcessByName+0x43>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findProcessByName>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%ebp
mov    %rdi,0x10(%rsp)
test   %rdi,%rdi
je     1312 <findProcessByName+0x162>
lea    0xe28(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1312 <findProcessByName+0x162>
mov    %rax,%r12
mov    %rax,%rdi
call   1090 <readdir@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     130a <findProcessByName+0x15a>
mov    %rax,%rbx
mov    0x2dcf(%rip),%r14
lea    0xdf5(%rip),%r13
jmp    1238 <findProcessByName+0x88>
mov    %r15,%r14
mov    %r14,%rdi
call   1030 <free@plt>
mov    0x2db4(%rip),%r14
mov    %r12,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1305 <findProcessByName+0x155>
mov    (%rbx),%rax
cmp    (%r14),%rax
jne    1224 <findProcessByName+0x74>
mov    0x8(%rbx),%rbp
mov    %rbp,%rdi
call   10c0 <atoi@plt>
mov    %eax,0xc(%rsp)
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
lea    0xb(%rbp),%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1224 <findProcessByName+0x74>
mov    %rax,%r15
movslq %ebp,%rbp
mov    0x8(%rbx),%rdx
mov    %rax,%rdi
lea    0xd8c(%rip),%rsi
call   10d0 <sprintf@plt>
movb   $0x0,0xa(%rbp,%r15,1)
mov    0x2d63(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1212 <findProcessByName+0x62>
mov    %rax,%r14
add    $0xffffffff,%ebx
mov    %r15,%rdi
mov    %rax,%rsi
mov    %ebx,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     12f8 <findProcessByName+0x148>
cltq
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
mov    %r13,%rsi
call   10b0 <strtok@plt>
test   %rax,%rax
je     12e4 <findProcessByName+0x134>
xchg   %ax,%ax
mov    %rax,%rbx
xor    %edi,%edi
mov    %r13,%rsi
call   10b0 <strtok@plt>
test   %rax,%rax
jne    12d0 <findProcessByName+0x120>
jmp    12e6 <findProcessByName+0x136>
xor    %ebx,%ebx
mov    %rbx,%rdi
mov    0x10(%rsp),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1323 <findProcessByName+0x173>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    1215 <findProcessByName+0x65>
mov    $0xffffffff,%ebp
mov    %r12,%rdi
call   1070 <closedir@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    0xc(%rsp),%ebp
jmp    130a <findProcessByName+0x15a>

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@.str = [7 x i8] c"/proc/\00"
@DT_DIR = global i64 0
@.str.1 = [13 x i8] c"/proc/%s/exe\00"
@PATH_MAX = global i32 0
@.str.2 = [2 x i8] c"/\00"
define i32 @findProcessByName(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %3
%13 = load i8*, i8** %3
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %120
16: ; preds = %1
%17 = call i32* @opendir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %5
%18 = load i32*, i32** %5
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %119
20: ; preds = %16
br label %21
21: ; preds = %111, %80, %69, %50, %31, %20
%22 = load i32*, i32** %5
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %4
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %25, label %116
25: ; preds = %21
%26 = load %struct.dirent*, %struct.dirent** %4
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i64, i64* %27
%29 = load i64, i64* @DT_DIR
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %25
br label %21
32: ; preds = %25
%33 = load %struct.dirent*, %struct.dirent** %4
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 1
%35 = load i8*, i8** %34
%36 = call i32 @atoi(i8* %35)
store i32 %36, i32* %6
%37 = load %struct.dirent*, %struct.dirent** %4
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 1
%39 = load i8*, i8** %38
%40 = call i32 @strlen(i8* %39)
%41 = add nsw i32 10, %40
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = mul i64 %44, 1
%46 = trunc i64 %45 to i32
%47 = call i8* @malloc(i32 %46)
store i8* %47, i8** %8
%48 = load i8*, i8** %8
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %51
50: ; preds = %32
br label %21
51: ; preds = %32
%52 = load i8*, i8** %8
%53 = load %struct.dirent*, %struct.dirent** %4
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 1
%55 = load i8*, i8** %54
%56 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %55)
%57 = load i8*, i8** %8
%58 = load i32, i32* %7
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %57, i64 %60
store i8 0, i8* %61
%62 = load i32, i32* @PATH_MAX
%63 = sext i32 %62 to i64
%64 = mul i64 %63, 1
%65 = trunc i64 %64 to i32
%66 = call i8* @malloc(i32 %65)
store i8* %66, i8** %9
%67 = load i8*, i8** %9
%68 = icmp eq i8* %67, null
br i1 %68, label %69, label %72
69: ; preds = %51
%70 = load i8*, i8** %8
%71 = call i32 @free(i8* %70)
br label %21
72: ; preds = %51
%73 = load i8*, i8** %8
%74 = load i8*, i8** %9
%75 = load i32, i32* @PATH_MAX
%76 = sub nsw i32 %75, 1
%77 = call i32 @readlink(i8* %73, i8* %74, i32 %76)
store i32 %77, i32* %10
%78 = load i32, i32* %10
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %85
80: ; preds = %72
%81 = load i8*, i8** %8
%82 = call i32 @free(i8* %81)
%83 = load i8*, i8** %9
%84 = call i32 @free(i8* %83)
br label %21
85: ; preds = %72
%86 = load i8*, i8** %9
%87 = load i32, i32* %10
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds i8, i8* %86, i64 %88
store i8 0, i8* %89
store i8* null, i8** %11
%90 = load i8*, i8** %9
%91 = call i8* @strtok(i8* %90, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %91, i8** %12
br label %92
92: ; preds = %95, %85
%93 = load i8*, i8** %12
%94 = icmp ne i8* %93, null
br i1 %94, label %95, label %98
95: ; preds = %92
%96 = load i8*, i8** %12
store i8* %96, i8** %11
%97 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %97, i8** %12
br label %92
98: ; preds = %92
%99 = load i8*, i8** %11
%100 = load i8*, i8** %3
%101 = call i64 @strcmp(i8* %99, i8* %100)
%102 = icmp eq i64 %101, 0
br i1 %102, label %103, label %111
103: ; preds = %98
%104 = load i8*, i8** %8
%105 = call i32 @free(i8* %104)
%106 = load i8*, i8** %9
%107 = call i32 @free(i8* %106)
%108 = load i32*, i32** %5
%109 = call i32 @closedir(i32* %108)
%110 = load i32, i32* %6
store i32 %110, i32* %2
br label %120
111: ; preds = %98
%112 = load i8*, i8** %8
%113 = call i32 @free(i8* %112)
%114 = load i8*, i8** %9
%115 = call i32 @free(i8* %114)
br label %21
116: ; preds = %21
%117 = load i32*, i32** %5
%118 = call i32 @closedir(i32* %117)
br label %119
119: ; preds = %116, %16
store i32 -1, i32* %2
br label %120
120: ; preds = %119, %103, %15
%121 = load i32, i32* %2
ret i32 %121
}

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