#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 /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ chdir(char *);
int errno;
int /*<<< orphan*/ free(char **);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
char **realloc(char **, int);
int strlen(char *);
char *strtok(char *, char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc == 1) {
    write(2, "which: which programename\n", 28);
    return 1;
  }
  char *path = getenv("PATH");
  char **subpath = NULL;
  unsigned int count = 0;
  path = strtok(path, ":");
  while (path) {
    if ((subpath = realloc(subpath, sizeof(char *) * ++count)) == NULL) {
      perror("which: realloc");
      return errno;
    }
    subpath[count - 1] = path;
    path = strtok(NULL, ":");
  }
  int i, j;
  for (i = 1; i < argc; i++) {
    for (j = 0; j < count; j++) {
      if (chdir(subpath[j]) == 0) {
        if (access(argv[i], X_OK) == 0) {
          write(1, subpath[j], strlen(subpath[j]));
          write(1, "/", 1);
          write(1, argv[i], strlen(argv[i]));
          write(1, "\n", 1);
          break;
        }
      }
    }
  }
  free(subpath);
  return 1;
}


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   $0x1,-0x8(%rbp)
jne    11d2 <main+0x42>
mov    $0x2,%edi
lea    0xe44(%rip),%rsi
mov    $0x1c,%edx
call   1050 <write@plt>
movl   $0x1,-0x4(%rbp)
jmp    13af <main+0x21f>
lea    0xe42(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1282 <main+0xf2>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    %eax,%eax
shl    $0x3,%rax
mov    %eax,%esi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1255 <main+0xc5>
lea    0xde3(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13af <main+0x21f>
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    %rdx,(%rax,%rcx,8)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdac(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x18(%rbp)
jmp    1205 <main+0x75>
movl   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    139f <main+0x20f>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jae    138c <main+0x1fc>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1070 <chdir@plt>
cmp    $0x0,%rax
jne    1379 <main+0x1e9>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jne    1374 <main+0x1e4>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <strlen@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%edi
call   1050 <write@plt>
mov    $0x1,%edx
lea    0xd0d(%rip),%rsi
mov    %edx,%edi
call   1050 <write@plt>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <strlen@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%edi
call   1050 <write@plt>
mov    $0x1,%edx
lea    0xcb1(%rip),%rsi
mov    %edx,%edi
call   1050 <write@plt>
jmp    138c <main+0x1fc>
jmp    1379 <main+0x1e9>
jmp    137e <main+0x1ee>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    129c <main+0x10c>
jmp    1391 <main+0x201>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1289 <main+0xf9>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movl   $0x1,-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
jne    11bb <main+0x2b>
lea    0xe59(%rip),%rsi
mov    $0x2,%edi
mov    $0x1c,%edx
call   1050 <write@plt>
jmp    131d <main+0x18d>
mov    %rsi,%r14
mov    %edi,(%rsp)
lea    0xe53(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe4c(%rip),%rsi
mov    %rax,%rdi
call   10b0 <strtok@plt>
test   %rax,%rax
je     1238 <main+0xa8>
mov    %rax,%r12
xor    %r15d,%r15d
mov    $0x8,%ebp
lea    0xe2d(%rip),%r13
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <realloc@plt>
test   %rax,%rax
je     1331 <main+0x1a1>
mov    %rax,%rbx
mov    %r15d,%eax
mov    %r12,(%rbx,%rax,8)
xor    %edi,%edi
mov    %r13,%rsi
call   10b0 <strtok@plt>
mov    %rax,%r12
add    $0x8,%ebp
add    $0x1,%r15d
test   %rax,%rax
jne    1200 <main+0x70>
jmp    123d <main+0xad>
xor    %ebx,%ebx
xor    %r15d,%r15d
mov    (%rsp),%eax
cmp    $0x2,%eax
jl     1315 <main+0x185>
mov    %eax,%eax
mov    %rax,(%rsp)
mov    %r15d,%r12d
mov    $0x1,%r13d
jmp    126e <main+0xde>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r13
cmp    (%rsp),%r13
je     1315 <main+0x185>
test   %r15d,%r15d
je     1260 <main+0xd0>
xor    %ebp,%ebp
jmp    1289 <main+0xf9>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    %rbp,%r12
je     1260 <main+0xd0>
mov    (%rbx,%rbp,8),%rdi
call   1070 <chdir@plt>
test   %rax,%rax
jne    1280 <main+0xf0>
mov    (%r14,%r13,8),%rdi
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
test   %rax,%rax
jne    1280 <main+0xf0>
mov    (%rbx,%rbp,8),%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    $0x1,%edi
lea    0xd5c(%rip),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    (%r14,%r13,8),%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    $0x1,%edi
lea    0xd13(%rip),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
jmp    1260 <main+0xd0>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcea(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c94(%rip),%rax
mov    (%rax),%eax
jmp    1322 <main+0x192>

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

LLVM IR (-O2):
@.str = [27 x i8] c"which: which programename\0A\00"
@.str.1 = [5 x i8] c"PATH\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [15 x i8] c"which: realloc\00"
@errno = global i32 0
@X_OK = global i32 0
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 28)
br label %68
6: ; preds = %2
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i8* @strtok(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %18
10: ; preds = %29, %6
%11 = phi i8** [ null, %6 ], [ %24, %29 ]
%12 = phi i32 [ 0, %6 ], [ %22, %29 ]
%13 = icmp sgt i32 %0, 1
br i1 %13, label %14, label %66
14: ; preds = %10
%15 = icmp eq i32 %12, 0
%16 = zext i32 %0 to i64
%17 = zext i32 %12 to i64
br label %34
18: ; preds = %6, %29
%19 = phi i32 [ %22, %29 ], [ 0, %6 ]
%20 = phi i8** [ %24, %29 ], [ null, %6 ]
%21 = phi i8* [ %32, %29 ], [ %8, %6 ]
%22 = add i32 %19, 1
%23 = shl i32 %22, 3
%24 = tail call i8** @realloc(i8** %20, i32 %23)
%25 = icmp eq i8** %24, null
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%28 = load i32, i32* @errno
br label %68
29: ; preds = %18
%30 = zext i32 %19 to i64
%31 = getelementptr inbounds i8*, i8** %24, i64 %30
store i8* %21, i8** %31
%32 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp eq i8* %32, null
br i1 %33, label %10, label %18
34: ; preds = %14, %63
%35 = phi i64 [ 1, %14 ], [ %64, %63 ]
br i1 %15, label %63, label %36
36: ; preds = %34
%37 = getelementptr inbounds i8*, i8** %1, i64 %35
br label %38
38: ; preds = %36, %60
%39 = phi i64 [ 0, %36 ], [ %61, %60 ]
%40 = getelementptr inbounds i8*, i8** %11, i64 %39
%41 = load i8*, i8** %40
%42 = tail call i64 @chdir(i8* %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %60
44: ; preds = %38
%45 = load i8*, i8** %37
%46 = load i32, i32* @X_OK
%47 = tail call i64 @access(i8* %45, i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %60
49: ; preds = %44
%50 = and i64 %39, 4294967295
%51 = getelementptr inbounds i8*, i8** %11, i64 %50
%52 = load i8*, i8** %51
%53 = tail call i32 @strlen(i8* %52)
%54 = tail call i32 @write(i32 1, i8* %52, i32 %53)
%55 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
%56 = load i8*, i8** %37
%57 = tail call i32 @strlen(i8* %56)
%58 = tail call i32 @write(i32 1, i8* %56, i32 %57)
%59 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
br label %63
60: ; preds = %38, %44
%61 = add nuw nsw i64 %39, 1
%62 = icmp eq i64 %61, %17
br i1 %62, label %63, label %38
63: ; preds = %60, %34, %49
%64 = add nuw nsw i64 %35, 1
%65 = icmp eq i64 %64, %16
br i1 %65, label %66, label %34
66: ; preds = %63, %10
%67 = tail call i32 @free(i8** %11)
br label %68
68: ; preds = %26, %66, %4
%69 = phi i32 [ 1, %4 ], [ %28, %26 ], [ 1, %66 ]
ret i32 %69
}