#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*/ env_destroy(char **);
int /*<<< orphan*/ env_dup_var(char **, char *);
int execute(char *, char **, char **);
char *find_bin(char *, char *);
int /*<<< orphan*/ free(char *);
char **ft_mdsplit(int /*<<< orphan*/, char *);
char **ft_split(char *, char *);
int test_direct_path(char *, char **, char **, int *);
int exec_binary_from_path(char *path, char **env, char **argv, int *not_found) {
  char **pathlst;
  char *bin_path;
  int ret;
  size_t i;
  i = 0;
  if (!path)
    pathlst = ft_mdsplit(env_dup_var(env, "PATH"), ":");
  else
    pathlst = ft_split(path, ":");
  while (pathlst && pathlst[i] != NULL) {
    if ((bin_path = find_bin(pathlst[i++], argv[0])) != NULL) {
      ret = execute(bin_path, argv, env);
      free(bin_path);
      env_destroy(pathlst);
      return (ret);
    }
  }
  env_destroy(pathlst);
  return (test_direct_path(path, env, argv, not_found));
}


x86 assembly (clang, -O0):
<exec_binary_from_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x48(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d2 <exec_binary_from_path+0x52>
mov    -0x18(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1050 <env_dup_var@plt>
mov    %eax,%edi
lea    0xe41(%rip),%rsi
call   1070 <ft_mdsplit@plt>
mov    %rax,-0x30(%rbp)
jmp    11e6 <exec_binary_from_path+0x66>
mov    -0x10(%rbp),%rdi
lea    0xe28(%rip),%rsi
call   1090 <ft_split@plt>
mov    %rax,-0x30(%rbp)
jmp    11eb <exec_binary_from_path+0x6b>
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x49(%rbp)
je     120e <exec_binary_from_path+0x8e>
mov    -0x30(%rbp),%rax
mov    -0x48(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
setne  %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    121e <exec_binary_from_path+0x9e>
jmp    1285 <exec_binary_from_path+0x105>
mov    -0x30(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x48(%rbp)
mov    (%rax,%rcx,8),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   1080 <find_bin@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1280 <exec_binary_from_path+0x100>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <execute@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1040 <env_destroy@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a6 <exec_binary_from_path+0x126>
jmp    11eb <exec_binary_from_path+0x6b>
mov    -0x30(%rbp),%rdi
call   1040 <env_destroy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   10a0 <test_direct_path@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_binary_from_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r12
test   %rdi,%rdi
mov    %rcx,(%rsp)
je     11ae <exec_binary_from_path+0x2e>
lea    0xe61(%rip),%rsi
mov    %r12,%rdi
call   1090 <ft_split@plt>
jmp    11cb <exec_binary_from_path+0x4b>
lea    0xe4b(%rip),%rsi
mov    %r14,%rdi
call   1050 <env_dup_var@plt>
lea    0xe41(%rip),%rsi
mov    %eax,%edi
call   1070 <ft_mdsplit@plt>
mov    %rax,%r15
test   %rax,%rax
je     122e <exec_binary_from_path+0xae>
mov    %r15,%r13
cs nopw 0x0(%rax,%rax,1)
mov    0x0(%r13),%rdi
test   %rdi,%rdi
je     122e <exec_binary_from_path+0xae>
mov    (%rbx),%rsi
call   1080 <find_bin@plt>
add    $0x8,%r13
test   %rax,%rax
je     11e0 <exec_binary_from_path+0x60>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <execute@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1040 <env_destroy@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1040 <env_destroy@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    (%rsp),%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <test_direct_path@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
define i32 @exec_binary_from_path(i8* %0, i8** %1, i8** %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i8**
%9 = alloca i32*
%10 = alloca i8**
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i64
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i8** %2, i8*** %8
store i32* %3, i32** %9
store i64 0, i64* %13
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %20, label %16
16: ; preds = %4
%17 = load i8**, i8*** %7
%18 = call i32 @env_dup_var(i8** %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%19 = call i8** @ft_mdsplit(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8** %19, i8*** %10
br label %23
20: ; preds = %4
%21 = load i8*, i8** %6
%22 = call i8** @ft_split(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8** %22, i8*** %10
br label %23
23: ; preds = %20, %16
br label %24
24: ; preds = %56, %23
%25 = load i8**, i8*** %10
%26 = icmp ne i8** %25, null
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i8**, i8*** %10
%29 = load i64, i64* %13
%30 = getelementptr inbounds i8*, i8** %28, i64 %29
%31 = load i8*, i8** %30
%32 = icmp ne i8* %31, null
br label %33
33: ; preds = %27, %24
%34 = phi i1 [ false, %24 ], [ %32, %27 ]
br i1 %34, label %35, label %57
35: ; preds = %33
%36 = load i8**, i8*** %10
%37 = load i64, i64* %13
%38 = add i64 %37, 1
store i64 %38, i64* %13
%39 = getelementptr inbounds i8*, i8** %36, i64 %37
%40 = load i8*, i8** %39
%41 = load i8**, i8*** %8
%42 = getelementptr inbounds i8*, i8** %41, i64 0
%43 = load i8*, i8** %42
%44 = call i8* @find_bin(i8* %40, i8* %43)
store i8* %44, i8** %11
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %56
46: ; preds = %35
%47 = load i8*, i8** %11
%48 = load i8**, i8*** %8
%49 = load i8**, i8*** %7
%50 = call i32 @execute(i8* %47, i8** %48, i8** %49)
store i32 %50, i32* %12
%51 = load i8*, i8** %11
%52 = call i32 @free(i8* %51)
%53 = load i8**, i8*** %10
%54 = call i32 @env_destroy(i8** %53)
%55 = load i32, i32* %12
store i32 %55, i32* %5
br label %65
56: ; preds = %35
br label %24
57: ; preds = %33
%58 = load i8**, i8*** %10
%59 = call i32 @env_destroy(i8** %58)
%60 = load i8*, i8** %6
%61 = load i8**, i8*** %7
%62 = load i8**, i8*** %8
%63 = load i32*, i32** %9
%64 = call i32 @test_direct_path(i8* %60, i8** %61, i8** %62, i32* %63)
store i32 %64, i32* %5
br label %65
65: ; preds = %57, %46
%66 = load i32, i32* %5
ret i32 %66
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
define i32 @exec_binary_from_path(i8* %0, i8** %1, i8** %2, i32* %3) {
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %9
6: ; preds = %4
%7 = tail call i32 @env_dup_var(i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = tail call i8** @ft_mdsplit(i32 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %11
9: ; preds = %4
%10 = tail call i8** @ft_split(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %6
%12 = phi i8** [ %10, %9 ], [ %8, %6 ]
%13 = icmp eq i8** %12, null
br i1 %13, label %28, label %14
14: ; preds = %11, %19
%15 = phi i64 [ %20, %19 ], [ 0, %11 ]
%16 = getelementptr inbounds i8*, i8** %12, i64 %15
%17 = load i8*, i8** %16
%18 = icmp eq i8* %17, null
br i1 %18, label %28, label %19
19: ; preds = %14
%20 = add i64 %15, 1
%21 = load i8*, i8** %2
%22 = tail call i8* @find_bin(i8* %17, i8* %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %14, label %24
24: ; preds = %19
%25 = tail call i32 @execute(i8* %22, i8** %2, i8** %1)
%26 = tail call i32 @free(i8* %22)
%27 = tail call i32 @env_destroy(i8** %12)
br label %31
28: ; preds = %14, %11
%29 = tail call i32 @env_destroy(i8** %12)
%30 = tail call i32 @test_direct_path(i8* %0, i8** %1, i8** %2, i32* %3)
br label %31
31: ; preds = %28, %24
%32 = phi i32 [ %25, %24 ], [ %30, %28 ]
ret i32 %32
}