#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 F_OK;
int X_OK;
scalar_t__ access(char const *, int);
char *find_env_by_key(char *, char *const *);
char *resolve_path2(char const *, char *);
int /*<<< orphan*/ *strchr(char const *, char);
char *strdup(char const *);
char *resolve_path(const char *file, char *const *arge) {
  char *path;
  path = find_env_by_key("PATH=", arge);
  if (strchr(file, '/') == NULL) {
    return resolve_path2(file, path);
  }
  if (access(file, X_OK | F_OK) == 0) {
    return strdup(file);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<resolve_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe95(%rip),%rdi
call   1050 <find_env_by_key@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
jne    11a2 <resolve_path+0x52>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <resolve_path2@plt>
mov    %rax,-0x8(%rbp)
jmp    11e1 <resolve_path+0x91>
mov    -0x10(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    11d9 <resolve_path+0x89>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11e1 <resolve_path+0x91>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_path>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xea2(%rip),%rdi
call   1050 <find_env_by_key@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11a1 <resolve_path+0x51>
mov    0x2e69(%rip),%rax
mov    0x2e4a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1060 <access@plt>
test   %rax,%rax
je     11b3 <resolve_path+0x63>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <resolve_path2@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <strdup@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"PATH=\00"
@X_OK = global i32 0
@F_OK = global i32 0
define i8* @resolve_path(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%7 = load i8**, i8*** %5
%8 = call i8* @find_env_by_key(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %7)
store i8* %8, i8** %6
%9 = load i8*, i8** %4
%10 = call i32* @strchr(i8* %9, i8 signext 47)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i8*, i8** %6
%15 = call i8* @resolve_path2(i8* %13, i8* %14)
store i8* %15, i8** %3
br label %27
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = load i32, i32* @X_OK
%19 = load i32, i32* @F_OK
%20 = or i32 %18, %19
%21 = call i64 @access(i8* %17, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i8*, i8** %4
%25 = call i8* @strdup(i8* %24)
store i8* %25, i8** %3
br label %27
26: ; preds = %16
store i8* null, i8** %3
br label %27
27: ; preds = %26, %23, %12
%28 = load i8*, i8** %3
ret i8* %28
}

LLVM IR (-O2):
@.str = [6 x i8] c"PATH=\00"
@X_OK = global i32 0
@F_OK = global i32 0
define i8* @resolve_path(i8* %0, i8** %1) {
%3 = tail call i8* @find_env_by_key(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %1)
%4 = tail call i32* @strchr(i8* %0, i8 signext 47)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i8* @resolve_path2(i8* %0, i8* %3)
br label %16
8: ; preds = %2
%9 = load i32, i32* @X_OK
%10 = load i32, i32* @F_OK
%11 = or i32 %10, %9
%12 = tail call i64 @access(i8* %0, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i8* @strdup(i8* %0)
br label %16
16: ; preds = %8, %14, %6
%17 = phi i8* [ %7, %6 ], [ %15, %14 ], [ null, %8 ]
ret i8* %17
}