#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*/ free(char *);
char *getenv(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *strtok(char *, char *);
int fs_util_file_exists_in_path(char *filePath) {
  struct stat buff;
  char *tok, *local_pathStr;
  char testPath[1024];
  int result = -1;
  char *pathStr = getenv("PATH");
  local_pathStr = malloc(strlen(pathStr) + 1);
  strcpy(local_pathStr, pathStr);
  if (stat(filePath, &buff) >= 0) {
    free(local_pathStr);
    return 0;
  }
  tok = strtok(local_pathStr, ":");
  while (tok != NULL) {
    sprintf(testPath, "%s/%s", tok, filePath);
    if (stat(testPath, &buff) >= 0) {
      result = 0;
      break;
    }
    tok = strtok(NULL, ":");
  }
  free(local_pathStr);
  return result;
}


x86 assembly (clang, -O0):
<fs_util_file_exists_in_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x434(%rbp)
ff ff ff
lea    0xe60(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x440(%rbp)
mov    -0x440(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x440(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jl     1204 <fs_util_file_exists_in_path+0x84>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1292 <fs_util_file_exists_in_path+0x112>
mov    -0x28(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1280 <fs_util_file_exists_in_path+0x100>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdce(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x430(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jl     1267 <fs_util_file_exists_in_path+0xe7>
movl   $0x0,-0x434(%rbp)
jmp    1280 <fs_util_file_exists_in_path+0x100>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd93(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    1218 <fs_util_file_exists_in_path+0x98>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x434(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_util_file_exists_in_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%rbx
lea    0xe65(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
call   1050 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
xor    %ebp,%ebp
test   %rax,%rax
js     11f2 <fs_util_file_exists_in_path+0x72>
mov    %r14,%rdi
call   1040 <free@plt>
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0c(%rip),%rsi
mov    %r14,%rdi
call   1090 <strtok@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11d6 <fs_util_file_exists_in_path+0x56>
lea    0xdf5(%rip),%r12
lea    0x10(%rsp),%rbp
lea    0x8(%rsp),%r13
lea    0xde2(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   10a0 <sprintf@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1070 <stat@plt>
test   %rax,%rax
jns    126a <fs_util_file_exists_in_path+0xea>
xor    %edi,%edi
mov    %r15,%rsi
call   1090 <strtok@plt>
test   %rax,%rax
jne    1230 <fs_util_file_exists_in_path+0xb0>
mov    $0xffffffff,%ebp
jmp    11d6 <fs_util_file_exists_in_path+0x56>
xor    %ebp,%ebp
jmp    11d6 <fs_util_file_exists_in_path+0x56>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @fs_util_file_exists_in_path(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %3
store i32 -1, i32* %8
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %9
%11 = load i8*, i8** %9
%12 = call i64 @strlen(i8* %11)
%13 = add nsw i64 %12, 1
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = load i8*, i8** %9
%17 = call i32 @strcpy(i8* %15, i8* %16)
%18 = load i8*, i8** %3
%19 = call i64 @stat(i8* %18, %struct.stat* %4)
%20 = icmp sge i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %1
%22 = load i8*, i8** %6
%23 = call i32 @free(i8* %22)
store i32 0, i32* %2
br label %45
24: ; preds = %1
%25 = load i8*, i8** %6
%26 = call i8* @strtok(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %26, i8** %5
br label %27
27: ; preds = %39, %24
%28 = load i8*, i8** %5
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %41
30: ; preds = %27
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%32 = load i8*, i8** %5
%33 = load i8*, i8** %3
%34 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %32, i8* %33)
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%36 = call i64 @stat(i8* %35, %struct.stat* %4)
%37 = icmp sge i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %30
store i32 0, i32* %8
br label %41
39: ; preds = %30
%40 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %40, i8** %5
br label %27
41: ; preds = %38, %27
%42 = load i8*, i8** %6
%43 = call i32 @free(i8* %42)
%44 = load i32, i32* %8
store i32 %44, i32* %2
br label %45
45: ; preds = %41, %21
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @fs_util_file_exists_in_path(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca [1024 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = tail call i64 @strlen(i8* dereferenceable(1) %6)
%8 = add nsw i64 %7, 1
%9 = tail call i8* @malloc(i64 %8)
%10 = tail call i32 @strcpy(i8* %9, i8* %6)
%11 = call i64 @stat(i8* %0, %struct.stat* %2)
%12 = icmp sgt i64 %11, -1
br i1 %12, label %24, label %13
13: ; preds = %1
%14 = call i8* @strtok(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %24, label %16
16: ; preds = %13, %21
%17 = phi i8* [ %22, %21 ], [ %14, %13 ]
%18 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %17, i8* %0)
%19 = call i64 @stat(i8* %5, %struct.stat* %2)
%20 = icmp sgt i64 %19, -1
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %16
24: ; preds = %16, %21, %13, %1
%25 = phi i32 [ 0, %1 ], [ -1, %13 ], [ 0, %16 ], [ -1, %21 ]
%26 = call i32 @free(i8* %9)
ret i32 %25
}