#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*/ F_OK;
int PATH_MAX;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
scalar_t__ strchr(char const *, char);
char *strdup(char const *);
size_t strlen(char const *);
char *path_exists(const char *name) {
  const char *path, *cursor;
  size_t count;
  size_t namelen;
  static char *buf = NULL;
  if (buf == NULL) {
    free(buf);
    buf = malloc(PATH_MAX * sizeof(char));
  }
  if (name == NULL || name[0] == '\0') {
    return NULL;
  }
  if (strchr(name, '/') && access(name, F_OK) == 0) {
    return strdup(name);
  }
  namelen = strlen(name);
  path = "/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/opt/local/bin";
  cursor = path;
  do {
    for (path = cursor; *cursor != 0 && *cursor != ':'; cursor++)
      continue;
    if (cursor == path) {
      path = ".";
      count = 1;
    } else {
      count = cursor - path;
    }
    memcpy(buf, path, count);
    buf[count] = '/';
    memcpy(buf + count + 1, name, namelen);
    buf[count + namelen + 1] = '\0';
    if (access(buf, F_OK) == 0) {
      return buf;
    }
  } while (*cursor++ == ':');
  return NULL;
}


x86 assembly (clang, -O0):
<path_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,0x2edc(%rip)
jne    11b2 <path_exists+0x42>
mov    0x2ecf(%rip),%rdi
call   1030 <free@plt>
mov    0x2e53(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,0x2eae(%rip)
cmpq   $0x0,-0x10(%rbp)
je     11cd <path_exists+0x5d>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11da <path_exists+0x6a>
movq   $0x0,-0x8(%rbp)
jmp    1372 <path_exists+0x202>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     1220 <path_exists+0xb0>
mov    -0x10(%rbp),%rdi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jne    1220 <path_exists+0xb0>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1372 <path_exists+0x202>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
lea    0xdcc(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x31(%rbp)
je     126d <path_exists+0xfd>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    127d <path_exists+0x10d>
jmp    1293 <path_exists+0x123>
jmp    1282 <path_exists+0x112>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1248 <path_exists+0xd8>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    12b9 <path_exists+0x149>
lea    0xd94(%rip),%rax
mov    %rax,-0x18(%rbp)
movq   $0x1,-0x28(%rbp)
jmp    12c8 <path_exists+0x158>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    0x2d91(%rip),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <memcpy@plt>
mov    0x2d7d(%rip),%rax
mov    -0x28(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    0x2d6e(%rip),%rdi
add    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <memcpy@plt>
mov    0x2d52(%rip),%rax
mov    -0x28(%rbp),%rcx
add    -0x30(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    0x2d3e(%rip),%rdi
mov    0x2ca7(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jne    134a <path_exists+0x1da>
mov    0x2d1f(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1372 <path_exists+0x202>
jmp    134f <path_exists+0x1df>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movsbl (%rax),%eax
cmp    $0x3a,%eax
je     1240 <path_exists+0xd0>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_exists>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
cmpq   $0x0,0x2eda(%rip)
je     1192 <path_exists+0x22>
test   %r15,%r15
jne    11b7 <path_exists+0x47>
jmp    12bd <path_exists+0x14d>
xor    %edi,%edi
call   1030 <free@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
call   1070 <malloc@plt>
mov    %rax,0x2eb2(%rip)
test   %r15,%r15
je     12bd <path_exists+0x14d>
cmpb   $0x0,(%r15)
je     12bd <path_exists+0x14d>
mov    %r15,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11ed <path_exists+0x7d>
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1080 <access@plt>
test   %rax,%rax
je     12d7 <path_exists+0x167>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %rax,%r13
add    $0x1,%r13
lea    0xdfa(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x0(%rbp,%rbx,1),%eax
test   %al,%al
je     1240 <path_exists+0xd0>
cmp    $0x3a,%al
je     1240 <path_exists+0xd0>
add    $0x1,%rbx
jmp    1220 <path_exists+0xb0>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %rbx,%rbx
mov    %rbp,%rsi
lea    0xdef(%rip),%rax
cmove  %rax,%rsi
mov    %rbx,%r14
mov    $0x1,%eax
cmove  %rax,%r14
mov    0x2dfc(%rip),%rdi
mov    %r14,%rdx
call   1060 <memcpy@plt>
mov    0x2ded(%rip),%rax
movb   $0x2f,(%rax,%r14,1)
lea    (%rax,%r14,1),%rdi
add    $0x1,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <memcpy@plt>
mov    0x2dce(%rip),%rdi
add    %r13,%r14
movb   $0x0,(%rdi,%r14,1)
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
test   %rax,%rax
je     12ce <path_exists+0x15e>
cmpb   $0x3a,0x0(%rbp,%rbx,1)
lea    0x1(%rbp,%rbx,1),%rbp
je     1210 <path_exists+0xa0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d8b(%rip),%rax
jmp    12bf <path_exists+0x14f>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <strdup@plt>

LLVM IR (-O0):
@path_exists.buf = internal global i8* null
@PATH_MAX = global i32 0
@F_OK = global i32 0
@.str = [60 x i8] c"/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/opt/local/bin\00"
@.str.1 = [2 x i8] c".\00"
define i8* @path_exists(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i8*, i8** @path_exists.buf
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = load i8*, i8** @path_exists.buf
%12 = call i32 @free(i8* %11)
%13 = load i32, i32* @PATH_MAX
%14 = sext i32 %13 to i64
%15 = mul i64 %14, 1
%16 = trunc i64 %15 to i32
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** @path_exists.buf
br label %18
18: ; preds = %10, %1
%19 = load i8*, i8** %3
%20 = icmp eq i8* %19, null
br i1 %20, label %27, label %21
21: ; preds = %18
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21, %18
store i8* null, i8** %2
br label %108
28: ; preds = %21
%29 = load i8*, i8** %3
%30 = call i64 @strchr(i8* %29, i8 signext 47)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %28
%33 = load i8*, i8** %3
%34 = load i32, i32* @F_OK
%35 = call i64 @access(i8* %33, i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i8*, i8** %3
%39 = call i8* @strdup(i8* %38)
store i8* %39, i8** %2
br label %108
40: ; preds = %32, %28
%41 = load i8*, i8** %3
%42 = call i64 @strlen(i8* %41)
store i64 %42, i64* %7
store i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8** %4
%43 = load i8*, i8** %4
store i8* %43, i8** %5
br label %44
44: ; preds = %101, %40
%45 = load i8*, i8** %5
store i8* %45, i8** %4
br label %46
46: ; preds = %59, %44
%47 = load i8*, i8** %5
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %46
%52 = load i8*, i8** %5
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp ne i32 %54, 58
br label %56
56: ; preds = %51, %46
%57 = phi i1 [ false, %46 ], [ %55, %51 ]
br i1 %57, label %58, label %62
58: ; preds = %56
br label %59
59: ; preds = %58
%60 = load i8*, i8** %5
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %5
br label %46
62: ; preds = %56
%63 = load i8*, i8** %5
%64 = load i8*, i8** %4
%65 = icmp eq i8* %63, %64
br i1 %65, label %66, label %67
66: ; preds = %62
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %4
store i64 1, i64* %6
br label %73
67: ; preds = %62
%68 = load i8*, i8** %5
%69 = load i8*, i8** %4
%70 = ptrtoint i8* %68 to i64
%71 = ptrtoint i8* %69 to i64
%72 = sub i64 %70, %71
store i64 %72, i64* %6
br label %73
73: ; preds = %67, %66
%74 = load i8*, i8** @path_exists.buf
%75 = load i8*, i8** %4
%76 = load i64, i64* %6
%77 = call i32 @memcpy(i8* %74, i8* %75, i64 %76)
%78 = load i8*, i8** @path_exists.buf
%79 = load i64, i64* %6
%80 = getelementptr inbounds i8, i8* %78, i64 %79
store i8 47, i8* %80
%81 = load i8*, i8** @path_exists.buf
%82 = load i64, i64* %6
%83 = getelementptr inbounds i8, i8* %81, i64 %82
%84 = getelementptr inbounds i8, i8* %83, i64 1
%85 = load i8*, i8** %3
%86 = load i64, i64* %7
%87 = call i32 @memcpy(i8* %84, i8* %85, i64 %86)
%88 = load i8*, i8** @path_exists.buf
%89 = load i64, i64* %6
%90 = load i64, i64* %7
%91 = add i64 %89, %90
%92 = add i64 %91, 1
%93 = getelementptr inbounds i8, i8* %88, i64 %92
store i8 0, i8* %93
%94 = load i8*, i8** @path_exists.buf
%95 = load i32, i32* @F_OK
%96 = call i64 @access(i8* %94, i32 %95)
%97 = icmp eq i64 %96, 0
br i1 %97, label %98, label %100
98: ; preds = %73
%99 = load i8*, i8** @path_exists.buf
store i8* %99, i8** %2
br label %108
100: ; preds = %73
br label %101
101: ; preds = %100
%102 = load i8*, i8** %5
%103 = getelementptr inbounds i8, i8* %102, i32 1
store i8* %103, i8** %5
%104 = load i8, i8* %102
%105 = sext i8 %104 to i32
%106 = icmp eq i32 %105, 58
br i1 %106, label %44, label %107
107: ; preds = %101
store i8* null, i8** %2
br label %108
108: ; preds = %107, %98, %37, %27
%109 = load i8*, i8** %2
ret i8* %109
}

LLVM IR (-O2):
@path_exists.buf = internal unnamed_addr global i8* null
@PATH_MAX = global i32 0
@F_OK = global i32 0
@.str = [60 x i8] c"/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/opt/local/bin\00"
@.str.1 = [2 x i8] c".\00"
define i8* @path_exists(i8* %0) {
%2 = load i8*, i8** @path_exists.buf
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = tail call i32 @free(i8* null)
%6 = load i32, i32* @PATH_MAX
%7 = tail call i8* @malloc(i32 %6)
store i8* %7, i8** @path_exists.buf
br label %8
8: ; preds = %4, %1
%9 = icmp eq i8* %0, null
br i1 %9, label %57, label %10
10: ; preds = %8
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 0
br i1 %12, label %57, label %13
13: ; preds = %10
%14 = tail call i64 @strchr(i8* %0, i8 signext 47)
%15 = icmp eq i64 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %13
%17 = load i32, i32* @F_OK
%18 = tail call i64 @access(i8* %0, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i8* @strdup(i8* %0)
br label %57
22: ; preds = %16, %13
%23 = tail call i64 @strlen(i8* %0)
%24 = add i64 %23, 1
br label %25
25: ; preds = %53, %22
%26 = phi i8* [ getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), %22 ], [ %54, %53 ]
br label %27
27: ; preds = %30, %25
%28 = phi i8* [ %26, %25 ], [ %31, %30 ]
%29 = load i8, i8* %28
switch i8 %29, label %30 [
i8 0, label %32
i8 58, label %32
]
30: ; preds = %27
%31 = getelementptr inbounds i8, i8* %28, i64 1
br label %27
32: ; preds = %27, %27
%33 = icmp eq i8* %28, %26
%34 = ptrtoint i8* %28 to i64
%35 = ptrtoint i8* %26 to i64
%36 = sub i64 %34, %35
%37 = select i1 %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %26
%38 = select i1 %33, i64 1, i64 %36
%39 = load i8*, i8** @path_exists.buf
%40 = tail call i32 @memcpy(i8* %39, i8* %37, i64 %38)
%41 = load i8*, i8** @path_exists.buf
%42 = getelementptr inbounds i8, i8* %41, i64 %38
store i8 47, i8* %42
%43 = getelementptr inbounds i8, i8* %42, i64 1
%44 = tail call i32 @memcpy(i8* %43, i8* %0, i64 %23)
%45 = load i8*, i8** @path_exists.buf
%46 = add i64 %24, %38
%47 = getelementptr inbounds i8, i8* %45, i64 %46
store i8 0, i8* %47
%48 = load i32, i32* @F_OK
%49 = tail call i64 @access(i8* %45, i32 %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %32
%52 = load i8*, i8** @path_exists.buf
br label %57
53: ; preds = %32
%54 = getelementptr inbounds i8, i8* %28, i64 1
%55 = load i8, i8* %28
%56 = icmp eq i8 %55, 58
br i1 %56, label %25, label %57
57: ; preds = %53, %8, %10, %51, %20
%58 = phi i8* [ %21, %20 ], [ %52, %51 ], [ null, %10 ], [ null, %8 ], [ null, %53 ]
ret i8* %58
}