char *pathsearch(argv0, name, modemask)
char *argv0;
char *name;
int modemask;
{
  char *pathlist;
  char *path, *cp;
  char c;
  extern int access();
  cp = strrchr(argv0, '/');
  if (cp) {
    c = *cp;
    *cp = 0;
    path = (char *)malloc(strlen(argv0) + strlen(name) + 2);
    if (path)
      sprintf(path, "%s/%s", argv0, name);
    *cp = c;
  } else {
    extern char *getenv();
    pathlist = getenv("PATH");
    if (pathlist == 0)
      pathlist = ".:/bin:/usr/bin";
    path = (char *)malloc(strlen(pathlist) + strlen(name) + 2);
    if (path != 0) {
      while (*pathlist) {
        cp = strchr(pathlist, ':');
        if (cp == 0)
          cp = &pathlist[strlen(pathlist)];
        c = *cp;
        *cp = 0;
        sprintf(path, "%s/%s", pathlist, name);
        *cp = c;
        if (c == 0)
          pathlist = "";
        else
          pathlist = &cp[1];
        if (access(path, modemask) == 0)
          break;
      }
    }
  }
  return path;
}


x86 assembly (clang, -O0):
<pathsearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     120d <pathsearch+0x9d>
mov    -0x30(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x31(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11ff <pathsearch+0x8f>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    -0x31(%rbp),%cl
mov    -0x30(%rbp),%rax
mov    %cl,(%rax)
jmp    133f <pathsearch+0x1cf>
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1235 <pathsearch+0xc5>
lea    0xdda(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     133a <pathsearch+0x1ca>
jmp    126f <pathsearch+0xff>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     1335 <pathsearch+0x1c5>
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12b8 <pathsearch+0x148>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x31(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xd25(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    -0x31(%rbp),%cl
mov    -0x30(%rbp),%rax
mov    %cl,(%rax)
movsbl -0x31(%rbp),%eax
cmp    $0x0,%eax
jne    1308 <pathsearch+0x198>
lea    0xd0b(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    1314 <pathsearch+0x1a4>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x0,%al
call   1080 <access@plt>
cmp    $0x0,%eax
jne    1330 <pathsearch+0x1c0>
jmp    1335 <pathsearch+0x1c5>
jmp    126f <pathsearch+0xff>
jmp    133a <pathsearch+0x1ca>
jmp    133f <pathsearch+0x1cf>
mov    -0x28(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pathsearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11f0 <pathsearch+0x80>
mov    %rax,%rbx
mov    (%rax),%r14b
movb   $0x0,(%rax)
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
je     11db <pathsearch+0x6b>
lea    0xe35(%rip),%rsi
mov    %r13,%rdi
mov    %r12,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %r14b,(%rbx)
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0f(%rip),%rdi
xor    %eax,%eax
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe03(%rip),%rbp
cmovne %rax,%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     12ba <pathsearch+0x14a>
mov    %rax,%r13
jmp    128b <pathsearch+0x11b>
nopw   0x0(%rax,%rax,1)
movzbl (%rbx),%r12d
movb   $0x0,(%rbx)
mov    %r13,%rdi
lea    0xdaf(%rip),%rsi
mov    %rbp,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %r12b,(%rbx)
add    $0x1,%rbx
test   %r12b,%r12b
lea    0xd9b(%rip),%rax
cmove  %rax,%rbx
mov    %r13,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1080 <access@plt>
mov    %rbx,%rbp
test   %eax,%eax
je     11de <pathsearch+0x6e>
cmpb   $0x0,0x0(%rbp)
je     11de <pathsearch+0x6e>
mov    %rbp,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1240 <pathsearch+0xd0>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
add    %rbp,%rbx
jmp    1240 <pathsearch+0xd0>
xor    %r13d,%r13d
jmp    11de <pathsearch+0x6e>

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

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [5 x i8] c"PATH\00"
@.str.2 = [16 x i8] c".:/bin:/usr/bin\00"
@.str.3 = [1 x i8] zeroinitializer
define i8* @pathsearch(i8* %0, i8* %1, i32 %2) {
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i32 47)
%5 = icmp eq i8* %4, null
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = load i8, i8* %4
store i8 0, i8* %4
%8 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%9 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%10 = add i64 %8, 2
%11 = add i64 %10, %9
%12 = tail call i8* @malloc(i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %6
%15 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
br label %16
16: ; preds = %14, %6
store i8 %7, i8* %4
br label %46
17: ; preds = %3
%18 = tail call i8* (i8*, ...) bitcast (i8* (...)* @getenv to i8* (i8*, ...)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i8* %18, null
%20 = select i1 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %18
%21 = tail call i64 @strlen(i8* dereferenceable(1) %20)
%22 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%23 = add i64 %21, 2
%24 = add i64 %23, %22
%25 = tail call i8* @malloc(i64 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %46, label %27
27: ; preds = %17, %37
%28 = phi i8* [ %43, %37 ], [ %20, %17 ]
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 0
br i1 %30, label %46, label %31
31: ; preds = %27
%32 = tail call i8* @strchr(i8* %28, i32 58)
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = tail call i64 @strlen(i8* %28)
%36 = getelementptr inbounds i8, i8* %28, i64 %35
br label %37
37: ; preds = %34, %31
%38 = phi i8* [ %36, %34 ], [ %32, %31 ]
%39 = load i8, i8* %38
store i8 0, i8* %38
%40 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %25, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %28, i8* %1)
store i8 %39, i8* %38
%41 = icmp eq i8 %39, 0
%42 = getelementptr inbounds i8, i8* %38, i64 1
%43 = select i1 %41, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8* %42
%44 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @access to i32 (i8*, i32, ...)*)(i8* %25, i32 %2)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %27
46: ; preds = %37, %27, %17, %16
%47 = phi i8* [ %12, %16 ], [ null, %17 ], [ %25, %27 ], [ %25, %37 ]
ret i8* %47
}