#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 /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ X_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
int is_valid_test(const char *path) {
  struct stat st;
  if (access(path, X_OK) < 0)
    return 0;
  if (stat(path, &st) < 0)
    return 0;
  if (!S_ISREG(st.st_mode))
    return 0;
  return 1;
}


x86 assembly (clang, -O0):
<is_valid_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0x0,%rax
jge    1164 <is_valid_test+0x34>
movl   $0x0,-0x4(%rbp)
jmp    11ab <is_valid_test+0x7b>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0x0,%rax
jge    1187 <is_valid_test+0x57>
movl   $0x0,-0x4(%rbp)
jmp    11ab <is_valid_test+0x7b>
mov    -0x18(%rbp),%edi
call   1040 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11a4 <is_valid_test+0x74>
movl   $0x0,-0x4(%rbp)
jmp    11ab <is_valid_test+0x7b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_valid_test>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
xor    %ebp,%ebp
test   %rax,%rax
js     116b <is_valid_test+0x3b>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <stat@plt>
test   %rax,%rax
js     116b <is_valid_test+0x3b>
mov    (%rsp),%edi
call   1040 <S_ISREG@plt>
xor    %ebp,%ebp
test   %eax,%eax
setne  %bpl
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@X_OK = global i32 0
define i32 @is_valid_test(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @X_OK
%7 = call i64 @access(i8* %5, i32 %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 0, i32* %2
br label %22
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i64 @stat(i8* %11, %struct.stat* %4)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
store i32 0, i32* %2
br label %22
15: ; preds = %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @S_ISREG(i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %15
store i32 0, i32* %2
br label %22
21: ; preds = %15
store i32 1, i32* %2
br label %22
22: ; preds = %21, %20, %14, %9
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@X_OK = global i32 0
define i32 @is_valid_test(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @X_OK
%5 = tail call i64 @access(i8* %0, i32 %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %1
%8 = call i64 @stat(i8* %0, %struct.stat* %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @S_ISREG(i32 %12)
%14 = icmp ne i32 %13, 0
%15 = zext i1 %14 to i32
br label %16
16: ; preds = %10, %7, %1
%17 = phi i32 [ 0, %1 ], [ 0, %7 ], [ %15, %10 ]
ret i32 %17
}