#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 PATH_MAX;
int access(char *, int);
int /*<<< orphan*/ *getcwd(char *, int);
char *getenv(char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int search_path(char *cmd, char *res_path, int access_mode) {
  char *cp;
  int ret;
  char *pathenv;
  char tmppath[PATH_MAX];
  char curpath[PATH_MAX];
  char *path;
  int lastpath;
  int toolong = 0;
  if ((cmd[0] == '/') || ((cp = strchr(cmd, '/')) != NULL)) {
    if (access(cmd, access_mode) == 0) {
      if (cmd[0] != '/') {
        if (getcwd(curpath, PATH_MAX) == NULL) {
          strcpy(res_path, curpath);
          return -1;
        }
        if ((strlen(curpath) + strlen(cmd) + 1) > (size_t)PATH_MAX) {
          sprintf(res_path, "cmd (as relative path) and cwd is longer than %d",
                  PATH_MAX);
          return -1;
        }
        sprintf(res_path, "%s/%s", curpath, cmd);
      } else
        strcpy(res_path, cmd);
      return 0;
    } else {
      sprintf(res_path, "file %s not found", cmd);
      return -1;
    }
  }
  if ((pathenv = getenv("PATH")) == NULL) {
    sprintf(res_path, "Unable to get PATH env. variable");
    return -1;
  }
  curpath[0] = '\0';
  cp = pathenv;
  path = pathenv;
  lastpath = 0;
  for (;;) {
    if (lastpath)
      break;
    if (cp != pathenv)
      path = ++cp;
    for (; ((*cp != ':') && (*cp != '\0')); cp++)
      ;
    strncpy(tmppath, path, (cp - path));
    tmppath[cp - path] = '\0';
    if (*cp == '\0')
      lastpath = 1;
    if (strlen(tmppath) + strlen(cmd) + 2 > (size_t)PATH_MAX) {
      toolong++;
      continue;
    }
    sprintf(res_path, "%s/%s", tmppath, cmd);
    if ((ret = access(res_path, access_mode)) == 0) {
      return 0;
    }
  }
  if (toolong)
    sprintf(res_path, "Unable to find file, %d path/file strings were too long",
            toolong);
  else
    strcpy(res_path, "Unable to find file");
  return 1;
}


x86 assembly (clang, -O0):
<search_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x40(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x78(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x48(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
movl   $0x0,-0x60(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     1213 <search_path+0x93>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     133e <search_path+0x1be>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1090 <access@plt>
cmp    $0x0,%eax
jne    1315 <search_path+0x195>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     12f5 <search_path+0x175>
mov    -0x70(%rbp),%rdi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
call   1080 <getcwd@plt>
cmp    $0x0,%rax
jne    1274 <search_path+0xf4>
mov    -0x70(%rbp),%rsi
mov    -0x18(%rbp),%rdi
call   1050 <strcpy@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1530 <search_path+0x3b0>
mov    -0x70(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x7c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x7c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    0x2d54(%rip),%rcx
movslq (%rcx),%rcx
cmp    %rcx,%rax
jbe    12d6 <search_path+0x156>
mov    -0x18(%rbp),%rdi
mov    0x2d3d(%rip),%rax
mov    (%rax),%edx
lea    0xd44(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1530 <search_path+0x3b0>
mov    -0x70(%rbp),%rdx
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xd48(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
jmp    1302 <search_path+0x182>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1530 <search_path+0x3b0>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xd13(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1530 <search_path+0x3b0>
lea    0xd04(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    137d <search_path+0x1fd>
mov    -0x18(%rbp),%rdi
lea    0xceb(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1530 <search_path+0x3b0>
mov    -0x70(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     13aa <search_path+0x22a>
jmp    14ee <search_path+0x36e>
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
je     13c8 <search_path+0x248>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    %rax,-0x58(%rbp)
jmp    13cd <search_path+0x24d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x3a,%ecx
mov    %al,-0x7d(%rbp)
je     13f2 <search_path+0x272>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x7d(%rbp)
mov    -0x7d(%rbp),%al
test   $0x1,%al
jne    1402 <search_path+0x282>
jmp    1418 <search_path+0x298>
jmp    1407 <search_path+0x287>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    13cd <search_path+0x24d>
mov    -0x78(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x58(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    -0x78(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x58(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    145c <search_path+0x2dc>
movl   $0x1,-0x5c(%rbp)
mov    -0x78(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x84(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x84(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
mov    0x2b66(%rip),%rcx
movslq (%rcx),%rcx
cmp    %rcx,%rax
jbe    14a4 <search_path+0x324>
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
jmp    139b <search_path+0x21b>
mov    -0x78(%rbp),%rdx
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xb7a(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1090 <access@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jne    14e9 <search_path+0x369>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1530 <search_path+0x3b0>
jmp    139b <search_path+0x21b>
cmpl   $0x0,-0x60(%rbp)
je     1512 <search_path+0x392>
mov    -0x18(%rbp),%rdi
mov    -0x60(%rbp),%edx
lea    0xb69(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
jmp    1522 <search_path+0x3a2>
mov    -0x18(%rbp),%rdi
lea    0xb8a(%rip),%rsi
call   1050 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,-0x38(%rbp)
mov    %rsi,-0x30(%rbp)
mov    %rdi,%r12
mov    0x2e4e(%rip),%r15
mov    (%r15),%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,%rsp
mov    %rsp,%r13
sub    %rax,%r13
mov    %r13,%rsp
cmpb   $0x2f,(%rdi)
je     11d9 <search_path+0x59>
mov    %rcx,%r14
mov    %r12,%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     1225 <search_path+0xa5>
mov    %r12,%rdi
mov    -0x38(%rbp),%esi
call   1090 <access@plt>
test   %eax,%eax
je     1207 <search_path+0x87>
lea    0xe48(%rip),%rsi
mov    -0x30(%rbp),%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   10a0 <sprintf@plt>
mov    $0xffffffff,%ebx
jmp    140a <search_path+0x28a>
cmpb   $0x2f,(%r12)
jne    133b <search_path+0x1bb>
mov    -0x30(%rbp),%rdi
mov    %r12,%rsi
call   1050 <strcpy@plt>
xor    %ebx,%ebx
jmp    140a <search_path+0x28a>
lea    0xe1d(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1387 <search_path+0x207>
mov    %r12,-0x50(%rbp)
movb   $0x0,0x0(%r13)
xor    %r15d,%r15d
mov    %rax,%r12
mov    %rax,%rsi
movl   $0x0,-0x34(%rbp)
mov    %rax,-0x40(%rbp)
mov    -0x50(%rbp),%r13
nopl   0x0(%rax,%rax,1)
test   $0x1,%r15b
jne    13ab <search_path+0x22b>
lea    0x1(%r12),%rcx
cmp    %rax,%r12
cmovne %rcx,%r12
cmovne %rcx,%rsi
mov    %r12,%rbx
sub    %rsi,%rbx
add    %r14,%rbx
mov    %r12d,%edx
sub    %esi,%edx
nopl   0x0(%rax,%rax,1)
movzbl (%r12),%ecx
test   %cl,%cl
je     12b0 <search_path+0x130>
cmp    $0x3a,%cl
je     12b0 <search_path+0x130>
add    $0x1,%r12
add    $0x1,%rbx
add    $0x1,%edx
jmp    1290 <search_path+0x110>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rsi,-0x48(%rbp)
call   1040 <strncpy@plt>
movb   $0x0,(%rbx)
cmpb   $0x0,(%r12)
sete   %r15b
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1060 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
mov    0x2d0a(%rip),%rcx
cmp    (%rcx),%eax
ja     132a <search_path+0x1aa>
mov    -0x30(%rbp),%rbx
mov    %rbx,%rdi
lea    0xd39(%rip),%rsi
mov    %r14,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
mov    -0x38(%rbp),%esi
call   1090 <access@plt>
test   %eax,%eax
mov    $0x0,%ebx
mov    -0x40(%rbp),%rax
mov    -0x48(%rbp),%rsi
jne    1260 <search_path+0xe0>
jmp    140a <search_path+0x28a>
addl   $0x1,-0x34(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x48(%rbp),%rsi
jmp    1260 <search_path+0xe0>
mov    (%r15),%esi
mov    %r13,%rdi
call   1080 <getcwd@plt>
test   %rax,%rax
je     13c6 <search_path+0x246>
mov    %r13,%rdi
call   1060 <strlen@plt>
mov    %eax,%r14d
mov    %r12,%rdi
call   1060 <strlen@plt>
add    %r14d,%eax
add    $0x1,%eax
mov    (%r15),%edx
cmp    %edx,%eax
jbe    13d9 <search_path+0x259>
lea    0xc8e(%rip),%rsi
mov    -0x30(%rbp),%rdi
xor    %eax,%eax
call   10a0 <sprintf@plt>
mov    $0xffffffff,%ebx
jmp    140a <search_path+0x28a>
movups 0xcd0(%rip),%xmm0
mov    -0x30(%rbp),%rax
movups %xmm0,0x10(%rax)
movups 0xcb1(%rip),%xmm0
movups %xmm0,(%rax)
movb   $0x0,0x20(%rax)
mov    $0xffffffff,%ebx
jmp    140a <search_path+0x28a>
mov    -0x34(%rbp),%edx
test   %edx,%edx
je     13f5 <search_path+0x275>
lea    0xcb6(%rip),%rsi
mov    -0x30(%rbp),%rdi
xor    %eax,%eax
call   10a0 <sprintf@plt>
jmp    1405 <search_path+0x285>
mov    -0x30(%rbp),%rdi
mov    %r13,%rsi
call   1050 <strcpy@plt>
mov    $0xffffffff,%ebx
jmp    140a <search_path+0x28a>
lea    0xc51(%rip),%rsi
xor    %ebx,%ebx
mov    -0x30(%rbp),%rdi
mov    %r13,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   10a0 <sprintf@plt>
jmp    140a <search_path+0x28a>
lea    0xcab(%rip),%rsi
mov    -0x30(%rbp),%rdi
call   1050 <strcpy@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [49 x i8] c"cmd (as relative path) and cwd is longer than %d\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [18 x i8] c"file %s not found\00"
@.str.3 = [5 x i8] c"PATH\00"
@.str.4 = [33 x i8] c"Unable to get PATH env. variable\00"
@.str.5 = [56 x i8] c"Unable to find file, %d path/file strings were too long\00"
@.str.6 = [20 x i8] c"Unable to find file\00"
define i32 @search_path(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%18 = load i32, i32* @PATH_MAX
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %11
%21 = alloca i8, i64 %19
store i64 %19, i64* %12
%22 = load i32, i32* @PATH_MAX
%23 = zext i32 %22 to i64
%24 = alloca i8, i64 %23
store i64 %23, i64* %13
store i32 0, i32* %16
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 47
br i1 %29, label %34, label %30
30: ; preds = %3
%31 = load i8*, i8** %5
%32 = call i8* @strchr(i8* %31, i8 signext 47)
store i8* %32, i8** %8
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %79
34: ; preds = %30, %3
%35 = load i8*, i8** %5
%36 = load i32, i32* %7
%37 = call i32 @access(i8* %35, i32 %36)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %75
39: ; preds = %34
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i64 0
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 47
br i1 %44, label %45, label %70
45: ; preds = %39
%46 = load i32, i32* @PATH_MAX
%47 = call i32* @getcwd(i8* %24, i32 %46)
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i8*, i8** %6
%51 = call i32 @strcpy(i8* %50, i8* %24)
store i32 -1, i32* %4
store i32 1, i32* %17
br label %171
52: ; preds = %45
%53 = call i32 @strlen(i8* %24)
%54 = load i8*, i8** %5
%55 = call i32 @strlen(i8* %54)
%56 = add nsw i32 %53, %55
%57 = add nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = load i32, i32* @PATH_MAX
%60 = sext i32 %59 to i64
%61 = icmp ugt i64 %58, %60
br i1 %61, label %62, label %66
62: ; preds = %52
%63 = load i8*, i8** %6
%64 = load i32, i32* @PATH_MAX
%65 = call i32 (i8*, i8*, ...) @sprintf(i8* %63, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %64)
store i32 -1, i32* %4
store i32 1, i32* %17
br label %171
66: ; preds = %52
%67 = load i8*, i8** %6
%68 = load i8*, i8** %5
%69 = call i32 (i8*, i8*, ...) @sprintf(i8* %67, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %68)
br label %74
70: ; preds = %39
%71 = load i8*, i8** %6
%72 = load i8*, i8** %5
%73 = call i32 @strcpy(i8* %71, i8* %72)
br label %74
74: ; preds = %70, %66
store i32 0, i32* %4
store i32 1, i32* %17
br label %171
75: ; preds = %34
%76 = load i8*, i8** %6
%77 = load i8*, i8** %5
%78 = call i32 (i8*, i8*, ...) @sprintf(i8* %76, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %77)
store i32 -1, i32* %4
store i32 1, i32* %17
br label %171
79: ; preds = %30
%80 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i8* %80, i8** %10
%81 = icmp eq i8* %80, null
br i1 %81, label %82, label %85
82: ; preds = %79
%83 = load i8*, i8** %6
%84 = call i32 (i8*, i8*, ...) @sprintf(i8* %83, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
store i32 1, i32* %17
br label %171
85: ; preds = %79
%86 = getelementptr inbounds i8, i8* %24, i64 0
store i8 0, i8* %86
%87 = load i8*, i8** %10
store i8* %87, i8** %8
%88 = load i8*, i8** %10
store i8* %88, i8** %14
store i32 0, i32* %15
br label %89
89: ; preds = %159, %147, %85
%90 = load i32, i32* %15
%91 = icmp ne i32 %90, 0
br i1 %91, label %92, label %93
92: ; preds = %89
br label %160
93: ; preds = %89
%94 = load i8*, i8** %8
%95 = load i8*, i8** %10
%96 = icmp ne i8* %94, %95
br i1 %96, label %97, label %100
97: ; preds = %93
%98 = load i8*, i8** %8
%99 = getelementptr inbounds i8, i8* %98, i32 1
store i8* %99, i8** %8
store i8* %99, i8** %14
br label %100
100: ; preds = %97, %93
br label %101
101: ; preds = %114, %100
%102 = load i8*, i8** %8
%103 = load i8, i8* %102
%104 = sext i8 %103 to i32
%105 = icmp ne i32 %104, 58
br i1 %105, label %106, label %111
106: ; preds = %101
%107 = load i8*, i8** %8
%108 = load i8, i8* %107
%109 = sext i8 %108 to i32
%110 = icmp ne i32 %109, 0
br label %111
111: ; preds = %106, %101
%112 = phi i1 [ false, %101 ], [ %110, %106 ]
br i1 %112, label %113, label %117
113: ; preds = %111
br label %114
114: ; preds = %113
%115 = load i8*, i8** %8
%116 = getelementptr inbounds i8, i8* %115, i32 1
store i8* %116, i8** %8
br label %101
117: ; preds = %111
%118 = load i8*, i8** %14
%119 = load i8*, i8** %8
%120 = load i8*, i8** %14
%121 = ptrtoint i8* %119 to i64
%122 = ptrtoint i8* %120 to i64
%123 = sub i64 %121, %122
%124 = trunc i64 %123 to i32
%125 = call i32 @strncpy(i8* %21, i8* %118, i32 %124)
%126 = load i8*, i8** %8
%127 = load i8*, i8** %14
%128 = ptrtoint i8* %126 to i64
%129 = ptrtoint i8* %127 to i64
%130 = sub i64 %128, %129
%131 = getelementptr inbounds i8, i8* %21, i64 %130
store i8 0, i8* %131
%132 = load i8*, i8** %8
%133 = load i8, i8* %132
%134 = sext i8 %133 to i32
%135 = icmp eq i32 %134, 0
br i1 %135, label %136, label %137
136: ; preds = %117
store i32 1, i32* %15
br label %137
137: ; preds = %136, %117
%138 = call i32 @strlen(i8* %21)
%139 = load i8*, i8** %5
%140 = call i32 @strlen(i8* %139)
%141 = add nsw i32 %138, %140
%142 = add nsw i32 %141, 2
%143 = sext i32 %142 to i64
%144 = load i32, i32* @PATH_MAX
%145 = sext i32 %144 to i64
%146 = icmp ugt i64 %143, %145
br i1 %146, label %147, label %150
147: ; preds = %137
%148 = load i32, i32* %16
%149 = add nsw i32 %148, 1
store i32 %149, i32* %16
br label %89
150: ; preds = %137
%151 = load i8*, i8** %6
%152 = load i8*, i8** %5
%153 = call i32 (i8*, i8*, ...) @sprintf(i8* %151, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %21, i8* %152)
%154 = load i8*, i8** %6
%155 = load i32, i32* %7
%156 = call i32 @access(i8* %154, i32 %155)
store i32 %156, i32* %9
%157 = icmp eq i32 %156, 0
br i1 %157, label %158, label %159
158: ; preds = %150
store i32 0, i32* %4
store i32 1, i32* %17
br label %171
159: ; preds = %150
br label %89
160: ; preds = %92
%161 = load i32, i32* %16
%162 = icmp ne i32 %161, 0
br i1 %162, label %163, label %167
163: ; preds = %160
%164 = load i8*, i8** %6
%165 = load i32, i32* %16
%166 = call i32 (i8*, i8*, ...) @sprintf(i8* %164, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0), i32 %165)
br label %170
167: ; preds = %160
%168 = load i8*, i8** %6
%169 = call i32 @strcpy(i8* %168, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %170
170: ; preds = %167, %163
store i32 1, i32* %4
store i32 1, i32* %17
br label %171
171: ; preds = %170, %158, %82, %75, %74, %62, %49
%172 = load i8*, i8** %11
%173 = load i32, i32* %4
ret i32 %173
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [49 x i8] c"cmd (as relative path) and cwd is longer than %d\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [18 x i8] c"file %s not found\00"
@.str.3 = [5 x i8] c"PATH\00"
@.str.4 = [33 x i8] c"Unable to get PATH env. variable\00"
@.str.5 = [56 x i8] c"Unable to find file, %d path/file strings were too long\00"
@.str.6 = [20 x i8] c"Unable to find file\00"
define i32 @search_path(i8* %0, i8* %1, i32 %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = alloca i8, i64 %5
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 47
br i1 %9, label %13, label %10
10: ; preds = %3
%11 = tail call i8* @strchr(i8* %0, i8 signext 47)
%12 = icmp eq i8* %11, null
br i1 %12, label %40, label %13
13: ; preds = %10, %3
%14 = tail call i32 @access(i8* %0, i32 %2)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %38
16: ; preds = %13
%17 = load i8, i8* %0
%18 = icmp eq i8 %17, 47
br i1 %18, label %36, label %19
19: ; preds = %16
%20 = load i32, i32* @PATH_MAX
%21 = call i32* @getcwd(i8* %7, i32 %20)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @strcpy(i8* %1, i8* %7)
br label %91
25: ; preds = %19
%26 = call i32 @strlen(i8* %7)
%27 = call i32 @strlen(i8* %0)
%28 = add i32 %26, 1
%29 = add i32 %28, %27
%30 = load i32, i32* @PATH_MAX
%31 = icmp ugt i32 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %30)
br label %91
34: ; preds = %25
%35 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %7, i8* %0)
br label %91
36: ; preds = %16
%37 = tail call i32 @strcpy(i8* %1, i8* %0)
br label %91
38: ; preds = %13
%39 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %91
40: ; preds = %10
%41 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %44
43: ; preds = %40
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(33) %1, i8* align 1 dereferenceable(33) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i64 33, i1 false)
br label %91
44: ; preds = %40
store i8 0, i8* %7
br label %45
45: ; preds = %79, %44
%46 = phi i8* [ %60, %79 ], [ %41, %44 ]
%47 = phi i8* [ %58, %79 ], [ %41, %44 ]
%48 = phi i1 [ %72, %79 ], [ false, %44 ]
%49 = phi i32 [ %80, %79 ], [ 0, %44 ]
br label %50
50: ; preds = %45, %81
%51 = phi i8* [ %60, %81 ], [ %46, %45 ]
%52 = phi i8* [ %58, %81 ], [ %47, %45 ]
%53 = phi i1 [ %72, %81 ], [ %48, %45 ]
br i1 %53, label %85, label %54
54: ; preds = %50
%55 = icmp eq i8* %51, %41
%56 = getelementptr inbounds i8, i8* %51, i64 1
%57 = select i1 %55, i8* %51, i8* %56
%58 = select i1 %55, i8* %52, i8* %56
br label %59
59: ; preds = %62, %54
%60 = phi i8* [ %57, %54 ], [ %63, %62 ]
%61 = load i8, i8* %60
switch i8 %61, label %62 [
i8 58, label %64
i8 0, label %64
]
62: ; preds = %59
%63 = getelementptr inbounds i8, i8* %60, i64 1
br label %59
64: ; preds = %59, %59
%65 = ptrtoint i8* %60 to i64
%66 = ptrtoint i8* %58 to i64
%67 = sub i64 %65, %66
%68 = trunc i64 %67 to i32
%69 = call i32 @strncpy(i8* %6, i8* %58, i32 %68)
%70 = getelementptr inbounds i8, i8* %6, i64 %67
store i8 0, i8* %70
%71 = load i8, i8* %60
%72 = icmp eq i8 %71, 0
%73 = call i32 @strlen(i8* %6)
%74 = call i32 @strlen(i8* %0)
%75 = add i32 %73, 2
%76 = add i32 %75, %74
%77 = load i32, i32* @PATH_MAX
%78 = icmp ugt i32 %76, %77
br i1 %78, label %79, label %81
79: ; preds = %64
%80 = add nuw nsw i32 %49, 1
br label %45
81: ; preds = %64
%82 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %0)
%83 = call i32 @access(i8* %1, i32 %2)
%84 = icmp eq i32 %83, 0
br i1 %84, label %91, label %50
85: ; preds = %50
%86 = icmp eq i32 %49, 0
br i1 %86, label %89, label %87
87: ; preds = %85
%88 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0), i32 %49)
br label %91
89: ; preds = %85
%90 = call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %91
91: ; preds = %81, %87, %89, %34, %36, %43, %38, %32, %23
%92 = phi i32 [ -1, %23 ], [ -1, %32 ], [ -1, %38 ], [ -1, %43 ], [ 0, %36 ], [ 0, %34 ], [ 1, %89 ], [ 1, %87 ], [ 0, %81 ]
ret i32 %92
}