#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 */
typedef int /*<<< orphan*/ partial;

/* Variables and functions */
scalar_t__ ENAMETOOLONG;
scalar_t__ ENOENT;
scalar_t__ errno;
int execve(char const *, char *const *, char *const *);
char *getenv(char *);
int /*<<< orphan*/ strcat(char *, char const *);
char *strchr(char const *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char const *);
int execvep(const char *path, char *const argv[], char *const envp[]) {
  if (!strchr(path, '/')) {
    char partial[128];
    char *p = getenv("PATH");
    char *p2;
    int pathlen;
    pathlen = strlen(path);
    if (!p)
      p = "/bin:/usr/bin";
    for (; p && *p;) {
      strcpy(partial, p);
      p2 = strchr(partial, ':');
      if (p2)
        *p2 = '\0';
      if (strlen(partial))
        strcat(partial, "/");
      if ((strlen(partial) + pathlen) > sizeof(partial)) {
        errno = ENAMETOOLONG;
        return -1;
      }
      strcat(partial, path);
      execve(partial, argv, envp);
      if (errno != ENOENT)
        return -1;
      p2 = strchr(p, ':');
      if (p2)
        p = p2 + 1;
      else
        p = 0;
    }
    return -1;
  } else
    return execve(path, argv, envp);
}


x86 assembly (clang, -O0):
<execvep>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
jne    135f <execvep+0x1ff>
lea    0xe6a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0xa8(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0xb4(%rbp)
cmpq   $0x0,-0xa8(%rbp)
jne    11cd <execvep+0x6d>
lea    0xe3f(%rip),%rax
mov    %rax,-0xa8(%rbp)
jmp    11d2 <execvep+0x72>
xor    %eax,%eax
cmpq   $0x0,-0xa8(%rbp)
mov    %al,-0xb5(%rbp)
je     11fe <execvep+0x9e>
mov    -0xa8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0xb5(%rbp)
mov    -0xb5(%rbp),%al
test   $0x1,%al
jne    1211 <execvep+0xb1>
jmp    1353 <execvep+0x1f3>
lea    -0xa0(%rbp),%rdi
mov    -0xa8(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0xa0(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0xb0(%rbp)
je     1254 <execvep+0xf4>
mov    -0xb0(%rbp),%rax
movb   $0x0,(%rax)
lea    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%eax
je     127c <execvep+0x11c>
lea    -0xa0(%rbp),%rdi
lea    0xd9c(%rip),%rsi
call   1080 <strcat@plt>
lea    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
add    -0xb4(%rbp),%eax
cltq
cmp    $0x80,%rax
jbe    12bc <execvep+0x15c>
mov    0x2d35(%rip),%rax
mov    (%rax),%rcx
mov    0x2d33(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1373 <execvep+0x213>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <execve@plt>
mov    0x2cf9(%rip),%rax
mov    (%rax),%rax
mov    0x2cd7(%rip),%rcx
cmp    (%rcx),%rax
je     1306 <execvep+0x1a6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1373 <execvep+0x213>
mov    -0xa8(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0xb0(%rbp)
je     1343 <execvep+0x1e3>
mov    -0xb0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xa8(%rbp)
jmp    134e <execvep+0x1ee>
movq   $0x0,-0xa8(%rbp)
jmp    11d2 <execvep+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    1373 <execvep+0x213>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <execve@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execvep>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11a8 <execvep+0x48>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <execve@plt>
lea    0xe51(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbp
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
test   %rbp,%rbp
lea    0xe39(%rip),%rbx
cmovne %rbp,%rbx
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpb   $0x0,(%rbx)
je     1295 <execvep+0x135>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbp,%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1209 <execvep+0xa9>
movb   $0x0,(%rax)
mov    %rbp,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     1224 <execvep+0xc4>
mov    %rbp,%rdi
lea    0xdf4(%rip),%rsi
call   1080 <strcat@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
add    %r12d,%eax
cmp    $0x81,%eax
jae    1281 <execvep+0x121>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1080 <strcat@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <execve@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%rax
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%rax
jne    1295 <execvep+0x135>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
jne    11e0 <execvep+0x80>
jmp    1295 <execvep+0x135>
mov    0x2d50(%rip),%rax
mov    (%rax),%rax
mov    0x2d4e(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [14 x i8] c"/bin:/usr/bin\00"
@.str.2 = [2 x i8] c"/\00"
@ENAMETOOLONG = global i64 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @execvep(i8* %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca [128 x i8]
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%12 = load i8*, i8** %5
%13 = call i8* @strchr(i8* %12, i8 signext 47)
%14 = icmp ne i8* %13, null
br i1 %14, label %82, label %15
15: ; preds = %3
%16 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %9
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %11
%19 = load i8*, i8** %9
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %15
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8** %9
br label %22
22: ; preds = %21, %15
br label %23
23: ; preds = %80, %22
%24 = load i8*, i8** %9
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i8*, i8** %9
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 0
br label %31
31: ; preds = %26, %23
%32 = phi i1 [ false, %23 ], [ %30, %26 ]
br i1 %32, label %33, label %81
33: ; preds = %31
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%35 = load i8*, i8** %9
%36 = call i32 @strcpy(i8* %34, i8* %35)
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%38 = call i8* @strchr(i8* %37, i8 signext 58)
store i8* %38, i8** %10
%39 = load i8*, i8** %10
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %43
41: ; preds = %33
%42 = load i8*, i8** %10
store i8 0, i8* %42
br label %43
43: ; preds = %41, %33
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%45 = call i32 @strlen(i8* %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%49 = call i32 @strcat(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %47, %43
%51 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%52 = call i32 @strlen(i8* %51)
%53 = load i32, i32* %11
%54 = add nsw i32 %52, %53
%55 = sext i32 %54 to i64
%56 = icmp ugt i64 %55, 128
br i1 %56, label %57, label %59
57: ; preds = %50
%58 = load i64, i64* @ENAMETOOLONG
store i64 %58, i64* @errno
store i32 -1, i32* %4
br label %87
59: ; preds = %50
%60 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%61 = load i8*, i8** %5
%62 = call i32 @strcat(i8* %60, i8* %61)
%63 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%64 = load i8**, i8*** %6
%65 = load i8**, i8*** %7
%66 = call i32 @execve(i8* %63, i8** %64, i8** %65)
%67 = load i64, i64* @errno
%68 = load i64, i64* @ENOENT
%69 = icmp ne i64 %67, %68
br i1 %69, label %70, label %71
70: ; preds = %59
store i32 -1, i32* %4
br label %87
71: ; preds = %59
%72 = load i8*, i8** %9
%73 = call i8* @strchr(i8* %72, i8 signext 58)
store i8* %73, i8** %10
%74 = load i8*, i8** %10
%75 = icmp ne i8* %74, null
br i1 %75, label %76, label %79
76: ; preds = %71
%77 = load i8*, i8** %10
%78 = getelementptr inbounds i8, i8* %77, i64 1
store i8* %78, i8** %9
br label %80
79: ; preds = %71
store i8* null, i8** %9
br label %80
80: ; preds = %79, %76
br label %23
81: ; preds = %31
store i32 -1, i32* %4
br label %87
82: ; preds = %3
%83 = load i8*, i8** %5
%84 = load i8**, i8*** %6
%85 = load i8**, i8*** %7
%86 = call i32 @execve(i8* %83, i8** %84, i8** %85)
store i32 %86, i32* %4
br label %87
87: ; preds = %82, %81, %70, %57
%88 = load i32, i32* %4
ret i32 %88
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [14 x i8] c"/bin:/usr/bin\00"
@.str.2 = [2 x i8] c"/\00"
@ENAMETOOLONG = global i64 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @execvep(i8* %0, i8** %1, i8** %2) {
%4 = alloca [128 x i8]
%5 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 47)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %44
7: ; preds = %3
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @strlen(i8* %0)
%11 = icmp eq i8* %9, null
%12 = select i1 %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %9
br label %13
13: ; preds = %7, %39
%14 = phi i8* [ %12, %7 ], [ %42, %39 ]
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %43, label %17
17: ; preds = %13
%18 = call i32 @strcpy(i8* %8, i8* %14)
%19 = call i8* @strchr(i8* %8, i8 signext 58)
%20 = icmp eq i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %17
store i8 0, i8* %19
br label %22
22: ; preds = %21, %17
%23 = call i32 @strlen(i8* %8)
%24 = icmp eq i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 @strcat(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = call i32 @strlen(i8* %8)
%29 = add nsw i32 %28, %10
%30 = icmp ugt i32 %29, 128
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i64, i64* @ENAMETOOLONG
store i64 %32, i64* @errno
br label %43
33: ; preds = %27
%34 = call i32 @strcat(i8* %8, i8* %0)
%35 = call i32 @execve(i8* %8, i8** %1, i8** %2)
%36 = load i64, i64* @errno
%37 = load i64, i64* @ENOENT
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %43
39: ; preds = %33
%40 = call i8* @strchr(i8* %14, i8 signext 58)
%41 = icmp eq i8* %40, null
%42 = getelementptr inbounds i8, i8* %40, i64 1
br i1 %41, label %43, label %13
43: ; preds = %13, %39, %33, %31
br label %46
44: ; preds = %3
%45 = tail call i32 @execve(i8* %0, i8** %1, i8** %2)
br label %46
46: ; preds = %44, %43
%47 = phi i32 [ %45, %44 ], [ -1, %43 ]
ret i32 %47
}