#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 /*<<< orphan*/ S_ISREG(int);
char *getenv(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char const *);
char *strchr(char *, char);
int strlen(char const *);
char *xstrdup(char *);
char *os_FindInPath(char const *name) {
  char *path;
  int trsize = 2048;
  char trbuf[2048];
  char *s, *p;
  int len;
  int nlen;
  struct stat buf;
  if (name == NULL || *name == 0)
    return NULL;
  if ((path = getenv("PATH")) == NULL)
    return NULL;
  s = path;
  p = s;
  nlen = strlen(name);
  for (; *p != 0; s = p + 1) {
    p = strchr(s, ':');
    if (p == NULL)
      p = strchr(s, 0);
    if ((len = p - s) >= trsize - 2) {
      continue;
    }
    memcpy(trbuf, s, len);
    trbuf[len++] = '/';
    trbuf[len] = 0;
    if (len + nlen >= trsize - 1) {
      continue;
    }
    strcat(trbuf, name);
    if (stat(trbuf, &buf) < 0) {
      continue;
    }
    if (!S_ISREG(buf.st_mode)) {
      continue;
    }
    if (!(buf.st_mode & 0111)) {
      continue;
    }
    return xstrdup(trbuf);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<os_FindInPath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x800,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b1 <os_FindInPath+0x31>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11be <os_FindInPath+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    137d <os_FindInPath+0x1fd>
lea    0xe3b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11e5 <os_FindInPath+0x65>
movq   $0x0,-0x8(%rbp)
jmp    137d <os_FindInPath+0x1fd>
mov    -0x18(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    -0x828(%rbp),%rax
mov    %rax,-0x830(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x838(%rbp)
mov    -0x830(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1375 <os_FindInPath+0x1f5>
mov    -0x828(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x830(%rbp)
cmpq   $0x0,-0x830(%rbp)
jne    125b <os_FindInPath+0xdb>
mov    -0x828(%rbp),%rdi
xor    %esi,%esi
call   1050 <strchr@plt>
mov    %rax,-0x830(%rbp)
mov    -0x830(%rbp),%rax
mov    -0x828(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x834(%rbp)
mov    -0x1c(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jl     1285 <os_FindInPath+0x105>
jmp    135e <os_FindInPath+0x1de>
lea    -0x820(%rbp),%rdi
mov    -0x828(%rbp),%rsi
mov    -0x834(%rbp),%edx
call   1080 <memcpy@plt>
mov    -0x834(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x834(%rbp)
cltq
movb   $0x2f,-0x820(%rbp,%rax,1)
2f
movslq -0x834(%rbp),%rax
movb   $0x0,-0x820(%rbp,%rax,1)
mov    -0x834(%rbp),%eax
add    -0x838(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     12e7 <os_FindInPath+0x167>
jmp    135e <os_FindInPath+0x1de>
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strcat@plt>
lea    -0x820(%rbp),%rdi
lea    -0x840(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    1319 <os_FindInPath+0x199>
jmp    135e <os_FindInPath+0x1de>
mov    -0x840(%rbp),%edi
call   1090 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1332 <os_FindInPath+0x1b2>
jmp    135e <os_FindInPath+0x1de>
mov    -0x840(%rbp),%eax
and    $0x49,%eax
cmp    $0x0,%eax
jne    1349 <os_FindInPath+0x1c9>
jmp    135e <os_FindInPath+0x1de>
lea    -0x820(%rbp),%rdi
call   1060 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    137d <os_FindInPath+0x1fd>
mov    -0x830(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x828(%rbp)
jmp    120d <os_FindInPath+0x8d>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_FindInPath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
test   %rdi,%rdi
je     1293 <os_FindInPath+0x113>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     1293 <os_FindInPath+0x113>
lea    0xe53(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1293 <os_FindInPath+0x113>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmpb   $0x0,(%r12)
je     1293 <os_FindInPath+0x113>
mov    %eax,%r13d
lea    0x10(%rsp),%r15
jmp    11ee <os_FindInPath+0x6e>
nopl   0x0(%rax,%rax,1)
lea    0x1(%r14),%r12
cmpb   $0x0,(%r14)
je     1293 <os_FindInPath+0x113>
mov    %r12,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1211 <os_FindInPath+0x91>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%r14
add    %r12,%r14
mov    %r14,%rbp
sub    %r12,%rbp
cmp    $0x7fd,%ebp
jg     11e0 <os_FindInPath+0x60>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1080 <memcpy@plt>
movslq %ebp,%rax
movslq %ebp,%rcx
movb   $0x2f,0x10(%rsp,%rcx,1)
movb   $0x0,0x11(%rsp,%rax,1)
add    $0x1,%rax
add    %r13d,%eax
cmp    $0x7fe,%eax
jg     11e0 <os_FindInPath+0x60>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10a0 <strcat@plt>
mov    %r15,%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
js     11e0 <os_FindInPath+0x60>
mov    0x8(%rsp),%edi
call   1090 <S_ISREG@plt>
test   %eax,%eax
je     11e0 <os_FindInPath+0x60>
testb  $0x49,0x8(%rsp)
je     11e0 <os_FindInPath+0x60>
lea    0x10(%rsp),%rdi
call   1060 <xstrdup@plt>
jmp    1295 <os_FindInPath+0x115>
xor    %eax,%eax
add    $0x818,%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"
define i8* @os_FindInPath(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [2048 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.stat
store i8* %0, i8** %3
store i32 2048, i32* %5
%12 = load i8*, i8** %3
%13 = icmp eq i8* %12, null
br i1 %13, label %19, label %14
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14, %1
store i8* null, i8** %2
br label %99
20: ; preds = %14
%21 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %21, i8** %4
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %20
store i8* null, i8** %2
br label %99
24: ; preds = %20
%25 = load i8*, i8** %4
store i8* %25, i8** %7
%26 = load i8*, i8** %7
store i8* %26, i8** %8
%27 = load i8*, i8** %3
%28 = call i32 @strlen(i8* %27)
store i32 %28, i32* %10
br label %29
29: ; preds = %95, %24
%30 = load i8*, i8** %8
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %98
34: ; preds = %29
%35 = load i8*, i8** %7
%36 = call i8* @strchr(i8* %35, i8 signext 58)
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = load i8*, i8** %7
%41 = call i8* @strchr(i8* %40, i8 signext 0)
store i8* %41, i8** %8
br label %42
42: ; preds = %39, %34
%43 = load i8*, i8** %8
%44 = load i8*, i8** %7
%45 = ptrtoint i8* %43 to i64
%46 = ptrtoint i8* %44 to i64
%47 = sub i64 %45, %46
%48 = trunc i64 %47 to i32
store i32 %48, i32* %9
%49 = load i32, i32* %5
%50 = sub nsw i32 %49, 2
%51 = icmp sge i32 %48, %50
br i1 %51, label %52, label %53
52: ; preds = %42
br label %95
53: ; preds = %42
%54 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%55 = load i8*, i8** %7
%56 = load i32, i32* %9
%57 = call i32 @memcpy(i8* %54, i8* %55, i32 %56)
%58 = load i32, i32* %9
%59 = add nsw i32 %58, 1
store i32 %59, i32* %9
%60 = sext i32 %58 to i64
%61 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 %60
store i8 47, i8* %61
%62 = load i32, i32* %9
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 %63
store i8 0, i8* %64
%65 = load i32, i32* %9
%66 = load i32, i32* %10
%67 = add nsw i32 %65, %66
%68 = load i32, i32* %5
%69 = sub nsw i32 %68, 1
%70 = icmp sge i32 %67, %69
br i1 %70, label %71, label %72
71: ; preds = %53
br label %95
72: ; preds = %53
%73 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%74 = load i8*, i8** %3
%75 = call i32 @strcat(i8* %73, i8* %74)
%76 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%77 = call i64 @stat(i8* %76, %struct.stat* %11)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %72
br label %95
80: ; preds = %72
%81 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%82 = load i32, i32* %81
%83 = call i32 @S_ISREG(i32 %82)
%84 = icmp ne i32 %83, 0
br i1 %84, label %86, label %85
85: ; preds = %80
br label %95
86: ; preds = %80
%87 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%88 = load i32, i32* %87
%89 = and i32 %88, 73
%90 = icmp ne i32 %89, 0
br i1 %90, label %92, label %91
91: ; preds = %86
br label %95
92: ; preds = %86
%93 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%94 = call i8* @xstrdup(i8* %93)
store i8* %94, i8** %2
br label %99
95: ; preds = %91, %85, %79, %71, %52
%96 = load i8*, i8** %8
%97 = getelementptr inbounds i8, i8* %96, i64 1
store i8* %97, i8** %7
br label %29
98: ; preds = %29
store i8* null, i8** %2
br label %99
99: ; preds = %98, %92, %23, %19
%100 = load i8*, i8** %2
ret i8* %100
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
define i8* @os_FindInPath(i8* %0) {
%2 = alloca [2048 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = icmp eq i8* %0, null
br i1 %6, label %61, label %7
7: ; preds = %1
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %61, label %10
10: ; preds = %7
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %61, label %13
13: ; preds = %10
%14 = tail call i32 @strlen(i8* %0)
%15 = load i8, i8* %11
%16 = icmp eq i8 %15, 0
br i1 %16, label %61, label %17
17: ; preds = %13
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %19
19: ; preds = %17, %57
%20 = phi i8* [ %11, %17 ], [ %58, %57 ]
%21 = call i8* @strchr(i8* %20, i8 signext 58)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i64 bitcast (i32 (i8*)* @strlen to i64 (i8*)*)(i8* %20)
%25 = getelementptr i8, i8* %20, i64 %24
br label %26
26: ; preds = %23, %19
%27 = phi i8* [ %25, %23 ], [ %21, %19 ]
%28 = ptrtoint i8* %27 to i64
%29 = ptrtoint i8* %20 to i64
%30 = sub i64 %28, %29
%31 = trunc i64 %30 to i32
%32 = icmp sgt i32 %31, 2045
br i1 %32, label %57, label %33
33: ; preds = %26
%34 = call i32 @memcpy(i8* %4, i8* %20, i32 %31)
%35 = add nsw i32 %31, 1
%36 = shl i64 %30, 32
%37 = ashr exact i64 %36, 32
%38 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 %37
store i8 47, i8* %38
%39 = sext i32 %35 to i64
%40 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 %39
store i8 0, i8* %40
%41 = add nsw i32 %35, %14
%42 = icmp sgt i32 %41, 2046
br i1 %42, label %57, label %43
43: ; preds = %33
%44 = call i32 @strcat(i8* %4, i8* %0)
%45 = call i64 @stat(i8* %4, %struct.stat* %3)
%46 = icmp slt i64 %45, 0
br i1 %46, label %57, label %47
47: ; preds = %43
%48 = load i32, i32* %18
%49 = call i32 @S_ISREG(i32 %48)
%50 = icmp eq i32 %49, 0
br i1 %50, label %57, label %51
51: ; preds = %47
%52 = load i32, i32* %18
%53 = and i32 %52, 73
%54 = icmp eq i32 %53, 0
br i1 %54, label %57, label %55
55: ; preds = %51
%56 = call i8* @xstrdup(i8* %4)
br label %61
57: ; preds = %51, %47, %43, %33, %26
%58 = getelementptr inbounds i8, i8* %27, i64 1
%59 = load i8, i8* %27
%60 = icmp eq i8 %59, 0
br i1 %60, label %61, label %19
61: ; preds = %57, %13, %10, %1, %7, %55
%62 = phi i8* [ %56, %55 ], [ null, %7 ], [ null, %1 ], [ null, %10 ], [ null, %13 ], [ null, %57 ]
ret i8* %62
}