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


x86 assembly (clang, -O0):
<find_pid_by_pname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     11e1 <find_pid_by_pname+0x41>
lea    0xe36(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11ed <find_pid_by_pname+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <find_pid_by_pname+0x13b>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12cc <find_pid_by_pname+0x12c>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   10b0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmp    -0x2c(%rbp),%eax
jne    1229 <find_pid_by_pname+0x89>
jmp    11f4 <find_pid_by_pname+0x54>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
lea    -0x50(%rbp),%rdi
mov    -0x2c(%rbp),%edx
lea    0xdbf(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xdc0(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x58(%rbp)
xor    %eax,%eax
cmp    -0x58(%rbp),%rax
jne    1271 <find_pid_by_pname+0xd1>
jmp    11f4 <find_pid_by_pname+0x54>
lea    -0x160(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    -0x160(%rbp),%rdi
mov    -0x58(%rbp),%rdx
mov    $0x100,%esi
call   1070 <fgets@plt>
mov    -0x58(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rdi
lea    -0x160(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12c7 <find_pid_by_pname+0x127>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    12cc <find_pid_by_pname+0x12c>
jmp    11f4 <find_pid_by_pname+0x54>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_pid_by_pname>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
mov    $0xffffffff,%r15d
mov    %rdi,0x8(%rsp)
test   %rdi,%rdi
je     12a9 <find_pid_by_pname+0x109>
lea    0xe34(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12a9 <find_pid_by_pname+0x109>
mov    %rax,%rbx
mov    $0xffffffff,%r15d
lea    0x10(%rsp),%rbp
lea    0x30(%rsp),%r12
jmp    11fe <find_pid_by_pname+0x5e>
nop
mov    $0x2,%eax
cmp    $0x3,%eax
je     12a1 <find_pid_by_pname+0x101>
mov    %rbx,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12a1 <find_pid_by_pname+0x101>
mov    (%rax),%edi
call   10b0 <atoi@plt>
test   %eax,%eax
je     11f0 <find_pid_by_pname+0x50>
mov    %eax,%r13d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    %rbp,%rdi
lea    0xdd0(%rip),%rsi
mov    %r13d,%edx
call   10c0 <sprintf@plt>
mov    %rbp,%rdi
lea    0xdcf(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11f0 <find_pid_by_pname+0x50>
mov    %rax,%r14
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    %r12,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
cmove  %r13d,%r15d
lea    (%rcx,%rcx,2),%eax
cmp    $0x3,%eax
jne    11fe <find_pid_by_pname+0x5e>
mov    %rbx,%rdi
call   1060 <closedir@plt>
mov    %r15d,%eax
add    $0x138,%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_by_pname(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca %struct.dirent*
%7 = alloca i32
%8 = alloca [32 x i8]
%9 = alloca i32*
%10 = alloca [256 x i8]
store i8* %0, i8** %3
store i32* null, i32** %4
%11 = load i8*, i8** %3
%12 = icmp eq i8* null, %11
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %4
%15 = icmp eq i32* null, %14
br i1 %15, label %16, label %17
16: ; preds = %13, %1
store i32 -1, i32* %2
br label %60
17: ; preds = %13
store i32 -1, i32* %5
br label %18
18: ; preds = %55, %40, %29, %17
%19 = load i32*, i32** %4
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %6
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %56
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %6
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @atoi(i32 %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp eq i32 0, %27
br i1 %28, label %29, label %30
29: ; preds = %22
br label %18
30: ; preds = %22
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%32 = call i32 @memset(i8* %31, i32 0, i32 32)
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%34 = load i32, i32* %7
%35 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %37, i32** %9
%38 = load i32*, i32** %9
%39 = icmp eq i32* null, %38
br i1 %39, label %40, label %41
40: ; preds = %30
br label %18
41: ; preds = %30
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%43 = call i32 @memset(i8* %42, i32 0, i32 256)
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%45 = load i32*, i32** %9
%46 = call i32 @fgets(i8* %44, i32 256, i32* %45)
%47 = load i32*, i32** %9
%48 = call i32 @fclose(i32* %47)
%49 = load i8*, i8** %3
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%51 = call i64 @strcmp(i8* %49, i8* %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %41
%54 = load i32, i32* %7
store i32 %54, i32* %5
br label %56
55: ; preds = %41
br label %18
56: ; preds = %53, %18
%57 = load i32*, i32** %4
%58 = call i32 @closedir(i32* %57)
%59 = load i32, i32* %5
store i32 %59, i32* %2
br label %60
60: ; preds = %56, %16
%61 = load i32, i32* %2
ret i32 %61
}

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_by_pname(i8* %0) {
%2 = alloca [32 x i8]
%3 = alloca [256 x i8]
%4 = icmp eq i8* %0, null
br i1 %4, label %43, label %5
5: ; preds = %1
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %43, label %8
8: ; preds = %5
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
br label %11
11: ; preds = %8, %36
%12 = phi i32 [ %37, %36 ], [ -1, %8 ]
%13 = call %struct.dirent* @readdir(i32* %6)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %40, label %15
15: ; preds = %11
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @atoi(i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %36, label %20
20: ; preds = %15
%21 = call i32 @memset(i8* %9, i32 0, i32 32)
%22 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%23 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %33, label %25
25: ; preds = %20
%26 = call i32 @memset(i8* %10, i32 0, i32 256)
%27 = call i32 @fgets(i8* %10, i32 256, i32* %23)
%28 = call i32 @fclose(i32* %23)
%29 = call i64 @strcmp(i8* %0, i8* %10)
%30 = icmp eq i64 %29, 0
%31 = select i1 %30, i32 %18, i32 %12
%32 = select i1 %30, i32 3, i32 0
br label %33
33: ; preds = %20, %25
%34 = phi i32 [ %31, %25 ], [ %12, %20 ]
%35 = phi i32 [ %32, %25 ], [ 2, %20 ]
br label %36
36: ; preds = %15, %33
%37 = phi i32 [ %34, %33 ], [ %12, %15 ]
%38 = phi i32 [ %35, %33 ], [ 2, %15 ]
%39 = icmp eq i32 %38, 3
br i1 %39, label %40, label %11
40: ; preds = %36, %11
%41 = phi i32 [ %37, %36 ], [ %12, %11 ]
%42 = call i32 @closedir(i32* %6)
br label %43
43: ; preds = %1, %5, %40
%44 = phi i32 [ %41, %40 ], [ -1, %5 ], [ -1, %1 ]
ret i32 %44
}