#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 atoi(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strcmp(char const *, char *);
int find_pid_of(const char *process_name) {
  int id;
  pid_t pid = -1;
  DIR *dir;
  FILE *fp;
  char filename[32];
  char cmdline[256];
  struct dirent *entry;
  if (process_name == NULL)
    return -1;
  dir = opendir("/proc");
  if (dir == NULL)
    return -1;
  while ((entry = readdir(dir)) != NULL) {
    id = atoi(entry->d_name);
    if (id != 0) {
      sprintf(filename, "/proc/%d/cmdline", id);
      fp = fopen(filename, "r");
      if (fp) {
        fgets(cmdline, sizeof(cmdline), fp);
        fclose(fp);
        if (strcmp(process_name, cmdline) == 0) {
          pid = id;
          break;
        }
      }
    }
  }
  closedir(dir);
  return pid;
}


x86 assembly (clang, -O0):
<find_pid_of>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bd <find_pid_of+0x2d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <find_pid_of+0x121>
lea    0xe3c(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e4 <find_pid_of+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <find_pid_of+0x121>
jmp    11e9 <find_pid_of+0x59>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x158(%rbp)
cmp    $0x0,%rax
je     12a2 <find_pid_of+0x112>
mov    -0x158(%rbp),%rax
mov    (%rax),%edi
call   10a0 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     129d <find_pid_of+0x10d>
lea    -0x50(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xdda(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xddb(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1298 <find_pid_of+0x108>
lea    -0x150(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x100,%esi
call   1060 <fgets@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rdi
lea    -0x150(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1293 <find_pid_of+0x103>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    12a2 <find_pid_of+0x112>
jmp    1298 <find_pid_of+0x108>
jmp    129d <find_pid_of+0x10d>
jmp    11e9 <find_pid_of+0x59>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_pid_of>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     1269 <find_pid_of+0xd9>
mov    %rdi,%r14
lea    0xe47(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1269 <find_pid_of+0xd9>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     125c <find_pid_of+0xcc>
mov    %rsp,%r12
lea    0x20(%rsp),%r15
jmp    11fd <find_pid_of+0x6d>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     125c <find_pid_of+0xcc>
mov    (%rax),%edi
call   10a0 <atoi@plt>
test   %eax,%eax
je     11f0 <find_pid_of+0x60>
mov    %eax,%ebp
mov    %r12,%rdi
lea    0xdf2(%rip),%rsi
mov    %eax,%edx
call   10b0 <sprintf@plt>
mov    %r12,%rdi
lea    0xdf2(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11f0 <find_pid_of+0x60>
mov    %rax,%r13
mov    %r15,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    11f0 <find_pid_of+0x60>
jmp    1261 <find_pid_of+0xd1>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    %ebp,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [17 x i8] c"/proc/%d/cmdline\00"
@.str.2 = [2 x i8] c"r\00"
define i32 @find_pid_of(i8* %0) {
%2 = alloca [32 x i8]
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = icmp eq i8* %0, null
br i1 %6, label %34, label %7
7: ; preds = %1
%8 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %34, label %10
10: ; preds = %7
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %31, label %13
13: ; preds = %10, %28
%14 = phi %struct.dirent* [ %29, %28 ], [ %11, %10 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @atoi(i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %28, label %19
19: ; preds = %13
%20 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%21 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %28, label %23
23: ; preds = %19
%24 = call i32 @fgets(i8* %5, i32 256, i32* %21)
%25 = call i32 @fclose(i32* %21)
%26 = call i64 @strcmp(i8* %0, i8* %5)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %19, %23, %13
%29 = call %struct.dirent* @readdir(i32* %8)
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %31, label %13
31: ; preds = %28, %23, %10
%32 = phi i32 [ -1, %10 ], [ %17, %23 ], [ -1, %28 ]
%33 = call i32 @closedir(i32* %8)
br label %34
34: ; preds = %7, %1, %31
%35 = phi i32 [ %32, %31 ], [ -1, %1 ], [ -1, %7 ]
ret i32 %35
}