#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 stat {
  scalar_t__ st_dev;
  scalar_t__ st_ino;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int find_exec_pid(const char *file) {
  char fullname[512];
  struct stat file_buf, exe_buf;
  if (stat(file, &file_buf) < 0) {
    perror(file);
    exit(2);
  }
  DIR *d = opendir("/proc");
  if (d) {
    struct dirent *e;
    while ((e = readdir(d))) {
      strcpy(fullname, "/proc/");
      strcat(fullname, e->d_name);
      strcat(fullname, "/exe");
      if (stat(fullname, &exe_buf) >= 0) {
        if (file_buf.st_dev == exe_buf.st_dev &&
            file_buf.st_ino == exe_buf.st_ino) {
          int pid = atoi(e->d_name);
          closedir(d);
          return pid;
        }
      }
    }
    closedir(d);
  }
  return -1;
}


x86 assembly (clang, -O0):
<find_exec_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x220(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    11cc <find_exec_pid+0x3c>
mov    -0x10(%rbp),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    0xe2d(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x238(%rbp)
cmpq   $0x0,-0x238(%rbp)
je     12d5 <find_exec_pid+0x145>
jmp    11f2 <find_exec_pid+0x62>
mov    -0x238(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x240(%rbp)
cmp    $0x0,%rax
je     12c9 <find_exec_pid+0x139>
lea    -0x210(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x210(%rbp),%rdi
mov    -0x240(%rbp),%rax
mov    (%rax),%rsi
call   10a0 <strcat@plt>
lea    -0x210(%rbp),%rdi
lea    0xdc7(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x210(%rbp),%rdi
lea    -0x230(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jl     12c4 <find_exec_pid+0x134>
mov    -0x220(%rbp),%rax
cmp    -0x230(%rbp),%rax
jne    12bf <find_exec_pid+0x12f>
mov    -0x218(%rbp),%rax
cmp    -0x228(%rbp),%rax
jne    12bf <find_exec_pid+0x12f>
mov    -0x240(%rbp),%rax
mov    (%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x244(%rbp)
mov    -0x238(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x244(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12dc <find_exec_pid+0x14c>
jmp    12c4 <find_exec_pid+0x134>
jmp    11f2 <find_exec_pid+0x62>
mov    -0x238(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_exec_pid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
mov    %rdi,%rbx
lea    0x10(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     128e <find_exec_pid+0xfe>
lea    0xe42(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1266 <find_exec_pid+0xd6>
mov    %rax,%r12
mov    %rax,%rdi
call   1070 <readdir@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1272 <find_exec_pid+0xe2>
mov    %rax,%rbp
lea    0xe17(%rip),%r15
lea    0x20(%rsp),%rbx
lea    0xe12(%rip),%r13
mov    %rsp,%r14
jmp    1210 <find_exec_pid+0x80>
mov    %r12,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     126d <find_exec_pid+0xdd>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    0x0(%rbp),%rsi
mov    %rbx,%rdi
call   10a0 <strcat@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10a0 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     1200 <find_exec_pid+0x70>
mov    0x10(%rsp),%rax
cmp    (%rsp),%rax
jne    1200 <find_exec_pid+0x70>
mov    0x18(%rsp),%rax
cmp    0x8(%rsp),%rax
jne    1200 <find_exec_pid+0x70>
mov    0x0(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
jmp    1272 <find_exec_pid+0xe2>
mov    $0xffffffff,%ebx
jmp    127a <find_exec_pid+0xea>
mov    $0xffffffff,%ebx
mov    %r12,%rdi
call   1050 <closedir@plt>
mov    %ebx,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/proc\00"
@.str.1 = [7 x i8] c"/proc/\00"
@.str.2 = [5 x i8] c"/exe\00"
define i32 @find_exec_pid(i8* %0) {
%2 = alloca [512 x i8]
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.stat* %3 to i8*
%7 = bitcast %struct.stat* %4 to i8*
%8 = call i64 @stat(i8* %0, %struct.stat* %3)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @perror(i8* %0)
%12 = call i32 @exit(i32 2)
unreachable
13: ; preds = %1
%14 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %51, label %16
16: ; preds = %13
%17 = call %struct.dirent* @readdir(i32* %14)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %48, label %19
19: ; preds = %16
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
br label %24
24: ; preds = %19, %41
%25 = phi %struct.dirent* [ %17, %19 ], [ %42, %41 ]
%26 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%28 = load i8*, i8** %27
%29 = call i32 @strcat(i8* %5, i8* %28)
%30 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%31 = call i64 @stat(i8* %5, %struct.stat* %4)
%32 = icmp sgt i64 %31, -1
br i1 %32, label %33, label %41
33: ; preds = %24
%34 = load i64, i64* %20
%35 = load i64, i64* %21
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = load i64, i64* %22
%39 = load i64, i64* %23
%40 = icmp eq i64 %38, %39
br i1 %40, label %44, label %41
41: ; preds = %33, %37, %24
%42 = call %struct.dirent* @readdir(i32* %14)
%43 = icmp eq %struct.dirent* %42, null
br i1 %43, label %48, label %24
44: ; preds = %37
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%46 = load i8*, i8** %45
%47 = call i32 @atoi(i8* %46)
br label %48
48: ; preds = %41, %16, %44
%49 = phi i32 [ %47, %44 ], [ -1, %16 ], [ -1, %41 ]
%50 = call i32 @closedir(i32* %14)
br label %51
51: ; preds = %48, %13
%52 = phi i32 [ -1, %13 ], [ %49, %48 ]
ret i32 %52
}