#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 PATH_MAX;
int /*<<< orphan*/ S_ISDIR(int);
int /*<<< orphan*/ S_ISREG(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, char const *);
scalar_t__ lstat(char const *, struct stat *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
void validate_exe(const char *root, const char *exe) {
  struct stat st;
  char path[PATH_MAX];
  if (exe[0] != '/')
    error(1, 0, "%s is not an absolute path", exe);
  if (root[0] != '/')
    error(1, 0, "%s is not an absolute path", root);
  if (lstat(root, &st) < 0)
    error(1, errno, "can't stat '%s'", root);
  if (!S_ISDIR(st.st_mode))
    error(1, 0, "%s is not a directory", root);
  exe++;
  snprintf(path, sizeof path, "%s/%s", root, exe);
  if (lstat(path, &st) < 0)
    error(1, errno, "can't stat '%s'", path);
  if (!S_ISREG(st.st_mode))
    error(1, 0, "%s is not a file", path);
  if ((st.st_mode & 0500) != 0500)
    error(1, 0, "%s is not executable", path);
}


x86 assembly (clang, -O0):
<validate_exe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11b7 <validate_exe+0x67>
mov    -0x10(%rbp),%rcx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe4e(%rip),%rdx
call   1070 <error@plt>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11de <validate_exe+0x8e>
mov    -0x8(%rbp),%rcx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe27(%rip),%rdx
call   1070 <error@plt>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0x0,%rax
jge    1213 <validate_exe+0xc3>
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edi
lea    0xe0d(%rip),%rdx
call   1070 <error@plt>
mov    -0x18(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    123b <validate_exe+0xeb>
mov    -0x8(%rbp),%rcx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xdf5(%rip),%rdx
call   1070 <error@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xde1(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x30(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0x0,%rax
jge    129a <validate_exe+0x14a>
mov    -0x30(%rbp),%rcx
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xd86(%rip),%rdx
call   1070 <error@plt>
mov    -0x18(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    12c2 <validate_exe+0x172>
mov    -0x30(%rbp),%rcx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xd8a(%rip),%rdx
call   1070 <error@plt>
mov    -0x18(%rbp),%eax
and    $0x140,%eax
cmp    $0x140,%eax
je     12ec <validate_exe+0x19c>
mov    -0x30(%rbp),%rcx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xd71(%rip),%rdx
call   1070 <error@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_exe>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsp,%r13
mov    0x2e82(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%r14
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
cmpb   $0x2f,(%rsi)
je     119e <validate_exe+0x4e>
lea    0xe71(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
mov    %rbx,%rcx
call   1070 <error@plt>
cmpb   $0x2f,(%r15)
je     11ba <validate_exe+0x6a>
lea    0xe55(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
mov    %r15,%rcx
call   1070 <error@plt>
lea    -0x30(%rbp),%rsi
mov    %r15,%rdi
call   1050 <lstat@plt>
test   %rax,%rax
jns    11e8 <validate_exe+0x98>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    0xe40(%rip),%rdx
mov    $0x1,%edi
mov    %r15,%rcx
call   1070 <error@plt>
mov    -0x30(%rbp),%edi
call   1030 <S_ISDIR@plt>
test   %eax,%eax
jne    120a <validate_exe+0xba>
lea    0xe30(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
mov    %r15,%rcx
call   1070 <error@plt>
add    $0x1,%rbx
lea    0xe2c(%rip),%rdx
mov    %r14,%rdi
mov    %r12d,%esi
mov    %r15,%rcx
mov    %rbx,%r8
call   1040 <snprintf@plt>
lea    -0x30(%rbp),%rsi
mov    %r14,%rdi
call   1050 <lstat@plt>
test   %rax,%rax
jns    1254 <validate_exe+0x104>
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
lea    0xdd4(%rip),%rdx
mov    $0x1,%edi
mov    %r14,%rcx
call   1070 <error@plt>
mov    -0x30(%rbp),%ebx
mov    %ebx,%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
jne    1278 <validate_exe+0x128>
lea    0xdde(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
mov    %r14,%rcx
call   1070 <error@plt>
not    %ebx
test   $0x140,%ebx
je     1298 <validate_exe+0x148>
lea    0xdcf(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
mov    %r14,%rcx
call   1070 <error@plt>
mov    %r13,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [27 x i8] c"%s is not an absolute path\00"
@errno = global i32 0
@.str.1 = [16 x i8] c"can't stat '%s'\00"
@.str.2 = [22 x i8] c"%s is not a directory\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [17 x i8] c"%s is not a file\00"
@.str.5 = [21 x i8] c"%s is not executable\00"
define void @validate_exe(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %6
%11 = alloca i8, i64 %9
store i64 %9, i64* %7
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 47
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %2
%21 = load i8*, i8** %3
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 47
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = load i8*, i8** %3
%28 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %26, %20
%30 = load i8*, i8** %3
%31 = call i64 @lstat(i8* %30, %struct.stat* %5)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* @errno
%35 = load i8*, i8** %3
%36 = call i32 @error(i32 1, i32 %34, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %33, %29
%38 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @S_ISDIR(i32 %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %37
%43 = load i8*, i8** %3
%44 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %42, %37
%46 = load i8*, i8** %4
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %4
%48 = trunc i64 %9 to i32
%49 = load i8*, i8** %3
%50 = load i8*, i8** %4
%51 = call i32 @snprintf(i8* %11, i32 %48, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %49, i8* %50)
%52 = call i64 @lstat(i8* %11, %struct.stat* %5)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %45
%55 = load i32, i32* @errno
%56 = call i32 @error(i32 1, i32 %55, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %57
57: ; preds = %54, %45
%58 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i32 @S_ISREG(i32 %59)
%61 = icmp ne i32 %60, 0
br i1 %61, label %64, label %62
62: ; preds = %57
%63 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %11)
br label %64
64: ; preds = %62, %57
%65 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%66 = load i32, i32* %65
%67 = and i32 %66, 320
%68 = icmp ne i32 %67, 320
br i1 %68, label %69, label %71
69: ; preds = %64
%70 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %11)
br label %71
71: ; preds = %69, %64
%72 = load i8*, i8** %6
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [27 x i8] c"%s is not an absolute path\00"
@errno = global i32 0
@.str.1 = [16 x i8] c"can't stat '%s'\00"
@.str.2 = [22 x i8] c"%s is not a directory\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [17 x i8] c"%s is not a file\00"
@.str.5 = [21 x i8] c"%s is not executable\00"
define void @validate_exe(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = load i8, i8* %1
%10 = icmp eq i8 %9, 47
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = tail call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %13
13: ; preds = %11, %2
%14 = load i8, i8* %0
%15 = icmp eq i8 %14, 47
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %16, %13
%19 = call i64 @lstat(i8* %0, %struct.stat* %3)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = tail call i32 @error(i32 1, i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %24
24: ; preds = %21, %18
%25 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%26 = load i32, i32* %25
%27 = tail call i32 @S_ISDIR(i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = tail call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %31
31: ; preds = %29, %24
%32 = getelementptr inbounds i8, i8* %1, i64 1
%33 = call i32 @snprintf(i8* %8, i32 %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %32)
%34 = call i64 @lstat(i8* %8, %struct.stat* %3)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i32, i32* @errno
%38 = call i32 @error(i32 1, i32 %37, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %39
39: ; preds = %36, %31
%40 = load i32, i32* %25
%41 = call i32 @S_ISREG(i32 %40)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %8)
br label %45
45: ; preds = %43, %39
%46 = and i32 %40, 320
%47 = icmp eq i32 %46, 320
br i1 %47, label %50, label %48
48: ; preds = %45
%49 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %8)
br label %50
50: ; preds = %48, %45
ret void
}