#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int PATH_MAX;
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
char *getenv(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char const *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int loki_valid_program(const char *program) {
  char temppath[PATH_MAX];
  char *path;
  char *last;
  int found;
  found = 0;
  path = getenv("PATH");
  do {
    temppath[0] = '\0';
    last = strchr(path, ':');
    if (!last)
      last = path + strlen(path);
    if (*path == '~') {
      strcpy(temppath, getenv("HOME") ? getenv("HOME") : ".");
      ++path;
    }
    if (last > (path + 1)) {
      strncat(temppath, path, (last - path));
      strcat(temppath, "/");
    }
    strcat(temppath, "./");
    strcat(temppath, program);
    if (access(temppath, X_OK) == 0) {
      struct stat s;
      if ((stat(temppath, &s) == 0) && (S_ISREG(s.st_mode)))
        ++found;
    }
    path = last + 1;
  } while (*last && !found);
  return found;
}


x86 assembly (clang, -O0):
<loki_valid_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    0xe2a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1224 <loki_valid_program+0x94>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    1288 <loki_valid_program+0xf8>
lea    0xdca(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     125f <loki_valid_program+0xcf>
lea    0xdb4(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x48(%rbp)
jmp    126f <loki_valid_program+0xdf>
lea    0xda4(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    126f <loki_valid_program+0xdf>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jbe    12c7 <loki_valid_program+0x137>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1070 <strncat@plt>
mov    -0x38(%rbp),%rdi
lea    0xd4b(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x38(%rbp),%rdi
lea    0xd3a(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
call   10a0 <access@plt>
cmp    $0x0,%rax
jne    1337 <loki_valid_program+0x1a7>
mov    -0x38(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    1332 <loki_valid_program+0x1a2>
mov    -0x30(%rbp),%edi
call   1090 <S_ISREG@plt>
cmp    $0x0,%rax
je     1332 <loki_valid_program+0x1a2>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1337 <loki_valid_program+0x1a7>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x49(%rbp)
je     1364 <loki_valid_program+0x1d4>
cmpl   $0x0,-0x2c(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    11df <loki_valid_program+0x4f>
mov    -0x2c(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loki_valid_program>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe3e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
lea    0xe3b(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%rbx)
mov    %r15,%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,%r12
test   %rax,%rax
je     12d0 <loki_valid_program+0x140>
cmpb   $0x7e,(%r15)
jne    122b <loki_valid_program+0x9b>
lea    0xdfc(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xdf2(%rip),%rcx
cmove  %rcx,%rax
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
add    $0x1,%r15
lea    0x1(%r15),%rax
cmp    %rax,%r12
jbe    1254 <loki_valid_program+0xc4>
mov    %r12d,%edx
sub    %r15d,%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strncat@plt>
mov    %rbx,%rdi
lea    0xdbe(%rip),%rsi
call   10b0 <strcat@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10b0 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10b0 <strcat@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    12b0 <loki_valid_program+0x120>
mov    %rbx,%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    12b0 <loki_valid_program+0x120>
mov    -0x30(%rbp),%edi
call   1090 <S_ISREG@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
xchg   %ax,%ax
cmpb   $0x0,(%r12)
je     12ed <loki_valid_program+0x15d>
add    $0x1,%r12
mov    %r12,%r15
test   %eax,%eax
je     11e0 <loki_valid_program+0x50>
jmp    12ed <loki_valid_program+0x15d>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%r12
add    %r15,%r12
cmpb   $0x7e,(%r15)
je     1202 <loki_valid_program+0x72>
jmp    122b <loki_valid_program+0x9b>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [5 x i8] c"PATH\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [3 x i8] c"./\00"
@X_OK = global i32 0
define i32 @loki_valid_program(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.stat
store i8* %0, i8** %2
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %3
%12 = alloca i8, i64 %10
store i64 %10, i64* %4
store i32 0, i32* %7
%13 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %5
br label %14
14: ; preds = %88, %1
%15 = getelementptr inbounds i8, i8* %12, i64 0
store i8 0, i8* %15
%16 = load i8*, i8** %5
%17 = call i8* @strchr(i8* %16, i8 signext 58)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %26, label %20
20: ; preds = %14
%21 = load i8*, i8** %5
%22 = load i8*, i8** %5
%23 = call i32 @strlen(i8* %22)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8* %25, i8** %6
br label %26
26: ; preds = %20, %14
%27 = load i8*, i8** %5
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 126
br i1 %30, label %31, label %42
31: ; preds = %26
%32 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %37
36: ; preds = %31
br label %37
37: ; preds = %36, %34
%38 = phi i8* [ %35, %34 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %36 ]
%39 = call i32 @strcpy(i8* %12, i8* %38)
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %5
br label %42
42: ; preds = %37, %26
%43 = load i8*, i8** %6
%44 = load i8*, i8** %5
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = icmp ugt i8* %43, %45
br i1 %46, label %47, label %57
47: ; preds = %42
%48 = load i8*, i8** %5
%49 = load i8*, i8** %6
%50 = load i8*, i8** %5
%51 = ptrtoint i8* %49 to i64
%52 = ptrtoint i8* %50 to i64
%53 = sub i64 %51, %52
%54 = trunc i64 %53 to i32
%55 = call i32 @strncat(i8* %12, i8* %48, i32 %54)
%56 = call i32 @strcat(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %57
57: ; preds = %47, %42
%58 = call i32 @strcat(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%59 = load i8*, i8** %2
%60 = call i32 @strcat(i8* %12, i8* %59)
%61 = load i32, i32* @X_OK
%62 = call i64 @access(i8* %12, i32 %61)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %76
64: ; preds = %57
%65 = call i64 @stat(i8* %12, %struct.stat* %8)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %75
67: ; preds = %64
%68 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%69 = load i32, i32* %68
%70 = call i64 @S_ISREG(i32 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %67
%73 = load i32, i32* %7
%74 = add nsw i32 %73, 1
store i32 %74, i32* %7
br label %75
75: ; preds = %72, %67, %64
br label %76
76: ; preds = %75, %57
%77 = load i8*, i8** %6
%78 = getelementptr inbounds i8, i8* %77, i64 1
store i8* %78, i8** %5
br label %79
79: ; preds = %76
%80 = load i8*, i8** %6
%81 = load i8, i8* %80
%82 = sext i8 %81 to i32
%83 = icmp ne i32 %82, 0
br i1 %83, label %84, label %88
84: ; preds = %79
%85 = load i32, i32* %7
%86 = icmp ne i32 %85, 0
%87 = xor i1 %86, true
br label %88
88: ; preds = %84, %79
%89 = phi i1 [ false, %79 ], [ %87, %84 ]
br i1 %89, label %14, label %90
90: ; preds = %88
%91 = load i32, i32* %7
%92 = load i8*, i8** %3
ret i32 %91
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [5 x i8] c"PATH\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [3 x i8] c"./\00"
@X_OK = global i32 0
define i32 @loki_valid_program(i8* %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = bitcast %struct.stat* %2 to i8*
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %9
9: ; preds = %54, %1
%10 = phi i8* [ %6, %1 ], [ %56, %54 ]
store i8 0, i8* %5
%11 = call i8* @strchr(i8* dereferenceable(1) %10, i8 signext 58)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = call i32 @strlen(i8* %10)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %10, i64 %15
br label %17
17: ; preds = %13, %9
%18 = phi i8* [ %11, %9 ], [ %16, %13 ]
%19 = load i8, i8* %10
%20 = icmp eq i8 %19, 126
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i8* %22, null
%24 = select i1 %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %22
%25 = call i32 @strcpy(i8* %5, i8* %24)
%26 = getelementptr inbounds i8, i8* %10, i64 1
br label %27
27: ; preds = %21, %17
%28 = phi i8* [ %26, %21 ], [ %10, %17 ]
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = icmp ugt i8* %18, %29
br i1 %30, label %31, label %38
31: ; preds = %27
%32 = ptrtoint i8* %18 to i64
%33 = ptrtoint i8* %28 to i64
%34 = sub i64 %32, %33
%35 = trunc i64 %34 to i32
%36 = call i32 @strncat(i8* %5, i8* %28, i32 %35)
%37 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %31, %27
%39 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%40 = call i32 @strcat(i8* %5, i8* %0)
%41 = load i32, i32* @X_OK
%42 = call i64 @access(i8* %5, i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %54
44: ; preds = %38
%45 = call i64 @stat(i8* %5, %struct.stat* %2)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %44
%48 = load i32, i32* %8
%49 = call i64 @S_ISREG(i32 %48)
%50 = icmp ne i64 %49, 0
%51 = zext i1 %50 to i32
br label %52
52: ; preds = %47, %44
%53 = phi i32 [ 0, %44 ], [ %51, %47 ]
br label %54
54: ; preds = %52, %38
%55 = phi i32 [ %53, %52 ], [ 0, %38 ]
%56 = getelementptr inbounds i8, i8* %18, i64 1
%57 = load i8, i8* %18
%58 = icmp ne i8 %57, 0
%59 = icmp eq i32 %55, 0
%60 = select i1 %58, i1 %59, i1 false
br i1 %60, label %9, label %61
61: ; preds = %54
ret i32 %55
}