#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*/ errno;
int /*<<< orphan*/ execve(char *, char **, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *prepName(char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int wait(int /*<<< orphan*/ *);
int launcher(int argc, char *argv[], char *path[], char *envp[]) {
  int rc = 0;
  if (*argv[0] == '/') {
    rc = fork();
    if (rc < 0) {
      fprintf(stderr, "Fork failed\n");
      exit(1);
    } else if (rc == 0) {
      execve(argv[0], argv, envp);
      fprintf(stderr, "Command not found\n\n");
      exit(1);
    } else {
      int wc = wait(NULL);
    }
    return 0;
  } else {
    rc = fork();
    char **currPath = path;
    if (rc < 0) {
      fprintf(stderr, "Fork failed\n");
      exit(1);
    } else if (rc == 0) {
      while (*currPath != 0) {
        char *newName = prepName(*currPath, argv[0]);
        execve(newName, argv, envp);
        free(newName);
        currPath += 1;
      }
      fprintf(stderr, "%s\n", strerror(errno));
      exit(1);
    } else {
      int wc = wait(NULL);
    }
  }
}


x86 assembly (clang, -O0):
<launcher>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1242 <launcher+0xc2>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11e6 <launcher+0x66>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jne    1225 <launcher+0xa5>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <execve@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    %eax,-0x28(%rbp)
jmp    1236 <launcher+0xb6>
movl   $0x0,-0x4(%rbp)
jmp    132c <launcher+0x1ac>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    127f <launcher+0xff>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    0xd92(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jne    1316 <launcher+0x196>
jmp    128e <launcher+0x10e>
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
je     12de <launcher+0x15e>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1040 <prepName@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <execve@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x30(%rbp)
jmp    128e <launcher+0x10e>
mov    0x2cfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xd1c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    %eax,-0x3c(%rbp)
jmp    1327 <launcher+0x1a7>
jmp    132c <launcher+0x1ac>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<launcher>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%r15
mov    (%rsi),%rax
mov    (%rax),%bl
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0x2f,%bl
jne    11c9 <launcher+0x49>
test   %eax,%eax
js     11e2 <launcher+0x62>
jne    11cf <launcher+0x4f>
mov    (%r15),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <execve@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
jmp    11f2 <launcher+0x72>
test   %eax,%eax
js     11e2 <launcher+0x62>
je     1203 <launcher+0x83>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x0(%r13),%rdi
test   %rdi,%rdi
je     123e <launcher+0xbe>
add    $0x8,%r13
mov    (%r15),%rsi
call   1040 <prepName@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <execve@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x0(%r13),%rdi
add    $0x8,%r13
test   %rdi,%rdi
jne    1210 <launcher+0x90>
mov    0x2d9b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdc5(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [13 x i8] c"Fork failed\0A\00"
@.str.1 = [20 x i8] c"Command not found\0A\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define i32 @launcher(i32 %0, i8** %1, i8** %2, i8** %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i8**
%9 = alloca i8**
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8**
%13 = alloca i8*
%14 = alloca i32
store i32 %0, i32* %6
store i8** %1, i8*** %7
store i8** %2, i8*** %8
store i8** %3, i8*** %9
store i32 0, i32* %10
%15 = load i8**, i8*** %7
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 47
br i1 %20, label %21, label %46
21: ; preds = %4
%22 = call i32 (...) @fork()
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = load i32, i32* %10
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %29
%33 = load i8**, i8*** %7
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = load i8**, i8*** %7
%37 = load i8**, i8*** %9
%38 = call i32 @execve(i8* %35, i8** %36, i8** %37)
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %29
%43 = call i32 @wait(i32* null)
store i32 %43, i32* %11
br label %44
44: ; preds = %42
br label %45
45: ; preds = %44
store i32 0, i32* %5
br label %88
46: ; preds = %4
%47 = call i32 (...) @fork()
store i32 %47, i32* %10
%48 = load i8**, i8*** %8
store i8** %48, i8*** %12
%49 = load i32, i32* %10
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %46
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %46
%56 = load i32, i32* %10
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %84
58: ; preds = %55
br label %59
59: ; preds = %63, %58
%60 = load i8**, i8*** %12
%61 = load i8*, i8** %60
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %78
63: ; preds = %59
%64 = load i8**, i8*** %12
%65 = load i8*, i8** %64
%66 = load i8**, i8*** %7
%67 = getelementptr inbounds i8*, i8** %66, i64 0
%68 = load i8*, i8** %67
%69 = call i8* @prepName(i8* %65, i8* %68)
store i8* %69, i8** %13
%70 = load i8*, i8** %13
%71 = load i8**, i8*** %7
%72 = load i8**, i8*** %9
%73 = call i32 @execve(i8* %70, i8** %71, i8** %72)
%74 = load i8*, i8** %13
%75 = call i32 @free(i8* %74)
%76 = load i8**, i8*** %12
%77 = getelementptr inbounds i8*, i8** %76, i64 1
store i8** %77, i8*** %12
br label %59
78: ; preds = %59
%79 = load i32, i32* @stderr
%80 = load i32, i32* @errno
%81 = call i8* @strerror(i32 %80)
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %81)
%83 = call i32 @exit(i32 1)
unreachable
84: ; preds = %55
%85 = call i32 @wait(i32* null)
store i32 %85, i32* %14
br label %86
86: ; preds = %84
br label %87
87: ; preds = %86
br label %88
88: ; preds = %45, %87
%89 = load i32, i32* %5
ret i32 %89
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [13 x i8] c"Fork failed\0A\00"
@.str.1 = [20 x i8] c"Command not found\0A\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define i32 @launcher(i32 %0, i8** %1, i8** nocapture %2, i8** %3) {
%5 = load i8*, i8** %1
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 47
%8 = tail call i32 (...) @fork()
%9 = icmp slt i32 %8, 0
br i1 %7, label %10, label %23
10: ; preds = %4
br i1 %9, label %11, label %15
11: ; preds = %10
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %10
%16 = icmp eq i32 %8, 0
br i1 %16, label %17, label %49
17: ; preds = %15
%18 = load i8*, i8** %1
%19 = tail call i32 @execve(i8* %18, i8** %1, i8** %3)
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %4
br i1 %9, label %24, label %28
24: ; preds = %23
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %23
%29 = icmp eq i32 %8, 0
br i1 %29, label %30, label %49
30: ; preds = %28
%31 = load i8*, i8** %2
%32 = icmp eq i8* %31, null
br i1 %32, label %43, label %33
33: ; preds = %30, %33
%34 = phi i8* [ %41, %33 ], [ %31, %30 ]
%35 = phi i8** [ %40, %33 ], [ %2, %30 ]
%36 = load i8*, i8** %1
%37 = tail call i8* @prepName(i8* %34, i8* %36)
%38 = tail call i32 @execve(i8* %37, i8** %1, i8** %3)
%39 = tail call i32 @free(i8* %37)
%40 = getelementptr inbounds i8*, i8** %35, i64 1
%41 = load i8*, i8** %40
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %33
43: ; preds = %33, %30
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = tail call i8* @strerror(i32 %45)
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = tail call i32 @exit(i32 1)
unreachable
49: ; preds = %28, %15
%50 = tail call i32 @wait(i32* null)
ret i32 0
}