#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 R_OK;
int X_OK;
scalar_t__ access(char const *, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getenv(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, ...);
char *strtok(char *, char *);
char *xxstrdup(int /*<<< orphan*/);
int find_executable(const char *exe_name, const char *env_path_var,
                    char *exe_path, int max_length) {
  char *env_paths;
  char *cur_path;
  if (access(exe_name, R_OK | X_OK) == 0) {
    snprintf(exe_path, max_length, "%s", exe_name);
    return 1;
  }
  if (!getenv(env_path_var))
    return 0;
  env_paths = xxstrdup(getenv(env_path_var));
  for (cur_path = strtok(env_paths, ":"); cur_path;
       cur_path = strtok(NULL, ":")) {
    snprintf(exe_path, max_length, "%s/%s", cur_path, exe_name);
    if (access(exe_path, R_OK | X_OK) == 0)
      goto fe_cleanup;
  }
fe_cleanup:
  free(env_paths);
  return cur_path != NULL;
}


x86 assembly (clang, -O0):
<find_executable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    0x2e5d(%rip),%rax
or     (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    11c1 <find_executable+0x61>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rcx
lea    0xe57(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1295 <find_executable+0x135>
mov    -0x18(%rbp),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%eax
jne    11df <find_executable+0x7f>
movl   $0x0,-0x4(%rbp)
jmp    1295 <find_executable+0x135>
mov    -0x18(%rbp),%rdi
call   1030 <getenv@plt>
mov    %eax,%edi
call   1080 <xxstrdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe02(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1277 <find_executable+0x117>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xdda(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    0x2da5(%rip),%rax
or     (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    1259 <find_executable+0xf9>
jmp    127c <find_executable+0x11c>
jmp    125e <find_executable+0xfe>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd97(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x38(%rbp)
jmp    1207 <find_executable+0xa7>
jmp    127c <find_executable+0x11c>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x38(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
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
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e5a(%rip),%rax
mov    0x2e63(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <access@plt>
test   %rax,%rax
je     1229 <find_executable+0xc9>
mov    %r15,%rdi
call   1030 <getenv@plt>
test   %eax,%eax
je     1248 <find_executable+0xe8>
mov    %eax,%edi
call   1080 <xxstrdup@plt>
mov    %rax,%r15
lea    0xe48(%rip),%rsi
mov    %rax,%rdi
call   1070 <strtok@plt>
test   %rax,%rax
mov    %r15,(%rsp)
je     124d <find_executable+0xed>
lea    0xe2e(%rip),%r12
lea    0xe25(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %r12,%rdx
mov    %rax,%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
call   1060 <access@plt>
test   %rax,%rax
je     1252 <find_executable+0xf2>
xor    %r15d,%r15d
xor    %edi,%edi
mov    %r13,%rsi
call   1070 <strtok@plt>
test   %rax,%rax
jne    11e0 <find_executable+0x80>
jmp    1258 <find_executable+0xf8>
lea    0xdd5(%rip),%rdx
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    $0x1,%r15d
jmp    1261 <find_executable+0x101>
xor    %r15d,%r15d
jmp    1261 <find_executable+0x101>
xor    %r15d,%r15d
jmp    1258 <find_executable+0xf8>
mov    $0x1,%r15d
mov    (%rsp),%rdi
call   1040 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@R_OK = global i32 0
@X_OK = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @find_executable(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*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i8*, i8** %6
%13 = load i32, i32* @R_OK
%14 = load i32, i32* @X_OK
%15 = or i32 %13, %14
%16 = call i64 @access(i8* %12, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %4
%19 = load i8*, i8** %8
%20 = load i32, i32* %9
%21 = load i8*, i8** %6
%22 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %19, i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 1, i32* %5
br label %60
23: ; preds = %4
%24 = load i8*, i8** %7
%25 = call i32 @getenv(i8* %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %23
store i32 0, i32* %5
br label %60
28: ; preds = %23
%29 = load i8*, i8** %7
%30 = call i32 @getenv(i8* %29)
%31 = call i8* @xxstrdup(i32 %30)
store i8* %31, i8** %10
%32 = load i8*, i8** %10
%33 = call i8* @strtok(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %33, i8** %11
br label %34
34: ; preds = %51, %28
%35 = load i8*, i8** %11
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %53
37: ; preds = %34
%38 = load i8*, i8** %8
%39 = load i32, i32* %9
%40 = load i8*, i8** %11
%41 = load i8*, i8** %6
%42 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %38, i32 %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %40, i8* %41)
%43 = load i8*, i8** %8
%44 = load i32, i32* @R_OK
%45 = load i32, i32* @X_OK
%46 = or i32 %44, %45
%47 = call i64 @access(i8* %43, i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %37
br label %54
50: ; preds = %37
br label %51
51: ; preds = %50
%52 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %52, i8** %11
br label %34
53: ; preds = %34
br label %54
54: ; preds = %53, %49
%55 = load i8*, i8** %10
%56 = call i32 @free(i8* %55)
%57 = load i8*, i8** %11
%58 = icmp ne i8* %57, null
%59 = zext i1 %58 to i32
store i32 %59, i32* %5
br label %60
60: ; preds = %54, %27, %18
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
@R_OK = global i32 0
@X_OK = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @find_executable(i8* %0, i8* nocapture %1, i8* %2, i32 %3) {
%5 = load i32, i32* @R_OK
%6 = load i32, i32* @X_OK
%7 = or i32 %6, %5
%8 = tail call i64 @access(i8* %0, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %2, i32 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %33
12: ; preds = %4
%13 = tail call i32 @getenv(i8* %1)
%14 = icmp eq i32 %13, 0
br i1 %14, label %33, label %15
15: ; preds = %12
%16 = tail call i8* @xxstrdup(i32 %13)
%17 = tail call i8* @strtok(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %30, label %19
19: ; preds = %15, %27
%20 = phi i8* [ %28, %27 ], [ %17, %15 ]
%21 = tail call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %2, i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %20, i8* %0)
%22 = load i32, i32* @R_OK
%23 = load i32, i32* @X_OK
%24 = or i32 %23, %22
%25 = tail call i64 @access(i8* %2, i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %19
%28 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %19
30: ; preds = %19, %27, %15
%31 = phi i32 [ 0, %15 ], [ 0, %27 ], [ 1, %19 ]
%32 = tail call i32 @free(i8* %16)
br label %33
33: ; preds = %12, %30, %10
%34 = phi i32 [ 1, %10 ], [ %31, %30 ], [ 0, %12 ]
ret i32 %34
}