#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 */

/* Variables and functions */
int EACCES;
int ENOENT;
int errno;
int /*<<< orphan*/ execve(char *, char *const *, char *const *);
int execveat(int, char const *, char *const *, char *const *, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *getenv(char *);
char *searchpath3(char const *, int /*<<< orphan*/ *, char *);
scalar_t__ strchr(char const *, char);
int execvpeat(int dirfd, const char *file, char *const argv[],
              char *const envp[], int flags) {
  char *pathname = NULL;
  int eacces = 0;
  int saved_errno;
  if (strchr(file, '/') || !*file)
    return execveat(dirfd, file, argv, envp, flags);
  if (getenv("PATH") == NULL) {
    execveat(dirfd, file, argv, envp, flags);
    if (errno == EACCES)
      eacces = 1;
    else if (errno != ENOENT)
      return -1;
  }
  pathname = searchpath3(file, NULL, "");
  if (pathname == NULL)
    return -1;
  execve(pathname, argv, envp);
  saved_errno = errno;
  free(pathname);
  if (eacces && (saved_errno == ENOENT))
    saved_errno = EACCES;
  errno = saved_errno;
  return -1;
}


x86 assembly (clang, -O0):
<execvpeat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
jne    11af <execvpeat+0x4f>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11cf <execvpeat+0x6f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
call   1060 <execveat@plt>
mov    %eax,-0x4(%rbp)
jmp    12df <execvpeat+0x17f>
lea    0xe2a(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
jne    124f <execvpeat+0xef>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
call   1060 <execveat@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%eax
jne    1221 <execvpeat+0xc1>
movl   $0x1,-0x34(%rbp)
jmp    124a <execvpeat+0xea>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%eax
je     1245 <execvpeat+0xe5>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <execvpeat+0x17f>
jmp    124a <execvpeat+0xea>
jmp    124f <execvpeat+0xef>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    0xda6(%rip),%rdx
call   1080 <searchpath3@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    127e <execvpeat+0x11e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <execvpeat+0x17f>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <execve@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
cmpl   $0x0,-0x34(%rbp)
je     12cc <execvpeat+0x16c>
mov    -0x38(%rbp),%eax
mov    0x2d10(%rip),%rcx
cmp    (%rcx),%eax
jne    12cc <execvpeat+0x16c>
mov    0x2d21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%ecx
mov    0x2d02(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execvpeat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r12d
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsi,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11a3 <execvpeat+0x43>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r12d,%r8d
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <execveat@plt>
cmpb   $0x0,(%rbx)
je     1188 <execvpeat+0x28>
lea    0xe51(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1222 <execvpeat+0xc2>
xor    %r12d,%r12d
lea    0xe41(%rip),%rdx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <searchpath3@plt>
test   %rax,%rax
je     1214 <execvpeat+0xb4>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <execve@plt>
mov    0x2dee(%rip),%r14
mov    (%r14),%ebp
mov    %rbx,%rdi
call   1040 <free@plt>
mov    0x2dcc(%rip),%rcx
mov    %ebp,%eax
cmp    (%rcx),%ebp
jne    120b <execvpeat+0xab>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
test   %r12b,%r12b
cmovne %eax,%ebp
mov    %ebp,(%r14)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r12d,%r8d
call   1060 <execveat@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    0x2da3(%rip),%rcx
mov    $0x1,%r12b
cmp    (%rcx),%eax
je     11bc <execvpeat+0x5c>
mov    0x2d71(%rip),%rcx
cmp    (%rcx),%eax
je     11b9 <execvpeat+0x59>
jmp    1214 <execvpeat+0xb4>

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@errno = global i32 0
@EACCES = global i32 0
@ENOENT = global i32 0
@.str.1 = [1 x i8] zeroinitializer
define i32 @execvpeat(i32 %0, i8* %1, i8** %2, i8** %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8**
%10 = alloca i8**
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8** %2, i8*** %9
store i8** %3, i8*** %10
store i32 %4, i32* %11
store i8* null, i8** %12
store i32 0, i32* %13
%15 = load i8*, i8** %8
%16 = call i64 @strchr(i8* %15, i8 signext 47)
%17 = icmp ne i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %5
%19 = load i8*, i8** %8
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %29, label %22
22: ; preds = %18, %5
%23 = load i32, i32* %7
%24 = load i8*, i8** %8
%25 = load i8**, i8*** %9
%26 = load i8**, i8*** %10
%27 = load i32, i32* %11
%28 = call i32 @execveat(i32 %23, i8* %24, i8** %25, i8** %26, i32 %27)
store i32 %28, i32* %6
br label %74
29: ; preds = %18
%30 = call i32* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %50
32: ; preds = %29
%33 = load i32, i32* %7
%34 = load i8*, i8** %8
%35 = load i8**, i8*** %9
%36 = load i8**, i8*** %10
%37 = load i32, i32* %11
%38 = call i32 @execveat(i32 %33, i8* %34, i8** %35, i8** %36, i32 %37)
%39 = load i32, i32* @errno
%40 = load i32, i32* @EACCES
%41 = icmp eq i32 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %32
store i32 1, i32* %13
br label %49
43: ; preds = %32
%44 = load i32, i32* @errno
%45 = load i32, i32* @ENOENT
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %43
store i32 -1, i32* %6
br label %74
48: ; preds = %43
br label %49
49: ; preds = %48, %42
br label %50
50: ; preds = %49, %29
%51 = load i8*, i8** %8
%52 = call i8* @searchpath3(i8* %51, i32* null, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i8* %52, i8** %12
%53 = load i8*, i8** %12
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %56
55: ; preds = %50
store i32 -1, i32* %6
br label %74
56: ; preds = %50
%57 = load i8*, i8** %12
%58 = load i8**, i8*** %9
%59 = load i8**, i8*** %10
%60 = call i32 @execve(i8* %57, i8** %58, i8** %59)
%61 = load i32, i32* @errno
store i32 %61, i32* %14
%62 = load i8*, i8** %12
%63 = call i32 @free(i8* %62)
%64 = load i32, i32* %13
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %56
%67 = load i32, i32* %14
%68 = load i32, i32* @ENOENT
%69 = icmp eq i32 %67, %68
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = load i32, i32* @EACCES
store i32 %71, i32* %14
br label %72
72: ; preds = %70, %66, %56
%73 = load i32, i32* %14
store i32 %73, i32* @errno
store i32 -1, i32* %6
br label %74
74: ; preds = %72, %55, %47, %22
%75 = load i32, i32* %6
ret i32 %75
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@errno = global i32 0
@EACCES = global i32 0
@ENOENT = global i32 0
@.str.1 = [1 x i8] zeroinitializer
define i32 @execvpeat(i32 %0, i8* %1, i8** %2, i8** %3, i32 %4) {
%6 = tail call i64 @strchr(i8* %1, i8 signext 47)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = load i8, i8* %1
%10 = icmp eq i8 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8, %5
%12 = tail call i32 @execveat(i32 %0, i8* %1, i8** %2, i8** %3, i32 %4)
br label %37
13: ; preds = %8
%14 = tail call i32* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %24
16: ; preds = %13
%17 = tail call i32 @execveat(i32 %0, i8* %1, i8** %2, i8** %3, i32 %4)
%18 = load i32, i32* @errno
%19 = load i32, i32* @EACCES
%20 = icmp eq i32 %18, %19
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* @ENOENT
%23 = icmp eq i32 %18, %22
br i1 %23, label %24, label %37
24: ; preds = %16, %21, %13
%25 = phi i1 [ false, %21 ], [ false, %13 ], [ true, %16 ]
%26 = tail call i8* @searchpath3(i8* %1, i32* null, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp eq i8* %26, null
br i1 %27, label %37, label %28
28: ; preds = %24
%29 = tail call i32 @execve(i8* %26, i8** %2, i8** %3)
%30 = load i32, i32* @errno
%31 = tail call i32 @free(i8* %26)
%32 = load i32, i32* @ENOENT
%33 = icmp eq i32 %30, %32
%34 = select i1 %25, i1 %33, i1 false
%35 = load i32, i32* @EACCES
%36 = select i1 %34, i32 %35, i32 %30
store i32 %36, i32* @errno
br label %37
37: ; preds = %24, %21, %28, %11
%38 = phi i32 [ %12, %11 ], [ -1, %28 ], [ -1, %21 ], [ -1, %24 ]
ret i32 %38
}