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

/* Variables and functions */
int atoi(int /*<<< orphan*/);
scalar_t__ check_proc(int, char const *, char const *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
pid_t lookup_pid(const char *cmd, const char *path) {
  DIR *dir = NULL;
  struct dirent *ent = NULL;
  pid_t pid = -1, rv = -1;
  dir = opendir("/proc");
  while ((ent = readdir(dir)) != NULL) {
    pid = atoi(ent->d_name);
    if (pid < 1)
      continue;
    if (check_proc(pid, cmd, path) == 0) {
      rv = pid;
      break;
    }
  }
  closedir(dir);
  dir = NULL;
  return rv;
}


x86 assembly (clang, -O0):
<lookup_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
lea    0xe7b(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11ec <lookup_pid+0x9c>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1070 <atoi@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jge    11c2 <lookup_pid+0x72>
jmp    118e <lookup_pid+0x3e>
mov    -0x24(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1060 <check_proc@plt>
cmp    $0x0,%rax
jne    11e7 <lookup_pid+0x97>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    11ec <lookup_pid+0x9c>
jmp    118e <lookup_pid+0x3e>
mov    -0x18(%rbp),%rdi
call   1040 <closedir@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_pid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe9c(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
jne    118d <lookup_pid+0x3d>
mov    $0xffffffff,%ebp
jmp    11ac <lookup_pid+0x5c>
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     1179 <lookup_pid+0x29>
mov    (%rax),%edi
call   1070 <atoi@plt>
test   %eax,%eax
jle    1180 <lookup_pid+0x30>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <check_proc@plt>
test   %rax,%rax
jne    1180 <lookup_pid+0x30>
mov    %rbx,%rdi
call   1040 <closedir@plt>
mov    %ebp,%eax
add    $0x8,%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 @lookup_pid(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32* null, i32** %5
store %struct.dirent* null, %struct.dirent** %6
store i32 -1, i32* %7
store i32 -1, i32* %8
%9 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
br label %10
10: ; preds = %30, %21, %2
%11 = load i32*, i32** %5
%12 = call %struct.dirent* @readdir(i32* %11)
store %struct.dirent* %12, %struct.dirent** %6
%13 = icmp ne %struct.dirent* %12, null
br i1 %13, label %14, label %31
14: ; preds = %10
%15 = load %struct.dirent*, %struct.dirent** %6
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @atoi(i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 1
br i1 %20, label %21, label %22
21: ; preds = %14
br label %10
22: ; preds = %14
%23 = load i32, i32* %7
%24 = load i8*, i8** %3
%25 = load i8*, i8** %4
%26 = call i64 @check_proc(i32 %23, i8* %24, i8* %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = load i32, i32* %7
store i32 %29, i32* %8
br label %31
30: ; preds = %22
br label %10
31: ; preds = %28, %10
%32 = load i32*, i32** %5
%33 = call i32 @closedir(i32* %32)
store i32* null, i32** %5
%34 = load i32, i32* %8
ret i32 %34
}

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