#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*/ EXIT_FAILURE;
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getenv(char *);
char *joinpath(char *, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ safe_execv(char *, char const *, char **);
char *strdup(int /*<<< orphan*/);
char *strtok_r(char *, char *, char **);
__attribute__((noreturn)) void
exec_from_path(const char *cmd, const char *exe_path, char *argv[]) {
  char *path = strdup(getenv("PATH"));
  if (!path)
    errx(EXIT_FAILURE, "command %s not found", cmd);
  char *saveptr = NULL, *segment = strtok_r(path, ":", &saveptr);
  for (; segment; segment = strtok_r(NULL, ":", &saveptr)) {
    char *full_path = joinpath(segment, cmd, NULL);
    safe_execv(full_path, exe_path, argv);
    free(full_path);
  }
  errx(EXIT_FAILURE, "command %s not found", cmd);
}


x86 assembly (clang, -O0):
<exec_from_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe75(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bf <exec_from_path+0x4f>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe4b(%rip),%rsi
call   1050 <errx@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe48(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1236 <exec_from_path+0xc6>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <joinpath@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <safe_execv@plt>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdf6(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
jmp    11df <exec_from_path+0x6f>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdbb(%rip),%rsi
call   1050 <errx@plt>

x86 assembly (clang, -O2):
<exec_from_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe75(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edi
call   1090 <strdup@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11b7 <exec_from_path+0x47>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
mov    %r14,%rdx
call   1050 <errx@plt>
movq   $0x0,(%rsp)
lea    0xe54(%rip),%rsi
mov    %rsp,%rdx
mov    %r12,%rdi
call   1060 <strtok_r@plt>
test   %rax,%rax
je     1218 <exec_from_path+0xa8>
lea    0xe3d(%rip),%r12
mov    %rsp,%r13
mov    %rax,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1080 <joinpath@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1070 <safe_execv@plt>
mov    %rbp,%rdi
call   1040 <free@plt>
xor    %edi,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   1060 <strtok_r@plt>
test   %rax,%rax
jne    11e0 <exec_from_path+0x70>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
mov    %r14,%rdx
call   1050 <errx@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [21 x i8] c"command %s not found\00"
@.str.2 = [2 x i8] c":\00"
define void @exec_from_path(i8* %0, i8* %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8** %2, i8*** %6
%11 = call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = call i8* @strdup(i32 %11)
store i8* %12, i8** %7
%13 = load i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %3
%16 = load i32, i32* @EXIT_FAILURE
%17 = load i8*, i8** %4
%18 = call i32 @errx(i32 %16, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %15, %3
store i8* null, i8** %8
%20 = load i8*, i8** %7
%21 = call i8* @strtok_r(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %8)
store i8* %21, i8** %9
br label %22
22: ; preds = %35, %19
%23 = load i8*, i8** %9
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %37
25: ; preds = %22
%26 = load i8*, i8** %9
%27 = load i8*, i8** %4
%28 = call i8* @joinpath(i8* %26, i8* %27, i32* null)
store i8* %28, i8** %10
%29 = load i8*, i8** %10
%30 = load i8*, i8** %5
%31 = load i8**, i8*** %6
%32 = call i32 @safe_execv(i8* %29, i8* %30, i8** %31)
%33 = load i8*, i8** %10
%34 = call i32 @free(i8* %33)
br label %35
35: ; preds = %25
%36 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %8)
store i8* %36, i8** %9
br label %22
37: ; preds = %22
%38 = load i32, i32* @EXIT_FAILURE
%39 = load i8*, i8** %4
%40 = call i32 @errx(i32 %38, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %39)
unreachable
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [21 x i8] c"command %s not found\00"
@.str.2 = [2 x i8] c":\00"
define void @exec_from_path(i8* %0, i8* %1, i8** %2) {
%4 = alloca i8*
%5 = tail call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = tail call i8* @strdup(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @errx(i32 %9, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %11
11: ; preds = %8, %3
%12 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%13 = call i8* @strtok_r(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %4)
%14 = icmp eq i8* %13, null
br i1 %14, label %22, label %15
15: ; preds = %11, %15
%16 = phi i8* [ %20, %15 ], [ %13, %11 ]
%17 = call i8* @joinpath(i8* %16, i8* %0, i32* null)
%18 = call i32 @safe_execv(i8* %17, i8* %1, i8** %2)
%19 = call i32 @free(i8* %17)
%20 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %4)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %15
22: ; preds = %15, %11
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @errx(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0)
unreachable
}