#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;
};
typedef int pid_t;
typedef int /*<<< orphan*/ lcontents;

/* Variables and functions */
int /*<<< orphan*/ _POSIX_PATH_MAX;
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strcmp(char *, char const *);
int strlen(char const *);
scalar_t__ strncmp(char *, char const *, int);
bool pid_is_exec(pid_t pid, const struct stat *esb) {
  char lname[32];
  char lcontents[_POSIX_PATH_MAX + 1];
  char *filename;
  const char deleted[] = " (deleted)";
  int nread;
  struct stat sb;
  sprintf(lname, "/proc/%d/exe", pid);
  nread = readlink(lname, lcontents, sizeof(lcontents) - 1);
  if (nread == -1)
    return false;
  filename = lcontents;
  filename[nread] = '\0';
  if (strncmp(filename, deleted, strlen(deleted)) == 0)
    filename += strlen(deleted);
  else if (strcmp(filename + nread - strlen(deleted), deleted) == 0)
    filename[nread - strlen(deleted)] = '\0';
  if (stat(filename, &sb) != 0)
    return false;
  return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
}


x86 assembly (clang, -O0):
<pid_is_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x78(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x80(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    0xe54(%rip),%rax
mov    %rax,-0x53(%rbp)
mov    0xe51(%rip),%ax
mov    %ax,-0x4b(%rbp)
mov    0xe49(%rip),%al
mov    %al,-0x49(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe39(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x80(%rbp),%rsi
mov    -0x78(%rbp),%rax
lea    -0x30(%rbp),%rdi
sub    $0x1,%rax
mov    %eax,%edx
call   1040 <readlink@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0xffffffff,-0x58(%rbp)
jne    120e <pid_is_exec+0xae>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    1365 <pid_is_exec+0x205>
mov    -0x80(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
movslq -0x58(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x48(%rbp),%rax
mov    %rax,-0x90(%rbp)
lea    -0x53(%rbp),%rax
mov    %rax,-0x88(%rbp)
lea    -0x53(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x90(%rbp),%rdi
mov    -0x88(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    127e <pid_is_exec+0x11e>
lea    -0x53(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
jmp    12f8 <pid_is_exec+0x198>
mov    -0x48(%rbp),%rax
movslq -0x58(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x98(%rbp)
lea    -0x53(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x98(%rbp),%rdi
movslq %eax,%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
lea    -0x53(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12f3 <pid_is_exec+0x193>
mov    -0x48(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x58(%rbp),%eax
mov    %eax,-0xa4(%rbp)
lea    -0x53(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xa4(%rbp),%ecx
mov    %eax,%edx
mov    -0xa0(%rbp),%rax
sub    %edx,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12f8 <pid_is_exec+0x198>
mov    -0x48(%rbp),%rdi
lea    -0x68(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     1322 <pid_is_exec+0x1c2>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    1365 <pid_is_exec+0x205>
mov    -0x68(%rbp),%rcx
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
mov    %al,-0xa5(%rbp)
jne    1350 <pid_is_exec+0x1f0>
mov    -0x60(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    0x8(%rcx),%rax
sete   %al
mov    %al,-0xa5(%rbp)
mov    -0xa5(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_is_exec>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsi,%r13
mov    %edi,%edx
mov    %rsp,%r14
mov    0x2e78(%rip),%rax
mov    (%rax),%r15d
lea    0x1(%r15),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movabs $0x6574656c65642820,%rax
65 74 65
mov    %rax,-0x48(%rbp)
movl   $0x296465,-0x41(%rbp)
lea    0xe57(%rip),%rsi
lea    -0x80(%rbp),%r12
mov    %r12,%rdi
call   1080 <sprintf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <readlink@plt>
mov    %eax,%r12d
xor    %eax,%eax
cmp    $0xffffffff,%r12d
je     127f <pid_is_exec+0x11f>
mov    %r13,-0x30(%rbp)
mov    %r14,-0x38(%rbp)
movslq %r12d,%r13
movb   $0x0,(%rbx,%r13,1)
lea    -0x48(%rbp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <strlen@plt>
cltq
test   %r15,%r15
je     1244 <pid_is_exec+0xe4>
add    %rbx,%r13
sub    %rax,%r13
lea    -0x48(%rbp),%rsi
mov    %r13,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1247 <pid_is_exec+0xe7>
lea    -0x48(%rbp),%rdi
call   1050 <strlen@plt>
sub    %eax,%r12d
movslq %r12d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    1247 <pid_is_exec+0xe7>
add    %rax,%rbx
mov    -0x38(%rbp),%r14
mov    -0x30(%rbp),%r15
lea    -0x58(%rbp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %rax,%rax
je     1264 <pid_is_exec+0x104>
xor    %eax,%eax
jmp    127f <pid_is_exec+0x11f>
mov    -0x58(%rbp),%rax
cmp    (%r15),%rax
mov    $0x0,%eax
jne    127f <pid_is_exec+0x11f>
mov    -0x50(%rbp),%rcx
xor    %eax,%eax
cmp    0x8(%r15),%rcx
sete   %al
mov    %r14,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@_POSIX_PATH_MAX = global i32 0
@__const.pid_is_exec.deleted = [11 x i8] c" (deleted)\00"
@.str = [13 x i8] c"/proc/%d/exe\00"
define i32 @pid_is_exec(i32 %0, %struct.stat* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat*
%6 = alloca [32 x i8]
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca [11 x i8]
%11 = alloca i32
%12 = alloca %struct.stat
%13 = alloca i32
store i32 %0, i32* %4
store %struct.stat* %1, %struct.stat** %5
%14 = load i32, i32* @_POSIX_PATH_MAX
%15 = add nsw i32 %14, 1
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %7
%18 = alloca i8, i64 %16
store i64 %16, i64* %8
%19 = bitcast [11 x i8]* %10 to i8*
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%21 = load i32, i32* %4
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%24 = sub i64 %16, 1
%25 = trunc i64 %24 to i32
%26 = call i32 @readlink(i8* %23, i8* %18, i32 %25)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %13
br label %91
30: ; preds = %2
store i8* %18, i8** %9
%31 = load i8*, i8** %9
%32 = load i32, i32* %11
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %9
%36 = getelementptr inbounds [11 x i8], [11 x i8]* %10, i64 0, i64 0
%37 = getelementptr inbounds [11 x i8], [11 x i8]* %10, i64 0, i64 0
%38 = call i32 @strlen(i8* %37)
%39 = call i64 @strncmp(i8* %35, i8* %36, i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %30
%42 = getelementptr inbounds [11 x i8], [11 x i8]* %10, i64 0, i64 0
%43 = call i32 @strlen(i8* %42)
%44 = load i8*, i8** %9
%45 = sext i32 %43 to i64
%46 = getelementptr inbounds i8, i8* %44, i64 %45
store i8* %46, i8** %9
br label %69
47: ; preds = %30
%48 = load i8*, i8** %9
%49 = load i32, i32* %11
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = getelementptr inbounds [11 x i8], [11 x i8]* %10, i64 0, i64 0
%53 = call i32 @strlen(i8* %52)
%54 = sext i32 %53 to i64
%55 = sub i64 0, %54
%56 = getelementptr inbounds i8, i8* %51, i64 %55
%57 = getelementptr inbounds [11 x i8], [11 x i8]* %10, i64 0, i64 0
%58 = call i64 @strcmp(i8* %56, i8* %57)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %68
60: ; preds = %47
%61 = load i8*, i8** %9
%62 = load i32, i32* %11
%63 = getelementptr inbounds [11 x i8], [11 x i8]* %10, i64 0, i64 0
%64 = call i32 @strlen(i8* %63)
%65 = sub nsw i32 %62, %64
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %61, i64 %66
store i8 0, i8* %67
br label %68
68: ; preds = %60, %47
br label %69
69: ; preds = %68, %41
%70 = load i8*, i8** %9
%71 = call i64 @stat(i8* %70, %struct.stat* %12)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %69
store i32 0, i32* %3
store i32 1, i32* %13
br label %91
74: ; preds = %69
%75 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%76 = load i64, i64* %75
%77 = load %struct.stat*, %struct.stat** %5
%78 = getelementptr inbounds %struct.stat, %struct.stat* %77, i32 0, i32 0
%79 = load i64, i64* %78
%80 = icmp eq i64 %76, %79
br i1 %80, label %81, label %88
81: ; preds = %74
%82 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%83 = load i64, i64* %82
%84 = load %struct.stat*, %struct.stat** %5
%85 = getelementptr inbounds %struct.stat, %struct.stat* %84, i32 0, i32 1
%86 = load i64, i64* %85
%87 = icmp eq i64 %83, %86
br label %88
88: ; preds = %81, %74
%89 = phi i1 [ false, %74 ], [ %87, %81 ]
%90 = zext i1 %89 to i32
store i32 %90, i32* %3
store i32 1, i32* %13
br label %91
91: ; preds = %88, %73, %29
%92 = load i8*, i8** %7
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@_POSIX_PATH_MAX = global i32 0
@__const.pid_is_exec.deleted = [11 x i8] c" (deleted)\00"
@.str = [13 x i8] c"/proc/%d/exe\00"
define i32 @pid_is_exec(i32 %0, %struct.stat* nocapture %1) {
%3 = alloca [32 x i8]
%4 = alloca [11 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @_POSIX_PATH_MAX
%8 = add nsw i32 %7, 1
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 0
%13 = bitcast %struct.stat* %5 to i8*
%14 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %0)
%15 = call i32 @readlink(i8* %6, i8* %11, i32 %7)
%16 = icmp eq i32 %15, -1
br i1 %16, label %54, label %17
17: ; preds = %2
%18 = sext i32 %15 to i64
%19 = getelementptr inbounds i8, i8* %11, i64 %18
store i8 0, i8* %19
%20 = call i32 @strlen(i8* %12)
%21 = call i64 @strncmp(i8* %11, i8* %12, i32 %20)
%22 = icmp eq i64 %21, 0
%23 = call i32 @strlen(i8* %12)
%24 = sext i32 %23 to i64
br i1 %22, label %25, label %27
25: ; preds = %17
%26 = getelementptr inbounds i8, i8* %11, i64 %24
br label %37
27: ; preds = %17
%28 = sub nsw i64 0, %24
%29 = getelementptr inbounds i8, i8* %19, i64 %28
%30 = call i64 @strcmp(i8* %29, i8* %12)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = call i32 @strlen(i8* %12)
%34 = sub nsw i32 %15, %33
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %11, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %27, %32, %25
%38 = phi i8* [ %26, %25 ], [ %11, %32 ], [ %11, %27 ]
%39 = call i64 @stat(i8* %38, %struct.stat* %5)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %54
41: ; preds = %37
%42 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%43 = load i64, i64* %42
%44 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%45 = load i64, i64* %44
%46 = icmp eq i64 %43, %45
br i1 %46, label %47, label %54
47: ; preds = %41
%48 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%49 = load i64, i64* %48
%50 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%51 = load i64, i64* %50
%52 = icmp eq i64 %49, %51
%53 = zext i1 %52 to i32
br label %54
54: ; preds = %41, %47, %37, %2
%55 = phi i32 [ 0, %2 ], [ 0, %37 ], [ 0, %41 ], [ %53, %47 ]
ret i32 %55
}