#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*/ snprintf(char *, int, 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;
  static char buf[512];
  static 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';
      }
      snprintf(buf, 512, "%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    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b1 <main+0x41>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135a <main+0x1ea>
lea    0xe64(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11d6 <main+0x66>
lea    0xe4f(%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     1353 <main+0x1e3>
mov    -0x20(%rbp),%rsi
lea    0x3065(%rip),%rdi
call   1040 <strcpy@plt>
lea    0x3059(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0x304e(%rip),%rax
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    124a <main+0xda>
jmp    131a <main+0x1aa>
mov    -0x28(%rbp),%rdi
mov    $0x3a,%esi
call   1070 <index@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1275 <main+0x105>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    127c <main+0x10c>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1299 <main+0x129>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    12a9 <main+0x139>
lea    0xd81(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    12a9 <main+0x139>
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    (%rax),%r8
lea    0x2db5(%rip),%rdi
mov    $0x200,%esi
lea    0xd5c(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    %rax,-0x28(%rbp)
lea    0x2d8d(%rip),%rdi
mov    $0x1,%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jne    1315 <main+0x1a5>
lea    0xd34(%rip),%rdi
lea    0x2d6b(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1234 <main+0xc4>
cmpl   $0x0,-0x18(%rbp)
jne    133d <main+0x1cd>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rdx
lea    0xcf3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1342 <main+0x1d2>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
jmp    11f2 <main+0x82>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     119f <main+0x2f>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%eax
jmp    12bd <main+0x14d>
mov    %rsi,%r12
mov    %edi,%ebx
lea    0xe71(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe67(%rip),%rcx
cmove  %rcx,%rax
mov    %rax,(%rsp)
mov    %ebx,%eax
movq   $0x0,(%r12,%rax,8)
cmpq   $0x0,0x8(%r12)
je     12bb <main+0x14b>
add    $0x8,%r12
lea    0x2e8d(%rip),%rbx
lea    0xe39(%rip),%r13
jmp    1209 <main+0x99>
nopl   0x0(%rax)
test   %ebp,%ebp
je     12a0 <main+0x130>
cmpq   $0x0,0x8(%r12)
lea    0x8(%r12),%r12
je     12bb <main+0x14b>
lea    0x3060(%rip),%r14
mov    %r14,%rdi
mov    (%rsp),%rsi
call   1040 <strcpy@plt>
xor    %ebp,%ebp
jmp    124f <main+0xdf>
mov    (%r12),%r8
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r13,%rdx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1090 <access@plt>
test   %rax,%rax
je     1280 <main+0x110>
lea    0x1(%r15),%r14
test   %r15,%r15
je     11f0 <main+0x80>
mov    %r14,%rdi
mov    $0x3a,%esi
call   1070 <index@plt>
mov    %rax,%r15
test   %rax,%rax
je     1268 <main+0xf8>
movb   $0x0,(%r15)
cmpb   $0x0,(%r14)
lea    0xdae(%rip),%rcx
je     1220 <main+0xb0>
mov    %r14,%rcx
jmp    1220 <main+0xb0>
nopw   0x0(%rax,%rax,1)
lea    0xdab(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
jmp    1246 <main+0xd6>
cs nopw 0x0(%rax,%rax,1)
mov    (%r12),%rsi
lea    0xd7e(%rip),%rdi
mov    (%rsp),%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f8 <main+0x88>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@main.buf = internal global [512 x i8] zeroinitializer
@main.patbuf = internal global [512 x i8] zeroinitializer
@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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %83
16: ; preds = %2
%17 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %17, i8** %8
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %16
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %8
br label %20
20: ; preds = %19, %16
%21 = load i8**, i8*** %5
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8*, i8** %21, i64 %23
store i8* null, i8** %24
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i32 1
store i8** %26, i8*** %5
br label %27
27: ; preds = %79, %20
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %82
31: ; preds = %27
%32 = load i8*, i8** %8
%33 = call i32 @strcpy(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @main.patbuf, i64 0, i64 0), i8* %32)
store i8* getelementptr inbounds ([512 x i8], [512 x i8]* @main.patbuf, i64 0, i64 0), i8** %9
store i8* getelementptr inbounds ([512 x i8], [512 x i8]* @main.patbuf, i64 0, i64 0), i8** %10
store i32 0, i32* %7
store i32 0, i32* %6
br label %34
34: ; preds = %69, %31
%35 = load i32, i32* %6
%36 = icmp ne i32 %35, 0
%37 = xor i1 %36, true
br i1 %37, label %38, label %70
38: ; preds = %34
%39 = load i8*, i8** %9
%40 = call i8* @index(i8* %39, i8 signext 58)
store i8* %40, i8** %10
%41 = load i8*, i8** %10
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %48
46: ; preds = %38
%47 = load i8*, i8** %10
store i8 0, i8* %47
br label %48
48: ; preds = %46, %43
%49 = load i8*, i8** %9
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %48
%54 = load i8*, i8** %9
br label %56
55: ; preds = %48
br label %56
56: ; preds = %55, %53
%57 = phi i8* [ %54, %53 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %55 ]
%58 = load i8**, i8*** %5
%59 = load i8*, i8** %58
%60 = call i32 @snprintf(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @main.buf, i64 0, i64 0), i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %57, i8* %59)
%61 = load i8*, i8** %10
%62 = getelementptr inbounds i8, i8* %61, i32 1
store i8* %62, i8** %10
store i8* %62, i8** %9
%63 = call i64 @access(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @main.buf, i64 0, i64 0), i32 1)
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %56
%66 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([512 x i8], [512 x i8]* @main.buf, i64 0, i64 0))
%67 = load i32, i32* %7
%68 = add nsw i32 %67, 1
store i32 %68, i32* %7
br label %69
69: ; preds = %65, %56
br label %34
70: ; preds = %34
%71 = load i32, i32* %7
%72 = icmp ne i32 %71, 0
br i1 %72, label %78, label %73
73: ; preds = %70
%74 = load i8**, i8*** %5
%75 = load i8*, i8** %74
%76 = load i8*, i8** %8
%77 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %75, i8* %76)
br label %78
78: ; preds = %73, %70
br label %79
79: ; preds = %78
%80 = load i8**, i8*** %5
%81 = getelementptr inbounds i8*, i8** %80, i32 1
store i8** %81, i8*** %5
br label %27
82: ; preds = %27
store i32 0, i32* %3
br label %83
83: ; preds = %82, %13
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
@main.buf = internal global [512 x i8] zeroinitializer
@main.patbuf = internal global [512 x i8] zeroinitializer
@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 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %48
7: ; preds = %2
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i8* %8, null
%10 = select i1 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %8
%11 = zext i32 %0 to i64
%12 = getelementptr inbounds i8*, i8** %1, i64 %11
store i8* null, i8** %12
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = icmp eq i8* %14, null
br i1 %15, label %48, label %16
16: ; preds = %7, %44
%17 = phi i8** [ %45, %44 ], [ %13, %7 ]
%18 = tail call i32 @strcpy(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @main.patbuf, i64 0, i64 0), i8* %10)
br label %19
19: ; preds = %16, %37
%20 = phi i8* [ getelementptr inbounds ([512 x i8], [512 x i8]* @main.patbuf, i64 0, i64 0), %16 ], [ %31, %37 ]
%21 = phi i32 [ 0, %16 ], [ %38, %37 ]
%22 = tail call i8* @index(i8* %20, i8 signext 58)
%23 = icmp eq i8* %22, null
br i1 %23, label %25, label %24
24: ; preds = %19
store i8 0, i8* %22
br label %25
25: ; preds = %19, %24
%26 = load i8, i8* %20
%27 = icmp eq i8 %26, 0
%28 = select i1 %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %20
%29 = load i8*, i8** %17
%30 = tail call i32 @snprintf(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @main.buf, i64 0, i64 0), i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %28, i8* %29)
%31 = getelementptr inbounds i8, i8* %22, i64 1
%32 = tail call i64 @access(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @main.buf, i64 0, i64 0), i32 1)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([512 x i8], [512 x i8]* @main.buf, i64 0, i64 0))
%36 = add nsw i32 %21, 1
br label %37
37: ; preds = %34, %25
%38 = phi i32 [ %36, %34 ], [ %21, %25 ]
br i1 %23, label %39, label %19
39: ; preds = %37
%40 = icmp eq i32 %38, 0
br i1 %40, label %41, label %44
41: ; preds = %39
%42 = load i8*, i8** %17
%43 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %42, i8* %10)
br label %44
44: ; preds = %39, %41
%45 = getelementptr inbounds i8*, i8** %17, i64 1
%46 = load i8*, i8** %45
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %16
48: ; preds = %44, %7, %4
%49 = phi i32 [ 1, %4 ], [ 0, %7 ], [ 0, %44 ]
ret i32 %49
}