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

/* Variables and functions */
int PATH_MAX;
int execv(char const *, char *const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *const, char const *);
int my_execvp(const char *file, char *const argv[]) {
  char *const paths[] = {"/sbin",           "/bin",     "/usr/sbin", "/usr/bin",
                         "/usr/local/sbin", "/usr/bin", NULL};
  int i, ret;
  if (file[0] == '/')
    return execv(file, argv);
  for (i = 0; paths[i] != NULL; i++) {
    char cmd[PATH_MAX];
    snprintf(cmd, sizeof(cmd), "%s/%s", paths[i], file);
    ret = execv(cmd, argv);
  }
  return ret;
}


x86 assembly (clang, -O0):
<my_execvp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1040 <memset@plt>
lea    0xec6(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xeac(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe92(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xe90(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe7c(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11b7 <my_execvp+0x87>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <execv@plt>
mov    %eax,-0x4(%rbp)
jmp    1240 <my_execvp+0x110>
movl   $0x0,-0x54(%rbp)
movslq -0x54(%rbp),%rax
cmpq   $0x0,-0x50(%rbp,%rax,8)
je     123a <my_execvp+0x10a>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x70(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x68(%rbp)
mov    %eax,%esi
movslq -0x54(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rcx
mov    -0x10(%rbp),%r8
lea    0xe13(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <execv@plt>
mov    %eax,-0x58(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,%rsp
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11be <my_execvp+0x8e>
mov    -0x58(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_execvp>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
movq   $0x0,-0x30(%rbp)
lea    0xed7(%rip),%rcx
mov    %rcx,-0x60(%rbp)
lea    0xebd(%rip),%rax
mov    %rax,-0x58(%rbp)
lea    0xea4(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xea3(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xea1(%rip),%rdx
mov    %rdx,-0x40(%rbp)
mov    %rax,-0x38(%rbp)
cmpb   $0x2f,(%rdi)
jne    1198 <my_execvp+0x78>
mov    %r12,%rdi
mov    %r14,%rsi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <execv@plt>
lea    -0x58(%rbp),%r13
nopl   0x0(%rax)
mov    %rsp,%r15
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
lea    0xe59(%rip),%rdx
mov    %r12,%r8
call   1030 <snprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <execv@plt>
mov    %r15,%rsp
mov    0x0(%r13),%rcx
add    $0x8,%r13
test   %rcx,%rcx
jne    11a0 <my_execvp+0x80>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"/sbin\00"
@.str.1 = [5 x i8] c"/bin\00"
@.str.2 = [10 x i8] c"/usr/sbin\00"
@.str.3 = [9 x i8] c"/usr/bin\00"
@.str.4 = [16 x i8] c"/usr/local/sbin\00"
@PATH_MAX = global i32 0
@.str.5 = [6 x i8] c"%s/%s\00"
define i32 @my_execvp(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca [7 x i8*]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
%11 = bitcast [7 x i8*]* %6 to i8*
%12 = bitcast i8* %11 to [7 x i8*]*
%13 = getelementptr inbounds [7 x i8*], [7 x i8*]* %12, i32 0, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i8** %13
%14 = getelementptr inbounds [7 x i8*], [7 x i8*]* %12, i32 0, i32 1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i32 0, i32 0), i8** %14
%15 = getelementptr inbounds [7 x i8*], [7 x i8*]* %12, i32 0, i32 2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i32 0, i32 0), i8** %15
%16 = getelementptr inbounds [7 x i8*], [7 x i8*]* %12, i32 0, i32 3
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i32 0, i32 0), i8** %16
%17 = getelementptr inbounds [7 x i8*], [7 x i8*]* %12, i32 0, i32 4
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i32 0, i32 0), i8** %17
%18 = getelementptr inbounds [7 x i8*], [7 x i8*]* %12, i32 0, i32 5
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i32 0, i32 0), i8** %18
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 47
br i1 %23, label %24, label %28
24: ; preds = %2
%25 = load i8*, i8** %4
%26 = load i8**, i8*** %5
%27 = call i32 @execv(i8* %25, i8** %26)
store i32 %27, i32* %3
br label %55
28: ; preds = %2
store i32 0, i32* %7
br label %29
29: ; preds = %50, %28
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [7 x i8*], [7 x i8*]* %6, i64 0, i64 %31
%33 = load i8*, i8** %32
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %53
35: ; preds = %29
%36 = load i32, i32* @PATH_MAX
%37 = zext i32 %36 to i64
%38 = call i8* @llvm.stacksave()
store i8* %38, i8** %9
%39 = alloca i8, i64 %37
store i64 %37, i64* %10
%40 = trunc i64 %37 to i32
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [7 x i8*], [7 x i8*]* %6, i64 0, i64 %42
%44 = load i8*, i8** %43
%45 = load i8*, i8** %4
%46 = call i32 @snprintf(i8* %39, i32 %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %44, i8* %45)
%47 = load i8**, i8*** %5
%48 = call i32 @execv(i8* %39, i8** %47)
store i32 %48, i32* %8
%49 = load i8*, i8** %9
br label %50
50: ; preds = %35
%51 = load i32, i32* %7
%52 = add nsw i32 %51, 1
store i32 %52, i32* %7
br label %29
53: ; preds = %29
%54 = load i32, i32* %8
store i32 %54, i32* %3
br label %55
55: ; preds = %53, %24
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@.str = [6 x i8] c"/sbin\00"
@.str.1 = [5 x i8] c"/bin\00"
@.str.2 = [10 x i8] c"/usr/sbin\00"
@.str.3 = [9 x i8] c"/usr/bin\00"
@.str.4 = [16 x i8] c"/usr/local/sbin\00"
@PATH_MAX = global i32 0
@.str.5 = [6 x i8] c"%s/%s\00"
define i32 @my_execvp(i8* %0, i8** %1) {
%3 = alloca [7 x i8*]
%4 = bitcast [7 x i8*]* %3 to i8*
%5 = getelementptr inbounds [7 x i8*], [7 x i8*]* %3, i64 0, i64 6
%6 = bitcast i8** %5 to i64*
store i64 0, i64* %6
%7 = getelementptr inbounds [7 x i8*], [7 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [7 x i8*], [7 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [7 x i8*], [7 x i8*]* %3, i64 0, i64 2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [7 x i8*], [7 x i8*]* %3, i64 0, i64 3
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [7 x i8*], [7 x i8*]* %3, i64 0, i64 4
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds [7 x i8*], [7 x i8*]* %3, i64 0, i64 5
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8** %12
%13 = load i8, i8* %0
%14 = icmp eq i8 %13, 47
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 @execv(i8* %0, i8** %1)
br label %30
17: ; preds = %2, %17
%18 = phi i64 [ %26, %17 ], [ 0, %2 ]
%19 = phi i8* [ %28, %17 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %2 ]
%20 = load i32, i32* @PATH_MAX
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
%23 = alloca i8, i64 %21
%24 = call i32 @snprintf(i8* %23, i32 %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %19, i8* %0)
%25 = call i32 @execv(i8* %23, i8** %1)
%26 = add nuw i64 %18, 1
%27 = getelementptr inbounds [7 x i8*], [7 x i8*]* %3, i64 0, i64 %26
%28 = load i8*, i8** %27
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %17
30: ; preds = %17, %15
%31 = phi i32 [ %16, %15 ], [ %25, %17 ]
ret i32 %31
}