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

/* Variables and functions */
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
scalar_t__ getegid();
scalar_t__ geteuid();
scalar_t__ stat(char const *, struct stat *);
int is_executable(const char *fname) {
  struct stat tmp;
  if (stat(fname, &tmp) < 0) {
    return -1;
  }
  if (geteuid() == 0) {
    return 1;
  } else if (geteuid() == tmp.st_uid) {
    return tmp.st_mode & S_IXUSR;
  } else if (getegid() == tmp.st_gid) {
    return tmp.st_mode & S_IXGRP;
  } else {
    return tmp.st_mode & S_IXOTH;
  }
}


x86 assembly (clang, -O0):
<is_executable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    115f <is_executable+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <is_executable+0xa5>
mov    $0x0,%al
call   1030 <geteuid@plt>
cmp    $0x0,%rax
jne    117c <is_executable+0x4c>
movl   $0x1,-0x4(%rbp)
jmp    11d5 <is_executable+0xa5>
mov    $0x0,%al
call   1030 <geteuid@plt>
cmp    -0x28(%rbp),%rax
jne    11a1 <is_executable+0x71>
mov    -0x20(%rbp),%eax
mov    0x2e49(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d5 <is_executable+0xa5>
mov    $0x0,%al
call   1050 <getegid@plt>
cmp    -0x18(%rbp),%rax
jne    11c6 <is_executable+0x96>
mov    -0x20(%rbp),%eax
mov    0x2e1c(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d5 <is_executable+0xa5>
mov    -0x20(%rbp),%eax
mov    0x2e18(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_executable>:
sub    $0x18,%rsp
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
js     116c <is_executable+0x3c>
xor    %eax,%eax
call   1030 <geteuid@plt>
test   %rax,%rax
je     1176 <is_executable+0x46>
xor    %eax,%eax
call   1030 <geteuid@plt>
cmp    (%rsp),%rax
jne    1180 <is_executable+0x50>
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
add    $0x18,%rsp
ret
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret
mov    $0x1,%eax
add    $0x18,%rsp
ret
xor    %eax,%eax
call   1050 <getegid@plt>
mov    %rax,%rcx
mov    0x8(%rsp),%eax
cmp    0x10(%rsp),%rcx
jne    119e <is_executable+0x6e>
mov    0x2e3c(%rip),%rcx
jmp    11a5 <is_executable+0x75>
mov    0x2e43(%rip),%rcx
and    (%rcx),%eax
add    $0x18,%rsp
ret

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

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