#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 */
typedef int /*<<< orphan*/ opath;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ *strchr(char const *, char);
size_t strcspn(char const *, char *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int /*<<< orphan*/ strspn(char const *, char *);
bool resolve_path(char *out, size_t outsz, const char *filename) {
  const char *env_path;
  const char *ppath;
  size_t plen;
  size_t flen;
  char opath[PATH_MAX];
  const char *psrc = NULL;
  if (strchr(filename, '/') != NULL) {
    psrc = filename;
    goto exit;
  }
  env_path = getenv("PATH");
  if (env_path == NULL) {
    env_path = ".:/bin:/usr/bin";
  }
  ppath = env_path;
  flen = strlen(filename);
  plen = 0;
  while (*ppath != '\0') {
    ppath += plen;
    if (*ppath == '\0') {
      break;
    }
    ppath += strspn(ppath, ":");
    plen = strcspn(ppath, ":");
    if ((plen + flen + 1) >= sizeof(opath)) {
      continue;
    }
    strncpy(opath, ppath, plen);
    opath[plen] = '\0';
    if (opath[plen - 1] != '/')
      strcat(opath, "/");
    strcat(opath, filename);
    if (access(opath, X_OK) == 0) {
      psrc = opath;
      goto exit;
    }
  }
exit:
  if (psrc == NULL) {
    ;
    return false;
  }
  if (outsz < (strlen(psrc) + 1)) {
    ;
    return false;
  }
  strncpy(out, psrc, outsz);
  ;
  return true;
}


x86 assembly (clang, -O0):
<resolve_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x70(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
je     11f4 <resolve_path+0x74>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    133e <resolve_path+0x1be>
lea    0xe05(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    121a <resolve_path+0x9a>
lea    0xdef(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cltq
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1339 <resolve_path+0x1b9>
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    126a <resolve_path+0xea>
jmp    1339 <resolve_path+0x1b9>
mov    -0x30(%rbp),%rdi
lea    0xda0(%rip),%rsi
call   1070 <strspn@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xd80(%rip),%rsi
call   1080 <strcspn@plt>
mov    -0x70(%rbp),%rcx
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    -0x40(%rbp),%rax
add    $0x1,%rax
cmp    %rcx,%rax
jb     12bc <resolve_path+0x13c>
jmp    1239 <resolve_path+0xb9>
mov    -0x68(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x68(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     12fe <resolve_path+0x17e>
mov    -0x68(%rbp),%rdi
lea    0xd1e(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x68(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10a0 <strcat@plt>
mov    -0x68(%rbp),%rdi
mov    0x2cca(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jne    1334 <resolve_path+0x1b4>
mov    -0x68(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    133e <resolve_path+0x1be>
jmp    1239 <resolve_path+0xb9>
jmp    133e <resolve_path+0x1be>
cmpq   $0x0,-0x58(%rbp)
jne    135c <resolve_path+0x1dc>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    13b4 <resolve_path+0x234>
mov    -0x18(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x58(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x78(%rbp),%rax
add    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    1395 <resolve_path+0x215>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    13b4 <resolve_path+0x234>
mov    -0x10(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <strncpy@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e4f(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%r15
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %rdx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1201 <resolve_path+0x81>
test   %rbx,%rbx
je     12e6 <resolve_path+0x166>
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rcx
xor    %eax,%eax
cmp    %r14,%rcx
ja     12e8 <resolve_path+0x168>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <strncpy@plt>
mov    $0x1,%eax
jmp    12e8 <resolve_path+0x168>
mov    %r13,-0x50(%rbp)
lea    0xdf4(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xdea(%rip),%r13
cmovne %rax,%r13
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmpb   $0x0,0x0(%r13)
je     12e6 <resolve_path+0x166>
mov    %r12,-0x30(%rbp)
mov    %rbx,-0x40(%rbp)
mov    %r14,-0x38(%rbp)
cltq
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
lea    0xdbb(%rip),%rbx
mov    %rbx,%rsi
call   1070 <strspn@plt>
movslq %eax,%r14
add    %r13,%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <strcspn@plt>
mov    %rax,%r12
mov    -0x48(%rbp),%rax
add    %r12,%rax
cmp    -0x50(%rbp),%rax
jae    12cf <resolve_path+0x14f>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1040 <strncpy@plt>
movb   $0x0,(%r15,%r12,1)
cmpb   $0x2f,-0x1(%r15,%r12,1)
je     12ad <resolve_path+0x12d>
mov    %r15,%rdi
lea    0xd6f(%rip),%rsi
call   10a0 <strcat@plt>
mov    %r15,%rdi
mov    -0x40(%rbp),%rsi
call   10a0 <strcat@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1090 <access@plt>
test   %rax,%rax
je     12f7 <resolve_path+0x177>
cmpb   $0x0,(%r14)
je     12e6 <resolve_path+0x166>
mov    %r12,%r13
add    %r14,%r13
cmpb   $0x0,(%r14,%r12,1)
jne    1250 <resolve_path+0xd0>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rbx
mov    -0x38(%rbp),%r14
mov    -0x30(%rbp),%r12
jmp    11d0 <resolve_path+0x50>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [5 x i8] c"PATH\00"
@.str.1 = [16 x i8] c".:/bin:/usr/bin\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [2 x i8] c"/\00"
@X_OK = global i32 0
define i32 @resolve_path(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%16 = load i32, i32* @PATH_MAX
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %12
%19 = alloca i8, i64 %17
store i64 %17, i64* %13
store i8* null, i8** %14
%20 = load i8*, i8** %7
%21 = call i32* @strchr(i8* %20, i8 signext 47)
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %3
%24 = load i8*, i8** %7
store i8* %24, i8** %14
br label %86
25: ; preds = %3
%26 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %25
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8** %8
br label %30
30: ; preds = %29, %25
%31 = load i8*, i8** %8
store i8* %31, i8** %9
%32 = load i8*, i8** %7
%33 = call i32 @strlen(i8* %32)
%34 = sext i32 %33 to i64
store i64 %34, i64* %11
store i64 0, i64* %10
br label %35
35: ; preds = %84, %62, %30
%36 = load i8*, i8** %9
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %85
40: ; preds = %35
%41 = load i64, i64* %10
%42 = load i8*, i8** %9
%43 = getelementptr inbounds i8, i8* %42, i64 %41
store i8* %43, i8** %9
%44 = load i8*, i8** %9
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %40
br label %85
49: ; preds = %40
%50 = load i8*, i8** %9
%51 = call i32 @strspn(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%52 = load i8*, i8** %9
%53 = sext i32 %51 to i64
%54 = getelementptr inbounds i8, i8* %52, i64 %53
store i8* %54, i8** %9
%55 = load i8*, i8** %9
%56 = call i64 @strcspn(i8* %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i64 %56, i64* %10
%57 = load i64, i64* %10
%58 = load i64, i64* %11
%59 = add i64 %57, %58
%60 = add i64 %59, 1
%61 = icmp uge i64 %60, %17
br i1 %61, label %62, label %63
62: ; preds = %49
br label %35
63: ; preds = %49
%64 = load i8*, i8** %9
%65 = load i64, i64* %10
%66 = call i32 @strncpy(i8* %19, i8* %64, i64 %65)
%67 = load i64, i64* %10
%68 = getelementptr inbounds i8, i8* %19, i64 %67
store i8 0, i8* %68
%69 = load i64, i64* %10
%70 = sub i64 %69, 1
%71 = getelementptr inbounds i8, i8* %19, i64 %70
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = icmp ne i32 %73, 47
br i1 %74, label %75, label %77
75: ; preds = %63
%76 = call i32 @strcat(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %77
77: ; preds = %75, %63
%78 = load i8*, i8** %7
%79 = call i32 @strcat(i8* %19, i8* %78)
%80 = load i32, i32* @X_OK
%81 = call i64 @access(i8* %19, i32 %80)
%82 = icmp eq i64 %81, 0
br i1 %82, label %83, label %84
83: ; preds = %77
store i8* %19, i8** %14
br label %86
84: ; preds = %77
br label %35
85: ; preds = %48, %35
br label %86
86: ; preds = %85, %83, %23
%87 = load i8*, i8** %14
%88 = icmp eq i8* %87, null
br i1 %88, label %89, label %90
89: ; preds = %86
store i32 0, i32* %4
store i32 1, i32* %15
br label %103
90: ; preds = %86
%91 = load i64, i64* %6
%92 = load i8*, i8** %14
%93 = call i32 @strlen(i8* %92)
%94 = add nsw i32 %93, 1
%95 = sext i32 %94 to i64
%96 = icmp ult i64 %91, %95
br i1 %96, label %97, label %98
97: ; preds = %90
store i32 0, i32* %4
store i32 1, i32* %15
br label %103
98: ; preds = %90
%99 = load i8*, i8** %5
%100 = load i8*, i8** %14
%101 = load i64, i64* %6
%102 = call i32 @strncpy(i8* %99, i8* %100, i64 %101)
store i32 1, i32* %4
store i32 1, i32* %15
br label %103
103: ; preds = %98, %97, %89
%104 = load i8*, i8** %12
%105 = load i32, i32* %4
ret i32 %105
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [5 x i8] c"PATH\00"
@.str.1 = [16 x i8] c".:/bin:/usr/bin\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [2 x i8] c"/\00"
@X_OK = global i32 0
define i32 @resolve_path(i8* %0, i64 %1, i8* %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call i32* @strchr(i8* %2, i8 signext 47)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %49
9: ; preds = %3
%10 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i8* %10, null
%12 = select i1 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %10
%13 = tail call i32 @strlen(i8* %2)
%14 = load i8, i8* %12
%15 = icmp eq i8 %14, 0
br i1 %15, label %59, label %16
16: ; preds = %9
%17 = sext i32 %13 to i64
%18 = add nsw i64 %17, 1
br label %19
19: ; preds = %16, %30
%20 = phi i8* [ %12, %16 ], [ %33, %30 ]
%21 = call i32 @strspn(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = call i64 @strcspn(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = add i64 %18, %24
%26 = icmp ult i64 %25, %5
br i1 %26, label %35, label %27
27: ; preds = %19, %44
%28 = load i8, i8* %23
%29 = icmp eq i8 %28, 0
br i1 %29, label %59, label %30
30: ; preds = %27
%31 = getelementptr inbounds i8, i8* %23, i64 %24
%32 = load i8, i8* %31
%33 = getelementptr inbounds i8, i8* %23, i64 %24
%34 = icmp eq i8 %32, 0
br i1 %34, label %59, label %19
35: ; preds = %19
%36 = call i32 @strncpy(i8* %6, i8* %23, i64 %24)
%37 = getelementptr inbounds i8, i8* %6, i64 %24
store i8 0, i8* %37
%38 = add i64 %24, -1
%39 = getelementptr inbounds i8, i8* %6, i64 %38
%40 = load i8, i8* %39
%41 = icmp eq i8 %40, 47
br i1 %41, label %44, label %42
42: ; preds = %35
%43 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %35
%45 = call i32 @strcat(i8* %6, i8* %2)
%46 = load i32, i32* @X_OK
%47 = call i64 @access(i8* %6, i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %51, label %27
49: ; preds = %3
%50 = icmp eq i8* %2, null
br i1 %50, label %59, label %51
51: ; preds = %44, %49
%52 = phi i8* [ %2, %49 ], [ %6, %44 ]
%53 = call i32 @strlen(i8* %52)
%54 = add nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = icmp ugt i64 %55, %1
br i1 %56, label %59, label %57
57: ; preds = %51
%58 = call i32 @strncpy(i8* %0, i8* %52, i64 %1)
br label %59
59: ; preds = %30, %27, %9, %51, %49, %57
%60 = phi i32 [ 1, %57 ], [ 0, %49 ], [ 0, %51 ], [ 0, %9 ], [ 0, %27 ], [ 0, %30 ]
ret i32 %60
}