#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__ S_ISREG(int);
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
int lstat(char *, struct stat *);
int isexecutable(char *filepath) {
  struct stat buf;
  int rc = lstat(filepath, &buf);
  if (rc < 0)
    return 0;
  if (S_ISREG(buf.st_mode)) {
    if (buf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<isexecutable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <lstat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1152 <isexecutable+0x32>
movl   $0x0,-0x4(%rbp)
jmp    11a5 <isexecutable+0x85>
mov    -0x18(%rbp),%edi
call   1040 <S_ISREG@plt>
cmp    $0x0,%rax
je     119e <isexecutable+0x7e>
mov    -0x18(%rbp),%eax
mov    0x2e72(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e61(%rip),%rdx
or     (%rdx),%ecx
mov    0x2e68(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1199 <isexecutable+0x79>
movl   $0x1,-0x4(%rbp)
jmp    11a5 <isexecutable+0x85>
jmp    119e <isexecutable+0x7e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isexecutable>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
call   1030 <lstat@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
js     116d <isexecutable+0x4d>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1040 <S_ISREG@plt>
test   %rax,%rax
je     116b <isexecutable+0x4b>
mov    0x2e92(%rip),%rax
mov    0x2e83(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e88(%rip),%rax
or     (%rax),%ecx
mov    $0x1,%eax
test   %ebx,%ecx
jne    116d <isexecutable+0x4d>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@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 @lstat(i8* %0, %struct.stat* %2)
%5 = icmp slt i32 %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 = tail call i64 @S_ISREG(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %19, label %11
11: ; preds = %6
%12 = load i32, i32* @S_IXUSR
%13 = load i32, i32* @S_IXGRP
%14 = or i32 %13, %12
%15 = load i32, i32* @S_IXOTH
%16 = or i32 %14, %15
%17 = and i32 %16, %8
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %11, %6
br label %20
20: ; preds = %11, %1, %19
%21 = phi i32 [ 0, %19 ], [ 0, %1 ], [ 1, %11 ]
ret i32 %21
}