#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 */
char *FN_make_path(char *, char const *);
char *FN_normalize(char *);
int /*<<< orphan*/ R_OK;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *strdup(char *);
char *strtok(char *, char *);
char *posix_search_in_path(const char *file) {
  char *env, *path, *dir, *test, *ret;
  env = getenv("PATH");
  if (!env)
    return 0;
  path = strdup(env);
  for (dir = strtok(path, ":"); dir != 0; dir = strtok(0, ":")) {
    test = FN_make_path(dir, file);
    if (test) {
      if (!access(test, R_OK)) {
        ret = FN_normalize(test);
        free(test);
        return ret;
      }
      free(test);
    }
  }
  free(path);
  return 0;
}


x86 assembly (clang, -O0):
<posix_search_in_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe7d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a4 <posix_search_in_path+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1262 <posix_search_in_path+0xf2>
mov    -0x18(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1251 <posix_search_in_path+0xe1>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <FN_make_path@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1233 <posix_search_in_path+0xc3>
mov    -0x30(%rbp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%eax
jne    122a <posix_search_in_path+0xba>
mov    -0x30(%rbp),%rdi
call   1050 <FN_normalize@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1262 <posix_search_in_path+0xf2>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
jmp    1238 <posix_search_in_path+0xc8>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdc2(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    11c5 <posix_search_in_path+0x55>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<posix_search_in_path>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r12
lea    0xe7d(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1204 <posix_search_in_path+0x94>
mov    %rax,%rdi
call   1090 <strdup@plt>
mov    %rax,%r14
lea    0xe66(%rip),%rsi
mov    %rax,%rdi
call   1070 <strtok@plt>
test   %rax,%rax
je     11fc <posix_search_in_path+0x8c>
lea    0xe52(%rip),%r15
mov    0x2e26(%rip),%r13
jmp    11cf <posix_search_in_path+0x5f>
nopl   0x0(%rax)
xor    %edi,%edi
mov    %r15,%rsi
call   1070 <strtok@plt>
test   %rax,%rax
je     11fc <posix_search_in_path+0x8c>
mov    %rax,%rdi
mov    %r12,%rsi
call   1080 <FN_make_path@plt>
test   %rax,%rax
je     11c0 <posix_search_in_path+0x50>
mov    %rax,%rbx
mov    0x0(%r13),%esi
mov    %rax,%rdi
call   1060 <access@plt>
test   %eax,%eax
je     1210 <posix_search_in_path+0xa0>
mov    %rbx,%rdi
call   1040 <free@plt>
jmp    11c0 <posix_search_in_path+0x50>
mov    %r14,%rdi
call   1040 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1050 <FN_normalize@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r14,%rax
jmp    1206 <posix_search_in_path+0x96>

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

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@R_OK = global i32 0
define i8* @posix_search_in_path(i8* %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %26, label %4
4: ; preds = %1
%5 = tail call i8* @strdup(i8* %2)
%6 = tail call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %24, label %8
8: ; preds = %4, %21
%9 = phi i8* [ %22, %21 ], [ %6, %4 ]
%10 = tail call i8* @FN_make_path(i8* %9, i8* %0)
%11 = icmp eq i8* %10, null
br i1 %11, label %21, label %12
12: ; preds = %8
%13 = load i32, i32* @R_OK
%14 = tail call i32 @access(i8* %10, i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = tail call i8* @FN_normalize(i8* %10)
%18 = tail call i32 @free(i8* %10)
br label %26
19: ; preds = %12
%20 = tail call i32 @free(i8* %10)
br label %21
21: ; preds = %8, %19
%22 = tail 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 %8
24: ; preds = %21, %4
%25 = tail call i32 @free(i8* %5)
br label %26
26: ; preds = %1, %24, %16
%27 = phi i8* [ %17, %16 ], [ null, %24 ], [ null, %1 ]
ret i8* %27
}