#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__ ENOENT;
scalar_t__ S_ISREG(int);
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
scalar_t__ errno;
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ warning(char *, char const *, ...);
bool is_executable(const char *location) {
  struct stat st;
  if (stat(location, &st)) {
    if (errno != ENOENT) {
      warning("Failed to stat '%s': %s", location, strerror(errno));
    }
    return false;
  }
  if (S_ISREG(st.st_mode) &&
      (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0) {
    return true;
  }
  warning("File in path with executable name is not regular executable: %s",
          location);
  return false;
}


x86 assembly (clang, -O0):
<is_executable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     11b4 <is_executable+0x74>
mov    0x2e5e(%rip),%rax
mov    (%rax),%rax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
je     11a8 <is_executable+0x68>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1030 <warning@plt>
movl   $0x0,-0x4(%rbp)
jmp    1214 <is_executable+0xd4>
mov    -0x18(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%rax
je     11fb <is_executable+0xbb>
mov    -0x18(%rbp),%eax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2dff(%rip),%rdx
or     (%rdx),%ecx
mov    0x2e06(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11fb <is_executable+0xbb>
movl   $0x1,-0x4(%rbp)
jmp    1214 <is_executable+0xd4>
mov    -0x10(%rbp),%rsi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1030 <warning@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_executable>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
je     1187 <is_executable+0x47>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rdi
mov    0x2e54(%rip),%rax
xor    %ebp,%ebp
cmp    (%rax),%rdi
je     11cc <is_executable+0x8c>
call   1060 <strerror@plt>
lea    0xe89(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <warning@plt>
jmp    11cc <is_executable+0x8c>
mov    (%rsp),%edi
call   1050 <S_ISREG@plt>
test   %rax,%rax
je     11b9 <is_executable+0x79>
mov    0x2e45(%rip),%rax
mov    0x2e36(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e3b(%rip),%rax
or     (%rax),%ecx
mov    $0x1,%ebp
test   %ecx,(%rsp)
jne    11cc <is_executable+0x8c>
lea    0xe58(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <warning@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [24 x i8] c"Failed to stat '%s': %s\00"
@S_IXUSR = global i32 0
@S_IXGRP = global i32 0
@S_IXOTH = global i32 0
@.str.1 = [64 x i8] c"File in path with executable name is not regular executable: %s\00"
define i32 @is_executable(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @stat(i8* %5, %struct.stat* %4)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %1
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp ne i64 %9, %10
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = load i8*, i8** %3
%14 = load i64, i64* @errno
%15 = call i32 @strerror(i64 %14)
%16 = call i32 (i8*, i8*, ...) @warning(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %15)
br label %17
17: ; preds = %12, %8
store i32 0, i32* %2
br label %37
18: ; preds = %1
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i64 @S_ISREG(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %18
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @S_IXUSR
%27 = load i32, i32* @S_IXGRP
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IXOTH
%30 = or i32 %28, %29
%31 = and i32 %25, %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %23
store i32 1, i32* %2
br label %37
34: ; preds = %23, %18
%35 = load i8*, i8** %3
%36 = call i32 (i8*, i8*, ...) @warning(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0), i8* %35)
store i32 0, i32* %2
br label %37
37: ; preds = %34, %33, %17
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [24 x i8] c"Failed to stat '%s': %s\00"
@S_IXUSR = global i32 0
@S_IXGRP = global i32 0
@S_IXOTH = global i32 0
@.str.1 = [64 x i8] c"File in path with executable name is not regular executable: %s\00"
define i32 @is_executable(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %13, label %6
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %29, label %10
10: ; preds = %6
%11 = call i32 @strerror(i64 %7)
%12 = call i32 (i8*, i8*, ...) @warning(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %11)
br label %29
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @S_ISREG(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %27, label %18
18: ; preds = %13
%19 = load i32, i32* %14
%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 %27, label %29
27: ; preds = %18, %13
%28 = call i32 (i8*, i8*, ...) @warning(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %29
29: ; preds = %18, %6, %10, %27
%30 = phi i32 [ 0, %27 ], [ 0, %10 ], [ 0, %6 ], [ 1, %18 ]
ret i32 %30
}