#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 ENOENT;
int errno;
int execve(char const *, char *const *, char *const *);
int /*<<< orphan*/ free(char *);
char *searchpath3(char const *, int /*<<< orphan*/ *, char *);
scalar_t__ strchr(char const *, char);
int execvpe(const char *file, char *const argv[], char *const envp[]) {
  char *pathname = NULL;
  int saved_errno;
  if (strchr(file, '/'))
    return execve(file, argv, envp);
  if (!*file)
    return errno = ENOENT, -1;
  pathname = searchpath3(file, NULL, ".");
  if (pathname == NULL)
    return -1;
  execve(pathname, argv, envp);
  saved_errno = errno;
  free(pathname);
  errno = saved_errno;
  return -1;
}


x86 assembly (clang, -O0):
<execvpe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     118d <execvpe+0x4d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <execve@plt>
mov    %eax,-0x4(%rbp)
jmp    1220 <execvpe+0xe0>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11b8 <execvpe+0x78>
mov    0x2e2f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e36(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <execvpe+0xe0>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    0xe39(%rip),%rdx
call   1060 <searchpath3@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e7 <execvpe+0xa7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <execvpe+0xe0>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <execve@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2dc9(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execvpe>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1177 <execvpe+0x37>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <execve@plt>
cmpb   $0x0,(%rbx)
je     11ba <execvpe+0x7a>
lea    0xe7d(%rip),%rdx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <searchpath3@plt>
test   %rax,%rax
je     11cc <execvpe+0x8c>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <execve@plt>
mov    0x2e36(%rip),%r14
mov    (%r14),%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,(%r14)
jmp    11cc <execvpe+0x8c>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    0x2e16(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@ENOENT = global i32 0
@errno = global i32 0
@.str = [2 x i8] c".\00"
define i32 @execvpe(i8* %0, i8** %1, i8** %2) {
%4 = tail call i64 @strchr(i8* %0, i8 signext 47)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @execve(i8* %0, i8** %1, i8** %2)
br label %20
8: ; preds = %3
%9 = load i8, i8* %0
%10 = icmp eq i8 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = load i32, i32* @ENOENT
store i32 %12, i32* @errno
br label %20
13: ; preds = %8
%14 = tail call i8* @searchpath3(i8* %0, i32* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = tail call i32 @execve(i8* %14, i8** %1, i8** %2)
%18 = load i32, i32* @errno
%19 = tail call i32 @free(i8* %14)
store i32 %18, i32* @errno
br label %20
20: ; preds = %13, %16, %11, %6
%21 = phi i32 [ %7, %6 ], [ -1, %16 ], [ -1, %11 ], [ -1, %13 ]
ret i32 %21
}