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

/* Variables and functions */
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 *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, long);
int /*<<< orphan*/ strcmp(char *, char const *);
char *strtok(char *, char *);
long strtol(int /*<<< orphan*/, char **, int);
pid_t proc_find(const char *name) {
  DIR *dir;
  struct dirent *ent;
  char *endptr;
  char buf[512];
  if (!(dir = opendir("/proc"))) {
    perror("can't open /proc");
    return -1;
  }
  while ((ent = readdir(dir)) != NULL) {
    long lpid = strtol(ent->d_name, &endptr, 10);
    if (*endptr != '\0') {
      continue;
    }
    snprintf(buf, sizeof(buf), "/proc/%ld/cmdline", lpid);
    FILE *fp = fopen(buf, "r");
    if (fp) {
      if (fgets(buf, sizeof(buf), fp) != NULL) {
        char *first = strtok(buf, " ");
        if (!strcmp(first, name)) {
          fclose(fp);
          closedir(dir);
          return (pid_t)lpid;
        }
      }
      fclose(fp);
    }
  }
  closedir(dir);
  return -1;
}


x86 assembly (clang, -O0):
<proc_find>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe45(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11f1 <proc_find+0x41>
lea    0xe20(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <proc_find+0x173>
jmp    11f6 <proc_find+0x46>
mov    -0x18(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1313 <proc_find+0x163>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %rax,-0x238(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     123d <proc_find+0x8d>
jmp    11f6 <proc_find+0x46>
lea    -0x230(%rbp),%rdi
mov    -0x238(%rbp),%rcx
mov    $0x200,%esi
lea    0xdba(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x230(%rbp),%rdi
lea    0xdb9(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x240(%rbp)
cmpq   $0x0,-0x240(%rbp)
je     130e <proc_find+0x15e>
lea    -0x230(%rbp),%rdi
mov    -0x240(%rbp),%rdx
mov    $0x200,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1302 <proc_find+0x152>
lea    -0x230(%rbp),%rdi
lea    0xd71(%rip),%rsi
call   10d0 <strtok@plt>
mov    %rax,-0x248(%rbp)
mov    -0x248(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    12fd <proc_find+0x14d>
mov    -0x240(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x238(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1323 <proc_find+0x173>
jmp    1302 <proc_find+0x152>
mov    -0x240(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11f6 <proc_find+0x46>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_find>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdi,(%rsp)
lea    0xe3f(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12dc <proc_find+0x12c>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12b9 <proc_find+0x109>
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r13
lea    0xe24(%rip),%rbp
jmp    1229 <proc_find+0x79>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12b9 <proc_find+0x109>
mov    (%rax),%edi
mov    %r15,%rsi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
jne    1218 <proc_find+0x68>
mov    %r13,%rdi
mov    $0x200,%esi
lea    0xdbd(%rip),%rdx
mov    %r14,%rcx
call   1050 <snprintf@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1218 <proc_find+0x68>
mov    %rax,%r12
mov    %r13,%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1210 <proc_find+0x60>
mov    %r13,%rdi
lea    0xd97(%rip),%rsi
call   10d0 <strtok@plt>
mov    %rax,%rdi
mov    (%rsp),%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
jne    1210 <proc_find+0x60>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1060 <closedir@plt>
jmp    12c7 <proc_find+0x117>
mov    %rbx,%rdi
call   1060 <closedir@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd1d(%rip),%rdi
call   10c0 <perror@plt>
jmp    12c1 <proc_find+0x111>

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [17 x i8] c"can't open /proc\00"
@.str.2 = [18 x i8] c"/proc/%ld/cmdline\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c" \00"
define i32 @proc_find(i8* nocapture %0) {
%2 = alloca i8*
%3 = alloca [512 x i8]
%4 = bitcast i8** %2 to i8*
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%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 %11, label %8
8: ; preds = %1
%9 = call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %41, label %13
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %43
13: ; preds = %8, %38
%14 = phi %struct.dirent* [ %39, %38 ], [ %9, %8 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i64 @strtol(i32 %16, i8** %2, i32 10)
%18 = load i8*, i8** %2
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %38
21: ; preds = %13
%22 = call i32 @snprintf(i8* %5, i32 512, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 %17)
%23 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %38, label %25
25: ; preds = %21
%26 = call i32* @fgets(i8* %5, i32 512, i32* %23)
%27 = icmp eq i32* %26, null
br i1 %27, label %36, label %28
28: ; preds = %25
%29 = call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @strcmp(i8* dereferenceable(1) %29, i8* dereferenceable(1) %0)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = call i32 @fclose(i32* %23)
%34 = call i32 @closedir(i32* %6)
%35 = trunc i64 %17 to i32
br label %43
36: ; preds = %28, %25
%37 = call i32 @fclose(i32* %23)
br label %38
38: ; preds = %36, %21, %13
%39 = call %struct.dirent* @readdir(i32* %6)
%40 = icmp eq %struct.dirent* %39, null
br i1 %40, label %41, label %13
41: ; preds = %38, %8
%42 = call i32 @closedir(i32* %6)
br label %43
43: ; preds = %32, %41, %11
%44 = phi i32 [ -1, %41 ], [ -1, %11 ], [ %35, %32 ]
ret i32 %44
}