#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 /*<<< orphan*/ die(char *, ...);
char *getenv(char *);
scalar_t__ is_executable_file(char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
char const *strdup(char const *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strstr(char const *, char *);
const char *which(const char *command) {
  if (*command == 0) {
    die("empty command");
  }
  if (strstr(command, "/")) {
    if (is_executable_file(command)) {
      return strdup(command);
    } else {
      die("not an executable: %s", command);
    }
  }
  char buff[PATH_MAX];
  const int clen = strlen(command);
  const char *path, *end;
  for (path = getenv("PATH"); path; path = end ? (end + 1) : NULL) {
    end = strstr(path, ":");
    int plen = end ? (end - path) : strlen(path);
    if ((plen == 0) || (plen + clen + 2 > PATH_MAX)) {
      continue;
    }
    strncpy(buff, path, plen);
    buff[plen] = '/';
    strcpy(&buff[plen + 1], command);
    if (is_executable_file(buff)) {
      return strdup(buff);
    }
  }
  die("command not found: %s", command);
}


x86 assembly (clang, -O0):
<which>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11aa <which+0x2a>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     1200 <which+0x80>
mov    -0x10(%rbp),%rdi
call   1070 <is_executable_file@plt>
cmp    $0x0,%rax
je     11e9 <which+0x69>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1378 <which+0x1f8>
mov    -0x10(%rbp),%rsi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
jmp    1200 <which+0x80>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x24(%rbp)
lea    0xde7(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1358 <which+0x1d8>
mov    -0x30(%rbp),%rdi
lea    0xdcd(%rip),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1286 <which+0x106>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x50(%rbp)
jmp    1295 <which+0x115>
mov    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
cltq
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     12be <which+0x13e>
mov    -0x3c(%rbp),%eax
add    -0x24(%rbp),%eax
add    $0x2,%eax
mov    0x2d3a(%rip),%rcx
cmp    (%rcx),%eax
jle    12c3 <which+0x143>
jmp    1324 <which+0x1a4>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x48(%rbp),%rdi
movslq -0x3c(%rbp),%rax
movb   $0x2f,(%rdi,%rax,1)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x48(%rbp),%rdi
call   1070 <is_executable_file@plt>
cmp    $0x0,%rax
je     131f <which+0x19f>
mov    -0x48(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1371 <which+0x1f1>
jmp    1324 <which+0x1a4>
cmpq   $0x0,-0x38(%rbp)
je     1340 <which+0x1c0>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
jmp    134b <which+0x1cb>
xor    %eax,%eax
mov    %rax,-0x58(%rbp)
jmp    134b <which+0x1cb>
mov    -0x58(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1248 <which+0xc8>
mov    -0x10(%rbp),%rsi
lea    0xcca(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
movl   $0x0,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<which>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
jne    11a7 <which+0x27>
lea    0xe60(%rip),%rdi
xor    %eax,%eax
call   1090 <die@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     11ed <which+0x6d>
mov    %rbx,%rdi
call   1080 <is_executable_file@plt>
test   %rax,%rax
je     11dc <which+0x5c>
mov    %rbx,%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <strdup@plt>
lea    0xe2b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1090 <die@plt>
mov    %rsp,-0x38(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %rbx,-0x30(%rbp)
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,%r15d
lea    0xe03(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     12d2 <which+0x152>
mov    %rax,%rbx
add    $0x2,%r15d
jmp    124d <which+0xcd>
nopl   0x0(%rax,%rax,1)
lea    0x1(%r14),%rbx
test   %r14,%r14
je     12d2 <which+0x152>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1070 <strchr@plt>
mov    %rax,%r14
test   %rax,%rax
je     1270 <which+0xf0>
mov    %r14,%r13
sub    %rbx,%r13
test   %r13d,%r13d
jne    1280 <which+0x100>
jmp    1240 <which+0xc0>
nop
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,%r13d
test   %r13d,%r13d
je     1240 <which+0xc0>
lea    (%r15,%r13,1),%eax
mov    0x2d65(%rip),%rcx
cmp    (%rcx),%eax
jg     1240 <which+0xc0>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1040 <strncpy@plt>
movslq %r13d,%rax
movb   $0x2f,(%r12,%rax,1)
add    $0x1,%eax
movslq %eax,%rdi
add    %r12,%rdi
mov    -0x30(%rbp),%rsi
call   1050 <strcpy@plt>
mov    %r12,%rdi
call   1080 <is_executable_file@plt>
test   %rax,%rax
je     1240 <which+0xc0>
mov    %r12,%rdi
call   10a0 <strdup@plt>
jmp    12e4 <which+0x164>
lea    0xd50(%rip),%rdi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
call   1090 <die@plt>
mov    -0x38(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"empty command\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [22 x i8] c"not an executable: %s\00"
@PATH_MAX = global i32 0
@.str.3 = [5 x i8] c"PATH\00"
@.str.4 = [2 x i8] c":\00"
@.str.5 = [22 x i8] c"command not found: %s\00"
define i8* @which(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %1
%18 = load i8*, i8** %3
%19 = call i8* @strstr(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %32
21: ; preds = %17
%22 = load i8*, i8** %3
%23 = call i64 @is_executable_file(i8* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i8*, i8** %3
%27 = call i8* @strdup(i8* %26)
store i8* %27, i8** %2
br label %104
28: ; preds = %21
%29 = load i8*, i8** %3
%30 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %28
br label %32
32: ; preds = %31, %17
%33 = load i32, i32* @PATH_MAX
%34 = zext i32 %33 to i64
%35 = call i8* @llvm.stacksave()
store i8* %35, i8** %4
%36 = alloca i8, i64 %34
store i64 %34, i64* %5
%37 = load i8*, i8** %3
%38 = call i32 @strlen(i8* %37)
store i32 %38, i32* %6
%39 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i8* %39, i8** %7
br label %40
40: ; preds = %96, %32
%41 = load i8*, i8** %7
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %98
43: ; preds = %40
%44 = load i8*, i8** %7
%45 = call i8* @strstr(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %54
48: ; preds = %43
%49 = load i8*, i8** %8
%50 = load i8*, i8** %7
%51 = ptrtoint i8* %49 to i64
%52 = ptrtoint i8* %50 to i64
%53 = sub i64 %51, %52
br label %58
54: ; preds = %43
%55 = load i8*, i8** %7
%56 = call i32 @strlen(i8* %55)
%57 = sext i32 %56 to i64
br label %58
58: ; preds = %54, %48
%59 = phi i64 [ %53, %48 ], [ %57, %54 ]
%60 = trunc i64 %59 to i32
store i32 %60, i32* %9
%61 = load i32, i32* %9
%62 = icmp eq i32 %61, 0
br i1 %62, label %70, label %63
63: ; preds = %58
%64 = load i32, i32* %9
%65 = load i32, i32* %6
%66 = add nsw i32 %64, %65
%67 = add nsw i32 %66, 2
%68 = load i32, i32* @PATH_MAX
%69 = icmp sgt i32 %67, %68
br i1 %69, label %70, label %71
70: ; preds = %63, %58
br label %89
71: ; preds = %63
%72 = load i8*, i8** %7
%73 = load i32, i32* %9
%74 = call i32 @strncpy(i8* %36, i8* %72, i32 %73)
%75 = load i32, i32* %9
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %36, i64 %76
store i8 47, i8* %77
%78 = load i32, i32* %9
%79 = add nsw i32 %78, 1
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i8, i8* %36, i64 %80
%82 = load i8*, i8** %3
%83 = call i32 @strcpy(i8* %81, i8* %82)
%84 = call i64 @is_executable_file(i8* %36)
%85 = icmp ne i64 %84, 0
br i1 %85, label %86, label %88
86: ; preds = %71
%87 = call i8* @strdup(i8* %36)
store i8* %87, i8** %2
store i32 1, i32* %10
br label %101
88: ; preds = %71
br label %89
89: ; preds = %88, %70
%90 = load i8*, i8** %8
%91 = icmp ne i8* %90, null
br i1 %91, label %92, label %95
92: ; preds = %89
%93 = load i8*, i8** %8
%94 = getelementptr inbounds i8, i8* %93, i64 1
br label %96
95: ; preds = %89
br label %96
96: ; preds = %95, %92
%97 = phi i8* [ %94, %92 ], [ null, %95 ]
store i8* %97, i8** %7
br label %40
98: ; preds = %40
%99 = load i8*, i8** %3
%100 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %99)
store i32 0, i32* %10
br label %101
101: ; preds = %98, %86
%102 = load i8*, i8** %4
%103 = load i32, i32* %10
switch i32 %103, label %106 [
i32 0, label %104
i32 1, label %104
]
104: ; preds = %25, %101, %101
%105 = load i8*, i8** %2
ret i8* %105
106: ; preds = %101
unreachable
}

LLVM IR (-O2):
@.str = [14 x i8] c"empty command\00"
@.str.2 = [22 x i8] c"not an executable: %s\00"
@PATH_MAX = global i32 0
@.str.3 = [5 x i8] c"PATH\00"
@.str.5 = [22 x i8] c"command not found: %s\00"
define noalias i8* @which(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = tail call i8* @strchr(i8* %0, i32 47)
%8 = icmp eq i8* %7, null
br i1 %8, label %16, label %9
9: ; preds = %6
%10 = tail call i64 @is_executable_file(i8* %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i8* @strdup(i8* %0)
br label %64
14: ; preds = %9
%15 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %16
16: ; preds = %14, %6
%17 = load i32, i32* @PATH_MAX
%18 = zext i32 %17 to i64
%19 = tail call i8* @llvm.stacksave()
%20 = alloca i8, i64 %18
%21 = tail call i32 @strlen(i8* %0)
%22 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i8* %22, null
br i1 %23, label %60, label %24
24: ; preds = %16
%25 = add i32 %21, 2
br label %26
26: ; preds = %24, %58
%27 = phi i8* [ %22, %24 ], [ %59, %58 ]
%28 = call i8* @strchr(i8* %27, i32 58)
%29 = icmp eq i8* %28, null
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = ptrtoint i8* %28 to i64
%32 = ptrtoint i8* %27 to i64
%33 = sub i64 %31, %32
br label %37
34: ; preds = %26
%35 = call i32 @strlen(i8* %27)
%36 = zext i32 %35 to i64
br label %37
37: ; preds = %34, %30
%38 = phi i64 [ %33, %30 ], [ %36, %34 ]
%39 = trunc i64 %38 to i32
%40 = icmp eq i32 %39, 0
br i1 %40, label %58, label %41
41: ; preds = %37
%42 = add i32 %25, %39
%43 = load i32, i32* @PATH_MAX
%44 = icmp sgt i32 %42, %43
br i1 %44, label %58, label %45
45: ; preds = %41
%46 = call i32 @strncpy(i8* %20, i8* %27, i32 %39)
%47 = shl i64 %38, 32
%48 = ashr exact i64 %47, 32
%49 = getelementptr inbounds i8, i8* %20, i64 %48
store i8 47, i8* %49
%50 = add i64 %47, 4294967296
%51 = ashr exact i64 %50, 32
%52 = getelementptr inbounds i8, i8* %20, i64 %51
%53 = call i32 @strcpy(i8* %52, i8* %0)
%54 = call i64 @is_executable_file(i8* %20)
%55 = icmp eq i64 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %45
%57 = call i8* @strdup(i8* %20)
br label %62
58: ; preds = %41, %37, %45
%59 = getelementptr inbounds i8, i8* %28, i64 1
br i1 %29, label %60, label %26
60: ; preds = %58, %16
%61 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %62
62: ; preds = %56, %60
%63 = phi i8* [ %57, %56 ], [ undef, %60 ]
br label %64
64: ; preds = %62, %12
%65 = phi i8* [ %13, %12 ], [ %63, %62 ]
ret i8* %65
}