#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 S_IFREG;
int S_IRUSR;
int S_IXUSR;
int /*<<< orphan*/ errno;
scalar_t__ stat(char *, struct stat *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ svz_log(int, char *, char *);
int svz_process_check_executable(char *file, char **app) {
  struct stat buf;
  if (stat(file, &buf) < 0) {
    svz_log(1, "passthrough: stat: %s\n", strerror(errno));
    return -1;
  }
  if (!(buf.st_mode & S_IFREG) || !(buf.st_mode & S_IXUSR) ||
      !(buf.st_mode & S_IRUSR)) {
    svz_log(1, "passthrough: no executable: %s\n", file);
    return -1;
  }
  if (app != NULL)
    *app = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<svz_process_check_executable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0x0,%rax
jge    1185 <svz_process_check_executable+0x55>
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
mov    %rax,%rdx
mov    $0x1,%edi
lea    0xe8c(%rip),%rsi
call   1050 <svz_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <svz_process_check_executable+0xd2>
mov    -0x20(%rbp),%eax
mov    0x2e51(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11c4 <svz_process_check_executable+0x94>
mov    -0x20(%rbp),%eax
mov    0x2e44(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11c4 <svz_process_check_executable+0x94>
mov    -0x20(%rbp),%eax
mov    0x2e0f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11e5 <svz_process_check_executable+0xb5>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe43(%rip),%rsi
call   1050 <svz_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <svz_process_check_executable+0xd2>
cmpq   $0x0,-0x18(%rbp)
je     11fb <svz_process_check_executable+0xcb>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<svz_process_check_executable>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1030 <stat@plt>
test   %rax,%rax
js     118c <svz_process_check_executable+0x5c>
mov    (%rsp),%eax
mov    0x2e8f(%rip),%rcx
test   %eax,(%rcx)
je     117b <svz_process_check_executable+0x4b>
mov    0x2e8c(%rip),%rcx
test   %eax,(%rcx)
je     117b <svz_process_check_executable+0x4b>
mov    0x2e61(%rip),%rcx
test   %eax,(%rcx)
je     117b <svz_process_check_executable+0x4b>
xor    %eax,%eax
test   %r14,%r14
je     11b3 <svz_process_check_executable+0x83>
movq   $0x0,(%r14)
jmp    11b3 <svz_process_check_executable+0x83>
lea    0xe95(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
jmp    11a9 <svz_process_check_executable+0x79>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
lea    0xe5f(%rip),%rsi
mov    $0x1,%edi
mov    %rax,%rdx
call   1050 <svz_log@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [23 x i8] c"passthrough: stat: %s\0A\00"
@errno = global i32 0
@S_IFREG = global i32 0
@S_IXUSR = global i32 0
@S_IRUSR = global i32 0
@.str.1 = [32 x i8] c"passthrough: no executable: %s\0A\00"
define i32 @svz_process_check_executable(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca %struct.stat
store i8* %0, i8** %4
store i8** %1, i8*** %5
%7 = load i8*, i8** %4
%8 = call i64 @stat(i8* %7, %struct.stat* %6)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @svz_log(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %12)
store i32 -1, i32* %3
br label %41
14: ; preds = %2
%15 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @S_IFREG
%18 = and i32 %16, %17
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %14
%21 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%22 = load i32, i32* %21
%23 = load i32, i32* @S_IXUSR
%24 = and i32 %22, %23
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %20
%27 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = load i32, i32* @S_IRUSR
%30 = and i32 %28, %29
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %26, %20, %14
%33 = load i8*, i8** %4
%34 = call i32 @svz_log(i32 1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %33)
store i32 -1, i32* %3
br label %41
35: ; preds = %26
%36 = load i8**, i8*** %5
%37 = icmp ne i8** %36, null
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = load i8**, i8*** %5
store i8* null, i8** %39
br label %40
40: ; preds = %38, %35
store i32 0, i32* %3
br label %41
41: ; preds = %40, %32, %10
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [23 x i8] c"passthrough: stat: %s\0A\00"
@errno = global i32 0
@S_IFREG = global i32 0
@S_IXUSR = global i32 0
@S_IRUSR = global i32 0
@.str.1 = [32 x i8] c"passthrough: no executable: %s\0A\00"
define i32 @svz_process_check_executable(i8* %0, i8** writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 @svz_log(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %30
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = load i32, i32* @S_IFREG
%15 = and i32 %14, %13
%16 = icmp eq i32 %15, 0
br i1 %16, label %25, label %17
17: ; preds = %11
%18 = load i32, i32* @S_IXUSR
%19 = and i32 %18, %13
%20 = icmp eq i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = load i32, i32* @S_IRUSR
%23 = and i32 %22, %13
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21, %17, %11
%26 = call i32 @svz_log(i32 1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %30
27: ; preds = %21
%28 = icmp eq i8** %1, null
br i1 %28, label %30, label %29
29: ; preds = %27
store i8* null, i8** %1
br label %30
30: ; preds = %27, %29, %25, %7
%31 = phi i32 [ -1, %7 ], [ -1, %25 ], [ 0, %29 ], [ 0, %27 ]
ret i32 %31
}