#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 */
scalar_t__ malloc(size_t);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
char *strchr(char *, char);
size_t strlen(char *);
char *strrchr(char *, char);
char *pathsearch(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     120b <pathsearch+0x9b>
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     11fd <pathsearch+0x8d>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe08(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x31(%rbp),%cl
mov    -0x30(%rbp),%rax
mov    %cl,(%rax)
jmp    133b <pathsearch+0x1cb>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1233 <pathsearch+0xc3>
lea    0xddc(%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     1336 <pathsearch+0x1c6>
jmp    126d <pathsearch+0xfd>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     1331 <pathsearch+0x1c1>
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12b6 <pathsearch+0x146>
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    0xd27(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x31(%rbp),%cl
mov    -0x30(%rbp),%rax
mov    %cl,(%rax)
movsbl -0x31(%rbp),%eax
cmp    $0x0,%eax
jne    1304 <pathsearch+0x194>
lea    0xd0f(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    1310 <pathsearch+0x1a0>
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    132c <pathsearch+0x1bc>
jmp    1331 <pathsearch+0x1c1>
jmp    126d <pathsearch+0xfd>
jmp    1336 <pathsearch+0x1c6>
jmp    133b <pathsearch+0x1cb>
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,%rbx
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11e2 <pathsearch+0x72>
mov    %rax,%rbp
mov    (%rax),%r13b
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%r14,%rax,1),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     11d9 <pathsearch+0x69>
lea    0xe35(%rip),%rsi
mov    %r12,%rdi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1090 <sprintf@plt>
mov    %r13b,0x0(%rbp)
jmp    12a3 <pathsearch+0x133>
lea    0xe1d(%rip),%rdi
xor    %r12d,%r12d
xor    %eax,%eax
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe0e(%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     12a3 <pathsearch+0x133>
mov    %rax,%r13
jmp    1275 <pathsearch+0x105>
nop
movzbl (%rbx),%r12d
movb   $0x0,(%rbx)
mov    %r13,%rdi
lea    0xdbf(%rip),%rsi
mov    %rbp,%rdx
mov    %r15,%rcx
call   1090 <sprintf@plt>
mov    %r12b,(%rbx)
add    $0x1,%rbx
test   %r12b,%r12b
lea    0xdad(%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     12a0 <pathsearch+0x130>
cmpb   $0x0,0x0(%rbp)
je     12a0 <pathsearch+0x130>
mov    %rbp,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1230 <pathsearch+0xc0>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
add    %rbp,%rbx
jmp    1230 <pathsearch+0xc0>
mov    %r13,%r12
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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, i8 signext 47)
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %37
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 i64 @malloc(i64 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %34
29: ; preds = %15
%30 = load i8*, i8** %8
%31 = load i8*, i8** %4
%32 = load i8*, i8** %5
%33 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %32)
br label %34
34: ; preds = %29, %15
%35 = load i8, i8* %10
%36 = load i8*, i8** %9
store i8 %35, i8* %36
br label %94
37: ; preds = %3
%38 = 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* %38, i8** %7
%39 = load i8*, i8** %7
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %42
41: ; preds = %37
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8** %7
br label %42
42: ; preds = %41, %37
%43 = load i8*, i8** %7
%44 = call i64 @strlen(i8* %43)
%45 = load i8*, i8** %5
%46 = call i64 @strlen(i8* %45)
%47 = add i64 %44, %46
%48 = add i64 %47, 2
%49 = call i64 @malloc(i64 %48)
%50 = inttoptr i64 %49 to i8*
store i8* %50, i8** %8
%51 = load i8*, i8** %8
%52 = icmp ne i8* %51, null
br i1 %52, label %53, label %93
53: ; preds = %42
br label %54
54: ; preds = %91, %53
%55 = load i8*, i8** %7
%56 = load i8, i8* %55
%57 = icmp ne i8 %56, 0
br i1 %57, label %58, label %92
58: ; preds = %54
%59 = load i8*, i8** %7
%60 = call i8* @strchr(i8* %59, i8 signext 58)
store i8* %60, i8** %9
%61 = load i8*, i8** %9
%62 = icmp eq i8* %61, null
br i1 %62, label %63, label %68
63: ; preds = %58
%64 = load i8*, i8** %7
%65 = load i8*, i8** %7
%66 = call i64 @strlen(i8* %65)
%67 = getelementptr inbounds i8, i8* %64, i64 %66
store i8* %67, i8** %9
br label %68
68: ; preds = %63, %58
%69 = load i8*, i8** %9
%70 = load i8, i8* %69
store i8 %70, i8* %10
%71 = load i8*, i8** %9
store i8 0, i8* %71
%72 = load i8*, i8** %8
%73 = load i8*, i8** %7
%74 = load i8*, i8** %5
%75 = call i32 @sprintf(i8* %72, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %73, i8* %74)
%76 = load i8, i8* %10
%77 = load i8*, i8** %9
store i8 %76, i8* %77
%78 = load i8, i8* %10
%79 = sext i8 %78 to i32
%80 = icmp eq i32 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %68
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8** %7
br label %85
82: ; preds = %68
%83 = load i8*, i8** %9
%84 = getelementptr inbounds i8, i8* %83, i64 1
store i8* %84, i8** %7
br label %85
85: ; preds = %82, %81
%86 = load i8*, i8** %8
%87 = load i32, i32* %6
%88 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @access to i32 (i8*, i32, ...)*)(i8* %86, i32 %87)
%89 = icmp eq i32 %88, 0
br i1 %89, label %90, label %91
90: ; preds = %85
br label %92
91: ; preds = %85
br label %54
92: ; preds = %90, %54
br label %93
93: ; preds = %92, %42
br label %94
94: ; preds = %93, %34
%95 = load i8*, i8** %8
ret i8* %95
}

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, i8 signext 47)
%5 = icmp eq i8* %4, null
br i1 %5, label %18, 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 i64 @malloc(i64 %11)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %17, label %15
15: ; preds = %6
%16 = tail call i32 @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
br label %17
17: ; preds = %15, %6
store i8 %7, i8* %4
br label %48
18: ; preds = %3
%19 = 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))
%20 = icmp eq i8* %19, null
%21 = select i1 %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %19
%22 = tail call i64 @strlen(i8* dereferenceable(1) %21)
%23 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%24 = add i64 %22, 2
%25 = add i64 %24, %23
%26 = tail call i64 @malloc(i64 %25)
%27 = inttoptr i64 %26 to i8*
%28 = icmp eq i64 %26, 0
br i1 %28, label %48, label %29
29: ; preds = %18, %39
%30 = phi i8* [ %45, %39 ], [ %21, %18 ]
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 0
br i1 %32, label %48, label %33
33: ; preds = %29
%34 = tail call i8* @strchr(i8* %30, i8 signext 58)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = tail call i64 @strlen(i8* %30)
%38 = getelementptr inbounds i8, i8* %30, i64 %37
br label %39
39: ; preds = %36, %33
%40 = phi i8* [ %38, %36 ], [ %34, %33 ]
%41 = load i8, i8* %40
store i8 0, i8* %40
%42 = tail call i32 @sprintf(i8* %27, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %30, i8* %1)
store i8 %41, i8* %40
%43 = icmp eq i8 %41, 0
%44 = getelementptr inbounds i8, i8* %40, i64 1
%45 = select i1 %43, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8* %44
%46 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @access to i32 (i8*, i32, ...)*)(i8* %27, i32 %2)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %29
48: ; preds = %39, %29, %18, %17
%49 = phi i8* [ %13, %17 ], [ null, %18 ], [ %27, %29 ], [ %27, %39 ]
ret i8* %49
}