#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 MAXPATHLEN;
char *getenv(char *);
scalar_t__ is_directory(char *);
scalar_t__ is_executable(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, char *);
char *string_copy(char *);
char *strtok_r(char *, char *, char **);
char *get_executable_dir_from_path(char *name) {
  if (((name[0]) == '/'))
    return name;
  else {
    char *path = getenv("PATH");
    if (path != NULL) {
      char *p = string_copy(path);
      char *tmp;
      char *dir;
      while ((dir = strtok_r(p, ":", &tmp)) != NULL) {
        if (is_directory(dir)) {
          char filename[MAXPATHLEN];
          snprintf(filename, MAXPATHLEN, "%s%s%s", dir, "/", name);
          if (is_executable(filename)) {
            return string_copy(filename);
          }
        }
        p = NULL;
      }
    }
  }
  return name;
}


x86 assembly (clang, -O0):
<get_executable_dir_from_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1189 <get_executable_dir_from_path+0x29>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a9 <get_executable_dir_from_path+0x149>
lea    0xe70(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     129c <get_executable_dir_from_path+0x13c>
mov    -0x18(%rbp),%rdi
call   1080 <string_copy@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe49(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1297 <get_executable_dir_from_path+0x137>
mov    -0x30(%rbp),%rdi
call   1070 <is_directory@plt>
cmp    $0x0,%rax
je     128a <get_executable_dir_from_path+0x12a>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%r9
lea    0xddd(%rip),%rdx
lea    0xddd(%rip),%r8
call   1050 <snprintf@plt>
mov    -0x50(%rbp),%rdi
call   1040 <is_executable@plt>
cmp    $0x0,%rax
je     1262 <get_executable_dir_from_path+0x102>
mov    -0x50(%rbp),%rdi
call   1080 <string_copy@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1269 <get_executable_dir_from_path+0x109>
movl   $0x0,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x44(%rbp),%eax
test   %eax,%eax
je     1285 <get_executable_dir_from_path+0x125>
jmp    1280 <get_executable_dir_from_path+0x120>
jmp    12a9 <get_executable_dir_from_path+0x149>
jmp    128a <get_executable_dir_from_path+0x12a>
movq   $0x0,-0x20(%rbp)
jmp    11b1 <get_executable_dir_from_path+0x51>
jmp    129c <get_executable_dir_from_path+0x13c>
jmp    12a1 <get_executable_dir_from_path+0x141>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_executable_dir_from_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
cmpb   $0x2f,(%rdi)
je     123d <get_executable_dir_from_path+0xdd>
lea    0xe7f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     123d <get_executable_dir_from_path+0xdd>
mov    %rax,%rdi
call   1080 <string_copy@plt>
lea    0xe67(%rip),%rsi
lea    -0x30(%rbp),%rdx
mov    %rax,%rdi
call   1060 <strtok_r@plt>
test   %rax,%rax
je     123d <get_executable_dir_from_path+0xdd>
mov    %rax,%r12
mov    0x2e2b(%rip),%r14
jmp    11da <get_executable_dir_from_path+0x7a>
nop
xor    %edi,%edi
lea    0xe3c(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,%r12
test   %rax,%rax
je     123d <get_executable_dir_from_path+0xdd>
mov    %r12,%rdi
call   1070 <is_directory@plt>
test   %rax,%rax
je     11c0 <get_executable_dir_from_path+0x60>
mov    %rsp,%r15
mov    (%r14),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    (%r14),%esi
mov    %r13,%rdi
lea    0xdfc(%rip),%rdx
mov    %r12,%rcx
lea    0xdf9(%rip),%r8
mov    %rbx,%r9
call   1050 <snprintf@plt>
mov    %r13,%rdi
call   1040 <is_executable@plt>
test   %rax,%rax
jne    122f <get_executable_dir_from_path+0xcf>
mov    %r15,%rsp
jmp    11c0 <get_executable_dir_from_path+0x60>
mov    %r13,%rdi
call   1080 <string_copy@plt>
mov    %rax,%rbx
mov    %r15,%rsp
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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