#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*/ ac_log(int, char *);
long atol(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, long *, 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 *);
pid_t proc_find(const char *name) {
  DIR *dir;
  struct dirent *ent;
  char buf[512];
  long pid;
  char pname[100] = {
      0,
  };
  char state;
  FILE *fp = NULL;
  if (!(dir = opendir("/proc"))) {
    perror("can't open /proc");
    return -1;
  }
  while ((ent = readdir(dir)) != NULL) {
    long lpid = atol(ent->d_name);
    if (lpid < 0)
      continue;
    snprintf(buf, sizeof(buf), "/proc/%ld/stat", lpid);
    fp = fopen(buf, "r");
    if (fp) {
      if ((fscanf(fp, "%ld (%[^)]) %c", &pid, pname, &state)) != 3) {
        ac_log(2, "fscanf failed \n");
        fclose(fp);
        closedir(dir);
        return -1;
      }
      if (!strcmp(pname, 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    $0x2b0,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x290(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
movq   $0x0,-0x2a0(%rbp)
lea    0xe17(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    121f <proc_find+0x5f>
lea    0xdf2(%rip),%rdi
call   10e0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <proc_find+0x1a8>
jmp    1224 <proc_find+0x64>
mov    -0x18(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1358 <proc_find+0x198>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   10c0 <atol@plt>
mov    %rax,-0x2a8(%rbp)
cmpq   $0x0,-0x2a8(%rbp)
jge    1260 <proc_find+0xa0>
jmp    1224 <proc_find+0x64>
lea    -0x220(%rbp),%rdi
mov    -0x2a8(%rbp),%rcx
mov    $0x200,%esi
lea    0xd97(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    0xd93(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x2a0(%rbp)
cmpq   $0x0,-0x2a0(%rbp)
je     1353 <proc_find+0x193>
mov    -0x2a0(%rbp),%rdi
lea    -0x290(%rbp),%rcx
lea    0xd66(%rip),%rsi
lea    -0x228(%rbp),%rdx
lea    -0x291(%rbp),%r8
call   1070 <fscanf@plt>
cmp    $0x3,%eax
je     130a <proc_find+0x14a>
mov    $0x2,%edi
lea    0xd4d(%rip),%rsi
call   1090 <ac_log@plt>
mov    -0x2a0(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <proc_find+0x1a8>
lea    -0x290(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    1347 <proc_find+0x187>
mov    -0x2a0(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x2a8(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1368 <proc_find+0x1a8>
mov    -0x2a0(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1224 <proc_find+0x64>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_find>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x298,%rsp
mov    %rdi,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movl   $0x0,0x80(%rsp)
lea    0xe12(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1306 <proc_find+0x156>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12e3 <proc_find+0x133>
lea    0x90(%rsp),%r12
lea    0x20(%rsp),%rbp
jmp    1241 <proc_find+0x91>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12e3 <proc_find+0x133>
mov    (%rax),%edi
call   10b0 <atol@plt>
test   %rax,%rax
js     1230 <proc_find+0x80>
mov    %rax,%r15
mov    %r12,%rdi
mov    $0x200,%esi
lea    0xdb2(%rip),%rdx
mov    %rax,%rcx
call   1050 <snprintf@plt>
mov    %r12,%rdi
lea    0xdaf(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1230 <proc_find+0x80>
mov    %rax,%r13
mov    %rax,%rdi
lea    0xd9a(%rip),%rsi
lea    0x18(%rsp),%rdx
mov    %rbp,%rcx
lea    0xf(%rsp),%r8
call   1060 <fscanf@plt>
cmp    $0x3,%eax
jne    12ca <proc_find+0x11a>
mov    %rbp,%rdi
mov    0x10(%rsp),%rsi
call   1090 <strcmp@plt>
mov    %eax,%r14d
mov    %r13,%rdi
call   1030 <fclose@plt>
test   %r14d,%r14d
jne    1230 <proc_find+0x80>
mov    %rbx,%rdi
call   1070 <closedir@plt>
jmp    12f1 <proc_find+0x141>
lea    0xd60(%rip),%rsi
mov    $0x2,%edi
call   1080 <ac_log@plt>
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x298,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf3(%rip),%rdi
call   10d0 <perror@plt>
jmp    12eb <proc_find+0x13b>

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 = [15 x i8] c"/proc/%ld/stat\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [15 x i8] c"%ld (%[^)]) %c\00"
@.str.5 = [16 x i8] c"fscanf failed \0A\00"
define i32 @proc_find(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca [512 x i8]
%7 = alloca i64
%8 = alloca [100 x i8]
%9 = alloca i8
%10 = alloca i32*
%11 = alloca i64
store i8* %0, i8** %3
%12 = bitcast [100 x i8]* %8 to i8*
store i32* null, i32** %10
%13 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %4
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %1
%16 = 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 %68
17: ; preds = %1
br label %18
18: ; preds = %64, %29, %17
%19 = load i32*, i32** %4
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %5
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %65
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %5
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @atol(i32 %25)
store i64 %26, i64* %11
%27 = load i64, i64* %11
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %22
br label %18
30: ; preds = %22
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%32 = load i64, i64* %11
%33 = call i32 @snprintf(i8* %31, i32 512, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i64 %32)
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %6, 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** %10
%36 = load i32*, i32** %10
%37 = icmp ne i32* %36, null
br i1 %37, label %38, label %64
38: ; preds = %30
%39 = load i32*, i32** %10
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%41 = call i32 @fscanf(i32* %39, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64* %7, i8* %40, i8* %9)
%42 = icmp ne i32 %41, 3
br i1 %42, label %43, label %49
43: ; preds = %38
%44 = call i32 @ac_log(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%45 = load i32*, i32** %10
%46 = call i32 @fclose(i32* %45)
%47 = load i32*, i32** %4
%48 = call i32 @closedir(i32* %47)
store i32 -1, i32* %2
br label %68
49: ; preds = %38
%50 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%51 = load i8*, i8** %3
%52 = call i32 @strcmp(i8* %50, i8* %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %61, label %54
54: ; preds = %49
%55 = load i32*, i32** %10
%56 = call i32 @fclose(i32* %55)
%57 = load i32*, i32** %4
%58 = call i32 @closedir(i32* %57)
%59 = load i64, i64* %11
%60 = trunc i64 %59 to i32
store i32 %60, i32* %2
br label %68
61: ; preds = %49
%62 = load i32*, i32** %10
%63 = call i32 @fclose(i32* %62)
br label %64
64: ; preds = %61, %30
br label %18
65: ; preds = %18
%66 = load i32*, i32** %4
%67 = call i32 @closedir(i32* %66)
store i32 -1, i32* %2
br label %68
68: ; preds = %65, %54, %43, %15
%69 = load i32, i32* %2
ret i32 %69
}

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