#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;
  scalar_t__ st_uid;
  scalar_t__ st_gid;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int);
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
scalar_t__ getegid();
scalar_t__ geteuid();
int stat(char *, struct stat *);
int isExecutable(char *filename) {
  int result;
  struct stat statinfo;
  result = stat(filename, &statinfo);
  if (result < 0)
    return 0;
  if (!S_ISREG(statinfo.st_mode))
    return 0;
  if (statinfo.st_uid == geteuid())
    return statinfo.st_mode & S_IXUSR;
  if (statinfo.st_gid == getegid())
    return statinfo.st_mode & S_IXGRP;
  return statinfo.st_mode & S_IXOTH;
}


x86 assembly (clang, -O0):
<isExecutable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1172 <isExecutable+0x32>
movl   $0x0,-0x4(%rbp)
jmp    1204 <isExecutable+0xc4>
mov    -0x30(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%eax
jne    118f <isExecutable+0x4f>
movl   $0x0,-0x4(%rbp)
jmp    1204 <isExecutable+0xc4>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <geteuid@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jne    11c2 <isExecutable+0x82>
mov    -0x30(%rbp),%eax
mov    0x2e28(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <isExecutable+0xc4>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1060 <getegid@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jne    11f5 <isExecutable+0xb5>
mov    -0x30(%rbp),%eax
mov    0x2ded(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <isExecutable+0xc4>
mov    -0x30(%rbp),%eax
mov    0x2de9(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isExecutable>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsp,%rsi
call   1040 <stat@plt>
xor    %ebp,%ebp
test   %eax,%eax
js     11a1 <isExecutable+0x61>
mov    (%rsp),%ebx
mov    %ebx,%edi
call   1050 <S_ISREG@plt>
test   %eax,%eax
je     11a1 <isExecutable+0x61>
mov    0x8(%rsp),%rbp
xor    %eax,%eax
call   1030 <geteuid@plt>
cmp    %rax,%rbp
jne    117c <isExecutable+0x3c>
mov    0x2e66(%rip),%rax
jmp    119d <isExecutable+0x5d>
mov    0x10(%rsp),%rbp
xor    %eax,%eax
call   1060 <getegid@plt>
cmp    %rax,%rbp
jne    1196 <isExecutable+0x56>
mov    0x2e44(%rip),%rax
jmp    119d <isExecutable+0x5d>
mov    0x2e4b(%rip),%rax
and    (%rax),%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32, i64, i64 }
@S_IXUSR = global i32 0
@S_IXGRP = global i32 0
@S_IXOTH = global i32 0
define i32 @isExecutable(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 slt i32 %4, 0
br i1 %5, label %30, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i32 @S_ISREG(i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %30, label %11
11: ; preds = %6
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%13 = load i64, i64* %12
%14 = tail call i64 (...) @geteuid()
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i32, i32* @S_IXUSR
%18 = and i32 %17, %8
br label %30
19: ; preds = %11
%20 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 2
%21 = load i64, i64* %20
%22 = tail call i64 (...) @getegid()
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* @S_IXGRP
%26 = and i32 %25, %8
br label %30
27: ; preds = %19
%28 = load i32, i32* @S_IXOTH
%29 = and i32 %28, %8
br label %30
30: ; preds = %6, %1, %27, %24, %16
%31 = phi i32 [ %18, %16 ], [ %26, %24 ], [ %29, %27 ], [ 0, %1 ], [ 0, %6 ]
ret i32 %31
}