#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 *, int /*<<< orphan*/ *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcmp(char *, char *);
char *strtok_r(char *, char *, char **);
char *xmalloc(size_t);
char *xstrdup(char *);
char *which(char *mypath, char *portage_helper_path) {
  char *path = NULL, *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 *canpath;
  char *savedptr;
  dir = strtok_r(path, ":", &savedptr);
  while (dir) {
    char *canfile = path_join(dir, "install");
    canpath = realpath(canfile, NULL);
    free(canfile);
    if (!canpath)
      goto skip;
    if (!strcmp(mypath, canpath))
      goto skip;
    if (portage_helper_path)
      if (!strcmp(portage_helper_path, canpath))
        goto skip;
    struct stat s;
    if (stat(canpath, &s) == 0)
      if (S_ISREG(s.st_mode)) {
        free(path);
        return canpath;
      }
  skip:
    free(canpath);
    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    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe21(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1234 <which+0x74>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10e0 <confstr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   10e0 <confstr@plt>
jmp    1241 <which+0x81>
mov    -0x28(%rbp),%rdi
call   1060 <xstrdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdb9(%rip),%rsi
lea    -0x48(%rbp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1357 <which+0x197>
mov    -0x38(%rbp),%rdi
lea    0xd98(%rip),%rsi
call   1090 <path_join@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <realpath@plt>
mov    %rax,-0x40(%rbp)
mov    -0x50(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x40(%rbp)
jne    12a2 <which+0xe2>
jmp    1331 <which+0x171>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    12bd <which+0xfd>
jmp    1331 <which+0x171>
cmpq   $0x0,-0x18(%rbp)
je     12e8 <which+0x128>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    12e3 <which+0x123>
jmp    1331 <which+0x171>
jmp    12e8 <which+0x128>
mov    -0x40(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
jne    132c <which+0x16c>
mov    -0x58(%rbp),%edi
call   10c0 <S_ISREG@plt>
cmp    $0x0,%rax
je     1327 <which+0x167>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1391 <which+0x1d1>
jmp    132c <which+0x16c>
jmp    1331 <which+0x171>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xcc0(%rip),%rsi
lea    -0x48(%rbp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,-0x38(%rbp)
jmp    1259 <which+0x99>
cmpq   $0x0,-0x28(%rbp)
jne    137a <which+0x1ba>
mov    $0x1,%edi
lea    0xca1(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
jmp    1391 <which+0x1d1>
mov    -0x28(%rbp),%rdx
mov    $0x1,%edi
lea    0xcb9(%rip),%rsi
mov    $0x0,%al
call   10d0 <err@plt>
mov    -0x8(%rbp),%rax
add    $0x60,%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,%r13
mov    %rdi,0x18(%rsp)
lea    0xe23(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
mov    %rax,0x10(%rsp)
je     11f9 <which+0x39>
mov    %rax,%rdi
call   1060 <xstrdup@plt>
mov    %rax,%rbp
jmp    1228 <which+0x68>
mov    0x2de8(%rip),%r14
mov    (%r14),%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <confstr@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%rbp
mov    (%r14),%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   10e0 <confstr@plt>
lea    0xdd6(%rip),%rsi
lea    0x20(%rsp),%rdx
mov    %rbp,0x8(%rsp)
mov    %rbp,%rdi
call   1070 <strtok_r@plt>
test   %rax,%rax
je     12f4 <which+0x134>
lea    0xdb6(%rip),%rbx
lea    0xdad(%rip),%rbp
lea    0x20(%rsp),%r12
jmp    127a <which+0xba>
nop
mov    %r14,%rdi
call   1040 <free@plt>
xor    %edi,%edi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1070 <strtok_r@plt>
test   %rax,%rax
je     12f4 <which+0x134>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1090 <path_join@plt>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
call   10b0 <realpath@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1040 <free@plt>
test   %r14,%r14
je     1260 <which+0xa0>
mov    0x18(%rsp),%rdi
mov    %r14,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     1260 <which+0xa0>
test   %r13,%r13
je     12c7 <which+0x107>
mov    %r13,%rdi
mov    %r14,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     1260 <which+0xa0>
mov    %r14,%rdi
mov    %rsp,%rsi
call   10a0 <stat@plt>
test   %rax,%rax
jne    1260 <which+0xa0>
mov    (%rsp),%edi
call   10c0 <S_ISREG@plt>
test   %rax,%rax
je     1260 <which+0xa0>
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
jmp    1326 <which+0x166>
mov    0x10(%rsp),%rdx
test   %rdx,%rdx
je     1313 <which+0x153>
lea    0xd3e(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10d0 <err@plt>
jmp    1326 <which+0x166>
lea    0xcf5(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10d0 <err@plt>
mov    %r14,%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 = [2 x i8] c":\00"
@.str.2 = [8 x i8] c"install\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 i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
%14 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %26
17: ; preds = %2
%18 = load i32, i32* @_CS_PATH
%19 = call i64 @confstr(i32 %18, i8* null, i64 0)
store i64 %19, i64* %8
%20 = load i64, i64* %8
%21 = call i8* @xmalloc(i64 %20)
store i8* %21, i8** %6
%22 = load i32, i32* @_CS_PATH
%23 = load i8*, i8** %6
%24 = load i64, i64* %8
%25 = call i64 @confstr(i32 %22, i8* %23, i64 %24)
br label %29
26: ; preds = %2
%27 = load i8*, i8** %7
%28 = call i8* @xstrdup(i8* %27)
store i8* %28, i8** %6
br label %29
29: ; preds = %26, %17
%30 = load i8*, i8** %6
%31 = call i8* @strtok_r(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %11)
store i8* %31, i8** %9
br label %32
32: ; preds = %76, %29
%33 = load i8*, i8** %9
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %80
35: ; preds = %32
%36 = load i8*, i8** %9
%37 = call i8* @path_join(i8* %36, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i8* %37, i8** %12
%38 = load i8*, i8** %12
%39 = call i8* @realpath(i8* %38, i32* null)
store i8* %39, i8** %10
%40 = load i8*, i8** %12
%41 = call i32 @free(i8* %40)
%42 = load i8*, i8** %10
%43 = icmp ne i8* %42, null
br i1 %43, label %45, label %44
44: ; preds = %35
br label %76
45: ; preds = %35
%46 = load i8*, i8** %4
%47 = load i8*, i8** %10
%48 = call i32 @strcmp(i8* %46, i8* %47)
%49 = icmp ne i32 %48, 0
br i1 %49, label %51, label %50
50: ; preds = %45
br label %76
51: ; preds = %45
%52 = load i8*, i8** %5
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %61
54: ; preds = %51
%55 = load i8*, i8** %5
%56 = load i8*, i8** %10
%57 = call i32 @strcmp(i8* %55, i8* %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %60, label %59
59: ; preds = %54
br label %76
60: ; preds = %54
br label %61
61: ; preds = %60, %51
%62 = load i8*, i8** %10
%63 = call i64 @stat(i8* %62, %struct.stat* %13)
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %75
65: ; preds = %61
%66 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i64 @S_ISREG(i32 %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %65
%71 = load i8*, i8** %6
%72 = call i32 @free(i8* %71)
%73 = load i8*, i8** %10
store i8* %73, i8** %3
br label %88
74: ; preds = %65
br label %75
75: ; preds = %74, %61
br label %76
76: ; preds = %75, %59, %50, %44
%77 = load i8*, i8** %10
%78 = call i32 @free(i8* %77)
%79 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %11)
store i8* %79, i8** %9
br label %32
80: ; preds = %32
%81 = load i8*, i8** %7
%82 = icmp eq i8* %81, null
br i1 %82, label %83, label %85
83: ; preds = %80
%84 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0))
br label %88
85: ; preds = %80
%86 = load i8*, i8** %7
%87 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %86)
br label %88
88: ; preds = %70, %85, %83
%89 = load i8*, i8** %3
ret i8* %89
}

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