#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 /*<<< orphan*/ ENOENT;
int /*<<< orphan*/ X_OK;
int /*<<< orphan*/ __environ;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int execve(char const *, char *const *, int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ strcat(char *, char const *);
char *strchr(char const *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int execvp(const char *file, char *const argv[]) {
  const char *p, *z, *path = getenv("PATH");
  int l;
  if (strchr(file, '/'))
    return execve(file, argv, __environ);
  if (!path)
    path = "/usr/local/bin:/bin:/usr/bin";
  l = strlen(file) + strlen(path) + 2;
  for (p = path; p && *p; p = z) {
    char b[l];
    z = strchr(p, ':');
    if (z) {
      memcpy(b, p, z - p);
      b[z++ - p] = 0;
    } else
      strcpy(b, p);
    strcat(b, "/");
    strcat(b, file);
    if (!access(b, X_OK))
      return execve(b, argv, __environ);
  }
  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)
lea    0xe69(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
je     11d6 <execvp+0x56>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
call   1070 <execve@plt>
mov    %eax,-0x4(%rbp)
jmp    137e <execvp+0x1fe>
cmpq   $0x0,-0x30(%rbp)
jne    11ec <execvp+0x6c>
lea    0xe1d(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x51(%rbp)
je     1236 <execvp+0xb6>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    1246 <execvp+0xc6>
jmp    1365 <execvp+0x1e5>
mov    -0x34(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12c6 <execvp+0x146>
mov    -0x60(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    -0x60(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    -0x20(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12d3 <execvp+0x153>
mov    -0x60(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x60(%rbp),%rdi
lea    0xd44(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strcat@plt>
mov    -0x60(%rbp),%rdi
mov    0x2ce5(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%eax
jne    1330 <execvp+0x1b0>
mov    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2cde(%rip),%rax
mov    (%rax),%edx
call   1070 <execve@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1337 <execvp+0x1b7>
movl   $0x0,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4c(%rbp),%eax
test   %eax,%eax
je     1353 <execvp+0x1d3>
jmp    134e <execvp+0x1ce>
jmp    137e <execvp+0x1fe>
jmp    1358 <execvp+0x1d8>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1216 <execvp+0x96>
mov    0x2c54(%rip),%rax
mov    (%rax),%ecx
mov    0x2c5b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
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,%r12
lea    0xe62(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11da <execvp+0x5a>
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r14,%rsi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <execve@plt>
mov    %r14,-0x30(%rbp)
test   %rbx,%rbx
lea    0xe1d(%rip),%r14
cmovne %rbx,%r14
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rax,-0x38(%rbp)
nop
cmpb   $0x0,(%r14)
je     12b9 <execvp+0x139>
mov    %rsp,%r15
mov    %rsp,%rbx
sub    -0x38(%rbp),%rbx
mov    %rbx,%rsp
mov    %r14,%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1270 <execvp+0xf0>
mov    %rax,%r13
mov    %r15,-0x40(%rbp)
mov    %r12,%r15
mov    %rax,%r12
sub    %r14,%r12
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1080 <memcpy@plt>
add    $0x1,%r13
movb   $0x0,(%rbx,%r12,1)
mov    %r15,%r12
mov    -0x40(%rbp),%r15
mov    %r13,%r14
jmp    127e <execvp+0xfe>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
xor    %r14d,%r14d
mov    %rbx,%rdi
lea    0xd9a(%rip),%rsi
call   10a0 <strcat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   10a0 <strcat@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <access@plt>
test   %eax,%eax
je     12d2 <execvp+0x152>
mov    %r15,%rsp
test   %r14,%r14
jne    1210 <execvp+0x90>
mov    0x2d00(%rip),%rax
mov    (%rax),%eax
mov    0x2d07(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    12ea <execvp+0x16a>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    -0x30(%rbp),%rsi
call   1070 <execve@plt>
mov    %r15,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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