#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*/ execv(char *, char **);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s cmd [args...]\n", argv[0]);
    return 255;
  }
  char *pathlist = getenv("EXTPATH");
  char *pathbuf = NULL;
  if (pathlist) {
    pathbuf = malloc(strlen(pathlist) + strlen(argv[1]) + 2);
    char *dir = strtok(pathlist, ":");
    while (dir) {
      strcpy(pathbuf, dir);
      if (dir[strlen(dir) - 1] != '/')
        strcat(pathbuf, "/");
      strcat(pathbuf, argv[1]);
      execv(pathbuf, argv + 1);
      dir = strtok(NULL, ":");
    }
  } else {
    fprintf(stderr, "%s: warning: no EXTPATH environment variable found\n",
            argv[0]);
  }
  execvp(argv[1], argv + 1);
  if (pathbuf)
    free(pathbuf);
  fprintf(stderr, "%s: %s: command not found\n", argv[0], argv[1]);
  return 254;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ea <main+0x4a>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xff,-0x4(%rbp)
jmp    1351 <main+0x1b1>
lea    0xe28(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12dd <main+0x13d>
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
mov    -0x2c(%rbp),%edi
add    %eax,%edi
add    $0x2,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xddf(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12d8 <main+0x138>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     129d <main+0xfd>
mov    -0x20(%rbp),%rdi
lea    0xd8b(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   10b0 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   10c0 <execv@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd57(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    124b <main+0xab>
jmp    12fb <main+0x15b>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd31(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   10a0 <execvp@plt>
cmpq   $0x0,-0x20(%rbp)
je     1324 <main+0x184>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xd16(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xfe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x1,%edi
jg     11d7 <main+0x37>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe3a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xff,%eax
jmp    12f5 <main+0x155>
lea    0xe3b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     12ab <main+0x10b>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
lea    0x8(%r14),%r15
mov    0x8(%r14),%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0xe06(%rip),%rsi
mov    %rbp,%rdi
call   1090 <strtok@plt>
test   %rax,%rax
je     1291 <main+0xf1>
mov    %rax,%rbp
lea    0xdef(%rip),%r12
lea    0xdea(%rip),%r13
jmp    1268 <main+0xc8>
nopl   0x0(%rax,%rax,1)
mov    (%r15),%rsi
mov    %rbx,%rdi
call   10b0 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10c0 <execv@plt>
xor    %edi,%edi
mov    %r12,%rsi
call   1090 <strtok@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1291 <main+0xf1>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <strcpy@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbp,1)
je     1240 <main+0xa0>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10b0 <strcat@plt>
jmp    1240 <main+0xa0>
mov    (%r15),%rdi
mov    %r15,%rsi
call   10a0 <execvp@plt>
test   %rbx,%rbx
je     12d2 <main+0x132>
mov    %rbx,%rdi
call   1040 <free@plt>
jmp    12d2 <main+0x132>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd67(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0x8(%r14),%rsi
mov    0x8(%r14),%rdi
call   10a0 <execvp@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
mov    0x8(%r14),%rcx
lea    0xd70(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xfe,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"Usage: %s cmd [args...]\0A\00"
@.str.1 = [8 x i8] c"EXTPATH\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [52 x i8] c"%s: warning: no EXTPATH environment variable found\0A\00"
@.str.5 = [27 x i8] c"%s: %s: command not found\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 255, i32* %3
br label %91
17: ; preds = %2
%18 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %18, i8** %6
store i8* null, i8** %7
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %64
21: ; preds = %17
%22 = load i8*, i8** %6
%23 = call i32 @strlen(i8* %22)
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @strlen(i8* %26)
%28 = add nsw i32 %23, %27
%29 = add nsw i32 %28, 2
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %7
%31 = load i8*, i8** %6
%32 = call i8* @strtok(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %32, i8** %8
br label %33
33: ; preds = %52, %21
%34 = load i8*, i8** %8
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %63
36: ; preds = %33
%37 = load i8*, i8** %7
%38 = load i8*, i8** %8
%39 = call i32 @strcpy(i8* %37, i8* %38)
%40 = load i8*, i8** %8
%41 = load i8*, i8** %8
%42 = call i32 @strlen(i8* %41)
%43 = sub nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %40, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp ne i32 %47, 47
br i1 %48, label %49, label %52
49: ; preds = %36
%50 = load i8*, i8** %7
%51 = call i32 @strcat(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %52
52: ; preds = %49, %36
%53 = load i8*, i8** %7
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = call i32 @strcat(i8* %53, i8* %56)
%58 = load i8*, i8** %7
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 1
%61 = call i32 @execv(i8* %58, i8** %60)
%62 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %62, i8** %8
br label %33
63: ; preds = %33
br label %70
64: ; preds = %17
%65 = load i32, i32* @stderr
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 0
%68 = load i8*, i8** %67
%69 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i8* %68)
br label %70
70: ; preds = %64, %63
%71 = load i8**, i8*** %5
%72 = getelementptr inbounds i8*, i8** %71, i64 1
%73 = load i8*, i8** %72
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 1
%76 = call i32 @execvp(i8* %73, i8** %75)
%77 = load i8*, i8** %7
%78 = icmp ne i8* %77, null
br i1 %78, label %79, label %82
79: ; preds = %70
%80 = load i8*, i8** %7
%81 = call i32 @free(i8* %80)
br label %82
82: ; preds = %79, %70
%83 = load i32, i32* @stderr
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i64 0
%86 = load i8*, i8** %85
%87 = load i8**, i8*** %5
%88 = getelementptr inbounds i8*, i8** %87, i64 1
%89 = load i8*, i8** %88
%90 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %86, i8* %89)
store i32 254, i32* %3
br label %91
91: ; preds = %82, %11
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"Usage: %s cmd [args...]\0A\00"
@.str.1 = [8 x i8] c"EXTPATH\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [52 x i8] c"%s: warning: no EXTPATH environment variable found\0A\00"
@.str.5 = [27 x i8] c"%s: %s: command not found\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %57
8: ; preds = %2
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %38, label %11
11: ; preds = %8
%12 = tail call i32 @strlen(i8* %9)
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @strlen(i8* %14)
%16 = add i32 %12, 2
%17 = add i32 %16, %15
%18 = tail call i8* @malloc(i32 %17)
%19 = tail call i8* @strtok(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %45, label %21
21: ; preds = %11, %32
%22 = phi i8* [ %36, %32 ], [ %19, %11 ]
%23 = tail call i32 @strcpy(i8* %18, i8* %22)
%24 = tail call i32 @strlen(i8* %22)
%25 = add nsw i32 %24, -1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %22, i64 %26
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 47
br i1 %29, label %32, label %30
30: ; preds = %21
%31 = tail call i32 @strcat(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %21
%33 = load i8*, i8** %13
%34 = tail call i32 @strcat(i8* %18, i8* %33)
%35 = tail call i32 @execv(i8* %18, i8** %13)
%36 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp eq i8* %36, null
br i1 %37, label %45, label %21
38: ; preds = %8
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %1
%41 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i8* %40)
%42 = getelementptr inbounds i8*, i8** %1, i64 1
%43 = load i8*, i8** %42
%44 = tail call i32 @execvp(i8* %43, i8** %42)
br label %51
45: ; preds = %32, %11
%46 = load i8*, i8** %13
%47 = tail call i32 @execvp(i8* %46, i8** %13)
%48 = icmp eq i8* %18, null
br i1 %48, label %51, label %49
49: ; preds = %45
%50 = tail call i32 @free(i8* %18)
br label %51
51: ; preds = %38, %49, %45
%52 = phi i8** [ %42, %38 ], [ %13, %49 ], [ %13, %45 ]
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %1
%55 = load i8*, i8** %52
%56 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %54, i8* %55)
br label %57
57: ; preds = %51, %4
%58 = phi i32 [ 255, %4 ], [ 254, %51 ]
ret i32 %58
}