#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*/ free(char *);
char *getenv(char *);
scalar_t__ is_there(char *);
char *malloc(size_t);
int snprintf(char *, size_t, char *, char const *, char *);
int /*<<< orphan*/ *strchr(char *, char);
char *strdup(char const *);
int strlen(char *);
char *strsep(char **, char *);
char *which(char *filename) {
  size_t max_candidate;
  char *candidate, *path, *spath;
  const char *d, *p;
  if (strchr(filename, '/') != NULL && is_there(filename))
    return (strdup(filename));
  if ((p = getenv("PATH")) == NULL)
    return (NULL);
  spath = path = strdup(p);
  if (path == NULL)
    return (NULL);
  max_candidate = strlen(path) + strlen(filename) + 2;
  if ((candidate = malloc(max_candidate)) == NULL) {
    free(spath);
    return NULL;
  }
  while ((d = strsep(&path, ":")) != NULL) {
    if (*d == '\0')
      d = ".";
    if (snprintf(candidate, max_candidate, "%s/%s", d, filename) >=
        (int)max_candidate)
      continue;
    if (is_there(candidate)) {
      free(spath);
      return (candidate);
    }
  }
  free(path);
  free(candidate);
  return (NULL);
}


x86 assembly (clang, -O0):
<which>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
cmp    $0x0,%rax
je     11d9 <which+0x49>
mov    -0x10(%rbp),%rdi
call   1050 <is_there@plt>
cmp    $0x0,%rax
je     11d9 <which+0x49>
mov    -0x10(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    132e <which+0x19e>
lea    0xe20(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    1200 <which+0x70>
movq   $0x0,-0x8(%rbp)
jmp    132e <which+0x19e>
mov    -0x40(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1229 <which+0x99>
movq   $0x0,-0x8(%rbp)
jmp    132e <which+0x19e>
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    127b <which+0xeb>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    132e <which+0x19e>
jmp    1280 <which+0xf0>
lea    -0x28(%rbp),%rdi
lea    0xd7a(%rip),%rsi
call   10a0 <strsep@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1314 <which+0x184>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12b9 <which+0x129>
lea    0xd52(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xd39(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
jl     12e6 <which+0x156>
jmp    1280 <which+0xf0>
mov    -0x20(%rbp),%rdi
call   1050 <is_there@plt>
cmp    $0x0,%rax
je     130f <which+0x17f>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    132e <which+0x19e>
jmp    1280 <which+0xf0>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<which>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    $0x2f,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     11d3 <which+0x43>
mov    %r14,%rdi
call   1050 <is_there@plt>
test   %rax,%rax
je     11d3 <which+0x43>
mov    %r14,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <strdup@plt>
lea    0xe26(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     12c6 <which+0x136>
mov    %rax,%rdi
call   10b0 <strdup@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     12c6 <which+0x136>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1060 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
movslq %eax,%rbx
mov    %rbx,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     12be <which+0x12e>
mov    %rax,%r12
mov    %rbp,0x10(%rsp)
lea    0xdca(%rip),%rsi
lea    0x8(%rsp),%rdi
call   10a0 <strsep@plt>
test   %rax,%rax
je     12af <which+0x11f>
lea    0xdb8(%rip),%r13
lea    0xdad(%rip),%rbp
lea    0x8(%rsp),%r15
jmp    1270 <which+0xe0>
nop
mov    %r15,%rdi
mov    %rbp,%rsi
call   10a0 <strsep@plt>
test   %rax,%rax
je     12af <which+0x11f>
cmpb   $0x0,(%rax)
lea    0xd8d(%rip),%rcx
cmove  %rcx,%rax
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
mov    %rax,%rcx
mov    %r14,%r8
call   1080 <snprintf@plt>
cmp    %ebx,%eax
jge    1260 <which+0xd0>
mov    %r12,%rdi
call   1050 <is_there@plt>
test   %rax,%rax
je     1260 <which+0xd0>
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
jmp    12c9 <which+0x139>
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
mov    %r12,%rdi
jmp    12c1 <which+0x131>
mov    %rbp,%rdi
call   1040 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [6 x i8] c"%s/%s\00"
define i8* @which(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = call i32* @strchr(i8* %10, i8 signext 47)
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %20
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i64 @is_there(i8* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i8*, i8** %3
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** %2
br label %78
20: ; preds = %13, %1
%21 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %21, i8** %9
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %20
store i8* null, i8** %2
br label %78
24: ; preds = %20
%25 = load i8*, i8** %9
%26 = call i8* @strdup(i8* %25)
store i8* %26, i8** %6
store i8* %26, i8** %7
%27 = load i8*, i8** %6
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %24
store i8* null, i8** %2
br label %78
30: ; preds = %24
%31 = load i8*, i8** %6
%32 = call i32 @strlen(i8* %31)
%33 = load i8*, i8** %3
%34 = call i32 @strlen(i8* %33)
%35 = add nsw i32 %32, %34
%36 = add nsw i32 %35, 2
%37 = sext i32 %36 to i64
store i64 %37, i64* %4
%38 = load i64, i64* %4
%39 = call i8* @malloc(i64 %38)
store i8* %39, i8** %5
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %44
41: ; preds = %30
%42 = load i8*, i8** %7
%43 = call i32 @free(i8* %42)
store i8* null, i8** %2
br label %78
44: ; preds = %30
br label %45
45: ; preds = %72, %63, %44
%46 = call i8* @strsep(i8** %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %46, i8** %8
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %73
48: ; preds = %45
%49 = load i8*, i8** %8
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %48
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %8
br label %54
54: ; preds = %53, %48
%55 = load i8*, i8** %5
%56 = load i64, i64* %4
%57 = load i8*, i8** %8
%58 = load i8*, i8** %3
%59 = call i32 @snprintf(i8* %55, i64 %56, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %57, i8* %58)
%60 = load i64, i64* %4
%61 = trunc i64 %60 to i32
%62 = icmp sge i32 %59, %61
br i1 %62, label %63, label %64
63: ; preds = %54
br label %45
64: ; preds = %54
%65 = load i8*, i8** %5
%66 = call i64 @is_there(i8* %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %72
68: ; preds = %64
%69 = load i8*, i8** %7
%70 = call i32 @free(i8* %69)
%71 = load i8*, i8** %5
store i8* %71, i8** %2
br label %78
72: ; preds = %64
br label %45
73: ; preds = %45
%74 = load i8*, i8** %6
%75 = call i32 @free(i8* %74)
%76 = load i8*, i8** %5
%77 = call i32 @free(i8* %76)
store i8* null, i8** %2
br label %78
78: ; preds = %73, %68, %41, %29, %23, %17
%79 = load i8*, i8** %2
ret i8* %79
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [6 x i8] c"%s/%s\00"
define i8* @which(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = tail call i32* @strchr(i8* %0, i8 signext 47)
%5 = icmp eq i32* %4, null
br i1 %5, label %11, label %6
6: ; preds = %1
%7 = tail call i64 @is_there(i8* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i8* @strdup(i8* %0)
br label %49
11: ; preds = %6, %1
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %49, label %14
14: ; preds = %11
%15 = tail call i8* @strdup(i8* %12)
store i8* %15, i8** %2
%16 = icmp eq i8* %15, null
br i1 %16, label %49, label %17
17: ; preds = %14
%18 = tail call i32 @strlen(i8* %15)
%19 = tail call i32 @strlen(i8* %0)
%20 = add i32 %18, 2
%21 = add i32 %20, %19
%22 = sext i32 %21 to i64
%23 = tail call i8* @malloc(i64 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %28, label %25
25: ; preds = %17
%26 = call i8* @strsep(i8** %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp eq i8* %26, null
br i1 %27, label %45, label %30
28: ; preds = %17
%29 = tail call i32 @free(i8* %15)
br label %49
30: ; preds = %25, %37
%31 = phi i8* [ %38, %37 ], [ %26, %25 ]
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 0
%34 = select i1 %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %31
%35 = call i32 @snprintf(i8* %23, i64 %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %34, i8* %0)
%36 = icmp slt i32 %35, %21
br i1 %36, label %40, label %37
37: ; preds = %30, %40
%38 = call i8* @strsep(i8** %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%39 = icmp eq i8* %38, null
br i1 %39, label %45, label %30
40: ; preds = %30
%41 = call i64 @is_there(i8* %23)
%42 = icmp eq i64 %41, 0
br i1 %42, label %37, label %43
43: ; preds = %40
%44 = call i32 @free(i8* %15)
br label %49
45: ; preds = %37, %25
%46 = load i8*, i8** %2
%47 = call i32 @free(i8* %46)
%48 = call i32 @free(i8* %23)
br label %49
49: ; preds = %14, %11, %45, %43, %28, %9
%50 = phi i8* [ %10, %9 ], [ null, %28 ], [ %23, %43 ], [ null, %45 ], [ null, %11 ], [ null, %14 ]
ret i8* %50
}