#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*/ X_OK;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ join_path(char *, char const *);
int /*<<< orphan*/ printf(char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
char *strtok(char *, char *);
char *locate_path(const char *bname) {
  char *sys_path = NULL;
  char *pch = NULL;
  char *out_path = NULL;
  char full_path[1024] = {};
  if (bname == NULL)
    return strdup("");
  sys_path = strdup(getenv("PATH"));
  pch = strtok(sys_path, ":");
  while (pch != NULL) {
    strcpy(full_path, pch);
    join_path(full_path, bname);
    if (access(full_path, X_OK) != -1) {
      out_path = strdup(pch);
      break;
    }
    pch = strtok(NULL, ":");
  }
  free(sys_path);
  do {
    if (getenv("DEBUG") != NULL)
      printf("%-30s'%s'\n", "[locate_path]", out_path);
  } while (0);
  if (out_path == NULL)
    return strdup("");
  return out_path;
}


x86 assembly (clang, -O0):
<locate_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11ea <locate_path+0x5a>
lea    0xe3b(%rip),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12ed <locate_path+0x15d>
lea    0xe0f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdf8(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     128a <locate_path+0xfa>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strcpy@plt>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <join_path@plt>
lea    -0x430(%rbp),%rdi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
je     1271 <locate_path+0xe1>
mov    -0x20(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x28(%rbp)
jmp    128a <locate_path+0xfa>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd89(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    1216 <locate_path+0x86>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
lea    0xd6d(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     12c0 <locate_path+0x130>
mov    -0x28(%rbp),%rdx
lea    0xd59(%rip),%rdi
lea    0xd5d(%rip),%rsi
call   1060 <printf@plt>
jmp    12c5 <locate_path+0x135>
cmpq   $0x0,-0x28(%rbp)
jne    12e5 <locate_path+0x155>
lea    0xd40(%rip),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12ed <locate_path+0x15d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<locate_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r12
lea    0x10(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1080 <memset@plt>
test   %r12,%r12
je     124a <locate_path+0xba>
lea    0xe3b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   10b0 <strdup@plt>
mov    %rax,%rbp
lea    0xe29(%rip),%rsi
mov    %rax,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
mov    %rbp,0x8(%rsp)
je     1267 <locate_path+0xd7>
mov    %rax,%rbx
lea    0x10(%rsp),%r13
mov    0x2deb(%rip),%r15
lea    0xe01(%rip),%rbp
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
call   1050 <strcpy@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1070 <join_path@plt>
mov    (%r15),%esi
mov    %r13,%rdi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
jne    126c <locate_path+0xdc>
xor    %edi,%edi
mov    %rbp,%rsi
call   10a0 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1210 <locate_path+0x80>
jmp    1277 <locate_path+0xe7>
lea    0xdc6(%rip),%rdi
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <strdup@plt>
xor    %r14d,%r14d
jmp    1277 <locate_path+0xe7>
mov    %rbx,%rdi
call   10b0 <strdup@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
lea    0xd7f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     12a8 <locate_path+0x118>
lea    0xd74(%rip),%rdi
lea    0xd78(%rip),%rsi
mov    %r14,%rdx
call   1060 <printf@plt>
test   %r14,%r14
jne    12bc <locate_path+0x12c>
lea    0xd63(%rip),%rdi
call   10b0 <strdup@plt>
mov    %rax,%r14
mov    %r14,%rax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [5 x i8] c"PATH\00"
@.str.2 = [2 x i8] c":\00"
@X_OK = global i32 0
@.str.3 = [6 x i8] c"DEBUG\00"
@.str.4 = [11 x i8] c"%-30s'%s'\0A\00"
@.str.5 = [14 x i8] c"[locate_path]\00"
define i8* @locate_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [1024 x i8]
store i8* %0, i8** %3
store i8* null, i8** %4
store i8* null, i8** %5
store i8* null, i8** %6
%8 = bitcast [1024 x i8]* %7 to i8*
%9 = load i8*, i8** %3
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %2
br label %54
13: ; preds = %1
%14 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = call i8* @strdup(i8* %14)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = call i8* @strtok(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %17, i8** %5
br label %18
18: ; preds = %35, %13
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %37
21: ; preds = %18
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%23 = load i8*, i8** %5
%24 = call i32 @strcpy(i8* %22, i8* %23)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%26 = load i8*, i8** %3
%27 = call i32 @join_path(i8* %25, i8* %26)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%29 = load i32, i32* @X_OK
%30 = call i32 @access(i8* %28, i32 %29)
%31 = icmp ne i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %21
%33 = load i8*, i8** %5
%34 = call i8* @strdup(i8* %33)
store i8* %34, i8** %6
br label %37
35: ; preds = %21
%36 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %36, i8** %5
br label %18
37: ; preds = %32, %18
%38 = load i8*, i8** %4
%39 = call i32 @free(i8* %38)
br label %40
40: ; preds = %37
%41 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i8*, i8** %6
%45 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %43, %40
br label %47
47: ; preds = %46
%48 = load i8*, i8** %6
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %51, i8** %2
br label %54
52: ; preds = %47
%53 = load i8*, i8** %6
store i8* %53, i8** %2
br label %54
54: ; preds = %52, %50, %11
%55 = load i8*, i8** %2
ret i8* %55
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [5 x i8] c"PATH\00"
@.str.2 = [2 x i8] c":\00"
@X_OK = global i32 0
@.str.3 = [6 x i8] c"DEBUG\00"
@.str.4 = [11 x i8] c"%-30s'%s'\0A\00"
@.str.5 = [14 x i8] c"[locate_path]\00"
define i8* @locate_path(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %35
7: ; preds = %1
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i8* @strdup(i8* %8)
%10 = tail call i8* @strtok(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %24, label %12
12: ; preds = %7, %21
%13 = phi i8* [ %22, %21 ], [ %10, %7 ]
%14 = call i32 @strcpy(i8* %3, i8* %13)
%15 = call i32 @join_path(i8* %3, i8* %0)
%16 = load i32, i32* @X_OK
%17 = call i32 @access(i8* %3, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %21, label %19
19: ; preds = %12
%20 = call i8* @strdup(i8* %13)
br label %24
21: ; preds = %12
%22 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %12
24: ; preds = %21, %7, %19
%25 = phi i8* [ %20, %19 ], [ null, %7 ], [ null, %21 ]
%26 = call i32 @free(i8* %9)
%27 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i8* %27, null
br i1 %28, label %31, label %29
29: ; preds = %24
%30 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %25)
br label %31
31: ; preds = %29, %24
%32 = icmp eq i8* %25, null
br i1 %32, label %33, label %35
33: ; preds = %31
%34 = call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %35
35: ; preds = %31, %33, %5
%36 = phi i8* [ %6, %5 ], [ %34, %33 ], [ %25, %31 ]
ret i8* %36
}