#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 PATH_MAX;
char *filepath_sfind(char **, char *, char const *);
char *getenv(char *);
int /*<<< orphan*/ *realpath(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *,
                            ...);
int /*<<< orphan*/ strcpy(char *, char *);
char *filepath_sfinddef(char **respathv, char *filepath, const char *filename,
                        const char *def) {
  char t[PATH_MAX];
  char *ret;
  if ((respathv == NULL) || (respathv[0] == NULL) || (filepath == NULL))
    return NULL;
  ret = filepath_sfind(respathv, filepath, filename);
  if ((ret == NULL) && (def != NULL)) {
    ret = filepath;
    if (def[0] == '~') {
      snprintf(t, PATH_MAX, "%s/%s/%s", getenv("HOME"), def + 1, filename);
    } else {
      snprintf(t, PATH_MAX, "%s/%s", def, filename);
    }
    if (realpath(t, ret) == NULL) {
      strcpy(ret, t);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<filepath_sfinddef>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b8 <filepath_sfinddef+0x68>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     11b8 <filepath_sfinddef+0x68>
cmpq   $0x0,-0x18(%rbp)
jne    11cc <filepath_sfinddef+0x7c>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12ab <filepath_sfinddef+0x15b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <filepath_sfind@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    129c <filepath_sfinddef+0x14c>
cmpq   $0x0,-0x28(%rbp)
je     129c <filepath_sfinddef+0x14c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    1250 <filepath_sfinddef+0x100>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
lea    0xde7(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%esi
mov    %rax,%rcx
mov    -0x28(%rbp),%r8
add    $0x1,%r8
mov    -0x20(%rbp),%r9
lea    0xdbc(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    1273 <filepath_sfinddef+0x123>
mov    -0x50(%rbp),%rdi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xd97(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1070 <realpath@plt>
cmp    $0x0,%rax
jne    1297 <filepath_sfinddef+0x147>
mov    -0x50(%rbp),%rsi
mov    -0x40(%rbp),%rdi
call   1040 <strcpy@plt>
jmp    129c <filepath_sfinddef+0x14c>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filepath_sfinddef>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e8b(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %rdi,%rdi
je     11e9 <filepath_sfinddef+0x99>
mov    %rsi,%r14
xor    %eax,%eax
test   %rsi,%rsi
je     1225 <filepath_sfinddef+0xd5>
cmpq   $0x0,(%rdi)
je     1225 <filepath_sfinddef+0xd5>
mov    %rcx,%r12
mov    %rdx,%r15
mov    %r14,%rsi
call   1060 <filepath_sfind@plt>
test   %r12,%r12
je     1225 <filepath_sfinddef+0xd5>
test   %rax,%rax
jne    1225 <filepath_sfinddef+0xd5>
cmpb   $0x7e,(%r12)
mov    0x0(%r13),%r13d
jne    11ed <filepath_sfinddef+0x9d>
lea    0xe48(%rip),%rdi
call   1030 <getenv@plt>
add    $0x1,%r12
lea    0xe2f(%rip),%rdx
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %rax,%rcx
mov    %r12,%r8
mov    %r15,%r9
xor    %eax,%eax
call   1050 <snprintf@plt>
jmp    1207 <filepath_sfinddef+0xb7>
xor    %eax,%eax
jmp    1225 <filepath_sfinddef+0xd5>
lea    0xe0f(%rip),%rdx
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %r12,%rcx
mov    %r15,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <realpath@plt>
test   %rax,%rax
jne    1222 <filepath_sfinddef+0xd2>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [9 x i8] c"%s/%s/%s\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i8* @filepath_sfinddef(i8** %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
store i8** %0, i8*** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %10
%17 = alloca i8, i64 %15
store i64 %15, i64* %11
%18 = load i8**, i8*** %6
%19 = icmp eq i8** %18, null
br i1 %19, label %28, label %20
20: ; preds = %4
%21 = load i8**, i8*** %6
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = icmp eq i8* %23, null
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = load i8*, i8** %7
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %25, %20, %4
store i8* null, i8** %5
store i32 1, i32* %13
br label %68
29: ; preds = %25
%30 = load i8**, i8*** %6
%31 = load i8*, i8** %7
%32 = load i8*, i8** %8
%33 = call i8* @filepath_sfind(i8** %30, i8* %31, i8* %32)
store i8* %33, i8** %12
%34 = load i8*, i8** %12
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %66
36: ; preds = %29
%37 = load i8*, i8** %9
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %66
39: ; preds = %36
%40 = load i8*, i8** %7
store i8* %40, i8** %12
%41 = load i8*, i8** %9
%42 = getelementptr inbounds i8, i8* %41, i64 0
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 126
br i1 %45, label %46, label %53
46: ; preds = %39
%47 = load i32, i32* @PATH_MAX
%48 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%49 = load i8*, i8** %9
%50 = getelementptr inbounds i8, i8* %49, i64 1
%51 = load i8*, i8** %8
%52 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %17, i32 %47, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %48, i8* %50, i8* %51)
br label %58
53: ; preds = %39
%54 = load i32, i32* @PATH_MAX
%55 = load i8*, i8** %9
%56 = load i8*, i8** %8
%57 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %17, i32 %54, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %55, i8* %56)
br label %58
58: ; preds = %53, %46
%59 = load i8*, i8** %12
%60 = call i32* @realpath(i8* %17, i8* %59)
%61 = icmp eq i32* %60, null
br i1 %61, label %62, label %65
62: ; preds = %58
%63 = load i8*, i8** %12
%64 = call i32 @strcpy(i8* %63, i8* %17)
br label %65
65: ; preds = %62, %58
br label %66
66: ; preds = %65, %36, %29
%67 = load i8*, i8** %12
store i8* %67, i8** %5
store i32 1, i32* %13
br label %68
68: ; preds = %66, %28
%69 = load i8*, i8** %10
%70 = load i8*, i8** %5
ret i8* %70
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [9 x i8] c"%s/%s/%s\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i8* @filepath_sfinddef(i8** %0, i8* %1, i8* %2, i8* %3) {
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = icmp eq i8** %0, null
br i1 %8, label %34, label %9
9: ; preds = %4
%10 = load i8*, i8** %0
%11 = icmp eq i8* %10, null
%12 = icmp eq i8* %1, null
%13 = or i1 %12, %11
br i1 %13, label %34, label %14
14: ; preds = %9
%15 = tail call i8* @filepath_sfind(i8** %0, i8* %1, i8* %2)
%16 = icmp eq i8* %15, null
%17 = icmp ne i8* %3, null
%18 = and i1 %17, %16
br i1 %18, label %19, label %34
19: ; preds = %14
%20 = load i8, i8* %3
%21 = icmp eq i8 %20, 126
%22 = load i32, i32* @PATH_MAX
br i1 %21, label %23, label %27
23: ; preds = %19
%24 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = getelementptr inbounds i8, i8* %3, i64 1
%26 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %7, i32 %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %24, i8* %25, i8* %2)
br label %29
27: ; preds = %19
%28 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %7, i32 %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %3, i8* %2)
br label %29
29: ; preds = %27, %23
%30 = call i32* @realpath(i8* %7, i8* %1)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @strcpy(i8* %1, i8* %7)
br label %34
34: ; preds = %14, %32, %29, %4, %9
%35 = phi i8* [ null, %9 ], [ null, %4 ], [ %1, %32 ], [ %1, %29 ], [ %15, %14 ]
ret i8* %35
}