#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;
};
typedef int /*<<< orphan*/ fmt;

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int);
int /*<<< orphan*/ assert(char *);
char *calloc(int, size_t);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, ...);
int stat(char *, struct stat *);
size_t strlen(char *);
int path(char *name, char **out) {
  size_t i, last;
  char *path;
  assert(name);
  if ((path = getenv("PATH")) == 0)
    return -1;
  last = 0;
  for (i = 0; path[i]; i++) {
    if (path[i] == ':') {
      char *buf, fmt[10];
      size_t sz;
      struct stat info;
      if (i - last > 999)
        continue;
      snprintf(fmt, sizeof(fmt), "%%.%lds/%%s", i - last);
      sz = (i - last) + strlen(name) + 2;
      buf = calloc(sizeof(char), sz);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
      snprintf(buf, sz, fmt, &path[last], name);
#pragma GCC diagnostic pop
      last = i + 1;
      if (stat(buf, &info) == -1)
        continue;
      if (!S_ISDIR(info.st_mode) && (info.st_mode & 0111) != 0) {
        *out = buf;
        return 0;
      }
      free(buf);
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <assert@plt>
lea    0xe60(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11bf <path+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    130a <path+0x18a>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     1303 <path+0x183>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3a,%eax
jne    12ed <path+0x16d>
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
cmp    $0x3e7,%rax
jbe    120f <path+0x8f>
jmp    12f2 <path+0x172>
lea    -0x42(%rbp),%rdi
mov    -0x20(%rbp),%rcx
sub    -0x28(%rbp),%rcx
mov    $0xa,%esi
lea    0xdde(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
add    %rcx,%rax
add    $0x2,%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rsi
mov    $0x1,%edi
call   1080 <calloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x50(%rbp),%rsi
lea    -0x42(%rbp),%rdx
mov    -0x30(%rbp),%rcx
add    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0xffffffff,%eax
jne    12ad <path+0x12d>
jmp    12f2 <path+0x172>
mov    -0x58(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12e4 <path+0x164>
mov    -0x58(%rbp),%eax
and    $0x49,%eax
cmp    $0x0,%eax
je     12e4 <path+0x164>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    130a <path+0x18a>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
jmp    12f2 <path+0x172>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11cf <path+0x4f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %rdi,0x10(%rsp)
call   1090 <assert@plt>
lea    0xe5e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
mov    $0xffffffff,%eax
test   %r14,%r14
je     129c <path+0x11c>
mov    %rbx,0x8(%rsp)
xor    %r15d,%r15d
xor    %r13d,%r13d
mov    0x10(%rsp),%rbp
jmp    11dc <path+0x5c>
nopw   0x0(%rax,%rax,1)
test   %al,%al
je     128b <path+0x10b>
add    $0x1,%r13
movzbl (%r14,%r13,1),%eax
cmp    $0x3a,%al
jne    11d0 <path+0x50>
mov    %r13,%rcx
sub    %r15,%rcx
cmp    $0x3e7,%rcx
ja     11d8 <path+0x58>
mov    $0xa,%esi
lea    0x1e(%rsp),%rdi
lea    0xe00(%rip),%rdx
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    (%rax,%r13,1),%rbx
sub    %r15,%rbx
add    $0x2,%rbx
mov    $0x1,%edi
mov    %rbx,%rsi
call   1080 <calloc@plt>
mov    %rax,%r12
add    %r14,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
lea    0x1e(%rsp),%rdx
mov    %r15,%rcx
mov    %rbp,%r8
xor    %eax,%eax
call   1070 <snprintf@plt>
lea    0x1(%r13),%r15
mov    %r12,%rdi
mov    %rsp,%rsi
call   10a0 <stat@plt>
cmp    $0xffffffff,%eax
je     11d8 <path+0x58>
mov    %r14,%rbx
mov    (%rsp),%r14d
mov    %r14d,%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
jne    127b <path+0xfb>
and    $0x49,%r14d
jne    1292 <path+0x112>
mov    %r12,%rdi
call   1040 <free@plt>
mov    %rbx,%r14
jmp    11d8 <path+0x58>
mov    $0xffffffff,%eax
jmp    129c <path+0x11c>
mov    0x8(%rsp),%rax
mov    %r12,(%rax)
xor    %eax,%eax
add    $0x28,%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"
@.str.1 = [12 x i8] c"%%.%lds/%%s\00"
define i32 @path(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [10 x i8]
%11 = alloca i64
%12 = alloca %struct.stat
store i8* %0, i8** %4
store i8** %1, i8*** %5
%13 = load i8*, i8** %4
%14 = call i32 @assert(i8* %13)
%15 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %15, i8** %8
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %88
18: ; preds = %2
store i64 0, i64* %7
store i64 0, i64* %6
br label %19
19: ; preds = %84, %18
%20 = load i8*, i8** %8
%21 = load i64, i64* %6
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i8, i8* %22
%24 = icmp ne i8 %23, 0
br i1 %24, label %25, label %87
25: ; preds = %19
%26 = load i8*, i8** %8
%27 = load i64, i64* %6
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 58
br i1 %31, label %32, label %83
32: ; preds = %25
%33 = load i64, i64* %6
%34 = load i64, i64* %7
%35 = sub i64 %33, %34
%36 = icmp ugt i64 %35, 999
br i1 %36, label %37, label %38
37: ; preds = %32
br label %84
38: ; preds = %32
%39 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%40 = load i64, i64* %6
%41 = load i64, i64* %7
%42 = sub i64 %40, %41
%43 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %39, i64 10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %42)
%44 = load i64, i64* %6
%45 = load i64, i64* %7
%46 = sub i64 %44, %45
%47 = load i8*, i8** %4
%48 = call i64 @strlen(i8* %47)
%49 = add i64 %46, %48
%50 = add i64 %49, 2
store i64 %50, i64* %11
%51 = load i64, i64* %11
%52 = call i8* @calloc(i32 1, i64 %51)
store i8* %52, i8** %9
%53 = load i8*, i8** %9
%54 = load i64, i64* %11
%55 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%56 = load i8*, i8** %8
%57 = load i64, i64* %7
%58 = getelementptr inbounds i8, i8* %56, i64 %57
%59 = load i8*, i8** %4
%60 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %53, i64 %54, i8* %55, i8* %58, i8* %59)
%61 = load i64, i64* %6
%62 = add i64 %61, 1
store i64 %62, i64* %7
%63 = load i8*, i8** %9
%64 = call i32 @stat(i8* %63, %struct.stat* %12)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %67
66: ; preds = %38
br label %84
67: ; preds = %38
%68 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%69 = load i32, i32* %68
%70 = call i32 @S_ISDIR(i32 %69)
%71 = icmp ne i32 %70, 0
br i1 %71, label %80, label %72
72: ; preds = %67
%73 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%74 = load i32, i32* %73
%75 = and i32 %74, 73
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %72
%78 = load i8*, i8** %9
%79 = load i8**, i8*** %5
store i8* %78, i8** %79
store i32 0, i32* %3
br label %88
80: ; preds = %72, %67
%81 = load i8*, i8** %9
%82 = call i32 @free(i8* %81)
br label %83
83: ; preds = %80, %25
br label %84
84: ; preds = %83, %66, %37
%85 = load i64, i64* %6
%86 = add i64 %85, 1
store i64 %86, i64* %6
br label %19
87: ; preds = %19
store i32 -1, i32* %3
br label %88
88: ; preds = %87, %77, %17
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"PATH\00"
@.str.1 = [12 x i8] c"%%.%lds/%%s\00"
define i32 @path(i8* %0, i8** nocapture writeonly %1) {
%3 = alloca [10 x i8]
%4 = alloca %struct.stat
%5 = tail call i32 @assert(i8* %0)
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %46, label %8
8: ; preds = %2
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%10 = bitcast %struct.stat* %4 to i8*
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %12
12: ; preds = %8, %43
%13 = phi i64 [ %44, %43 ], [ 0, %8 ]
%14 = phi i64 [ %45, %43 ], [ 0, %8 ]
%15 = getelementptr inbounds i8, i8* %6, i64 %14
%16 = load i8, i8* %15
switch i8 %16, label %43 [
i8 0, label %46
i8 58, label %17
]
17: ; preds = %12
%18 = sub i64 %14, %13
%19 = icmp ugt i64 %18, 999
br i1 %19, label %40, label %20
20: ; preds = %17
%21 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %9, i64 10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %18)
%22 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%23 = add nuw nsw i64 %18, 2
%24 = add i64 %23, %22
%25 = tail call i8* @calloc(i32 1, i64 %24)
%26 = getelementptr inbounds i8, i8* %6, i64 %13
%27 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %25, i64 %24, i8* %9, i8* %26, i8* %0)
%28 = add i64 %14, 1
%29 = call i32 @stat(i8* %25, %struct.stat* %4)
%30 = icmp eq i32 %29, -1
br i1 %30, label %40, label %31
31: ; preds = %20
%32 = load i32, i32* %11
%33 = tail call i32 @S_ISDIR(i32 %32)
%34 = icmp ne i32 %33, 0
%35 = and i32 %32, 73
%36 = icmp eq i32 %35, 0
%37 = or i1 %34, %36
br i1 %37, label %38, label %42
38: ; preds = %31
%39 = tail call i32 @free(i8* %25)
br label %40
40: ; preds = %38, %17, %20
%41 = phi i64 [ %28, %20 ], [ %13, %17 ], [ %28, %38 ]
br label %43
42: ; preds = %31
store i8* %25, i8** %1
br label %46
43: ; preds = %40, %12
%44 = phi i64 [ %13, %12 ], [ %41, %40 ]
%45 = add i64 %14, 1
br label %12
46: ; preds = %12, %42, %2
%47 = phi i32 [ 0, %42 ], [ -1, %2 ], [ -1, %12 ]
ret i32 %47
}