#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 /*<<< orphan*/ DIR;

/* Variables and functions */
int atoi(int /*<<< orphan*/);
int /*<<< orphan*/ commandFromPid(int, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char const *, char *);
int findPid(const char *progname) {
  char name[256];
  DIR *dir;
  int pid = -1;
  struct dirent *ds;
  dir = opendir("/proc");
  if (dir == NULL)
    return -1;
  while ((ds = readdir(dir)) != NULL) {
    pid = -1;
    pid = atoi(ds->d_name);
    if (pid != -1) {
      commandFromPid(pid, name);
      if (strcmp(progname, name) == 0)
        return pid;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<findPid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x11c(%rbp)
ff ff ff
lea    0xe90(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jne    1196 <findPid+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <findPid+0xde>
jmp    119b <findPid+0x4b>
mov    -0x118(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
je     1227 <findPid+0xd7>
movl   $0xffffffff,-0x11c(%rbp)
ff ff ff
mov    -0x128(%rbp),%rax
mov    (%rax),%edi
call   1060 <atoi@plt>
mov    %eax,-0x11c(%rbp)
cmpl   $0xffffffff,-0x11c(%rbp)
je     1222 <findPid+0xd2>
mov    -0x11c(%rbp),%edi
lea    -0x110(%rbp),%rsi
call   1070 <commandFromPid@plt>
mov    -0x10(%rbp),%rdi
lea    -0x110(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    121d <findPid+0xcd>
mov    -0x11c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122e <findPid+0xde>
jmp    1222 <findPid+0xd2>
jmp    119b <findPid+0x4b>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findPid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%r14
lea    0xe99(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11c7 <findPid+0x77>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     11c7 <findPid+0x77>
mov    %rsp,%r15
jmp    119d <findPid+0x4d>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     11c7 <findPid+0x77>
mov    (%rax),%edi
call   1060 <atoi@plt>
cmp    $0xffffffff,%eax
je     1190 <findPid+0x40>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
call   1070 <commandFromPid@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    1190 <findPid+0x40>
jmp    11cc <findPid+0x7c>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/proc\00"
define i32 @findPid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.dirent*
store i8* %0, i8** %3
store i32 -1, i32* %6
%8 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %37
12: ; preds = %1
br label %13
13: ; preds = %35, %12
%14 = load i32*, i32** %5
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %7
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %36
17: ; preds = %13
store i32 -1, i32* %6
%18 = load %struct.dirent*, %struct.dirent** %7
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @atoi(i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, -1
br i1 %23, label %24, label %35
24: ; preds = %17
%25 = load i32, i32* %6
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%27 = call i32 @commandFromPid(i32 %25, i8* %26)
%28 = load i8*, i8** %3
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%30 = call i64 @strcmp(i8* %28, i8* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = load i32, i32* %6
store i32 %33, i32* %2
br label %37
34: ; preds = %24
br label %35
35: ; preds = %34, %17
br label %13
36: ; preds = %13
store i32 -1, i32* %2
br label %37
37: ; preds = %36, %32, %11
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/proc\00"
define i32 @findPid(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %22, label %6
6: ; preds = %1
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %22, label %9
9: ; preds = %6, %19
%10 = phi %struct.dirent* [ %20, %19 ], [ %7, %6 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @atoi(i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %19, label %15
15: ; preds = %9
%16 = call i32 @commandFromPid(i32 %13, i8* %3)
%17 = call i64 @strcmp(i8* %0, i8* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15, %9
%20 = call %struct.dirent* @readdir(i32* %4)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %22, label %9
22: ; preds = %15, %19, %6, %1
%23 = phi i32 [ -1, %1 ], [ -1, %6 ], [ %13, %15 ], [ -1, %19 ]
ret i32 %23
}