#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 /*<<< orphan*/ EACCES;
int /*<<< orphan*/ EISDIR;
int /*<<< orphan*/ ENOENT;
scalar_t__ S_ISDIR(int);
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
int /*<<< orphan*/ errno;
scalar_t__ stat(char *, struct stat *);
int exec_ok(char *filename) {
  struct stat st;
  if (stat(filename, &st) < 0) {
    errno = ENOENT;
    return 0;
  }
  if (S_ISDIR(st.st_mode)) {
    errno = EISDIR;
    return 0;
  }
  if (!(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
    errno = EACCES;
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<exec_ok>:
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
jge    1161 <exec_ok+0x41>
mov    0x2e5e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e65(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11df <exec_ok+0xbf>
mov    -0x18(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1191 <exec_ok+0x71>
mov    0x2e5e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e35(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11df <exec_ok+0xbf>
mov    -0x18(%rbp),%eax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e2c(%rip),%rdx
or     (%rdx),%ecx
mov    0x2e3b(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    11d8 <exec_ok+0xb8>
mov    0x2e07(%rip),%rax
mov    (%rax),%ecx
mov    0x2dee(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11df <exec_ok+0xbf>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_ok>:
push   %rax
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
js     1144 <exec_ok+0x24>
mov    (%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
je     114d <exec_ok+0x2d>
mov    0x2e96(%rip),%rax
jmp    117b <exec_ok+0x5b>
mov    0x2e5d(%rip),%rax
jmp    117b <exec_ok+0x5b>
mov    0x2e8c(%rip),%rax
mov    0x2e75(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e82(%rip),%rax
or     (%rax),%ecx
mov    $0x1,%eax
test   %ecx,(%rsp)
je     1174 <exec_ok+0x54>
pop    %rcx
ret
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    0x2e34(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@ENOENT = global i32 0
@errno = global i32 0
@EISDIR = global i32 0
@S_IXUSR = global i32 0
@S_IXGRP = global i32 0
@S_IXOTH = global i32 0
@EACCES = global i32 0
define i32 @exec_ok(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 slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load i32, i32* @ENOENT
store i32 %9, i32* @errno
store i32 0, i32* %2
br label %30
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i64 @S_ISDIR(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = load i32, i32* @EISDIR
store i32 %16, i32* @errno
store i32 0, i32* %2
br label %30
17: ; preds = %10
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load i32, i32* @S_IXUSR
%21 = load i32, i32* @S_IXGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IXOTH
%24 = or i32 %22, %23
%25 = and i32 %19, %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %17
%28 = load i32, i32* @EACCES
store i32 %28, i32* @errno
store i32 0, i32* %2
br label %30
29: ; preds = %17
store i32 1, i32* %2
br label %30
30: ; preds = %29, %27, %15, %8
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOENT = global i32 0
@errno = global i32 0
@EISDIR = global i32 0
@S_IXUSR = global i32 0
@S_IXGRP = global i32 0
@S_IXOTH = global i32 0
@EACCES = global i32 0
define i32 @exec_ok(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %20, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = call i64 @S_ISDIR(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %6
%12 = load i32, i32* %7
%13 = load i32, i32* @S_IXUSR
%14 = load i32, i32* @S_IXGRP
%15 = or i32 %14, %13
%16 = load i32, i32* @S_IXOTH
%17 = or i32 %15, %16
%18 = and i32 %17, %12
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %11, %6, %1
%21 = phi i32* [ @ENOENT, %1 ], [ @EISDIR, %6 ], [ @EACCES, %11 ]
%22 = load i32, i32* %21
store i32 %22, i32* @errno
br label %23
23: ; preds = %20, %11
%24 = phi i32 [ 1, %11 ], [ 0, %20 ]
ret i32 %24
}