#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *get_path(char *, char *);
char *getenv(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ sprintlen(char *, char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  char *pathenv = getenv("PATH");
  if (!pathenv) {
    fprintf(stderr, "which: no path defined\n");
    return 1;
  }
  int i, fail = 0;
  for (i = 1; i < argc; i++) {
    char *path = get_path(pathenv, argv[i]);
    if (!path) {
      path = malloc(sprintlen("%s not found in (%s)", argv[i], pathenv) + 1);
      sprintf(path, "%s not found in (%s)", argv[i], pathenv);
      fail = 1;
    }
    puts(path);
    free(path);
  }
  return fail;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d2 <main+0x52>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1289 <main+0x109>
movl   $0x0,-0x20(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1283 <main+0x103>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1090 <get_path@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1263 <main+0xe3>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdf6(%rip),%rdi
call   1070 <sprintlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdc6(%rip),%rsi
call   10a0 <sprintf@plt>
movl   $0x1,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <puts@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e0 <main+0x60>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe69(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1239 <main+0xb9>
xor    %r12d,%r12d
cmp    $0x2,%ebp
jl     1254 <main+0xd4>
mov    %rax,%rbx
mov    %ebp,%r13d
xor    %r12d,%r12d
mov    $0x1,%r15d
jmp    11e9 <main+0x69>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
call   1050 <puts@plt>
mov    %rbp,%rdi
call   1040 <free@plt>
add    $0x1,%r15
cmp    %r15,%r13
je     1254 <main+0xd4>
mov    (%r14,%r15,8),%rsi
mov    %rbx,%rdi
call   1090 <get_path@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11d0 <main+0x50>
mov    (%r14,%r15,8),%rsi
lea    0xe15(%rip),%r12
mov    %r12,%rdi
mov    %rbx,%rdx
call   1070 <sprintlen@plt>
lea    0x1(%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,%rbp
mov    (%r14,%r15,8),%rdx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %rbx,%rcx
call   10a0 <sprintf@plt>
mov    $0x1,%r12d
jmp    11d0 <main+0x50>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"which: no path defined\0A\00"
@.str.2 = [21 x i8] c"%s not found in (%s)\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %59
16: ; preds = %2
store i32 0, i32* %8
store i32 1, i32* %7
br label %17
17: ; preds = %54, %16
%18 = load i32, i32* %7
%19 = load i32, i32* %4
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %57
21: ; preds = %17
%22 = load i8*, i8** %6
%23 = load i8**, i8*** %5
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8*, i8** %23, i64 %25
%27 = load i8*, i8** %26
%28 = call i8* @get_path(i8* %22, i8* %27)
store i8* %28, i8** %9
%29 = load i8*, i8** %9
%30 = icmp ne i8* %29, null
br i1 %30, label %49, label %31
31: ; preds = %21
%32 = load i8**, i8*** %5
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %32, i64 %34
%36 = load i8*, i8** %35
%37 = load i8*, i8** %6
%38 = call i64 @sprintlen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %36, i8* %37)
%39 = add nsw i64 %38, 1
%40 = call i8* @malloc(i64 %39)
store i8* %40, i8** %9
%41 = load i8*, i8** %9
%42 = load i8**, i8*** %5
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8*, i8** %42, i64 %44
%46 = load i8*, i8** %45
%47 = load i8*, i8** %6
%48 = call i32 @sprintf(i8* %41, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %46, i8* %47)
store i32 1, i32* %8
br label %49
49: ; preds = %31, %21
%50 = load i8*, i8** %9
%51 = call i32 @puts(i8* %50)
%52 = load i8*, i8** %9
%53 = call i32 @free(i8* %52)
br label %54
54: ; preds = %49
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %17
57: ; preds = %17
%58 = load i32, i32* %8
store i32 %58, i32* %3
br label %59
59: ; preds = %57, %13
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"which: no path defined\0A\00"
@.str.2 = [21 x i8] c"%s not found in (%s)\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = icmp sgt i32 %0, 1
br i1 %6, label %7, label %33
7: ; preds = %5
%8 = zext i32 %0 to i64
br label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %33
12: ; preds = %7, %26
%13 = phi i64 [ 1, %7 ], [ %31, %26 ]
%14 = phi i32 [ 0, %7 ], [ %27, %26 ]
%15 = getelementptr inbounds i8*, i8** %1, i64 %13
%16 = load i8*, i8** %15
%17 = tail call i8* @get_path(i8* %3, i8* %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %26
19: ; preds = %12
%20 = load i8*, i8** %15
%21 = tail call i64 @sprintlen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %20, i8* %3)
%22 = add nsw i64 %21, 1
%23 = tail call i8* @malloc(i64 %22)
%24 = load i8*, i8** %15
%25 = tail call i32 @sprintf(i8* dereferenceable(1) %23, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %24, i8* %3)
br label %26
26: ; preds = %19, %12
%27 = phi i32 [ %14, %12 ], [ 1, %19 ]
%28 = phi i8* [ %17, %12 ], [ %23, %19 ]
%29 = tail call i32 @puts(i8* dereferenceable(1) %28)
%30 = tail call i32 @free(i8* %28)
%31 = add nuw nsw i64 %13, 1
%32 = icmp eq i64 %31, %8
br i1 %32, label %33, label %12
33: ; preds = %26, %5, %9
%34 = phi i32 [ 1, %9 ], [ 0, %5 ], [ %27, %26 ]
ret i32 %34
}