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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, ...);
scalar_t__ stat(char *, struct stat *);
char *strtok_r(char *, char *, char **);
int getpathtoprog(char *buffer, int bufsize, char *prog) {
  char pathtest[PATH_MAX];
  char delims[] = ":\t\n";
  struct stat bufstat;
  char pathenv[4096];
  char *saveptr = 0;
  char *result;
  char *vp;
  int i;
  memset(buffer, 0, bufsize);
  if ((vp = getenv("PATH")) == NULL)
    return -1;
  snprintf(pathenv, sizeof(pathenv), "%s", vp);
  result = strtok_r(pathenv, delims, &saveptr);
  for (i = 0; result != NULL; i++) {
    snprintf(pathtest, sizeof(pathtest), "%s/%s", result, prog);
    if (stat(pathtest, &bufstat) == 0 && access(pathtest, X_OK) == 0) {
      snprintf(buffer, bufsize, "%s", pathtest);
      return 0;
    }
    result = strtok_r(NULL, delims, &saveptr);
  }
  return -1;
}


x86 assembly (clang, -O0):
<getpathtoprog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1070,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x1070(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x1068(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    0xe4e(%rip),%eax
mov    %eax,-0x34(%rbp)
movq   $0x0,-0x1048(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
lea    0xe2f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x1058(%rbp)
cmp    $0x0,%rax
jne    1201 <getpathtoprog+0xa1>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x1060(%rbp)
jmp    132c <getpathtoprog+0x1cc>
lea    -0x1040(%rbp),%rdi
mov    -0x1058(%rbp),%rcx
mov    $0x1000,%esi
lea    0xdf1(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    -0x1040(%rbp),%rdi
lea    -0x34(%rbp),%rsi
lea    -0x1048(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x1050(%rbp)
movl   $0x0,-0x105c(%rbp)
cmpq   $0x0,-0x1050(%rbp)
je     131b <getpathtoprog+0x1bb>
mov    -0x1068(%rbp),%rdi
mov    -0x1070(%rbp),%rax
mov    %eax,%esi
mov    -0x1050(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xd8f(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x1068(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    12ec <getpathtoprog+0x18c>
mov    -0x1068(%rbp),%rdi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jne    12ec <getpathtoprog+0x18c>
mov    -0x1068(%rbp),%rcx
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    0xd3d(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x1060(%rbp)
jmp    132c <getpathtoprog+0x1cc>
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x1048(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x1050(%rbp)
mov    -0x105c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x105c(%rbp)
jmp    124a <getpathtoprog+0xea>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x1060(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpathtoprog>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    %rdx,%r12
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e6c(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0xa093a,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
xor    %esi,%esi
mov    %r15d,%edx
call   1050 <memset@plt>
lea    0xe48(%rip),%rdi
call   1030 <getenv@plt>
mov    $0xffffffff,%ecx
test   %rax,%rax
je     1294 <getpathtoprog+0x134>
mov    %r14,-0x40(%rbp)
lea    0xe32(%rip),%rdx
lea    -0x1050(%rbp),%r14
mov    %r14,%rdi
mov    $0x1000,%esi
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
lea    -0x2c(%rbp),%rsi
lea    -0x38(%rbp),%rdx
mov    %r14,%rdi
call   1060 <strtok_r@plt>
test   %rax,%rax
je     128f <getpathtoprog+0x12f>
mov    %r15d,-0x30(%rbp)
lea    -0x2c(%rbp),%r15
lea    -0x38(%rbp),%r14
jmp    1232 <getpathtoprog+0xd2>
cs nopw 0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <strtok_r@plt>
test   %rax,%rax
je     128f <getpathtoprog+0x12f>
mov    %rbx,%rdi
mov    %r13d,%esi
lea    0xdc6(%rip),%rdx
mov    %rax,%rcx
mov    %r12,%r8
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %rbx,%rdi
lea    -0x48(%rbp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    1220 <getpathtoprog+0xc0>
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
test   %rax,%rax
jne    1220 <getpathtoprog+0xc0>
lea    0xd8e(%rip),%rdx
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
xor    %ecx,%ecx
jmp    1294 <getpathtoprog+0x134>
mov    $0xffffffff,%ecx
mov    %ecx,%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 }
@PATH_MAX = global i32 0
@__const.getpathtoprog.delims = [4 x i8] c":\09\0A\00"
@.str = [5 x i8] c"PATH\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@X_OK = global i32 0
define i32 @getpathtoprog(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca [4 x i8]
%11 = alloca %struct.stat
%12 = alloca [4096 x i8]
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%18 = load i32, i32* @PATH_MAX
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %8
%21 = alloca i8, i64 %19
store i64 %19, i64* %9
%22 = bitcast [4 x i8]* %10 to i8*
store i8* null, i8** %13
%23 = load i8*, i8** %5
%24 = load i32, i32* %6
%25 = call i32 @memset(i8* %23, i32 0, i32 %24)
%26 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %26, i8** %15
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %3
store i32 -1, i32* %4
store i32 1, i32* %17
br label %61
29: ; preds = %3
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%31 = load i8*, i8** %15
%32 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %30, i32 4096, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%34 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%35 = call i8* @strtok_r(i8* %33, i8* %34, i8** %13)
store i8* %35, i8** %14
store i32 0, i32* %16
br label %36
36: ; preds = %57, %29
%37 = load i8*, i8** %14
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %60
39: ; preds = %36
%40 = trunc i64 %19 to i32
%41 = load i8*, i8** %14
%42 = load i8*, i8** %7
%43 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %21, i32 %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %41, i8* %42)
%44 = call i64 @stat(i8* %21, %struct.stat* %11)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %54
46: ; preds = %39
%47 = load i32, i32* @X_OK
%48 = call i64 @access(i8* %21, i32 %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i8*, i8** %5
%52 = load i32, i32* %6
%53 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %51, i32 %52, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %21)
store i32 0, i32* %4
store i32 1, i32* %17
br label %61
54: ; preds = %46, %39
%55 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%56 = call i8* @strtok_r(i8* null, i8* %55, i8** %13)
store i8* %56, i8** %14
br label %57
57: ; preds = %54
%58 = load i32, i32* %16
%59 = add nsw i32 %58, 1
store i32 %59, i32* %16
br label %36
60: ; preds = %36
store i32 -1, i32* %4
store i32 1, i32* %17
br label %61
61: ; preds = %60, %50, %28
%62 = load i8*, i8** %8
%63 = load i32, i32* %4
ret i32 %63
}

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