#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 *, int);
scalar_t__ asprintf(char **, char *, char *, char const *);
int /*<<< orphan*/ free(char *);
char *strdup(char const *);
scalar_t__ strlen(char *);
char *strtok_r(char *, char *, char **);
char *resolve_path2(const char *file, const char *path) {
  char *path2;
  char *cpath;
  char *cfile;
  char *ptmp = NULL;
  path2 = strdup(path);
  for (cpath = path2; (cpath = strtok_r(cpath, ":", &ptmp)) != NULL;
       cpath += strlen(cpath) + 1) {
    if (asprintf(&cfile, "%s/%s", cpath, file) > 0) {
      if (access(cfile, X_OK | F_OK) == 0) {
        free(path2);
        return cfile;
      }
      free(cfile);
    }
  }
  free(path2);
  return NULL;
}


x86 assembly (clang, -O0):
<resolve_path2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe68(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1234 <resolve_path2+0xd4>
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x30(%rbp),%rdi
lea    0xe40(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0x0,%rax
jle    1215 <resolve_path2+0xb5>
mov    -0x30(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    0x2deb(%rip),%rax
or     (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
jne    120c <resolve_path2+0xac>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1245 <resolve_path2+0xe5>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    121a <resolve_path2+0xba>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    118d <resolve_path2+0x2d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_path2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r13
movq   $0x0,0x10(%rsp)
mov    %rsi,%rdi
call   1080 <strdup@plt>
lea    0xe77(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rax,0x8(%rsp)
mov    %rax,%rdi
call   1050 <strtok_r@plt>
test   %rax,%rax
je     1224 <resolve_path2+0xc4>
mov    %rax,%rbx
lea    0xe54(%rip),%r15
mov    %rsp,%r12
lea    0xe48(%rip),%rbp
lea    0x10(%rsp),%r14
jmp    11e3 <resolve_path2+0x83>
nop
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbx,1),%rdi
add    $0x1,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1050 <strtok_r@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1224 <resolve_path2+0xc4>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1060 <asprintf@plt>
test   %rax,%rax
jle    11c0 <resolve_path2+0x60>
mov    (%rsp),%rdi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%rip),%rax
or     (%rax),%esi
call   1070 <access@plt>
test   %rax,%rax
je     1232 <resolve_path2+0xd2>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    11c0 <resolve_path2+0x60>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1240 <resolve_path2+0xe0>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    (%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c":\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@X_OK = global i32 0
@F_OK = global i32 0
define i8* @resolve_path2(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %9
%10 = load i8*, i8** %5
%11 = call i8* @strdup(i8* %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
store i8* %12, i8** %7
br label %13
13: ; preds = %37, %2
%14 = load i8*, i8** %7
%15 = call i8* @strtok_r(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %9)
store i8* %15, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %43
17: ; preds = %13
%18 = load i8*, i8** %7
%19 = load i8*, i8** %4
%20 = call i64 @asprintf(i8** %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* %19)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %17
%23 = load i8*, i8** %8
%24 = load i32, i32* @X_OK
%25 = load i32, i32* @F_OK
%26 = or i32 %24, %25
%27 = call i64 @access(i8* %23, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %22
%30 = load i8*, i8** %6
%31 = call i32 @free(i8* %30)
%32 = load i8*, i8** %8
store i8* %32, i8** %3
br label %46
33: ; preds = %22
%34 = load i8*, i8** %8
%35 = call i32 @free(i8* %34)
br label %36
36: ; preds = %33, %17
br label %37
37: ; preds = %36
%38 = load i8*, i8** %7
%39 = call i64 @strlen(i8* %38)
%40 = add nsw i64 %39, 1
%41 = load i8*, i8** %7
%42 = getelementptr inbounds i8, i8* %41, i64 %40
store i8* %42, i8** %7
br label %13
43: ; preds = %13
%44 = load i8*, i8** %6
%45 = call i32 @free(i8* %44)
store i8* null, i8** %3
br label %46
46: ; preds = %43, %29
%47 = load i8*, i8** %3
ret i8* %47
}

LLVM IR (-O2):
@.str = [2 x i8] c":\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@X_OK = global i32 0
@F_OK = global i32 0
define i8* @resolve_path2(i8* %0, i8* nocapture %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = bitcast i8** %3 to i8*
%6 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%7 = tail call i8* @strdup(i8* %1)
%8 = call i8* @strtok_r(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4)
%9 = icmp eq i8* %8, null
br i1 %9, label %33, label %10
10: ; preds = %2, %27
%11 = phi i8* [ %31, %27 ], [ %8, %2 ]
%12 = call i64 @asprintf(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %11, i8* %0)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = load i32, i32* @X_OK
%17 = load i32, i32* @F_OK
%18 = or i32 %17, %16
%19 = call i64 @access(i8* %15, i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = call i32 @free(i8* %7)
%23 = load i8*, i8** %3
br label %35
24: ; preds = %14
%25 = load i8*, i8** %3
%26 = call i32 @free(i8* %25)
br label %27
27: ; preds = %10, %24
%28 = call i64 @strlen(i8* %11)
%29 = add nsw i64 %28, 1
%30 = getelementptr inbounds i8, i8* %11, i64 %29
%31 = call i8* @strtok_r(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %10
33: ; preds = %27, %2
%34 = call i32 @free(i8* %7)
br label %35
35: ; preds = %33, %21
%36 = phi i8* [ %23, %21 ], [ null, %33 ]
ret i8* %36
}