#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 */
typedef int /*<<< orphan*/ va_list;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(int);
char *realpath(char *, char *);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
char *filepath_vsfindfile(char *str, const char *format, va_list ap) {
  char *realpathname;
  char *pathname;
  int len;
  pathname = malloc(PATH_MAX);
  if (pathname == NULL)
    return NULL;
  len = vsnprintf(pathname, PATH_MAX, format, ap);
  if ((len < 0) || (len >= PATH_MAX)) {
    free(pathname);
    return NULL;
  }
  realpathname = realpath(pathname, str);
  free(pathname);
  if (realpathname != NULL) {
    if (access(realpathname, R_OK) != 0) {
      return NULL;
    }
  }
  return realpathname;
}


x86 assembly (clang, -O0):
<filepath_vsfindfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    118d <filepath_vsfindfile+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    1236 <filepath_vsfindfile+0xe6>
mov    -0x30(%rbp),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     11c5 <filepath_vsfindfile+0x75>
mov    -0x34(%rbp),%eax
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%eax
jl     11db <filepath_vsfindfile+0x8b>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1236 <filepath_vsfindfile+0xe6>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <realpath@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x28(%rbp)
je     122e <filepath_vsfindfile+0xde>
mov    -0x28(%rbp),%rdi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
je     1229 <filepath_vsfindfile+0xd9>
movq   $0x0,-0x8(%rbp)
jmp    1236 <filepath_vsfindfile+0xe6>
jmp    122e <filepath_vsfindfile+0xde>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filepath_vsfindfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%r14
mov    0x2e85(%rip),%r13
mov    0x0(%r13),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11d3 <filepath_vsfindfile+0x83>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1040 <vsnprintf@plt>
test   %eax,%eax
js     11cb <filepath_vsfindfile+0x7b>
cmp    0x0(%r13),%eax
jge    11cb <filepath_vsfindfile+0x7b>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <realpath@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
test   %r14,%r14
je     11d6 <filepath_vsfindfile+0x86>
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <access@plt>
test   %rax,%rax
jne    11d3 <filepath_vsfindfile+0x83>
jmp    11d6 <filepath_vsfindfile+0x86>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@R_OK = global i32 0
define i8* @filepath_vsfindfile(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* @PATH_MAX
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %3
store i8* null, i8** %4
br label %48
16: ; preds = %3
%17 = load i8*, i8** %9
%18 = load i32, i32* @PATH_MAX
%19 = load i8*, i8** %6
%20 = load i32, i32* %7
%21 = call i32 @vsnprintf(i8* %17, i32 %18, i8* %19, i32 %20)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp slt i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %16
%25 = load i32, i32* %10
%26 = load i32, i32* @PATH_MAX
%27 = icmp sge i32 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %24, %16
%29 = load i8*, i8** %9
%30 = call i32 @free(i8* %29)
store i8* null, i8** %4
br label %48
31: ; preds = %24
%32 = load i8*, i8** %9
%33 = load i8*, i8** %5
%34 = call i8* @realpath(i8* %32, i8* %33)
store i8* %34, i8** %8
%35 = load i8*, i8** %9
%36 = call i32 @free(i8* %35)
%37 = load i8*, i8** %8
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %46
39: ; preds = %31
%40 = load i8*, i8** %8
%41 = load i32, i32* @R_OK
%42 = call i64 @access(i8* %40, i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %39
store i8* null, i8** %4
br label %48
45: ; preds = %39
br label %46
46: ; preds = %45, %31
%47 = load i8*, i8** %8
store i8* %47, i8** %4
br label %48
48: ; preds = %46, %44, %28, %15
%49 = load i8*, i8** %4
ret i8* %49
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@R_OK = global i32 0
define i8* @filepath_vsfindfile(i8* %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @PATH_MAX
%5 = tail call i8* @malloc(i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %24, label %7
7: ; preds = %3
%8 = tail call i32 @vsnprintf(i8* %5, i32 %4, i8* %1, i32 %2)
%9 = icmp sgt i32 %8, -1
%10 = load i32, i32* @PATH_MAX
%11 = icmp slt i32 %8, %10
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %15, label %13
13: ; preds = %7
%14 = tail call i32 @free(i8* %5)
br label %24
15: ; preds = %7
%16 = tail call i8* @realpath(i8* %5, i8* %0)
%17 = tail call i32 @free(i8* %5)
%18 = icmp eq i8* %16, null
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i32, i32* @R_OK
%21 = tail call i64 @access(i8* %16, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19, %15
br label %24
24: ; preds = %19, %3, %23, %13
%25 = phi i8* [ null, %13 ], [ %16, %23 ], [ null, %3 ], [ null, %19 ]
ret i8* %25
}