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

/* Variables and functions */
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ X_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
int is_executable_file(const char *path) {
  if (access(path, X_OK)) {
    return 0;
  }
  struct stat st;
  return ((stat(path, &st) == 0) && S_ISREG(st.st_mode));
}


x86 assembly (clang, -O0):
<is_executable_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0x0,%rax
je     1164 <is_executable_file+0x34>
movl   $0x0,-0x4(%rbp)
jmp    11a0 <is_executable_file+0x70>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
jne    1195 <is_executable_file+0x65>
mov    -0x18(%rbp),%edi
call   1040 <S_ISREG@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_executable_file>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    117b <is_executable_file+0x4b>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    117b <is_executable_file+0x4b>
mov    0x8(%rsp),%edi
call   1040 <S_ISREG@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@X_OK = global i32 0
define i32 @is_executable_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @X_OK
%7 = call i64 @access(i8* %5, i32 %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 0, i32* %2
br label %22
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i64 @stat(i8* %11, %struct.stat* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i64 @S_ISREG(i32 %16)
%18 = icmp ne i64 %17, 0
br label %19
19: ; preds = %14, %10
%20 = phi i1 [ false, %10 ], [ %18, %14 ]
%21 = zext i1 %20 to i32
store i32 %21, i32* %2
br label %22
22: ; preds = %19, %9
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@X_OK = global i32 0
define i32 @is_executable_file(i8* %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @X_OK
%4 = tail call i64 @access(i8* %0, i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %18
6: ; preds = %1
%7 = bitcast %struct.stat* %2 to i8*
%8 = call i64 @stat(i8* %0, %struct.stat* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %6
%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 ne i64 %13, 0
%15 = zext i1 %14 to i32
br label %16
16: ; preds = %10, %6
%17 = phi i32 [ 0, %6 ], [ %15, %10 ]
br label %18
18: ; preds = %1, %16
%19 = phi i32 [ %17, %16 ], [ 0, %1 ]
ret i32 %19
}