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

/* Variables and functions */
scalar_t__ S_ISREG(int);
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
int /*<<< orphan*/ X_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
scalar_t__ getuid();
scalar_t__ stat(char const *, struct stat *);
int path_is_exec(const char *path) {
  struct stat s;
  int found = 0;
  if (access(path, X_OK) == 0 && stat(path, &s) == 0 && S_ISREG(s.st_mode) &&
      (getuid() != 0 || (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0)) {
    found = 1;
  }
  return found;
}


x86 assembly (clang, -O0):
<path_is_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    11d9 <path_is_exec+0x99>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jne    11d9 <path_is_exec+0x99>
mov    -0x10(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%rax
je     11d9 <path_is_exec+0x99>
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
jne    11d2 <path_is_exec+0x92>
mov    -0x10(%rbp),%eax
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e1c(%rip),%rdx
or     (%rdx),%ecx
mov    0x2e23(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11d9 <path_is_exec+0x99>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_is_exec>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    11ab <path_is_exec+0x6b>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %rax,%rax
jne    11ab <path_is_exec+0x6b>
mov    (%rsp),%edi
call   1050 <S_ISREG@plt>
test   %rax,%rax
je     11ab <path_is_exec+0x6b>
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <getuid@plt>
test   %rax,%rax
jne    11a6 <path_is_exec+0x66>
mov    0x2e53(%rip),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e49(%rip),%rax
or     (%rax),%ecx
test   %ecx,(%rsp)
je     11ab <path_is_exec+0x6b>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@X_OK = global i32 0
@S_IXUSR = global i32 0
@S_IXGRP = global i32 0
@S_IXOTH = global i32 0
define i32 @path_is_exec(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
%5 = load i8*, i8** %2
%6 = load i32, i32* @X_OK
%7 = call i64 @access(i8* %5, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %32
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i64 @stat(i8* %10, %struct.stat* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %32
13: ; preds = %9
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @S_ISREG(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %32
18: ; preds = %13
%19 = call i64 (...) @getuid()
%20 = icmp ne i64 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %18
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @S_IXUSR
%25 = load i32, i32* @S_IXGRP
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IXOTH
%28 = or i32 %26, %27
%29 = and i32 %23, %28
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %21, %18
store i32 1, i32* %4
br label %32
32: ; preds = %31, %21, %13, %9, %1
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@X_OK = global i32 0
@S_IXUSR = global i32 0
@S_IXGRP = global i32 0
@S_IXOTH = global i32 0
define i32 @path_is_exec(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @X_OK
%5 = tail call i64 @access(i8* %0, i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %28
7: ; preds = %1
%8 = call i64 @stat(i8* %0, %struct.stat* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %28
10: ; preds = %7
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i64 @S_ISREG(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %28, label %15
15: ; preds = %10
%16 = call i64 (...) @getuid()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = load i32, i32* %11
%20 = load i32, i32* @S_IXUSR
%21 = load i32, i32* @S_IXGRP
%22 = or i32 %21, %20
%23 = load i32, i32* @S_IXOTH
%24 = or i32 %22, %23
%25 = and i32 %24, %19
%26 = icmp eq i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %18, %15
br label %28
28: ; preds = %27, %18, %10, %7, %1
%29 = phi i32 [ 1, %27 ], [ 0, %18 ], [ 0, %10 ], [ 0, %7 ], [ 0, %1 ]
ret i32 %29
}