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

/* Variables and functions */
#define ENOENT 130
#define ENOEXEC 129
#define ENOTDIR 128
int PATH_MAX;
int errno;
int /*<<< orphan*/ execv(char const *, char *const *);
char *getenv(char *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
char *strchr(char const *, char);
size_t strlen(char const *);
int execvp(const char *prog, char *const *args) {
  const char *searchpath, *s, *t;
  char progpath[PATH_MAX];
  size_t len;
  if (strchr(prog, '/') != NULL) {
    execv(prog, args);
    return -1;
  }
  searchpath = getenv("PATH");
  if (searchpath == NULL) {
    errno = ENOENT;
    return -1;
  }
  for (s = searchpath; s != NULL; s = t) {
    t = strchr(s, ':');
    if (t != NULL) {
      len = t - s;
      t++;
    } else {
      len = strlen(s);
    }
    if (len == 0) {
      continue;
    }
    if (len >= sizeof(progpath)) {
      continue;
    }
    memcpy(progpath, s, len);
    snprintf(progpath + len, sizeof(progpath) - len, "/%s", prog);
    execv(progpath, args);
    switch (errno) {
    case ENOENT:
    case ENOTDIR:
    case ENOEXEC:
      break;
    default:
      return -1;
    }
  }
  errno = ENOENT;
  return -1;
}


x86 assembly (clang, -O0):
<execvp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     11d8 <execvp+0x78>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <execv@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1333 <execvp+0x1d3>
lea    0xe21(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1213 <execvp+0xb3>
mov    0x2dde(%rip),%rax
movl   $0x82,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1333 <execvp+0x1d3>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1318 <execvp+0x1b8>
mov    -0x28(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1263 <execvp+0x103>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1270 <execvp+0x110>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1280 <execvp+0x120>
jmp    130b <execvp+0x1ab>
mov    -0x60(%rbp),%rax
cmp    %rax,-0x48(%rbp)
jb     1293 <execvp+0x133>
jmp    130b <execvp+0x1ab>
mov    -0x58(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x60(%rbp),%rcx
mov    -0x58(%rbp),%rdi
mov    -0x48(%rbp),%rax
add    %rax,%rdi
mov    %ecx,%esi
sub    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xd43(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x58(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <execv@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x2,%eax
ja     12f3 <execvp+0x193>
jmp    12ee <execvp+0x18e>
jmp    1306 <execvp+0x1a6>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1333 <execvp+0x1d3>
jmp    130b <execvp+0x1ab>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    121b <execvp+0xbb>
mov    0x2cb9(%rip),%rax
movl   $0x82,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execvp>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e72(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r13
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11b1 <execvp+0x51>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <execv@plt>
jmp    128f <execvp+0x12f>
lea    0xe48(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1282 <execvp+0x122>
mov    %rax,%r12
mov    %rbx,-0x30(%rbp)
mov    %r14,-0x38(%rbp)
jmp    11e9 <execvp+0x89>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %r12,%r12
je     1282 <execvp+0x122>
mov    %r12,%r14
mov    %r12,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1220 <execvp+0xc0>
mov    %rax,%r12
mov    %rax,%rbx
sub    %r14,%rbx
add    $0x1,%r12
test   %rbx,%rbx
jne    1233 <execvp+0xd3>
jmp    11e0 <execvp+0x80>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
xor    %r12d,%r12d
test   %rbx,%rbx
je     11e0 <execvp+0x80>
cmp    %r15,%rbx
jae    11e0 <execvp+0x80>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <memcpy@plt>
lea    (%rbx,%r13,1),%rdi
mov    %r15d,%esi
sub    %ebx,%esi
lea    0xdaf(%rip),%rdx
mov    -0x30(%rbp),%rcx
call   1060 <snprintf@plt>
mov    %r13,%rdi
mov    -0x38(%rbp),%rsi
call   1080 <execv@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
cmp    $0x2,%eax
jbe    11e0 <execvp+0x80>
jmp    128f <execvp+0x12f>
mov    0x2d4f(%rip),%rax
movl   $0x82,(%rax)
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [5 x i8] c"PATH\00"
@errno = global i32 0
@.str.1 = [4 x i8] c"/%s\00"
define i32 @execvp(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca i8, i64 %14
store i64 %14, i64* %10
%17 = load i8*, i8** %4
%18 = call i8* @strchr(i8* %17, i8 signext 47)
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = load i8*, i8** %4
%22 = load i8**, i8*** %5
%23 = call i32 @execv(i8* %21, i8** %22)
store i32 -1, i32* %3
store i32 1, i32* %12
br label %78
24: ; preds = %2
%25 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 130, i32* @errno
store i32 -1, i32* %3
store i32 1, i32* %12
br label %78
29: ; preds = %24
%30 = load i8*, i8** %6
store i8* %30, i8** %7
br label %31
31: ; preds = %75, %29
%32 = load i8*, i8** %7
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %77
34: ; preds = %31
%35 = load i8*, i8** %7
%36 = call i8* @strchr(i8* %35, i8 signext 58)
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %47
39: ; preds = %34
%40 = load i8*, i8** %8
%41 = load i8*, i8** %7
%42 = ptrtoint i8* %40 to i64
%43 = ptrtoint i8* %41 to i64
%44 = sub i64 %42, %43
store i64 %44, i64* %11
%45 = load i8*, i8** %8
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %8
br label %50
47: ; preds = %34
%48 = load i8*, i8** %7
%49 = call i64 @strlen(i8* %48)
store i64 %49, i64* %11
br label %50
50: ; preds = %47, %39
%51 = load i64, i64* %11
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %50
br label %75
54: ; preds = %50
%55 = load i64, i64* %11
%56 = icmp uge i64 %55, %14
br i1 %56, label %57, label %58
57: ; preds = %54
br label %75
58: ; preds = %54
%59 = load i8*, i8** %7
%60 = load i64, i64* %11
%61 = call i32 @memcpy(i8* %16, i8* %59, i64 %60)
%62 = load i64, i64* %11
%63 = getelementptr inbounds i8, i8* %16, i64 %62
%64 = load i64, i64* %11
%65 = sub i64 %14, %64
%66 = trunc i64 %65 to i32
%67 = load i8*, i8** %4
%68 = call i32 @snprintf(i8* %63, i32 %66, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %67)
%69 = load i8**, i8*** %5
%70 = call i32 @execv(i8* %16, i8** %69)
%71 = load i32, i32* @errno
switch i32 %71, label %73 [
i32 130, label %72
i32 128, label %72
i32 129, label %72
]
72: ; preds = %58, %58, %58
br label %74
73: ; preds = %58
store i32 -1, i32* %3
store i32 1, i32* %12
br label %78
74: ; preds = %72
br label %75
75: ; preds = %74, %57, %53
%76 = load i8*, i8** %8
store i8* %76, i8** %7
br label %31
77: ; preds = %31
store i32 130, i32* @errno
store i32 -1, i32* %3
store i32 1, i32* %12
br label %78
78: ; preds = %77, %73, %28, %20
%79 = load i8*, i8** %9
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [5 x i8] c"PATH\00"
@errno = global i32 0
@.str.1 = [4 x i8] c"/%s\00"
define i32 @execvp(i8* %0, i8** %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 47)
%7 = icmp eq i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @execv(i8* %0, i8** %1)
br label %44
10: ; preds = %2
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %10
store i32 130, i32* @errno
br label %44
14: ; preds = %10, %41
%15 = phi i8* [ %26, %41 ], [ %11, %10 ]
%16 = call i8* @strchr(i8* %15, i8 signext 58)
%17 = icmp eq i8* %16, null
br i1 %17, label %23, label %18
18: ; preds = %14
%19 = ptrtoint i8* %16 to i64
%20 = ptrtoint i8* %15 to i64
%21 = sub i64 %19, %20
%22 = getelementptr inbounds i8, i8* %16, i64 1
br label %25
23: ; preds = %14
%24 = call i64 @strlen(i8* %15)
br label %25
25: ; preds = %23, %18
%26 = phi i8* [ %22, %18 ], [ null, %23 ]
%27 = phi i64 [ %21, %18 ], [ %24, %23 ]
%28 = icmp eq i64 %27, 0
%29 = icmp uge i64 %27, %4
%30 = select i1 %28, i1 true, i1 %29
br i1 %30, label %41, label %31
31: ; preds = %25
%32 = call i32 @memcpy(i8* %5, i8* %15, i64 %27)
%33 = getelementptr inbounds i8, i8* %5, i64 %27
%34 = trunc i64 %27 to i32
%35 = sub i32 %3, %34
%36 = call i32 @snprintf(i8* %33, i32 %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%37 = call i32 @execv(i8* %5, i8** %1)
%38 = load i32, i32* @errno
%39 = add i32 %38, -128
%40 = icmp ult i32 %39, 3
br i1 %40, label %41, label %44
41: ; preds = %31, %25
%42 = icmp eq i8* %26, null
br i1 %42, label %43, label %14
43: ; preds = %41
store i32 130, i32* @errno
br label %44
44: ; preds = %31, %43, %13, %8
ret i32 -1
}