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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ snprintf(char *, int, char *, long);
scalar_t__ stat(char *, struct stat *);
int pid_is_exec(pid_t pid, const struct stat *esb) {
  struct stat sb;
  char buf[PATH_MAX];
  snprintf(buf, sizeof(buf), "/proc/%ld/exe", (long)pid);
  if (stat(buf, &sb) != 0)
    return 0;
  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    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2eb9(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe97(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     1198 <pid_is_exec+0x78>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    11d2 <pid_is_exec+0xb2>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
mov    %al,-0x49(%rbp)
jne    11c0 <pid_is_exec+0xa0>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    0x8(%rcx),%rax
sete   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%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   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rcx
mov    %rsp,%r15
mov    0x2eb3(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xea6(%rip),%rdx
mov    %rbx,%rdi
call   1030 <snprintf@plt>
lea    -0x28(%rbp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    118e <pid_is_exec+0x6e>
mov    -0x28(%rbp),%rcx
cmp    (%r14),%rcx
jne    118e <pid_is_exec+0x6e>
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
cmp    0x8(%r14),%rcx
sete   %al
mov    %r15,%rsp
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/proc/%ld/exe\00"
define i32 @pid_is_exec(i64 %0, %struct.stat* %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca %struct.stat*
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i64 %0, i64* %4
store %struct.stat* %1, %struct.stat** %5
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i8, i64 %11
store i64 %11, i64* %8
%14 = trunc i64 %11 to i32
%15 = load i64, i64* %4
%16 = call i32 @snprintf(i8* %13, i32 %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 %15)
%17 = call i64 @stat(i8* %13, %struct.stat* %6)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %9
br label %37
20: ; preds = %2
%21 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%22 = load i64, i64* %21
%23 = load %struct.stat*, %struct.stat** %5
%24 = getelementptr inbounds %struct.stat, %struct.stat* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = icmp eq i64 %22, %25
br i1 %26, label %27, label %34
27: ; preds = %20
%28 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%29 = load i64, i64* %28
%30 = load %struct.stat*, %struct.stat** %5
%31 = getelementptr inbounds %struct.stat, %struct.stat* %30, i32 0, i32 1
%32 = load i64, i64* %31
%33 = icmp eq i64 %29, %32
br label %34
34: ; preds = %27, %20
%35 = phi i1 [ false, %20 ], [ %33, %27 ]
%36 = zext i1 %35 to i32
store i32 %36, i32* %3
store i32 1, i32* %9
br label %37
37: ; preds = %34, %19
%38 = load i8*, i8** %7
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/proc/%ld/exe\00"
define i32 @pid_is_exec(i64 %0, %struct.stat* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = call i32 @snprintf(i8* %8, i32 %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 %0)
%10 = call i64 @stat(i8* %8, %struct.stat* %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %25
12: ; preds = %2
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = load i64, i64* %13
%15 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%16 = load i64, i64* %15
%17 = icmp eq i64 %14, %16
br i1 %17, label %18, label %25
18: ; preds = %12
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%22 = load i64, i64* %21
%23 = icmp eq i64 %20, %22
%24 = zext i1 %23 to i32
br label %25
25: ; preds = %12, %18, %2
%26 = phi i32 [ 0, %2 ], [ 0, %12 ], [ %24, %18 ]
ret i32 %26
}