#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 */

/* Variables and functions */
int /*<<< orphan*/ absolute_program(char const *);
int /*<<< orphan*/ efree(char *);
scalar_t__ emalloc(int);
int file_status(char *);
char *get_next_path_element(char const *, int *);
char *make_full_pathname(char *, char const *, int);
int /*<<< orphan*/ progwarn(char *, char *);
int /*<<< orphan*/ strcat(char *, char const *);
scalar_t__ strcpy(char *, char const *);
int strlen(char const *);
char *strrchr(char *, char);
char *find_command_in_path(const char *name, const char *path_list,
                           int *path_index) {
  char *found = NULL, *full_path;
  int status, name_len;
  int absolute_path_given = 0;
  char *abs_path = NULL;
  name_len = strlen(name);
  if (!absolute_program(name))
    absolute_path_given = 0;
  else {
    char *p;
    absolute_path_given = 1;
    if (abs_path)
      efree(abs_path);
    if (*name != '.' && *name != '/' && *name != '~') {
      abs_path = (char *)emalloc(3 + name_len);
      strcpy(abs_path, "./");
      strcat(abs_path, name);
    } else {
      abs_path = (char *)emalloc(1 + name_len);
      strcpy(abs_path, name);
    }
    path_list = abs_path;
    p = strrchr(abs_path, '/');
    *p++ = 0;
    name = p;
  }
  while (path_list && path_list[*path_index]) {
    char *path;
    if (absolute_path_given) {
      path =
          (char *)strcpy((char *)emalloc(1 + strlen(path_list)), (path_list));
      *path_index = strlen(path);
    } else
      path = get_next_path_element(path_list, path_index);
    if (!path)
      break;
    full_path = make_full_pathname(path, name, name_len);
    efree(path);
    status = file_status(full_path);
    if (status & 0x1) {
      if (status & 0x2) {
        found = full_path;
        break;
      } else
        progwarn("Found '%s' in PATH but is not executable", full_path);
    }
    efree(full_path);
  }
  return (found);
}


x86 assembly (clang, -O0):
<find_command_in_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <absolute_program@plt>
cmp    $0x0,%eax
jne    1205 <find_command_in_path+0x55>
movl   $0x0,-0x34(%rbp)
jmp    12d1 <find_command_in_path+0x121>
movl   $0x1,-0x34(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1220 <find_command_in_path+0x70>
mov    -0x40(%rbp),%rdi
call   1090 <efree@plt>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     1281 <find_command_in_path+0xd1>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     1281 <find_command_in_path+0xd1>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
je     1281 <find_command_in_path+0xd1>
mov    -0x30(%rbp),%edi
add    $0x3,%edi
call   10a0 <emalloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    0xd96(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10c0 <strcat@plt>
jmp    129d <find_command_in_path+0xed>
mov    -0x30(%rbp),%edi
add    $0x1,%edi
call   10a0 <emalloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x40(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x48(%rbp)
movb   $0x0,(%rax)
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d6 <find_command_in_path+0x126>
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x51(%rbp)
je     12fe <find_command_in_path+0x14e>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    130e <find_command_in_path+0x15e>
jmp    13e9 <find_command_in_path+0x239>
cmpl   $0x0,-0x34(%rbp)
je     1351 <find_command_in_path+0x1a1>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   10a0 <emalloc@plt>
mov    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    1362 <find_command_in_path+0x1b2>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10d0 <get_next_path_element@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1372 <find_command_in_path+0x1c2>
jmp    13e9 <find_command_in_path+0x239>
mov    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1030 <make_full_pathname@plt>
mov    %rax,-0x28(%rbp)
mov    -0x50(%rbp),%rdi
call   1090 <efree@plt>
mov    -0x28(%rbp),%rdi
call   10b0 <file_status@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     13db <find_command_in_path+0x22b>
mov    -0x2c(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     13c6 <find_command_in_path+0x216>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    13e9 <find_command_in_path+0x239>
mov    -0x28(%rbp),%rsi
lea    0xc32(%rip),%rdi
call   1050 <progwarn@plt>
jmp    13db <find_command_in_path+0x22b>
mov    -0x28(%rbp),%rdi
call   1090 <efree@plt>
jmp    12d6 <find_command_in_path+0x126>
mov    -0x20(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_command_in_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%rbp
call   1060 <strlen@plt>
mov    %rax,(%rsp)
mov    %rbp,%rdi
call   1080 <absolute_program@plt>
mov    %eax,%r15d
test   %eax,%eax
je     1247 <find_command_in_path+0x97>
mov    0x0(%rbp),%al
lea    -0x2e(%rax),%ecx
cmp    $0x2,%cl
jb     11eb <find_command_in_path+0x3b>
cmp    $0x7e,%al
jne    1207 <find_command_in_path+0x57>
mov    (%rsp),%rax
lea    0x1(%rax),%edi
call   10a0 <emalloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
jmp    1230 <find_command_in_path+0x80>
mov    (%rsp),%rax
lea    0x3(%rax),%edi
call   10a0 <emalloc@plt>
mov    %rax,%rbx
lea    0xde3(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10c0 <strcat@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,%rbp
movb   $0x0,(%rax)
add    $0x1,%rbp
test   %rbx,%rbx
jne    1278 <find_command_in_path+0xc8>
xor    %r13d,%r13d
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1090 <efree@plt>
movslq (%r12),%rax
cmpb   $0x0,(%rbx,%rax,1)
je     124c <find_command_in_path+0x9c>
mov    %rbx,%rdi
test   %r15d,%r15d
je     12c0 <find_command_in_path+0x110>
call   1060 <strlen@plt>
lea    0x1(%rax),%edi
call   10a0 <emalloc@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,(%r12)
test   %r14,%r14
jne    12d4 <find_command_in_path+0x124>
jmp    124c <find_command_in_path+0x9c>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rsi
call   10d0 <get_next_path_element@plt>
mov    %rax,%r14
test   %r14,%r14
je     124c <find_command_in_path+0x9c>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    (%rsp),%rdx
call   1030 <make_full_pathname@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1090 <efree@plt>
mov    %r13,%rdi
call   10b0 <file_status@plt>
test   $0x1,%al
je     1270 <find_command_in_path+0xc0>
test   $0x2,%al
jne    124f <find_command_in_path+0x9f>
lea    0xcf6(%rip),%rdi
mov    %r13,%rsi
call   1050 <progwarn@plt>
jmp    1270 <find_command_in_path+0xc0>

LLVM IR (-O0):
@.str = [3 x i8] c"./\00"
@.str.1 = [41 x i8] c"Found '%s' in PATH but is not executable\00"
define i8* @find_command_in_path(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* %2, i32** %6
store i8* null, i8** %7
store i32 0, i32* %11
store i8* null, i8** %12
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %10
%17 = load i8*, i8** %4
%18 = call i32 @absolute_program(i8* %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %3
store i32 0, i32* %11
br label %67
21: ; preds = %3
store i32 1, i32* %11
%22 = load i8*, i8** %12
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i8*, i8** %12
%26 = call i32 @efree(i8* %25)
br label %27
27: ; preds = %24, %21
%28 = load i8*, i8** %4
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 46
br i1 %31, label %32, label %52
32: ; preds = %27
%33 = load i8*, i8** %4
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 47
br i1 %36, label %37, label %52
37: ; preds = %32
%38 = load i8*, i8** %4
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 126
br i1 %41, label %42, label %52
42: ; preds = %37
%43 = load i32, i32* %10
%44 = add nsw i32 3, %43
%45 = call i64 @emalloc(i32 %44)
%46 = inttoptr i64 %45 to i8*
store i8* %46, i8** %12
%47 = load i8*, i8** %12
%48 = call i64 @strcpy(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%49 = load i8*, i8** %12
%50 = load i8*, i8** %4
%51 = call i32 @strcat(i8* %49, i8* %50)
br label %60
52: ; preds = %37, %32, %27
%53 = load i32, i32* %10
%54 = add nsw i32 1, %53
%55 = call i64 @emalloc(i32 %54)
%56 = inttoptr i64 %55 to i8*
store i8* %56, i8** %12
%57 = load i8*, i8** %12
%58 = load i8*, i8** %4
%59 = call i64 @strcpy(i8* %57, i8* %58)
br label %60
60: ; preds = %52, %42
%61 = load i8*, i8** %12
store i8* %61, i8** %5
%62 = load i8*, i8** %12
%63 = call i8* @strrchr(i8* %62, i8 signext 47)
store i8* %63, i8** %13
%64 = load i8*, i8** %13
%65 = getelementptr inbounds i8, i8* %64, i32 1
store i8* %65, i8** %13
store i8 0, i8* %64
%66 = load i8*, i8** %13
store i8* %66, i8** %4
br label %67
67: ; preds = %60, %20
br label %68
68: ; preds = %127, %67
%69 = load i8*, i8** %5
%70 = icmp ne i8* %69, null
br i1 %70, label %71, label %80
71: ; preds = %68
%72 = load i8*, i8** %5
%73 = load i32*, i32** %6
%74 = load i32, i32* %73
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %72, i64 %75
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
%79 = icmp ne i32 %78, 0
br label %80
80: ; preds = %71, %68
%81 = phi i1 [ false, %68 ], [ %79, %71 ]
br i1 %81, label %82, label %130
82: ; preds = %80
%83 = load i32, i32* %11
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %97
85: ; preds = %82
%86 = load i8*, i8** %5
%87 = call i32 @strlen(i8* %86)
%88 = add nsw i32 1, %87
%89 = call i64 @emalloc(i32 %88)
%90 = inttoptr i64 %89 to i8*
%91 = load i8*, i8** %5
%92 = call i64 @strcpy(i8* %90, i8* %91)
%93 = inttoptr i64 %92 to i8*
store i8* %93, i8** %14
%94 = load i8*, i8** %14
%95 = call i32 @strlen(i8* %94)
%96 = load i32*, i32** %6
store i32 %95, i32* %96
br label %101
97: ; preds = %82
%98 = load i8*, i8** %5
%99 = load i32*, i32** %6
%100 = call i8* @get_next_path_element(i8* %98, i32* %99)
store i8* %100, i8** %14
br label %101
101: ; preds = %97, %85
%102 = load i8*, i8** %14
%103 = icmp ne i8* %102, null
br i1 %103, label %105, label %104
104: ; preds = %101
br label %130
105: ; preds = %101
%106 = load i8*, i8** %14
%107 = load i8*, i8** %4
%108 = load i32, i32* %10
%109 = call i8* @make_full_pathname(i8* %106, i8* %107, i32 %108)
store i8* %109, i8** %8
%110 = load i8*, i8** %14
%111 = call i32 @efree(i8* %110)
%112 = load i8*, i8** %8
%113 = call i32 @file_status(i8* %112)
store i32 %113, i32* %9
%114 = load i32, i32* %9
%115 = and i32 %114, 1
%116 = icmp ne i32 %115, 0
br i1 %116, label %117, label %127
117: ; preds = %105
%118 = load i32, i32* %9
%119 = and i32 %118, 2
%120 = icmp ne i32 %119, 0
br i1 %120, label %121, label %123
121: ; preds = %117
%122 = load i8*, i8** %8
store i8* %122, i8** %7
br label %130
123: ; preds = %117
%124 = load i8*, i8** %8
%125 = call i32 @progwarn(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %124)
br label %126
126: ; preds = %123
br label %127
127: ; preds = %126, %105
%128 = load i8*, i8** %8
%129 = call i32 @efree(i8* %128)
br label %68
130: ; preds = %121, %104, %80
%131 = load i8*, i8** %7
ret i8* %131
}

LLVM IR (-O2):
@.str = [3 x i8] c"./\00"
@.str.1 = [41 x i8] c"Found '%s' in PATH but is not executable\00"
define i8* @find_command_in_path(i8* %0, i8* %1, i32* %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @absolute_program(i8* %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %24, label %7
7: ; preds = %3
%8 = load i8, i8* %0
switch i8 %8, label %9 [
i8 46, label %15
i8 47, label %15
i8 126, label %15
]
9: ; preds = %7
%10 = add nsw i32 %4, 3
%11 = tail call i64 @emalloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i64 @strcpy(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @strcat(i8* %12, i8* %0)
br label %20
15: ; preds = %7, %7, %7
%16 = add nsw i32 %4, 1
%17 = tail call i64 @emalloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
%19 = tail call i64 @strcpy(i8* %18, i8* %0)
br label %20
20: ; preds = %15, %9
%21 = phi i8* [ %12, %9 ], [ %18, %15 ]
%22 = tail call i8* @strrchr(i8* dereferenceable(1) %21, i8 signext 47)
%23 = getelementptr inbounds i8, i8* %22, i64 1
store i8 0, i8* %22
br label %24
24: ; preds = %3, %20
%25 = phi i8* [ %21, %20 ], [ %1, %3 ]
%26 = phi i8* [ %23, %20 ], [ %0, %3 ]
%27 = icmp eq i8* %25, null
br i1 %27, label %66, label %28
28: ; preds = %24
%29 = load i32, i32* %2
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %25, i64 %30
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 0
br i1 %33, label %66, label %34
34: ; preds = %28, %59
br i1 %6, label %43, label %35
35: ; preds = %34
%36 = tail call i32 @strlen(i8* %25)
%37 = add nsw i32 %36, 1
%38 = tail call i64 @emalloc(i32 %37)
%39 = inttoptr i64 %38 to i8*
%40 = tail call i64 @strcpy(i8* %39, i8* %25)
%41 = inttoptr i64 %40 to i8*
%42 = tail call i32 @strlen(i8* %41)
store i32 %42, i32* %2
br label %45
43: ; preds = %34
%44 = tail call i8* @get_next_path_element(i8* %25, i32* %2)
br label %45
45: ; preds = %43, %35
%46 = phi i8* [ %41, %35 ], [ %44, %43 ]
%47 = icmp eq i8* %46, null
br i1 %47, label %66, label %48
48: ; preds = %45
%49 = tail call i8* @make_full_pathname(i8* %46, i8* %26, i32 %4)
%50 = tail call i32 @efree(i8* %46)
%51 = tail call i32 @file_status(i8* %49)
%52 = and i32 %51, 1
%53 = icmp eq i32 %52, 0
br i1 %53, label %59, label %54
54: ; preds = %48
%55 = and i32 %51, 2
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %66
57: ; preds = %54
%58 = tail call i32 @progwarn(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %49)
br label %59
59: ; preds = %48, %57
%60 = tail call i32 @efree(i8* %49)
%61 = load i32, i32* %2
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %25, i64 %62
%64 = load i8, i8* %63
%65 = icmp eq i8 %64, 0
br i1 %65, label %66, label %34
66: ; preds = %54, %45, %59, %28, %24
%67 = phi i8* [ null, %24 ], [ null, %28 ], [ null, %59 ], [ null, %45 ], [ %49, %54 ]
ret i8* %67
}