#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 */
char *getenv(char *);
scalar_t__ is_exe(char const *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t const);
char *strchr(char const *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strdup(char const *);
char *bcc_procutils_which(const char *binpath) {
  char buffer[4096];
  const char *PATH;
  if (strchr(binpath, '/'))
    return is_exe(binpath) ? strdup(binpath) : 0;
  if (!(PATH = getenv("PATH")))
    return 0;
  while (PATH) {
    const char *next = strchr(PATH, ':') ?: strchr(PATH, '\0');
    const size_t path_len = next - PATH;
    if (path_len) {
      memcpy(buffer, PATH, path_len);
      buffer[path_len] = '/';
      strcpy(buffer + path_len + 1, binpath);
      if (is_exe(buffer))
        return strdup(buffer);
    }
    PATH = *next ? (next + 1) : 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<bcc_procutils_which>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     11cd <bcc_procutils_which+0x6d>
mov    -0x10(%rbp),%rdi
call   1060 <is_exe@plt>
cmp    $0x0,%rax
je     11af <bcc_procutils_which+0x4f>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x1030(%rbp)
jmp    11bd <bcc_procutils_which+0x5d>
xor    %eax,%eax
mov    %rax,-0x1030(%rbp)
jmp    11bd <bcc_procutils_which+0x5d>
mov    -0x1030(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    134f <bcc_procutils_which+0x1ef>
lea    0xe2c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
jne    11f7 <bcc_procutils_which+0x97>
movq   $0x0,-0x8(%rbp)
jmp    134f <bcc_procutils_which+0x1ef>
jmp    11fc <bcc_procutils_which+0x9c>
cmpq   $0x0,-0x1018(%rbp)
je     1347 <bcc_procutils_which+0x1e7>
mov    -0x1018(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x1038(%rbp)
cmp    $0x0,%rax
je     123f <bcc_procutils_which+0xdf>
mov    -0x1038(%rbp),%rax
mov    %rax,-0x1040(%rbp)
jmp    1254 <bcc_procutils_which+0xf4>
mov    -0x1018(%rbp),%rdi
xor    %esi,%esi
call   1050 <strchr@plt>
mov    %rax,-0x1040(%rbp)
mov    -0x1040(%rbp),%rax
mov    %rax,-0x1020(%rbp)
mov    -0x1020(%rbp),%rax
mov    -0x1018(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
je     12fc <bcc_procutils_which+0x19c>
lea    -0x1010(%rbp),%rdi
mov    -0x1018(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x1028(%rbp),%rax
movb   $0x2f,-0x1010(%rbp,%rax,1)
2f
lea    -0x1010(%rbp),%rdi
add    -0x1028(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x1010(%rbp),%rdi
call   1060 <is_exe@plt>
cmp    $0x0,%rax
je     12f7 <bcc_procutils_which+0x197>
lea    -0x1010(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    134f <bcc_procutils_which+0x1ef>
jmp    12fc <bcc_procutils_which+0x19c>
mov    -0x1020(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1326 <bcc_procutils_which+0x1c6>
mov    -0x1020(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1048(%rbp)
jmp    1334 <bcc_procutils_which+0x1d4>
xor    %eax,%eax
mov    %rax,-0x1048(%rbp)
jmp    1334 <bcc_procutils_which+0x1d4>
mov    -0x1048(%rbp),%rax
mov    %rax,-0x1018(%rbp)
jmp    11fc <bcc_procutils_which+0x9c>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bcc_procutils_which>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%r12
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11bd <bcc_procutils_which+0x4d>
mov    %r12,%rdi
call   1070 <is_exe@plt>
test   %rax,%rax
je     1252 <bcc_procutils_which+0xe2>
mov    %r12,%rdi
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <strdup@plt>
lea    0xe3c(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1252 <bcc_procutils_which+0xe2>
mov    %rax,%rbx
xor    %r14d,%r14d
mov    %rsp,%r15
jmp    11eb <bcc_procutils_which+0x7b>
nopl   (%rax)
lea    0x1(%r13),%rbx
cmpb   $0x0,0x0(%r13)
je     1255 <bcc_procutils_which+0xe5>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,%r13
test   %rax,%rax
jne    120e <bcc_procutils_which+0x9e>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%r13
add    %rbx,%r13
mov    %r13,%rbp
sub    %rbx,%rbp
je     11e0 <bcc_procutils_which+0x70>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1080 <memcpy@plt>
movb   $0x2f,(%rsp,%rbp,1)
lea    (%rsp,%rbp,1),%rdi
add    $0x1,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   1070 <is_exe@plt>
test   %rax,%rax
je     11e0 <bcc_procutils_which+0x70>
mov    %rsp,%rdi
call   1090 <strdup@plt>
mov    %rax,%r14
jmp    1255 <bcc_procutils_which+0xe5>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
define noalias i8* @bcc_procutils_which(i8* %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 47)
%5 = icmp eq i8* %4, null
br i1 %5, label %11, label %6
6: ; preds = %1
%7 = tail call i64 @is_exe(i8* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %40, label %9
9: ; preds = %6
%10 = tail call i8* @strdup(i8* %0)
br label %40
11: ; preds = %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 %40, label %14
14: ; preds = %11, %36
%15 = phi i8* [ %39, %36 ], [ %12, %11 ]
%16 = call i8* @strchr(i8* %15, i8 signext 58)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i64 @strlen(i8* %15)
%20 = getelementptr i8, i8* %15, i64 %19
br label %21
21: ; preds = %14, %18
%22 = phi i8* [ %20, %18 ], [ %16, %14 ]
%23 = ptrtoint i8* %22 to i64
%24 = ptrtoint i8* %15 to i64
%25 = sub i64 %23, %24
%26 = icmp eq i64 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %21
%28 = call i32 @memcpy(i8* %3, i8* %15, i64 %25)
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %25
store i8 47, i8* %29
%30 = getelementptr inbounds i8, i8* %29, i64 1
%31 = call i32 @strcpy(i8* %30, i8* %0)
%32 = call i64 @is_exe(i8* %3)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %27
%35 = call i8* @strdup(i8* %3)
br label %40
36: ; preds = %21, %27
%37 = load i8, i8* %22
%38 = icmp eq i8 %37, 0
%39 = getelementptr inbounds i8, i8* %22, i64 1
br i1 %38, label %40, label %14
40: ; preds = %36, %34, %11, %9, %6
%41 = phi i8* [ %10, %9 ], [ null, %6 ], [ null, %11 ], [ %35, %34 ], [ null, %36 ]
ret i8* %41
}