#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;
};

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
int pid_is_exec(int pid, const struct stat *esb) {
  struct stat sb;
  char buf[32];
  sprintf(buf, "/proc/%d/exe", 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    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xec3(%rip),%rsi
call   1040 <sprintf@plt>
lea    -0x40(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0x0,%rax
je     1165 <pid_is_exec+0x45>
movl   $0x0,-0x4(%rbp)
jmp    1198 <pid_is_exec+0x78>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
mov    %al,-0x41(%rbp)
jne    118d <pid_is_exec+0x6d>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    0x8(%rcx),%rax
sete   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_is_exec>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %edi,%edx
lea    0xecd(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <sprintf@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    116c <pid_is_exec+0x4c>
mov    (%rsp),%rcx
cmp    (%r14),%rcx
jne    116c <pid_is_exec+0x4c>
mov    0x8(%rsp),%rcx
xor    %eax,%eax
cmp    0x8(%r14),%rcx
sete   %al
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@.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 %struct.stat
%7 = alloca [32 x i8]
store i32 %0, i32* %4
store %struct.stat* %1, %struct.stat** %5
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%9 = load i32, i32* %4
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%12 = call i64 @stat(i8* %11, %struct.stat* %6)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 0, i32* %3
br label %32
15: ; preds = %2
%16 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%17 = load i64, i64* %16
%18 = load %struct.stat*, %struct.stat** %5
%19 = getelementptr inbounds %struct.stat, %struct.stat* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = icmp eq i64 %17, %20
br i1 %21, label %22, label %29
22: ; preds = %15
%23 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%24 = load i64, i64* %23
%25 = load %struct.stat*, %struct.stat** %5
%26 = getelementptr inbounds %struct.stat, %struct.stat* %25, i32 0, i32 1
%27 = load i64, i64* %26
%28 = icmp eq i64 %24, %27
br label %29
29: ; preds = %22, %15
%30 = phi i1 [ false, %15 ], [ %28, %22 ]
%31 = zext i1 %30 to i32
store i32 %31, i32* %3
br label %32
32: ; preds = %29, %14
%33 = load i32, i32* %3
ret i32 %33
}

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