#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 */
int S_IRUSR;
scalar_t__ S_ISREG(int);
int S_IXUSR;
int /*<<< orphan*/ perror(char *);
int stat(char const *, struct stat *);
int is_file_executable(const char *filename) {
  struct stat sb;
  if (stat(filename, &sb) == -1) {
    perror("stat");
    return 0;
  }
  if (S_ISREG(sb.st_mode) && (sb.st_mode & S_IRUSR) && (sb.st_mode & S_IXUSR))
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<is_file_executable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
jne    116a <is_file_executable+0x3a>
lea    0xea7(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11b9 <is_file_executable+0x89>
mov    -0x18(%rbp),%edi
call   1040 <S_ISREG@plt>
cmp    $0x0,%rax
je     11b2 <is_file_executable+0x82>
mov    -0x18(%rbp),%eax
mov    0x2e52(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11b2 <is_file_executable+0x82>
mov    -0x18(%rbp),%eax
mov    0x2e4d(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11b2 <is_file_executable+0x82>
movl   $0x1,-0x4(%rbp)
jmp    11b9 <is_file_executable+0x89>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_file_executable>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
je     1171 <is_file_executable+0x41>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1040 <S_ISREG@plt>
test   %rax,%rax
je     117d <is_file_executable+0x4d>
mov    0x2e7d(%rip),%rax
test   %ebx,(%rax)
je     117d <is_file_executable+0x4d>
mov    0x2e82(%rip),%rcx
mov    $0x1,%eax
test   %ebx,(%rcx)
jne    117f <is_file_executable+0x4f>
jmp    117d <is_file_executable+0x4d>
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@S_IRUSR = global i32 0
@S_IXUSR = global i32 0
define i32 @is_file_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 i32 @stat(i8* %5, %struct.stat* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %29
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i64 @S_ISREG(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @S_IRUSR
%19 = and i32 %17, %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %15
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @S_IXUSR
%25 = and i32 %23, %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 1, i32* %2
br label %29
28: ; preds = %21, %15, %10
store i32 0, i32* %2
br label %29
29: ; preds = %28, %27, %8
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@S_IRUSR = global i32 0
@S_IXUSR = global i32 0
define i32 @is_file_executable(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %22
8: ; preds = %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i64 @S_ISREG(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %8
%14 = load i32, i32* @S_IRUSR
%15 = and i32 %14, %10
%16 = icmp eq i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = load i32, i32* @S_IXUSR
%19 = and i32 %18, %10
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17, %13, %8
br label %22
22: ; preds = %17, %21, %6
%23 = phi i32 [ 0, %6 ], [ 0, %21 ], [ 1, %17 ]
ret i32 %23
}