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

/* Variables and functions */
scalar_t__ DT_DIR;
int atoi(char *);
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 *, char *);
int /*<<< orphan*/ sscanf(char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int getPidByName(char *task_name) {
  DIR *dir;
  struct dirent *ptr;
  FILE *fp;
  char filepath[64];
  char cur_task_name[64];
  char buf[1024];
  int pid_num = -1;
  dir = opendir("/proc");
  if (NULL != dir) {
    while ((ptr = readdir(dir)) != NULL) {
      if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0))
        continue;
      if (DT_DIR != ptr->d_type)
        continue;
      sprintf(filepath, "/proc/%s/status", ptr->d_name);
      fp = fopen(filepath, "r");
      if (NULL != fp) {
        if (fgets(buf, 1024 - 1, fp) == NULL) {
          fclose(fp);
          continue;
        }
        sscanf(buf, "%*s %s", cur_task_name);
        if (!strcmp(task_name, cur_task_name)) {
          pid_num = atoi(ptr->d_name);
        }
        fclose(fp);
      }
    }
    closedir(dir);
  }
  return pid_num;
}


x86 assembly (clang, -O0):
<getPidByName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4b0,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0x4a4(%rbp)
ff ff ff
lea    0xe40(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     130e <getPidByName+0x16e>
jmp    11da <getPidByName+0x3a>
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1305 <getPidByName+0x165>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe08(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     122b <getPidByName+0x8b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdea(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1230 <getPidByName+0x90>
jmp    11da <getPidByName+0x3a>
mov    0x2da9(%rip),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
cmp    0x8(%rcx),%rax
je     124d <getPidByName+0xad>
jmp    11da <getPidByName+0x3a>
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xdaa(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x60(%rbp),%rdi
lea    0xdaa(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
je     1300 <getPidByName+0x160>
lea    -0x4a0(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x3ff,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    12b1 <getPidByName+0x111>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11da <getPidByName+0x3a>
lea    -0x4a0(%rbp),%rdi
lea    -0xa0(%rbp),%rdx
lea    0xd55(%rip),%rsi
call   1090 <sscanf@plt>
mov    -0x8(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12f7 <getPidByName+0x157>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x4a4(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11da <getPidByName+0x3a>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x4a4(%rbp),%eax
add    $0x4b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getPidByName>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4a8,%rsp
mov    %rdi,%rbx
lea    0xe45(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12f0 <getPidByName+0x150>
mov    %rax,%rbp
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     12f7 <getPidByName+0x157>
mov    %rax,%r13
mov    %rbx,0x18(%rsp)
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xe13(%rip),%rbx
lea    0xe0b(%rip),%r12
mov    0x2dde(%rip),%r14
mov    %rbp,0x10(%rsp)
jmp    1234 <getPidByName+0x94>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    0x10(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1080 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12ff <getPidByName+0x15f>
mov    0x0(%r13),%rdi
mov    %rbx,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1220 <getPidByName+0x80>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1220 <getPidByName+0x80>
mov    (%r14),%rax
cmp    0x8(%r13),%rax
jne    1220 <getPidByName+0x80>
mov    0x0(%r13),%rdx
lea    0x60(%rsp),%rbp
mov    %rbp,%rdi
lea    0xd97(%rip),%rsi
call   10c0 <sprintf@plt>
mov    %rbp,%rdi
lea    0xd98(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1211 <getPidByName+0x71>
mov    %rax,%r15
lea    0xa0(%rsp),%rdi
mov    $0x3ff,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1209 <getPidByName+0x69>
lea    0xa0(%rsp),%rdi
lea    0xd60(%rip),%rsi
lea    0x20(%rsp),%rbp
mov    %rbp,%rdx
call   1090 <sscanf@plt>
mov    0x18(%rsp),%rdi
mov    %rbp,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1209 <getPidByName+0x69>
mov    0x0(%r13),%rdi
call   10b0 <atoi@plt>
mov    %eax,0xc(%rsp)
jmp    1209 <getPidByName+0x69>
mov    $0xffffffff,%eax
jmp    130b <getPidByName+0x16b>
movl   $0xffffffff,0xc(%rsp)
ff
mov    %rbp,%rdi
call   1050 <closedir@plt>
mov    0xc(%rsp),%eax
add    $0x4a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@DT_DIR = global i64 0
@.str.3 = [16 x i8] c"/proc/%s/status\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [7 x i8] c"%*s %s\00"
define i32 @getPidByName(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i32*
%6 = alloca [64 x i8]
%7 = alloca [64 x i8]
%8 = alloca [1024 x i8]
%9 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %9
%10 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = icmp ne i32* null, %11
br i1 %12, label %13, label %76
13: ; preds = %1
br label %14
14: ; preds = %72, %53, %37, %30, %13
%15 = load i32*, i32** %3
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %4
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %73
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %4
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %18
%25 = load %struct.dirent*, %struct.dirent** %4
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24, %18
br label %14
31: ; preds = %24
%32 = load i64, i64* @DT_DIR
%33 = load %struct.dirent*, %struct.dirent** %4
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 1
%35 = load i64, i64* %34
%36 = icmp ne i64 %32, %35
br i1 %36, label %37, label %38
37: ; preds = %31
br label %14
38: ; preds = %31
%39 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%40 = load %struct.dirent*, %struct.dirent** %4
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = call i32 @sprintf(i8* %39, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%45 = call i32* @fopen(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %45, i32** %5
%46 = load i32*, i32** %5
%47 = icmp ne i32* null, %46
br i1 %47, label %48, label %72
48: ; preds = %38
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%50 = load i32*, i32** %5
%51 = call i32* @fgets(i8* %49, i32 1023, i32* %50)
%52 = icmp eq i32* %51, null
br i1 %52, label %53, label %56
53: ; preds = %48
%54 = load i32*, i32** %5
%55 = call i32 @fclose(i32* %54)
br label %14
56: ; preds = %48
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%58 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%59 = call i32 @sscanf(i8* %57, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %58)
%60 = load i8*, i8** %2
%61 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%62 = call i64 @strcmp(i8* %60, i8* %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %69, label %64
64: ; preds = %56
%65 = load %struct.dirent*, %struct.dirent** %4
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i8*, i8** %66
%68 = call i32 @atoi(i8* %67)
store i32 %68, i32* %9
br label %69
69: ; preds = %64, %56
%70 = load i32*, i32** %5
%71 = call i32 @fclose(i32* %70)
br label %72
72: ; preds = %69, %38
br label %14
73: ; preds = %14
%74 = load i32*, i32** %3
%75 = call i32 @closedir(i32* %74)
br label %76
76: ; preds = %73, %1
%77 = load i32, i32* %9
ret i32 %77
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@DT_DIR = global i64 0
@.str.3 = [16 x i8] c"/proc/%s/status\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [7 x i8] c"%*s %s\00"
define i32 @getPidByName(i8* %0) {
%2 = alloca [64 x i8]
%3 = alloca [64 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%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 %61, label %10
10: ; preds = %1
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %58, label %13
13: ; preds = %10, %54
%14 = phi %struct.dirent* [ %56, %54 ], [ %11, %10 ]
%15 = phi i32 [ %55, %54 ], [ -1, %10 ]
br label %16
16: ; preds = %13, %26
%17 = phi %struct.dirent* [ %14, %13 ], [ %27, %26 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %16
%23 = load i8*, i8** %18
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %16, %22, %42, %29
%27 = call %struct.dirent* @readdir(i32* %8)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %58, label %16
29: ; preds = %22
%30 = load i64, i64* @DT_DIR
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 1
%32 = load i64, i64* %31
%33 = icmp eq i64 %30, %32
br i1 %33, label %34, label %26
34: ; preds = %29
%35 = load i8*, i8** %18
%36 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%38 = icmp eq i32* %37, null
br i1 %38, label %54, label %39
39: ; preds = %34
%40 = call i32* @fgets(i8* %7, i32 1023, i32* %37)
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @fclose(i32* %37)
br label %26
44: ; preds = %39
%45 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %6)
%46 = call i64 @strcmp(i8* %0, i8* %6)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i8*, i8** %18
%50 = call i32 @atoi(i8* %49)
br label %51
51: ; preds = %48, %44
%52 = phi i32 [ %15, %44 ], [ %50, %48 ]
%53 = call i32 @fclose(i32* %37)
br label %54
54: ; preds = %34, %51
%55 = phi i32 [ %52, %51 ], [ %15, %34 ]
%56 = call %struct.dirent* @readdir(i32* %8)
%57 = icmp eq %struct.dirent* %56, null
br i1 %57, label %58, label %13
58: ; preds = %54, %26, %10
%59 = phi i32 [ -1, %10 ], [ %15, %26 ], [ %55, %54 ]
%60 = call i32 @closedir(i32* %8)
br label %61
61: ; preds = %58, %1
%62 = phi i32 [ %59, %58 ], [ -1, %1 ]
ret i32 %62
}