#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__ access(char *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
char *index(char *, char);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char *argv[]) {
  int quit, found;
  char *envpath;
  char *path, *cp;
  char buf[200];
  char patbuf[512];
  if (argc < 2) {
    fprintf(stderr, "Usage: which cmd [cmd, ..]\n");
    return 1;
  }
  if ((envpath = getenv("PATH")) == 0) {
    envpath = ".";
  }
  argv[argc] = 0;
  for (argv++; *argv; argv++) {
    strcpy(patbuf, envpath);
    cp = path = patbuf;
    quit = found = 0;
    while (!quit) {
      cp = index(path, ':');
      if (cp == NULL) {
        quit++;
      } else {
        *cp = '\0';
      }
      sprintf(buf, "%s/%s", (*path ? path : "."), *argv);
      path = ++cp;
      if (access(buf, 1) == 0) {
        printf("%s\n", buf);
        found++;
      }
    }
    if (!found) {
      printf("No %s in %s\n", *argv, envpath);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x310,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b4 <main+0x44>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1368 <main+0x1f8>
lea    0xe61(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11d9 <main+0x69>
lea    0xe4c(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1361 <main+0x1f1>
lea    -0x300(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x300(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1246 <main+0xd6>
jmp    1328 <main+0x1b8>
mov    -0x28(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <index@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1271 <main+0x101>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1278 <main+0x108>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x100(%rbp),%rax
mov    %rax,-0x308(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12a6 <main+0x136>
mov    -0x28(%rbp),%rax
mov    %rax,-0x310(%rbp)
jmp    12b9 <main+0x149>
lea    0xd74(%rip),%rax
mov    %rax,-0x310(%rbp)
jmp    12b9 <main+0x149>
mov    -0x308(%rbp),%rdi
mov    -0x310(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xd4e(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    %rax,-0x28(%rbp)
lea    -0x100(%rbp),%rdi
mov    $0x1,%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jne    1323 <main+0x1b3>
lea    -0x100(%rbp),%rsi
lea    0xd1f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1230 <main+0xc0>
cmpl   $0x0,-0x18(%rbp)
jne    134b <main+0x1db>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rdx
lea    0xce5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1350 <main+0x1e0>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
jmp    11f5 <main+0x85>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x310,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2e8,%rsp
cmp    $0x1,%edi
jg     11a5 <main+0x35>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%eax
jmp    12ce <main+0x15e>
mov    %rsi,%r13
mov    %edi,%ebx
lea    0xe6b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe61(%rip),%rcx
cmove  %rcx,%rax
mov    %rax,0x8(%rsp)
mov    %ebx,%eax
movq   $0x0,0x0(%r13,%rax,8)
cmpq   $0x0,0x8(%r13)
je     12cc <main+0x15c>
add    $0x8,%r13
lea    0xe39(%rip),%r12
lea    0x10(%rsp),%rbx
jmp    1217 <main+0xa7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   %ebp,%ebp
je     12b0 <main+0x140>
cmpq   $0x0,0x8(%r13)
lea    0x8(%r13),%r13
je     12cc <main+0x15c>
lea    0xe0(%rsp),%r14
mov    %r14,%rdi
mov    0x8(%rsp),%rsi
call   1040 <strcpy@plt>
xor    %ebp,%ebp
jmp    125a <main+0xea>
mov    0x0(%r13),%rcx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <sprintf@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1080 <access@plt>
test   %rax,%rax
je     1290 <main+0x120>
lea    0x1(%r15),%r14
test   %r15,%r15
je     1200 <main+0x90>
mov    %r14,%rdi
mov    $0x3a,%esi
call   1060 <index@plt>
mov    %rax,%r15
test   %rax,%rax
je     1273 <main+0x103>
movb   $0x0,(%r15)
cmpb   $0x0,(%r14)
lea    0xda3(%rip),%rdx
je     1230 <main+0xc0>
mov    %r14,%rdx
jmp    1230 <main+0xc0>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0xd9b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
jmp    1251 <main+0xe1>
cs nopw 0x0(%rax,%rax,1)
mov    0x0(%r13),%rsi
lea    0xd6e(%rip),%rdi
mov    0x8(%rsp),%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1208 <main+0x98>
xor    %eax,%eax
add    $0x2e8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Usage: which cmd [cmd, ..]\0A\00"
@.str.1 = [5 x i8] c"PATH\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [13 x i8] c"No %s in %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [200 x i8]
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %52
11: ; preds = %2
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i8* %12, null
%14 = select i1 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %12
%15 = zext i32 %0 to i64
%16 = getelementptr inbounds i8*, i8** %1, i64 %15
store i8* null, i8** %16
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = icmp eq i8* %18, null
br i1 %19, label %52, label %20
20: ; preds = %11, %48
%21 = phi i8** [ %49, %48 ], [ %17, %11 ]
%22 = call i32 @strcpy(i8* %6, i8* %14)
br label %23
23: ; preds = %20, %41
%24 = phi i8* [ %6, %20 ], [ %35, %41 ]
%25 = phi i32 [ 0, %20 ], [ %42, %41 ]
%26 = call i8* @index(i8* %24, i8 signext 58)
%27 = icmp eq i8* %26, null
br i1 %27, label %29, label %28
28: ; preds = %23
store i8 0, i8* %26
br label %29
29: ; preds = %23, %28
%30 = load i8, i8* %24
%31 = icmp eq i8 %30, 0
%32 = select i1 %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %24
%33 = load i8*, i8** %21
%34 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %32, i8* %33)
%35 = getelementptr inbounds i8, i8* %26, i64 1
%36 = call i64 @access(i8* %5, i32 1)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %29
%39 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %5)
%40 = add nsw i32 %25, 1
br label %41
41: ; preds = %38, %29
%42 = phi i32 [ %40, %38 ], [ %25, %29 ]
br i1 %27, label %43, label %23
43: ; preds = %41
%44 = icmp eq i32 %42, 0
br i1 %44, label %45, label %48
45: ; preds = %43
%46 = load i8*, i8** %21
%47 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %46, i8* %14)
br label %48
48: ; preds = %43, %45
%49 = getelementptr inbounds i8*, i8** %21, i64 1
%50 = load i8*, i8** %49
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %20
52: ; preds = %48, %11, %8
%53 = phi i32 [ 1, %8 ], [ 0, %11 ], [ 0, %48 ]
ret i32 %53
}