#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strcspn(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ *strstr(char *, char *);
char *whereis(char *path, char *prog) {
  static char file[200];
  int i = 0, pathlen;
  char *_begin;
  struct stat status;
  _begin = path;
  pathlen = strlen(path);
  if (strstr(prog, "/") != NULL)
    return prog;
  memset(file, '\0', 200);
  while (access(file, X_OK) != 0 && path - _begin < pathlen + 2) {
    i = strcspn(path, ":");
    strcpy(file, "");
    strncat(file, path, i);
    strcat(file, "/");
    strcat(file, prog);
    path = path + i + 1;
  }
  if (!(path - _begin < pathlen + 2))
    return NULL;
  else {
    ;
    return file;
  }
}


x86 assembly (clang, -O0):
<whereis>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     11d2 <whereis+0x52>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12e2 <whereis+0x162>
lea    0x2e97(%rip),%rdi
xor    %esi,%esi
mov    $0xc8,%edx
call   1050 <memset@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0x2e7b(%rip),%rdi
call   1080 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x31(%rbp)
je     1229 <whereis+0xa9>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    -0x20(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
setl   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1239 <whereis+0xb9>
jmp    12ad <whereis+0x12d>
mov    -0x10(%rbp),%rdi
lea    0xdbe(%rip),%rsi
call   1070 <strcspn@plt>
mov    %eax,-0x1c(%rbp)
lea    0x2e1d(%rip),%rdi
lea    0xda7(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0x2e03(%rip),%rdi
call   1060 <strncat@plt>
lea    0x2df7(%rip),%rdi
lea    0xd80(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rsi
lea    0x2de0(%rip),%rdi
call   1090 <strcat@plt>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11e5 <whereis+0x65>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    -0x20(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jl     12d7 <whereis+0x157>
movq   $0x0,-0x8(%rbp)
jmp    12e2 <whereis+0x162>
lea    0x2d92(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<whereis>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
jne    127d <whereis+0xfd>
lea    0x2ebb(%rip),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc8,%edx
call   1060 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <access@plt>
add    $0x2,%ebx
movslq %ebx,%r13
test   %rax,%rax
je     126d <whereis+0xed>
lea    0x2e85(%rip),%rbp
mov    %r14,%rbx
xchg   %ax,%ax
mov    %rbx,%rax
sub    %r14,%rax
cmp    %r13,%rax
jge    1264 <whereis+0xe4>
mov    %rbx,%rdi
lea    0xdfd(%rip),%rsi
call   1080 <strcspn@plt>
mov    %eax,%r15d
mov    %rbp,%rdi
lea    0xdea(%rip),%rsi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1070 <strncat@plt>
mov    %rbp,%rdi
lea    0xdcc(%rip),%rsi
call   10a0 <strcat@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
call   10a0 <strcat@plt>
movslq %r15d,%rax
add    %rax,%rbx
add    $0x1,%rbx
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <access@plt>
test   %rax,%rax
jne    11f0 <whereis+0x70>
lea    0x2e05(%rip),%rbp
jmp    1270 <whereis+0xf0>
mov    %r14,%rbx
sub    %r14,%rbx
xor    %r12d,%r12d
cmp    %r13,%rbx
cmovl  %rbp,%r12
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@whereis.file = internal global [200 x i8] zeroinitializer
@.str = [2 x i8] c"/\00"
@X_OK = global i32 0
@.str.1 = [2 x i8] c":\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @whereis(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
%10 = load i8*, i8** %4
store i8* %10, i8** %8
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %7
%13 = load i8*, i8** %5
%14 = call i32* @strstr(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i8*, i8** %5
store i8* %17, i8** %3
br label %63
18: ; preds = %2
%19 = call i32 @memset(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i8 signext 0, i32 200)
br label %20
20: ; preds = %36, %18
%21 = load i32, i32* @X_OK
%22 = call i64 @access(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %20
%25 = load i8*, i8** %4
%26 = load i8*, i8** %8
%27 = ptrtoint i8* %25 to i64
%28 = ptrtoint i8* %26 to i64
%29 = sub i64 %27, %28
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 2
%32 = sext i32 %31 to i64
%33 = icmp slt i64 %29, %32
br label %34
34: ; preds = %24, %20
%35 = phi i1 [ false, %20 ], [ %33, %24 ]
br i1 %35, label %36, label %51
36: ; preds = %34
%37 = load i8*, i8** %4
%38 = call i32 @strcspn(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 %38, i32* %6
%39 = call i32 @strcpy(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%40 = load i8*, i8** %4
%41 = load i32, i32* %6
%42 = call i32 @strncat(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i8* %40, i32 %41)
%43 = call i32 @strcat(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%44 = load i8*, i8** %5
%45 = call i32 @strcat(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i8* %44)
%46 = load i8*, i8** %4
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = getelementptr inbounds i8, i8* %49, i64 1
store i8* %50, i8** %4
br label %20
51: ; preds = %34
%52 = load i8*, i8** %4
%53 = load i8*, i8** %8
%54 = ptrtoint i8* %52 to i64
%55 = ptrtoint i8* %53 to i64
%56 = sub i64 %54, %55
%57 = load i32, i32* %7
%58 = add nsw i32 %57, 2
%59 = sext i32 %58 to i64
%60 = icmp slt i64 %56, %59
br i1 %60, label %62, label %61
61: ; preds = %51
store i8* null, i8** %3
br label %63
62: ; preds = %51
store i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i8** %3
br label %63
63: ; preds = %62, %61, %16
%64 = load i8*, i8** %3
ret i8* %64
}

LLVM IR (-O2):
@whereis.file = internal global [200 x i8] zeroinitializer
@.str = [2 x i8] c"/\00"
@X_OK = global i32 0
@.str.1 = [2 x i8] c":\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @whereis(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i8* @strchr(i8* dereferenceable(1) %1, i32 47)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %40
6: ; preds = %2
%7 = tail call i32 @memset(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i8 signext 0, i32 200)
%8 = load i32, i32* @X_OK
%9 = tail call i64 @access(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i32 %8)
%10 = icmp eq i64 %9, 0
%11 = ptrtoint i8* %0 to i64
%12 = add nsw i32 %3, 2
%13 = sext i32 %12 to i64
br i1 %10, label %35, label %14
14: ; preds = %6, %19
%15 = phi i8* [ %27, %19 ], [ %0, %6 ]
%16 = ptrtoint i8* %15 to i64
%17 = sub i64 %16, %11
%18 = icmp slt i64 %17, %13
br i1 %18, label %19, label %33
19: ; preds = %14
%20 = tail call i32 @strcspn(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @strcpy(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%22 = tail call i32 @strncat(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i8* %15, i32 %20)
%23 = tail call i32 @strcat(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = tail call i32 @strcat(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i8* %1)
%25 = sext i32 %20 to i64
%26 = getelementptr inbounds i8, i8* %15, i64 1
%27 = getelementptr inbounds i8, i8* %26, i64 %25
%28 = load i32, i32* @X_OK
%29 = tail call i64 @access(i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %14
31: ; preds = %19
%32 = ptrtoint i8* %27 to i64
br label %35
33: ; preds = %14
%34 = ptrtoint i8* %15 to i64
br label %35
35: ; preds = %6, %33, %31
%36 = phi i64 [ %32, %31 ], [ %34, %33 ], [ %11, %6 ]
%37 = sub i64 %36, %11
%38 = icmp slt i64 %37, %13
%39 = select i1 %38, i8* getelementptr inbounds ([200 x i8], [200 x i8]* @whereis.file, i64 0, i64 0), i8* null
br label %40
40: ; preds = %35, %2
%41 = phi i8* [ %1, %2 ], [ %39, %35 ]
ret i8* %41
}