#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 */
char *getenv(char *);
int isExecutable(char *);
int /*<<< orphan*/ *realpath(char *, char *);
char *strchr(char *, char);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int pathOfExe(char *pth, char *exe) {
  char *searchpath;
  char *beg, *end;
  int stop, found;
  int len;
  if (strchr(exe, '/') != NULL) {
    if (realpath(exe, pth) == NULL)
      return 0;
    return isExecutable(pth);
  }
  searchpath = getenv("PATH");
  if (searchpath == NULL)
    return 0;
  if (strlen(searchpath) <= 0)
    return 0;
  beg = searchpath;
  stop = 0;
  found = 0;
  do {
    end = strchr(beg, ':');
    if (end == NULL) {
      stop = 1;
      strncpy(pth, beg, 200);
      len = strlen(pth);
    } else {
      strncpy(pth, beg, end - beg);
      pth[end - beg] = '\0';
      len = end - beg;
    }
    if (pth[len - 1] != '/')
      strncat(pth, "/", 1);
    strncat(pth, exe, 200 - len);
    found = isExecutable(pth);
    if (!stop)
      beg = end + 1;
  } while (!stop && !found);
  return 0;
}


x86 assembly (clang, -O0):
<pathOfExe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
cmp    $0x0,%rax
je     11cc <pathOfExe+0x5c>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <realpath@plt>
cmp    $0x0,%rax
jne    11bb <pathOfExe+0x4b>
movl   $0x0,-0x4(%rbp)
jmp    1342 <pathOfExe+0x1d2>
mov    -0x10(%rbp),%rdi
call   1040 <isExecutable@plt>
mov    %eax,-0x4(%rbp)
jmp    1342 <pathOfExe+0x1d2>
lea    0xe2d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f3 <pathOfExe+0x83>
movl   $0x0,-0x4(%rbp)
jmp    1342 <pathOfExe+0x1d2>
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x0,%rax
jg     1212 <pathOfExe+0xa2>
movl   $0x0,-0x4(%rbp)
jmp    1342 <pathOfExe+0x1d2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    126f <pathOfExe+0xff>
movl   $0x1,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0xc8,%edx
call   1050 <strncpy@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x3c(%rbp)
jmp    12aa <pathOfExe+0x13a>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x3c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     12d9 <pathOfExe+0x169>
mov    -0x10(%rbp),%rdi
lea    0xd36(%rip),%rsi
mov    $0x1,%edx
call   1080 <strncat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0xc8,%edx
sub    -0x3c(%rbp),%edx
call   1080 <strncat@plt>
mov    -0x10(%rbp),%rdi
call   1040 <isExecutable@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1310 <pathOfExe+0x1a0>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1315 <pathOfExe+0x1a5>
xor    %eax,%eax
cmpl   $0x0,-0x34(%rbp)
mov    %al,-0x3d(%rbp)
jne    1330 <pathOfExe+0x1c0>
cmpl   $0x0,-0x38(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    1228 <pathOfExe+0xb8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pathOfExe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     11b9 <pathOfExe+0x49>
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <realpath@plt>
test   %rax,%rax
je     11da <pathOfExe+0x6a>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <isExecutable@plt>
lea    0xe40(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11da <pathOfExe+0x6a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
test   %rax,%rax
jg     121f <pathOfExe+0xaf>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
nopl   0x0(%rax,%rax,1)
mov    $0xc8,%edx
sub    %ebp,%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <strncat@plt>
mov    %r15,%rdi
call   1040 <isExecutable@plt>
lea    0x1(%r13),%rcx
test   %r13,%r13
cmovne %rcx,%rbx
test   %eax,%eax
sete   %al
test   %al,%r12b
je     11da <pathOfExe+0x6a>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,%r13
test   %rax,%rax
je     1250 <pathOfExe+0xe0>
mov    %r13,%rbp
sub    %rbx,%rbp
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <strncpy@plt>
movb   $0x0,(%r15,%rbp,1)
jmp    126b <pathOfExe+0xfb>
xchg   %ax,%ax
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0xc8,%edx
call   1050 <strncpy@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbp
test   %r13,%r13
setne  %r12b
movslq %ebp,%rax
cmpb   $0x2f,-0x1(%rax,%r15,1)
je     11f0 <pathOfExe+0x80>
mov    %r15,%rdi
lea    0xd7a(%rip),%rsi
mov    $0x1,%edx
call   1080 <strncat@plt>
jmp    11f0 <pathOfExe+0x80>

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

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c"/\00"
define i32 @pathOfExe(i8* %0, i8* %1) {
%3 = tail call i8* @strchr(i8* dereferenceable(1) %1, i8 signext 47)
%4 = icmp eq i8* %3, null
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = tail call i32* @realpath(i8* %1, i8* %0)
%7 = icmp eq i32* %6, null
br i1 %7, label %49, label %8
8: ; preds = %5
%9 = tail call i32 @isExecutable(i8* %0)
br label %49
10: ; preds = %2
%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 %49, label %13
13: ; preds = %10
%14 = tail call i64 @strlen(i8* %11)
%15 = icmp slt i64 %14, 1
br i1 %15, label %49, label %16
16: ; preds = %13, %41
%17 = phi i8* [ %46, %41 ], [ %11, %13 ]
%18 = tail call i8* @strchr(i8* dereferenceable(1) %17, i8 signext 58)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = tail call i32 @strncpy(i8* %0, i8* %17, i32 200)
%22 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%23 = trunc i64 %22 to i32
br label %31
24: ; preds = %16
%25 = ptrtoint i8* %18 to i64
%26 = ptrtoint i8* %17 to i64
%27 = sub i64 %25, %26
%28 = trunc i64 %27 to i32
%29 = tail call i32 @strncpy(i8* %0, i8* %17, i32 %28)
%30 = getelementptr inbounds i8, i8* %0, i64 %27
store i8 0, i8* %30
br label %31
31: ; preds = %24, %20
%32 = phi i32 [ %23, %20 ], [ %28, %24 ]
%33 = xor i1 %19, true
%34 = add nsw i32 %32, -1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %0, i64 %35
%37 = load i8, i8* %36
%38 = icmp eq i8 %37, 47
br i1 %38, label %41, label %39
39: ; preds = %31
%40 = tail call i32 @strncat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %41
41: ; preds = %39, %31
%42 = sub nsw i32 200, %32
%43 = tail call i32 @strncat(i8* %0, i8* %1, i32 %42)
%44 = tail call i32 @isExecutable(i8* %0)
%45 = getelementptr inbounds i8, i8* %18, i64 1
%46 = select i1 %19, i8* %17, i8* %45
%47 = icmp eq i32 %44, 0
%48 = select i1 %33, i1 %47, i1 false
br i1 %48, label %16, label %49
49: ; preds = %41, %13, %10, %5, %8
%50 = phi i32 [ %9, %8 ], [ 0, %5 ], [ 0, %10 ], [ 0, %13 ], [ 0, %41 ]
ret i32 %50
}