#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 st_mode;
};

/* Variables and functions */
int BUFSIZ;
scalar_t__ S_ISREG(int);
int S_IXOTH;
char *getenv(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
char *strchr(char *, char);
short in_path(char *prog) {
  char *p = getenv("PATH");
  char exe[BUFSIZ];
  struct stat ss;
  if (p == NULL) {
    return 0;
  }
  while (1) {
    char *cn = strchr(p, ':');
    if (cn == NULL) {
      break;
    }
    *cn = '\0';
    snprintf(exe, BUFSIZ, "%s/%s", p, prog);
    *cn = ':';
    if (stat(exe, &ss) == 0) {
      if (S_ISREG(ss.st_mode) && (ss.st_mode & S_IXOTH)) {
        return 1;
      }
    }
    p = cn + 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<in_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe9d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b5 <in_path+0x65>
movw   $0x0,-0x2(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1283 <in_path+0x133>
jmp    11ba <in_path+0x6a>
jmp    11bf <in_path+0x6f>
mov    -0x18(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11e1 <in_path+0x91>
jmp    1276 <in_path+0x126>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xe01(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rax
movb   $0x3a,(%rax)
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    1265 <in_path+0x115>
mov    -0x30(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%rax
je     1260 <in_path+0x110>
mov    -0x30(%rbp),%eax
mov    0x2da5(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1260 <in_path+0x110>
movw   $0x1,-0x2(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1283 <in_path+0x133>
jmp    1265 <in_path+0x115>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11ba <in_path+0x6a>
movw   $0x0,-0x2(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
movswl -0x2(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<in_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe98(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
test   %rbx,%rbx
je     1233 <in_path+0xe3>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1233 <in_path+0xe3>
mov    %rax,%r15
lea    -0x30(%rbp),%r13
jmp    11dc <in_path+0x8c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,%r15
test   %rax,%rax
je     1233 <in_path+0xe3>
movb   $0x0,(%r15)
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
lea    0xe12(%rip),%rdx
mov    %rbx,%rcx
mov    %r14,%r8
call   1050 <snprintf@plt>
movb   $0x3a,(%r15)
mov    %r12,%rdi
mov    %r13,%rsi
call   1060 <stat@plt>
test   %rax,%rax
jne    11c0 <in_path+0x70>
mov    -0x30(%rbp),%edi
call   1070 <S_ISREG@plt>
test   %rax,%rax
je     11c0 <in_path+0x70>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
test   %eax,-0x30(%rbp)
je     11c0 <in_path+0x70>
mov    $0x1,%ax
jmp    1235 <in_path+0xe5>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
@BUFSIZ = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
@S_IXOTH = global i32 0
define signext i16 @in_path(i8* %0) {
%2 = alloca i16
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %3
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %4
%11 = load i32, i32* @BUFSIZ
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = load i8*, i8** %4
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %1
store i16 0, i16* %2
store i32 1, i32* %8
br label %52
18: ; preds = %1
br label %19
19: ; preds = %48, %18
br label %20
20: ; preds = %19
%21 = load i8*, i8** %4
%22 = call i8* @strchr(i8* %21, i8 signext 58)
store i8* %22, i8** %9
%23 = load i8*, i8** %9
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %20
br label %51
26: ; preds = %20
%27 = load i8*, i8** %9
store i8 0, i8* %27
%28 = load i32, i32* @BUFSIZ
%29 = load i8*, i8** %4
%30 = load i8*, i8** %3
%31 = call i32 @snprintf(i8* %14, i32 %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %29, i8* %30)
%32 = load i8*, i8** %9
store i8 58, i8* %32
%33 = call i64 @stat(i8* %14, %struct.stat* %7)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %48
35: ; preds = %26
%36 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i64 @S_ISREG(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %35
%41 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load i32, i32* @S_IXOTH
%44 = and i32 %42, %43
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %40
store i16 1, i16* %2
store i32 1, i32* %8
br label %52
47: ; preds = %40, %35
br label %48
48: ; preds = %47, %26
%49 = load i8*, i8** %9
%50 = getelementptr inbounds i8, i8* %49, i64 1
store i8* %50, i8** %4
br label %19
51: ; preds = %25
store i16 0, i16* %2
store i32 1, i32* %8
br label %52
52: ; preds = %51, %46, %17
%53 = load i8*, i8** %5
%54 = load i16, i16* %2
ret i16 %54
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
@BUFSIZ = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
@S_IXOTH = global i32 0
define signext i16 @in_path(i8* %0) {
%2 = alloca %struct.stat
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.stat* %2 to i8*
%8 = icmp eq i8* %3, null
br i1 %8, label %34, label %9
9: ; preds = %1
%10 = call i8* @strchr(i8* dereferenceable(1) %3, i8 signext 58)
%11 = icmp eq i8* %10, null
br i1 %11, label %34, label %12
12: ; preds = %9
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %14
14: ; preds = %12, %30
%15 = phi i8* [ %10, %12 ], [ %32, %30 ]
%16 = phi i8* [ %3, %12 ], [ %31, %30 ]
store i8 0, i8* %15
%17 = load i32, i32* @BUFSIZ
%18 = call i32 @snprintf(i8* %6, i32 %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* %0)
store i8 58, i8* %15
%19 = call i64 @stat(i8* %6, %struct.stat* %2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %14
%22 = load i32, i32* %13
%23 = call i64 @S_ISREG(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %21
%26 = load i32, i32* %13
%27 = load i32, i32* @S_IXOTH
%28 = and i32 %27, %26
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %14, %25, %21
%31 = getelementptr inbounds i8, i8* %15, i64 1
%32 = call i8* @strchr(i8* dereferenceable(1) %31, i8 signext 58)
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %14
34: ; preds = %30, %25, %9, %1
%35 = phi i16 [ 0, %1 ], [ 0, %9 ], [ 0, %30 ], [ 1, %25 ]
ret i16 %35
}