#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_IFLNK;
int S_IFREG;
int S_IXUSR;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ ft_print(char *, int, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
char *validate_file(char *path, int errors) {
  struct stat stat_info;
  if (stat(path, &stat_info) == 0) {
    if (!(stat_info.st_mode & S_IFREG || stat_info.st_mode & S_IFLNK)) {
      if (errors)
        ft_print("ft_minishell2 :\t"
                 "wrong filetype.\n",
                 2, 0);
      free(path);
      return (NULL);
    } else if (!(stat_info.st_mode & S_IXUSR)) {
      if (errors)
        ft_print("ft_minishell2 :\t"
                 "file is not executable.\n",
                 2, 0);
      free(path);
      return (NULL);
    } else
      return (path);
  } else if (errors)
    ft_print("ft_minishell2 :\t"
             "command not found.\n",
             2, 0);
  free(path);
  return (NULL);
}


x86 assembly (clang, -O0):
<validate_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jne    1208 <validate_file+0xd8>
mov    -0x18(%rbp),%eax
mov    0x2e80(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11b3 <validate_file+0x83>
mov    -0x18(%rbp),%eax
mov    0x2e63(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11b3 <validate_file+0x83>
cmpl   $0x0,-0x14(%rbp)
je     119d <validate_file+0x6d>
lea    0xe6f(%rip),%rdi
mov    $0x2,%esi
xor    %edx,%edx
call   1050 <ft_print@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    123b <validate_file+0x10b>
mov    -0x18(%rbp),%eax
mov    0x2e2b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11fb <validate_file+0xcb>
cmpl   $0x0,-0x14(%rbp)
je     11e5 <validate_file+0xb5>
lea    0xe48(%rip),%rdi
mov    $0x2,%esi
xor    %edx,%edx
call   1050 <ft_print@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    123b <validate_file+0x10b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123b <validate_file+0x10b>
cmpl   $0x0,-0x14(%rbp)
je     1225 <validate_file+0xf5>
lea    0xe31(%rip),%rdi
mov    $0x2,%esi
xor    %edx,%edx
call   1050 <ft_print@plt>
jmp    122a <validate_file+0xfa>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_file>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
je     1168 <validate_file+0x38>
test   %ebp,%ebp
je     115c <validate_file+0x2c>
lea    0xefa(%rip),%rdi
mov    $0x2,%esi
xor    %edx,%edx
call   1050 <ft_print@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    118c <validate_file+0x5c>
mov    (%rsp),%eax
mov    0x2e6e(%rip),%rcx
test   %eax,(%rcx)
jne    1181 <validate_file+0x51>
mov    0x2e5b(%rip),%rcx
test   %eax,(%rcx)
je     11a3 <validate_file+0x73>
mov    0x2e60(%rip),%rcx
test   %eax,(%rcx)
je     1196 <validate_file+0x66>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
test   %ebp,%ebp
je     115c <validate_file+0x2c>
lea    0xe80(%rip),%rdi
jmp    1150 <validate_file+0x20>
test   %ebp,%ebp
je     115c <validate_file+0x2c>
lea    0xe52(%rip),%rdi
jmp    1150 <validate_file+0x20>

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IFREG = global i32 0
@S_IFLNK = global i32 0
@.str = [33 x i8] c"ft_minishell2 :\09wrong filetype.\0A\00"
@S_IXUSR = global i32 0
@.str.1 = [41 x i8] c"ft_minishell2 :\09file is not executable.\0A\00"
@.str.2 = [36 x i8] c"ft_minishell2 :\09command not found.\0A\00"
define i8* @validate_file(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = call i64 @stat(i8* %7, %struct.stat* %6)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %46
10: ; preds = %2
%11 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%12 = load i32, i32* %11
%13 = load i32, i32* @S_IFREG
%14 = and i32 %12, %13
%15 = icmp ne i32 %14, 0
br i1 %15, label %30, label %16
16: ; preds = %10
%17 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* @S_IFLNK
%20 = and i32 %18, %19
%21 = icmp ne i32 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %16
%23 = load i32, i32* %5
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @ft_print(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 2, i32 0)
br label %27
27: ; preds = %25, %22
%28 = load i8*, i8** %4
%29 = call i32 @free(i8* %28)
store i8* null, i8** %3
br label %55
30: ; preds = %16, %10
%31 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i32, i32* @S_IXUSR
%34 = and i32 %32, %33
%35 = icmp ne i32 %34, 0
br i1 %35, label %44, label %36
36: ; preds = %30
%37 = load i32, i32* %5
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @ft_print(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 2, i32 0)
br label %41
41: ; preds = %39, %36
%42 = load i8*, i8** %4
%43 = call i32 @free(i8* %42)
store i8* null, i8** %3
br label %55
44: ; preds = %30
%45 = load i8*, i8** %4
store i8* %45, i8** %3
br label %55
46: ; preds = %2
%47 = load i32, i32* %5
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @ft_print(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 0)
br label %51
51: ; preds = %49, %46
br label %52
52: ; preds = %51
%53 = load i8*, i8** %4
%54 = call i32 @free(i8* %53)
store i8* null, i8** %3
br label %55
55: ; preds = %52, %44, %41, %27
%56 = load i8*, i8** %3
ret i8* %56
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IFREG = global i32 0
@S_IFLNK = global i32 0
@.str = [33 x i8] c"ft_minishell2 :\09wrong filetype.\0A\00"
@S_IXUSR = global i32 0
@.str.1 = [41 x i8] c"ft_minishell2 :\09file is not executable.\0A\00"
@.str.2 = [36 x i8] c"ft_minishell2 :\09command not found.\0A\00"
define i8* @validate_file(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %25
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @S_IFREG
%11 = and i32 %10, %9
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %7
%14 = load i32, i32* @S_IFLNK
%15 = and i32 %14, %9
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = icmp eq i32 %1, 0
br i1 %18, label %30, label %27
19: ; preds = %13, %7
%20 = load i32, i32* @S_IXUSR
%21 = and i32 %20, %9
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %19
%24 = icmp eq i32 %1, 0
br i1 %24, label %30, label %27
25: ; preds = %2
%26 = icmp eq i32 %1, 0
br i1 %26, label %30, label %27
27: ; preds = %25, %23, %17
%28 = phi i8* [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), %17 ], [ getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), %23 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), %25 ]
%29 = call i32 @ft_print(i8* %28, i32 2, i32 0)
br label %30
30: ; preds = %27, %25, %23, %17
%31 = call i32 @free(i8* %0)
br label %32
32: ; preds = %30, %19
%33 = phi i8* [ %0, %19 ], [ null, %30 ]
ret i8* %33
}