#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 {
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ X_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
char *create_string(char const *);
scalar_t__ search_exec_path(char const *, char const *, char *, int);
int snprintf(char *, int, char *, char const *, char const *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strchr(char const *, char);
int is_process_dev(const char *dev, const char *cwd, const char *exec_path,
                   char **path_ref) {
  char buf[PATH_MAX];
  int n;
  struct stat st;
  assert(dev != NULL);
  if (!strchr(dev, '/') &&
      (search_exec_path(exec_path, dev, buf, sizeof(buf)) == 0)) {
    dev = buf;
  } else if ((dev[0] != '/') && (cwd != NULL)) {
    n = snprintf(buf, sizeof(buf), "%s/%s", cwd, dev);
    if ((n < 0) || (n >= sizeof(buf))) {
      return (0);
    }
    dev = buf;
  }
  if (stat(dev, &st) < 0) {
    return (0);
  }
  if (!S_ISREG(st.st_mode)) {
    return (0);
  }
  if (access(dev, X_OK) < 0) {
    return (0);
  }
  if (path_ref) {
    *path_ref = create_string(dev);
  }
  return (1);
}


x86 assembly (clang, -O0):
<is_process_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
cmp    $0x0,%eax
jne    121f <is_process_dev+0x9f>
mov    -0x50(%rbp),%rdx
mov    -0x58(%rbp),%rax
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    %eax,%ecx
call   10a0 <search_exec_path@plt>
cmp    $0x0,%rax
jne    121f <is_process_dev+0x9f>
mov    -0x50(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1296 <is_process_dev+0x116>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     1291 <is_process_dev+0x111>
cmpq   $0x0,-0x18(%rbp)
je     1291 <is_process_dev+0x111>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xdad(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jl     1276 <is_process_dev+0xf6>
mov    -0x58(%rbp),%rcx
movslq -0x3c(%rbp),%rax
cmp    %rcx,%rax
jb     1289 <is_process_dev+0x109>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    133f <is_process_dev+0x1bf>
mov    -0x50(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1296 <is_process_dev+0x116>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    12c0 <is_process_dev+0x140>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    133f <is_process_dev+0x1bf>
mov    -0x40(%rbp),%edi
call   1080 <S_ISREG@plt>
cmp    $0x0,%eax
jne    12e4 <is_process_dev+0x164>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    133f <is_process_dev+0x1bf>
mov    -0x10(%rbp),%rdi
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jge    1313 <is_process_dev+0x193>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    133f <is_process_dev+0x1bf>
cmpq   $0x0,-0x28(%rbp)
je     1331 <is_process_dev+0x1b1>
mov    -0x10(%rbp),%rdi
call   1060 <create_string@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_process_dev>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,-0x38(%rbp)
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%rbx
mov    0x2e4b(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r12
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
test   %eax,%eax
jne    11ee <is_process_dev+0x6e>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r15d,%ecx
call   10a0 <search_exec_path@plt>
test   %rax,%rax
je     1222 <is_process_dev+0xa2>
test   %r13,%r13
je     121f <is_process_dev+0x9f>
cmpb   $0x2f,(%rbx)
je     121f <is_process_dev+0x9f>
lea    0xe01(%rip),%rdx
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r13,%rcx
mov    %rbx,%r8
call   1040 <snprintf@plt>
xor    %ebx,%ebx
test   %eax,%eax
js     1273 <is_process_dev+0xf3>
cltq
cmp    %r15,%rax
jb     1222 <is_process_dev+0xa2>
jmp    1273 <is_process_dev+0xf3>
mov    %rbx,%r12
lea    -0x30(%rbp),%rsi
mov    %r12,%rdi
call   1070 <stat@plt>
xor    %ebx,%ebx
test   %rax,%rax
js     1273 <is_process_dev+0xf3>
mov    -0x30(%rbp),%edi
call   1080 <S_ISREG@plt>
test   %eax,%eax
je     1273 <is_process_dev+0xf3>
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1090 <access@plt>
test   %rax,%rax
js     1273 <is_process_dev+0xf3>
mov    $0x1,%ebx
mov    -0x38(%rbp),%rax
test   %rax,%rax
je     1273 <is_process_dev+0xf3>
mov    %r12,%rdi
mov    %rax,%r14
call   1060 <create_string@plt>
mov    %rax,(%r14)
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@X_OK = global i32 0
define i32 @is_process_dev(i8* %0, i8* %1, i8* %2, i8** %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8**
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca %struct.stat
%14 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8** %3, i8*** %9
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %10
%18 = alloca i8, i64 %16
store i64 %16, i64* %11
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i8*, i8** %6
%24 = call i32 @strchr(i8* %23, i8 signext 47)
%25 = icmp ne i32 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %4
%27 = load i8*, i8** %8
%28 = load i8*, i8** %6
%29 = trunc i64 %16 to i32
%30 = call i64 @search_exec_path(i8* %27, i8* %28, i8* %18, i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26
store i8* %18, i8** %6
br label %56
33: ; preds = %26, %4
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i64 0
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 47
br i1 %38, label %39, label %55
39: ; preds = %33
%40 = load i8*, i8** %7
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %55
42: ; preds = %39
%43 = trunc i64 %16 to i32
%44 = load i8*, i8** %7
%45 = load i8*, i8** %6
%46 = call i32 @snprintf(i8* %18, i32 %43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %44, i8* %45)
store i32 %46, i32* %12
%47 = load i32, i32* %12
%48 = icmp slt i32 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %42
%50 = load i32, i32* %12
%51 = sext i32 %50 to i64
%52 = icmp uge i64 %51, %16
br i1 %52, label %53, label %54
53: ; preds = %49, %42
store i32 0, i32* %5
store i32 1, i32* %14
br label %81
54: ; preds = %49
store i8* %18, i8** %6
br label %55
55: ; preds = %54, %39, %33
br label %56
56: ; preds = %55, %32
%57 = load i8*, i8** %6
%58 = call i64 @stat(i8* %57, %struct.stat* %13)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %56
store i32 0, i32* %5
store i32 1, i32* %14
br label %81
61: ; preds = %56
%62 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i32 @S_ISREG(i32 %63)
%65 = icmp ne i32 %64, 0
br i1 %65, label %67, label %66
66: ; preds = %61
store i32 0, i32* %5
store i32 1, i32* %14
br label %81
67: ; preds = %61
%68 = load i8*, i8** %6
%69 = load i32, i32* @X_OK
%70 = call i64 @access(i8* %68, i32 %69)
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %67
store i32 0, i32* %5
store i32 1, i32* %14
br label %81
73: ; preds = %67
%74 = load i8**, i8*** %9
%75 = icmp ne i8** %74, null
br i1 %75, label %76, label %80
76: ; preds = %73
%77 = load i8*, i8** %6
%78 = call i8* @create_string(i8* %77)
%79 = load i8**, i8*** %9
store i8* %78, i8** %79
br label %80
80: ; preds = %76, %73
store i32 1, i32* %5
store i32 1, i32* %14
br label %81
81: ; preds = %80, %72, %66, %60, %53
%82 = load i8*, i8** %10
%83 = load i32, i32* %5
ret i32 %83
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@X_OK = global i32 0
define i32 @is_process_dev(i8* %0, i8* %1, i8* %2, i8** writeonly %3) {
%5 = alloca %struct.stat
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = bitcast %struct.stat* %5 to i8*
%10 = icmp ne i8* %0, null
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = tail call i32 @strchr(i8* %0, i8 signext 47)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %4
%16 = call i64 @search_exec_path(i8* %2, i8* %0, i8* %8, i32 %6)
%17 = icmp eq i64 %16, 0
br i1 %17, label %29, label %18
18: ; preds = %15, %4
%19 = load i8, i8* %0
%20 = icmp ne i8 %19, 47
%21 = icmp ne i8* %1, null
%22 = and i1 %21, %20
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = call i32 @snprintf(i8* %8, i32 %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %0)
%25 = icmp slt i32 %24, 0
%26 = sext i32 %24 to i64
%27 = icmp uge i64 %26, %7
%28 = select i1 %25, i1 true, i1 %27
br i1 %28, label %46, label %29
29: ; preds = %23, %15, %18
%30 = phi i8* [ %0, %18 ], [ %8, %15 ], [ %8, %23 ]
%31 = call i64 @stat(i8* %30, %struct.stat* %5)
%32 = icmp slt i64 %31, 0
br i1 %32, label %46, label %33
33: ; preds = %29
%34 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @S_ISREG(i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %46, label %38
38: ; preds = %33
%39 = load i32, i32* @X_OK
%40 = call i64 @access(i8* %30, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = icmp eq i8** %3, null
br i1 %43, label %46, label %44
44: ; preds = %42
%45 = call i8* @create_string(i8* %30)
store i8* %45, i8** %3
br label %46
46: ; preds = %42, %44, %38, %33, %29, %23
%47 = phi i32 [ 0, %23 ], [ 0, %29 ], [ 0, %33 ], [ 0, %38 ], [ 1, %44 ], [ 1, %42 ]
ret i32 %47
}