#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ _CS_PATH;
size_t confstr(int /*<<< orphan*/, char *, size_t);
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *path_join(char *, char *);
char *realpath(char const *, int /*<<< orphan*/ *);
scalar_t__ stat(char *, struct stat *);
int strcmp(char const *, char *);
char *strtok_r(char *, char *, char **);
char *xmalloc(size_t);
char *xstrdup(char *);
char *which(const char *mydir, const char *myargv0basename) {
  char *mycandir = realpath(mydir, NULL);
  char *path, *env_path = getenv("PATH");
  if (env_path == NULL) {
    size_t len = confstr(_CS_PATH, 0, 0);
    path = xmalloc(len);
    confstr(_CS_PATH, path, len);
  } else
    path = xstrdup(env_path);
  char *dir;
  char *candir;
  char *file;
  char *savedptr;
  struct stat s;
  int different_name = strcmp(myargv0basename, "install");
  dir = strtok_r(path, ":", &savedptr);
  while (dir) {
    candir = realpath(dir, NULL);
    if (!candir)
      goto skip;
    if ((!different_name) && (!strcmp(mycandir, candir)))
      goto skip;
    file = path_join(candir, "install");
    if (stat(file, &s) == 0)
      if (S_ISREG(s.st_mode)) {
        free(candir);
        free(mycandir);
        free(path);
        return file;
      }
    free(file);
  skip:
    free(candir);
    dir = strtok_r(NULL, ":", &savedptr);
  }
  if (env_path == NULL)
    err(1, "failed to find 'install' in standard utilities path");
  else
    err(1, "failed to find 'install' in PATH=%s", env_path);
}


x86 assembly (clang, -O0):
<which>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <realpath@plt>
mov    %rax,-0x20(%rbp)
lea    0xe18(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    123d <which+0x7d>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10e0 <confstr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   10e0 <confstr@plt>
jmp    124a <which+0x8a>
mov    -0x30(%rbp),%rdi
call   1060 <xstrdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdb0(%rip),%rsi
call   1080 <strcmp@plt>
mov    %eax,-0x64(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xda5(%rip),%rsi
lea    -0x58(%rbp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     135f <which+0x19f>
mov    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <realpath@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12a1 <which+0xe1>
jmp    1339 <which+0x179>
cmpl   $0x0,-0x64(%rbp)
jne    12c6 <which+0x106>
mov    -0x20(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    12c6 <which+0x106>
jmp    1339 <which+0x179>
mov    -0x48(%rbp),%rdi
lea    0xd34(%rip),%rsi
call   1090 <path_join@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
jne    1330 <which+0x170>
mov    -0x60(%rbp),%edi
call   10c0 <S_ISREG@plt>
cmp    $0x0,%rax
je     132b <which+0x16b>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1399 <which+0x1d9>
jmp    1330 <which+0x170>
mov    -0x50(%rbp),%rdi
call   1040 <free@plt>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xcc0(%rip),%rsi
lea    -0x58(%rbp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,-0x40(%rbp)
jmp    1275 <which+0xb5>
cmpq   $0x0,-0x30(%rbp)
jne    1382 <which+0x1c2>
mov    $0x1,%edi
lea    0xc99(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
jmp    1399 <which+0x1d9>
mov    -0x30(%rbp),%rdx
mov    $0x1,%edi
lea    0xcb1(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<which>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r15
xor    %esi,%esi
call   10b0 <realpath@plt>
mov    %rax,0x8(%rsp)
lea    0xe1c(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
mov    %rax,0x18(%rsp)
je     1200 <which+0x40>
mov    %rax,%rdi
call   1060 <xstrdup@plt>
mov    %rax,%rbx
jmp    122f <which+0x6f>
mov    0x2de1(%rip),%r14
mov    (%r14),%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <confstr@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%rbx
mov    (%r14),%edi
mov    %rax,%rsi
mov    %rbp,%rdx
call   10e0 <confstr@plt>
lea    0xdcf(%rip),%rsi
mov    %r15,%rdi
call   1080 <strcmp@plt>
mov    %eax,%ebp
lea    0xdc6(%rip),%rsi
lea    0x20(%rsp),%rdx
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
call   1070 <strtok_r@plt>
test   %rax,%rax
je     1316 <which+0x156>
lea    0xda4(%rip),%r13
lea    0x20(%rsp),%rbx
mov    %rsp,%r14
jmp    12a2 <which+0xe2>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
call   1040 <free@plt>
mov    %r12,%rdi
call   1040 <free@plt>
xor    %edi,%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1070 <strtok_r@plt>
test   %rax,%rax
je     1316 <which+0x156>
mov    %rax,%rdi
xor    %esi,%esi
call   10b0 <realpath@plt>
mov    %rax,%r12
test   %rax,%rax
je     1288 <which+0xc8>
test   %ebp,%ebp
jne    12c9 <which+0x109>
mov    0x8(%rsp),%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     1288 <which+0xc8>
mov    %r12,%rdi
lea    0xd32(%rip),%rsi
call   1090 <path_join@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
call   10a0 <stat@plt>
test   %rax,%rax
jne    1280 <which+0xc0>
mov    (%rsp),%edi
call   10c0 <S_ISREG@plt>
test   %rax,%rax
je     1280 <which+0xc0>
mov    %r12,%rdi
call   1040 <free@plt>
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
jmp    1348 <which+0x188>
mov    0x18(%rsp),%rdx
test   %rdx,%rdx
je     1335 <which+0x175>
lea    0xd1c(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10d0 <err@plt>
jmp    1348 <which+0x188>
lea    0xcd3(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10d0 <err@plt>
mov    %r15,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
@_CS_PATH = global i32 0
@.str.1 = [8 x i8] c"install\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [52 x i8] c"failed to find 'install' in standard utilities path\00"
@.str.4 = [36 x i8] c"failed to find 'install' in PATH=%s\00"
define i8* @which(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca %struct.stat
%15 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%16 = load i8*, i8** %4
%17 = call i8* @realpath(i8* %16, i32* null)
store i8* %17, i8** %6
%18 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %18, i8** %8
%19 = load i8*, i8** %8
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %30
21: ; preds = %2
%22 = load i32, i32* @_CS_PATH
%23 = call i64 @confstr(i32 %22, i8* null, i64 0)
store i64 %23, i64* %9
%24 = load i64, i64* %9
%25 = call i8* @xmalloc(i64 %24)
store i8* %25, i8** %7
%26 = load i32, i32* @_CS_PATH
%27 = load i8*, i8** %7
%28 = load i64, i64* %9
%29 = call i64 @confstr(i32 %26, i8* %27, i64 %28)
br label %33
30: ; preds = %2
%31 = load i8*, i8** %8
%32 = call i8* @xstrdup(i8* %31)
store i8* %32, i8** %7
br label %33
33: ; preds = %30, %21
%34 = load i8*, i8** %5
%35 = call i32 @strcmp(i8* %34, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 %35, i32* %15
%36 = load i8*, i8** %7
%37 = call i8* @strtok_r(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %13)
store i8* %37, i8** %10
br label %38
38: ; preds = %79, %33
%39 = load i8*, i8** %10
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %83
41: ; preds = %38
%42 = load i8*, i8** %10
%43 = call i8* @realpath(i8* %42, i32* null)
store i8* %43, i8** %11
%44 = load i8*, i8** %11
%45 = icmp ne i8* %44, null
br i1 %45, label %47, label %46
46: ; preds = %41
br label %79
47: ; preds = %41
%48 = load i32, i32* %15
%49 = icmp ne i32 %48, 0
br i1 %49, label %56, label %50
50: ; preds = %47
%51 = load i8*, i8** %6
%52 = load i8*, i8** %11
%53 = call i32 @strcmp(i8* %51, i8* %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %56, label %55
55: ; preds = %50
br label %79
56: ; preds = %50, %47
%57 = load i8*, i8** %11
%58 = call i8* @path_join(i8* %57, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %58, i8** %12
%59 = load i8*, i8** %12
%60 = call i64 @stat(i8* %59, %struct.stat* %14)
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %76
62: ; preds = %56
%63 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%64 = load i32, i32* %63
%65 = call i64 @S_ISREG(i32 %64)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %75
67: ; preds = %62
%68 = load i8*, i8** %11
%69 = call i32 @free(i8* %68)
%70 = load i8*, i8** %6
%71 = call i32 @free(i8* %70)
%72 = load i8*, i8** %7
%73 = call i32 @free(i8* %72)
%74 = load i8*, i8** %12
store i8* %74, i8** %3
br label %91
75: ; preds = %62
br label %76
76: ; preds = %75, %56
%77 = load i8*, i8** %12
%78 = call i32 @free(i8* %77)
br label %79
79: ; preds = %76, %55, %46
%80 = load i8*, i8** %11
%81 = call i32 @free(i8* %80)
%82 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %13)
store i8* %82, i8** %10
br label %38
83: ; preds = %38
%84 = load i8*, i8** %8
%85 = icmp eq i8* %84, null
br i1 %85, label %86, label %88
86: ; preds = %83
%87 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0))
br label %91
88: ; preds = %83
%89 = load i8*, i8** %8
%90 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %89)
br label %91
91: ; preds = %67, %88, %86
%92 = load i8*, i8** %3
ret i8* %92
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
@_CS_PATH = global i32 0
@.str.1 = [8 x i8] c"install\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [52 x i8] c"failed to find 'install' in standard utilities path\00"
@.str.4 = [36 x i8] c"failed to find 'install' in PATH=%s\00"
define i8* @which(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = tail call i8* @realpath(i8* %0, i32* null)
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = load i32, i32* @_CS_PATH
%10 = tail call i64 @confstr(i32 %9, i8* null, i64 0)
%11 = tail call i8* @xmalloc(i64 %10)
%12 = load i32, i32* @_CS_PATH
%13 = tail call i64 @confstr(i32 %12, i8* %11, i64 %10)
br label %16
14: ; preds = %2
%15 = tail call i8* @xstrdup(i8* %6)
br label %16
16: ; preds = %14, %8
%17 = phi i8* [ %11, %8 ], [ %15, %14 ]
%18 = bitcast i8** %3 to i8*
%19 = bitcast %struct.stat* %4 to i8*
%20 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%21 = call i8* @strtok_r(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %3)
%22 = icmp eq i8* %21, null
br i1 %22, label %52, label %23
23: ; preds = %16
%24 = icmp eq i32 %20, 0
%25 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %26
26: ; preds = %23, %48
%27 = phi i8* [ %21, %23 ], [ %50, %48 ]
%28 = call i8* @realpath(i8* %27, i32* null)
%29 = icmp eq i8* %28, null
br i1 %29, label %48, label %30
30: ; preds = %26
br i1 %24, label %31, label %34
31: ; preds = %30
%32 = call i32 @strcmp(i8* dereferenceable(1) %5, i8* %28)
%33 = icmp eq i32 %32, 0
br i1 %33, label %48, label %34
34: ; preds = %31, %30
%35 = call i8* @path_join(i8* %28, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%36 = call i64 @stat(i8* %35, %struct.stat* %4)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %34
%39 = load i32, i32* %25
%40 = call i64 @S_ISREG(i32 %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = call i32 @free(i8* %28)
%44 = call i32 @free(i8* %5)
%45 = call i32 @free(i8* %17)
br label %57
46: ; preds = %38, %34
%47 = call i32 @free(i8* %35)
br label %48
48: ; preds = %31, %26, %46
%49 = call i32 @free(i8* %28)
%50 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %3)
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %26
52: ; preds = %48, %16
br i1 %7, label %53, label %55
53: ; preds = %52
%54 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0))
br label %57
55: ; preds = %52
%56 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %57
57: ; preds = %53, %55, %42
%58 = phi i8* [ %35, %42 ], [ undef, %55 ], [ undef, %53 ]
ret i8* %58
}