#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 */
typedef int /*<<< orphan*/ fullpath;

/* Variables and functions */
int /*<<< orphan*/ X_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *path_val(char *const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *);
int /*<<< orphan*/ stderr;
char *strdup(char const *);
scalar_t__ strlen(char *);
char *strtok_r(char *, char *, char **);
char *pfind(const char *name, char *const envp[]) {
  char *tok;
  char *sp;
  char *path;
  char fullpath[1024 + 1];
  if (name == NULL) {
    fprintf(stderr, "pfind(): Null argument.\n");
    return NULL;
  }
  if (name[0] == '/' || name[0] == '.') {
    if (access(name, X_OK) == 0) {
      return strdup(name);
    }
    return NULL;
  }
  path = path_val(envp);
  if (path == NULL || strlen(path) <= 0) {
    fprintf(stderr, "Unable to get $PATH.\n");
    return NULL;
  }
  path = strdup(path);
  tok = strtok_r(path, ":", &sp);
  while (tok != NULL) {
    snprintf(fullpath, sizeof(fullpath) - 1, "%s/%s", tok, name);
    if (access(fullpath, X_OK) == 0) {
      free(path);
      return strdup(fullpath);
    }
    tok = strtok_r(NULL, ":", &sp);
  }
  free(path);
  return NULL;
}


x86 assembly (clang, -O0):
<pfind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c0 <pfind+0x40>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1323 <pfind+0x1a3>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11e0 <pfind+0x60>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    121b <pfind+0x9b>
mov    -0x10(%rbp),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jne    120e <pfind+0x8e>
mov    -0x10(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1323 <pfind+0x1a3>
movq   $0x0,-0x8(%rbp)
jmp    1323 <pfind+0x1a3>
mov    -0x18(%rbp),%rdi
call   1080 <path_val@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1246 <pfind+0xc6>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jg     1268 <pfind+0xe8>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1323 <pfind+0x1a3>
mov    -0x30(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xdaf(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1312 <pfind+0x192>
lea    -0x440(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x400,%esi
lea    0xd7e(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x440(%rbp),%rdi
mov    0x2d22(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jne    12f5 <pfind+0x175>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
lea    -0x440(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1323 <pfind+0x1a3>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd2f(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x20(%rbp)
jmp    128d <pfind+0x10d>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pfind>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
test   %rdi,%rdi
je     11d4 <pfind+0x54>
mov    %rdi,%rbx
mov    (%rdi),%al
and    $0xfe,%al
cmp    $0x2e,%al
jne    11e9 <pfind+0x69>
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <access@plt>
test   %rax,%rax
jne    12b8 <pfind+0x138>
mov    %rbx,%rdi
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <strdup@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
jmp    12b3 <pfind+0x133>
mov    %rsi,%rdi
call   1080 <path_val@plt>
test   %rax,%rax
je     12a3 <pfind+0x123>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
jle    12a3 <pfind+0x123>
mov    %rbp,%rdi
call   10a0 <strdup@plt>
lea    0xe12(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,(%rsp)
mov    %rax,%rdi
call   1060 <strtok_r@plt>
test   %rax,%rax
je     1298 <pfind+0x118>
lea    0xdf7(%rip),%r15
lea    0x10(%rsp),%r12
mov    0x2da2(%rip),%r14
lea    0xde2(%rip),%r13
lea    0x8(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    %rax,%rcx
mov    %rbx,%r8
call   1050 <snprintf@plt>
mov    (%r14),%esi
mov    %r12,%rdi
call   1090 <access@plt>
test   %rax,%rax
je     12cc <pfind+0x14c>
xor    %edi,%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1060 <strtok_r@plt>
test   %rax,%rax
jne    1260 <pfind+0xe0>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    12b8 <pfind+0x138>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
lea    0xd66(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rdi
call   1030 <free@plt>
lea    0x10(%rsp),%rdi
call   10a0 <strdup@plt>
jmp    12ba <pfind+0x13a>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"pfind(): Null argument.\0A\00"
@X_OK = global i32 0
@.str.1 = [22 x i8] c"Unable to get $PATH.\0A\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [6 x i8] c"%s/%s\00"
define noalias i8* @pfind(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca [1025 x i8]
%5 = bitcast i8** %3 to i8*
%6 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%7 = icmp eq i8* %0, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %48
11: ; preds = %2
%12 = load i8, i8* %0
%13 = and i8 %12, -2
%14 = icmp eq i8 %13, 46
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = load i32, i32* @X_OK
%17 = tail call i64 @access(i8* %0, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %48
19: ; preds = %15
%20 = tail call i8* @strdup(i8* %0)
br label %48
21: ; preds = %11
%22 = tail call i8* @path_val(i8** %1)
%23 = icmp eq i8* %22, null
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = tail call i64 @strlen(i8* %22)
%26 = icmp slt i64 %25, 1
br i1 %26, label %27, label %30
27: ; preds = %24, %21
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %48
30: ; preds = %24
%31 = tail call i8* @strdup(i8* %22)
%32 = call i8* @strtok_r(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %3)
%33 = icmp eq i8* %32, null
br i1 %33, label %46, label %34
34: ; preds = %30, %43
%35 = phi i8* [ %44, %43 ], [ %32, %30 ]
%36 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %35, i8* %0)
%37 = load i32, i32* @X_OK
%38 = call i64 @access(i8* %6, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %34
%41 = call i32 @free(i8* %31)
%42 = call i8* @strdup(i8* %6)
br label %48
43: ; preds = %34
%44 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %3)
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %34
46: ; preds = %43, %30
%47 = call i32 @free(i8* %31)
br label %48
48: ; preds = %15, %46, %40, %27, %19, %8
%49 = phi i8* [ null, %8 ], [ %20, %19 ], [ null, %27 ], [ %42, %40 ], [ null, %46 ], [ null, %15 ]
ret i8* %49
}