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


x86 assembly (clang, -O0):
<locate_binary>:
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_binary+0x5a>
lea    0xe3b(%rip),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12ed <locate_binary+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     128d <locate_binary+0xfd>
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     1274 <locate_binary+0xe4>
lea    -0x430(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x28(%rbp)
jmp    128d <locate_binary+0xfd>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd86(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    1216 <locate_binary+0x86>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
lea    0xd6a(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     12c3 <locate_binary+0x133>
mov    -0x28(%rbp),%rdx
lea    0xd56(%rip),%rdi
lea    0xd5a(%rip),%rsi
call   1060 <printf@plt>
jmp    12c8 <locate_binary+0x138>
cmpq   $0x0,-0x28(%rbp)
jne    12e5 <locate_binary+0x155>
mov    -0x10(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12ed <locate_binary+0x15d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<locate_binary>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r12
mov    %rsp,%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1080 <memset@plt>
test   %r12,%r12
je     1237 <locate_binary+0xa7>
lea    0xe41(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   10b0 <strdup@plt>
mov    %rax,%r15
lea    0xe2f(%rip),%rsi
mov    %rax,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
je     1254 <locate_binary+0xc4>
mov    %rsp,%rbx
mov    0x2dfb(%rip),%rbp
lea    0xe11(%rip),%r13
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <strcpy@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <join_path@plt>
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
jne    1259 <locate_binary+0xc9>
xor    %edi,%edi
mov    %r13,%rsi
call   10a0 <strtok@plt>
test   %rax,%rax
jne    1200 <locate_binary+0x70>
jmp    1264 <locate_binary+0xd4>
lea    0xdd9(%rip),%rdi
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <strdup@plt>
xor    %r14d,%r14d
jmp    1264 <locate_binary+0xd4>
mov    %rsp,%rdi
call   10b0 <strdup@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1040 <free@plt>
lea    0xd94(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1293 <locate_binary+0x103>
lea    0xd89(%rip),%rdi
lea    0xd8d(%rip),%rsi
mov    %r14,%rdx
call   1060 <printf@plt>
test   %r14,%r14
jne    12a3 <locate_binary+0x113>
mov    %r12,%rdi
call   10b0 <strdup@plt>
mov    %rax,%r14
mov    %r14,%rax
add    $0x408,%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 = [16 x i8] c"[locate_binary]\00"
define i8* @locate_binary(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 %55
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 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%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 ([16 x i8], [16 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 %53
50: ; preds = %47
%51 = load i8*, i8** %3
%52 = call i8* @strdup(i8* %51)
store i8* %52, i8** %2
br label %55
53: ; preds = %47
%54 = load i8*, i8** %6
store i8* %54, i8** %2
br label %55
55: ; preds = %53, %50, %11
%56 = load i8*, i8** %2
ret i8* %56
}

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 = [16 x i8] c"[locate_binary]\00"
define i8* @locate_binary(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* %3)
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 ([16 x i8], [16 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 i8* @strdup(i8* %0)
br label %35
35: ; preds = %31, %33, %5
%36 = phi i8* [ %6, %5 ], [ %34, %33 ], [ %25, %31 ]
ret i8* %36
}