#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 */
typedef int uint32_t;
struct dirent {
  int /*<<< orphan*/ d_name;
};
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 *);
long getpid();
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);
uint32_t zrpc_util_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;
    }
    if (lpid == getpid())
      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 lpid;
        }
      }
      fclose(fp);
    }
  }
  closedir(dir);
  return -1;
}


x86 assembly (clang, -O0):
<zrpc_util_proc_find>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe35(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1201 <zrpc_util_proc_find+0x41>
lea    0xe10(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <zrpc_util_proc_find+0x1a0>
jmp    1206 <zrpc_util_proc_find+0x46>
mov    -0x18(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1350 <zrpc_util_proc_find+0x190>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    %rax,-0x238(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     124d <zrpc_util_proc_find+0x8d>
jmp    1206 <zrpc_util_proc_find+0x46>
mov    -0x238(%rbp),%rax
mov    %rax,-0x250(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rcx
mov    -0x250(%rbp),%rax
cmp    %rcx,%rax
jne    127a <zrpc_util_proc_find+0xba>
jmp    1206 <zrpc_util_proc_find+0x46>
lea    -0x230(%rbp),%rdi
mov    -0x238(%rbp),%rcx
mov    $0x200,%esi
lea    0xd7d(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x230(%rbp),%rdi
lea    0xd7c(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x240(%rbp)
cmpq   $0x0,-0x240(%rbp)
je     134b <zrpc_util_proc_find+0x18b>
lea    -0x230(%rbp),%rdi
mov    -0x240(%rbp),%rdx
mov    $0x200,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     133f <zrpc_util_proc_find+0x17f>
lea    -0x230(%rbp),%rdi
lea    0xd34(%rip),%rsi
call   10e0 <strtok@plt>
mov    %rax,-0x248(%rbp)
mov    -0x248(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    133a <zrpc_util_proc_find+0x17a>
mov    -0x240(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x238(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1360 <zrpc_util_proc_find+0x1a0>
jmp    133f <zrpc_util_proc_find+0x17f>
mov    -0x240(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1206 <zrpc_util_proc_find+0x46>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zrpc_util_proc_find>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdi,(%rsp)
lea    0xe2f(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     12f4 <zrpc_util_proc_find+0x134>
mov    %rax,%rbx
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12d1 <zrpc_util_proc_find+0x111>
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r13
lea    0xe14(%rip),%rbp
jmp    1231 <zrpc_util_proc_find+0x71>
mov    %r12,%rdi
call   1040 <fclose@plt>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12d1 <zrpc_util_proc_find+0x111>
mov    (%rax),%edi
mov    %r15,%rsi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
jne    1220 <zrpc_util_proc_find+0x60>
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    %rax,%r14
je     1220 <zrpc_util_proc_find+0x60>
mov    %r13,%rdi
mov    $0x200,%esi
lea    0xda9(%rip),%rdx
mov    %r14,%rcx
call   1060 <snprintf@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1220 <zrpc_util_proc_find+0x60>
mov    %rax,%r12
mov    %r13,%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1211 <zrpc_util_proc_find+0x51>
mov    %r13,%rdi
lea    0xd7f(%rip),%rsi
call   10e0 <strtok@plt>
mov    %rax,%rdi
mov    (%rsp),%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
jne    1211 <zrpc_util_proc_find+0x51>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1070 <closedir@plt>
jmp    12df <zrpc_util_proc_find+0x11f>
mov    %rbx,%rdi
call   1070 <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    0xd05(%rip),%rdi
call   10d0 <perror@plt>
jmp    12d9 <zrpc_util_proc_find+0x119>

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 @zrpc_util_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 %70
15: ; preds = %1
br label %16
16: ; preds = %66, %34, %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 %67
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 = load i64, i64* %8
%32 = call i64 (...) @getpid()
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30
br label %16
35: ; preds = %30
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%37 = load i64, i64* %8
%38 = call i32 @snprintf(i8* %36, i32 512, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 %37)
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %40, i32** %9
%41 = load i32*, i32** %9
%42 = icmp ne i32* %41, null
br i1 %42, label %43, label %66
43: ; preds = %35
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%45 = load i32*, i32** %9
%46 = call i32* @fgets(i8* %44, i32 512, i32* %45)
%47 = icmp ne i32* %46, null
br i1 %47, label %48, label %63
48: ; preds = %43
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%50 = call i8* @strtok(i8* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %50, i8** %10
%51 = load i8*, i8** %10
%52 = load i8*, i8** %3
%53 = call i32 @strcmp(i8* %51, i8* %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %62, label %55
55: ; preds = %48
%56 = load i32*, i32** %9
%57 = call i32 @fclose(i32* %56)
%58 = load i32*, i32** %4
%59 = call i32 @closedir(i32* %58)
%60 = load i64, i64* %8
%61 = trunc i64 %60 to i32
store i32 %61, i32* %2
br label %70
62: ; preds = %48
br label %63
63: ; preds = %62, %43
%64 = load i32*, i32** %9
%65 = call i32 @fclose(i32* %64)
br label %66
66: ; preds = %63, %35
br label %16
67: ; preds = %16
%68 = load i32*, i32** %4
%69 = call i32 @closedir(i32* %68)
store i32 -1, i32* %2
br label %70
70: ; preds = %67, %55, %13
%71 = load i32, i32* %2
ret i32 %71
}

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 @zrpc_util_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 %44, 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 %46
13: ; preds = %8, %41
%14 = phi %struct.dirent* [ %42, %41 ], [ %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 %41
21: ; preds = %13
%22 = call i64 (...) @getpid()
%23 = icmp eq i64 %17, %22
br i1 %23, label %41, label %24
24: ; preds = %21
%25 = call i32 @snprintf(i8* %5, i32 512, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 %17)
%26 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %41, label %28
28: ; preds = %24
%29 = call i32* @fgets(i8* %5, i32 512, i32* %26)
%30 = icmp eq i32* %29, null
br i1 %30, label %39, label %31
31: ; preds = %28
%32 = call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @strcmp(i8* dereferenceable(1) %32, i8* dereferenceable(1) %0)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = call i32 @fclose(i32* %26)
%37 = call i32 @closedir(i32* %6)
%38 = trunc i64 %17 to i32
br label %46
39: ; preds = %31, %28
%40 = call i32 @fclose(i32* %26)
br label %41
41: ; preds = %39, %24, %21, %13
%42 = call %struct.dirent* @readdir(i32* %6)
%43 = icmp eq %struct.dirent* %42, null
br i1 %43, label %44, label %13
44: ; preds = %41, %8
%45 = call i32 @closedir(i32* %6)
br label %46
46: ; preds = %35, %44, %11
%47 = phi i32 [ -1, %44 ], [ -1, %11 ], [ %38, %35 ]
ret i32 %47
}