#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*/ free(char *);
int /*<<< orphan*/ get_name(char const *, char *, char const *, int);
char const *getenv(char *);
char *strdup(char const *);
char *strrchr(char const *, char);
char *strtok(char *, char *);
int from_path(const char *pre, const char *str) {
  char *path, *next;
  const char *name;
  int mode = 0;
  if (str[0] == '/') {
    path = strdup(str);
    *strrchr(path, '/') = '\0';
    name = strrchr(str, '/') + 1;
    mode = 01000000;
  } else {
    path = strdup(getenv("PWD"));
    name = str;
  }
  if (!pre || pre[0] == '\0') {
    if (!(path = strdup(getenv("PATH"))))
      return 1;
    for (next = strtok(path, ":"); next; next = strtok(NULL, ":"))
      get_name(pre, next, name, mode | 0111);
  } else {
    get_name(pre, path, name, mode | 0444);
  }
  free(path);
  return 0;
}


x86 assembly (clang, -O0):
<from_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11c7 <from_path+0x67>
mov    -0x18(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
movl   $0x40000,-0x34(%rbp)
jmp    11e7 <from_path+0x87>
lea    0xe32(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1070 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1202 <from_path+0xa2>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1284 <from_path+0x124>
lea    0xdfb(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1070 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1230 <from_path+0xd0>
movl   $0x1,-0x4(%rbp)
jmp    12ae <from_path+0x14e>
mov    -0x20(%rbp),%rdi
lea    0xdce(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     127f <from_path+0x11f>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x34(%rbp),%ecx
or     $0x49,%ecx
call   1080 <get_name@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd98(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    1244 <from_path+0xe4>
jmp    129e <from_path+0x13e>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x34(%rbp),%ecx
or     $0x124,%ecx
call   1080 <get_name@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<from_path>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
cmpb   $0x2f,(%rsi)
jne    11ae <from_path+0x4e>
mov    %rbx,%rdi
call   1070 <strdup@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,%rbx
add    $0x1,%rbx
mov    $0x40000,%ebp
test   %r14,%r14
jne    11cc <from_path+0x6c>
jmp    11ea <from_path+0x8a>
lea    0xe4b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1070 <strdup@plt>
mov    %rax,%r15
xor    %ebp,%ebp
test   %r14,%r14
je     11ea <from_path+0x8a>
cmpb   $0x0,(%r14)
je     11ea <from_path+0x8a>
or     $0x124,%ebp
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1080 <get_name@plt>
jmp    124f <from_path+0xef>
lea    0xe13(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1070 <strdup@plt>
test   %rax,%rax
je     125b <from_path+0xfb>
mov    %rax,%r15
lea    0xdfc(%rip),%rsi
mov    %rax,%rdi
call   1060 <strtok@plt>
test   %rax,%rax
je     124f <from_path+0xef>
or     $0x49,%ebp
lea    0xde5(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1080 <get_name@plt>
xor    %edi,%edi
mov    %r12,%rsi
call   1060 <strtok@plt>
test   %rax,%rax
jne    1230 <from_path+0xd0>
mov    %r15,%rdi
call   1040 <free@plt>
xor    %eax,%eax
jmp    1260 <from_path+0x100>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"PWD\00"
@.str.1 = [5 x i8] c"PATH\00"
@.str.2 = [2 x i8] c":\00"
define i32 @from_path(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %9
%10 = load i8*, i8** %5
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 47
br i1 %14, label %15, label %23
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = call i8* @strrchr(i8* %18, i8 signext 47)
store i8 0, i8* %19
%20 = load i8*, i8** %5
%21 = call i8* @strrchr(i8* %20, i8 signext 47)
%22 = getelementptr inbounds i8, i8* %21, i64 1
store i8* %22, i8** %8
store i32 262144, i32* %9
br label %27
23: ; preds = %2
%24 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%25 = call i8* @strdup(i8* %24)
store i8* %25, i8** %6
%26 = load i8*, i8** %5
store i8* %26, i8** %8
br label %27
27: ; preds = %23, %15
%28 = load i8*, i8** %4
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %36
30: ; preds = %27
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i64 0
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %57
36: ; preds = %30, %27
%37 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%38 = call i8* @strdup(i8* %37)
store i8* %38, i8** %6
%39 = icmp ne i8* %38, null
br i1 %39, label %41, label %40
40: ; preds = %36
store i32 1, i32* %3
br label %67
41: ; preds = %36
%42 = load i8*, i8** %6
%43 = call i8* @strtok(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %43, i8** %7
br label %44
44: ; preds = %54, %41
%45 = load i8*, i8** %7
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %56
47: ; preds = %44
%48 = load i8*, i8** %4
%49 = load i8*, i8** %7
%50 = load i8*, i8** %8
%51 = load i32, i32* %9
%52 = or i32 %51, 73
%53 = call i32 @get_name(i8* %48, i8* %49, i8* %50, i32 %52)
br label %54
54: ; preds = %47
%55 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %55, i8** %7
br label %44
56: ; preds = %44
br label %64
57: ; preds = %30
%58 = load i8*, i8** %4
%59 = load i8*, i8** %6
%60 = load i8*, i8** %8
%61 = load i32, i32* %9
%62 = or i32 %61, 292
%63 = call i32 @get_name(i8* %58, i8* %59, i8* %60, i32 %62)
br label %64
64: ; preds = %57, %56
%65 = load i8*, i8** %6
%66 = call i32 @free(i8* %65)
store i32 0, i32* %3
br label %67
67: ; preds = %64, %40
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [4 x i8] c"PWD\00"
@.str.1 = [5 x i8] c"PATH\00"
@.str.2 = [2 x i8] c":\00"
define i32 @from_path(i8* %0, i8* %1) {
%3 = load i8, i8* %1
%4 = icmp eq i8 %3, 47
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = tail call i8* @strdup(i8* %1)
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %6, i8 signext 47)
store i8 0, i8* %7
%8 = tail call i8* @strrchr(i8* %1, i8 signext 47)
%9 = getelementptr inbounds i8, i8* %8, i64 1
br label %13
10: ; preds = %2
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%12 = tail call i8* @strdup(i8* %11)
br label %13
13: ; preds = %10, %5
%14 = phi i8* [ %6, %5 ], [ %12, %10 ]
%15 = phi i8* [ %9, %5 ], [ %1, %10 ]
%16 = phi i32 [ 262144, %5 ], [ 0, %10 ]
%17 = icmp eq i8* %0, null
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = load i8, i8* %0
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %35
21: ; preds = %18, %13
%22 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i8* @strdup(i8* %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %41, label %25
25: ; preds = %21
%26 = tail call i8* @strtok(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i8* %26, null
br i1 %27, label %38, label %28
28: ; preds = %25
%29 = or i32 %16, 73
br label %30
30: ; preds = %28, %30
%31 = phi i8* [ %26, %28 ], [ %33, %30 ]
%32 = tail call i32 @get_name(i8* %0, i8* %31, i8* %15, i32 %29)
%33 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%34 = icmp eq i8* %33, null
br i1 %34, label %38, label %30
35: ; preds = %18
%36 = or i32 %16, 292
%37 = tail call i32 @get_name(i8* %0, i8* %14, i8* %15, i32 %36)
br label %38
38: ; preds = %30, %25, %35
%39 = phi i8* [ %14, %35 ], [ %23, %25 ], [ %23, %30 ]
%40 = tail call i32 @free(i8* %39)
br label %41
41: ; preds = %21, %38
%42 = phi i32 [ 0, %38 ], [ 1, %21 ]
ret i32 %42
}