#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*/ free(void *);
char *getenv(char *);
scalar_t__ malloc(size_t);
scalar_t__ myaccess(char const *);
char *strdup(char const *);
int strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, size_t);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strtok(char *, char *);
char *find_executable(const char *fn) {
  char *s, *path, *t = NULL;
  if (fn == NULL || *fn == '\0')
    return NULL;
  if (*fn == '/') {
    if (myaccess(fn) == 0)
      return strdup(fn);
    else
      return NULL;
  }
  if ((s = getenv("PATH")) == NULL) {
    return NULL;
  } else {
    path = strdup(s);
  }
  for (s = strtok(path, ":"); s; s = strtok(NULL, ":")) {
    size_t len = strlen(fn) + strlen(s) + 2;
    t = (char *)malloc(len);
    strncpy(t, s, len);
    strncat(t, "/", len);
    strncat(t, fn, len);
    if (myaccess(t) == 0)
      break;
    else {
      free((void *)t);
      t = NULL;
    }
  }
  free((void *)path);
  return t;
}


x86 assembly (clang, -O0):
<find_executable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11bf <find_executable+0x2f>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11cc <find_executable+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    1321 <find_executable+0x191>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    120e <find_executable+0x7e>
mov    -0x10(%rbp),%rdi
call   1060 <myaccess@plt>
cmp    $0x0,%rax
jne    1201 <find_executable+0x71>
mov    -0x10(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1321 <find_executable+0x191>
movq   $0x0,-0x8(%rbp)
jmp    1321 <find_executable+0x191>
lea    0xdeb(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1235 <find_executable+0xa5>
movq   $0x0,-0x8(%rbp)
jmp    1321 <find_executable+0x191>
mov    -0x18(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdb8(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1310 <find_executable+0x180>
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <strncpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xd54(%rip),%rsi
call   1080 <strncat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <strncat@plt>
mov    -0x28(%rbp),%rdi
call   1060 <myaccess@plt>
cmp    $0x0,%rax
jne    12e1 <find_executable+0x151>
jmp    1310 <find_executable+0x180>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x28(%rbp)
jmp    12f7 <find_executable+0x167>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd03(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x18(%rbp)
jmp    1256 <find_executable+0xc6>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_executable>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     12ac <find_executable+0x11c>
mov    %rdi,%r13
mov    (%rdi),%al
test   %al,%al
je     12ac <find_executable+0x11c>
cmp    $0x2f,%al
jne    11dc <find_executable+0x4c>
mov    %r13,%rdi
call   1060 <myaccess@plt>
test   %rax,%rax
jne    12ac <find_executable+0x11c>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <strdup@plt>
lea    0xe1d(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     12ac <find_executable+0x11c>
mov    %rax,%rdi
call   10b0 <strdup@plt>
mov    %rax,%rbx
lea    0xe02(%rip),%rsi
mov    %rax,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
mov    %rbx,(%rsp)
je     12c1 <find_executable+0x131>
mov    %rax,%rbp
lea    0xde5(%rip),%r12
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1070 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
movslq %eax,%rbx
mov    %rbx,%rdi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1050 <strncpy@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1080 <strncat@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1080 <strncat@plt>
mov    %r14,%rdi
call   1060 <myaccess@plt>
test   %rax,%rax
je     12c6 <find_executable+0x136>
mov    %r14,%rdi
call   1040 <free@plt>
xor    %edi,%edi
lea    0xd68(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1230 <find_executable+0xa0>
jmp    12c9 <find_executable+0x139>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r15d,%r15d
jmp    12c9 <find_executable+0x139>
mov    %r14,%r15
mov    (%rsp),%rdi
call   1040 <free@plt>
jmp    12af <find_executable+0x11f>

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c"/\00"
define i8* @find_executable(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %6
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %10, %1
store i8* null, i8** %2
br label %78
16: ; preds = %10
%17 = load i8*, i8** %3
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 47
br i1 %20, label %21, label %29
21: ; preds = %16
%22 = load i8*, i8** %3
%23 = call i64 @myaccess(i8* %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i8*, i8** %3
%27 = call i8* @strdup(i8* %26)
store i8* %27, i8** %2
br label %78
28: ; preds = %21
store i8* null, i8** %2
br label %78
29: ; preds = %16
%30 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %30, i8** %4
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %29
store i8* null, i8** %2
br label %78
33: ; preds = %29
%34 = load i8*, i8** %4
%35 = call i8* @strdup(i8* %34)
store i8* %35, i8** %5
br label %36
36: ; preds = %33
%37 = load i8*, i8** %5
%38 = call i8* @strtok(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %38, i8** %4
br label %39
39: ; preds = %72, %36
%40 = load i8*, i8** %4
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %74
42: ; preds = %39
%43 = load i8*, i8** %3
%44 = call i32 @strlen(i8* %43)
%45 = load i8*, i8** %4
%46 = call i32 @strlen(i8* %45)
%47 = add nsw i32 %44, %46
%48 = add nsw i32 %47, 2
%49 = sext i32 %48 to i64
store i64 %49, i64* %7
%50 = load i64, i64* %7
%51 = call i64 @malloc(i64 %50)
%52 = inttoptr i64 %51 to i8*
store i8* %52, i8** %6
%53 = load i8*, i8** %6
%54 = load i8*, i8** %4
%55 = load i64, i64* %7
%56 = call i32 @strncpy(i8* %53, i8* %54, i64 %55)
%57 = load i8*, i8** %6
%58 = load i64, i64* %7
%59 = call i32 @strncat(i8* %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i64 %58)
%60 = load i8*, i8** %6
%61 = load i8*, i8** %3
%62 = load i64, i64* %7
%63 = call i32 @strncat(i8* %60, i8* %61, i64 %62)
%64 = load i8*, i8** %6
%65 = call i64 @myaccess(i8* %64)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %42
br label %74
68: ; preds = %42
%69 = load i8*, i8** %6
%70 = call i32 @free(i8* %69)
store i8* null, i8** %6
br label %71
71: ; preds = %68
br label %72
72: ; preds = %71
%73 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %73, i8** %4
br label %39
74: ; preds = %67, %39
%75 = load i8*, i8** %5
%76 = call i32 @free(i8* %75)
%77 = load i8*, i8** %6
store i8* %77, i8** %2
br label %78
78: ; preds = %74, %32, %28, %25, %15
%79 = load i8*, i8** %2
ret i8* %79
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c"/\00"
define i8* @find_executable(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %40, label %3
3: ; preds = %1
%4 = load i8, i8* %0
switch i8 %4, label %10 [
i8 0, label %40
i8 47, label %5
]
5: ; preds = %3
%6 = tail call i64 @myaccess(i8* %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %40
8: ; preds = %5
%9 = tail call i8* @strdup(i8* %0)
br label %40
10: ; preds = %3
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %40, label %13
13: ; preds = %10
%14 = tail call i8* @strdup(i8* %11)
%15 = tail call i8* @strtok(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %37, label %17
17: ; preds = %13, %31
%18 = phi i8* [ %33, %31 ], [ %15, %13 ]
%19 = tail call i32 @strlen(i8* %0)
%20 = tail call i32 @strlen(i8* %18)
%21 = add i32 %19, 2
%22 = add i32 %21, %20
%23 = sext i32 %22 to i64
%24 = tail call i64 @malloc(i64 %23)
%25 = inttoptr i64 %24 to i8*
%26 = tail call i32 @strncpy(i8* %25, i8* %18, i64 %23)
%27 = tail call i32 @strncat(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i64 %23)
%28 = tail call i32 @strncat(i8* %25, i8* %0, i64 %23)
%29 = tail call i64 @myaccess(i8* %25)
%30 = icmp eq i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %17
%32 = tail call i32 @free(i8* %25)
%33 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp eq i8* %33, null
br i1 %34, label %37, label %17
35: ; preds = %17
%36 = inttoptr i64 %24 to i8*
br label %37
37: ; preds = %31, %35, %13
%38 = phi i8* [ null, %13 ], [ %36, %35 ], [ null, %31 ]
%39 = tail call i32 @free(i8* %14)
br label %40
40: ; preds = %10, %5, %3, %1, %37, %8
%41 = phi i8* [ %9, %8 ], [ %38, %37 ], [ null, %3 ], [ null, %1 ], [ null, %5 ], [ null, %10 ]
ret i8* %41
}