// File: /workspace/github_cpp_makefile_star2_O3/Fohlen[P]cubescript-standalone/stream.cpp
// Function: findfile(char const*, char const*)
const char *findfile(const char *filename, const char *mode)
{
    static string s;
    if(homedir[0])
    {
        formatstring(s)("%s%s", homedir, filename);
        if(fileexists(s, mode)) return s;
        if(mode[0]=='w' || mode[0]=='a')
        {
            string dirs;
            copystring(dirs, s);
            char *dir = strchr(dirs[0]==PATHDIV ? dirs+1 : dirs, PATHDIV);
            while(dir)
            {
                *dir = '\0';
                if(!fileexists(dirs, "d") && !createdir(dirs)) return s;
                *dir = PATHDIV;
                dir = strchr(dir+1, PATHDIV);
            }
            return s;
        }
    }
    if(mode[0]=='w' || mode[0]=='a') return filename;
    loopv(packagedirs)
    {
        packagedir &pf = packagedirs[i];
        if(pf.filter && strncmp(filename, pf.filter, pf.filterlen)) continue;
        formatstring(s)("%s%s", pf.dir, filename);
        if(fileexists(s, mode)) return s;
    }
    if(mode[0]=='e') return NULL;
    return filename;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, %r12
movq %rdi, %r14
cmpb $0x0, 0xbe33(%rip)      # 0x20140
je 0x1441d
leaq 0xc386(%rip), %rbx      # 0x206a0
leaq -0x140(%rbp), %rdi
movq %rbx, (%rdi)
leaq 0x49f7(%rip), %rsi      # 0x18d22
leaq 0xbe0e(%rip), %rdx      # 0x20140
movq %r14, %rcx
xorl %eax, %eax
callq 0x12e1e
movq %rbx, %rdi
movq %r12, %rsi
callq 0x13e29
testb %al, %al
jne 0x144e3
movzbl (%r12), %eax
cmpl $0x77, %eax
je 0x14362
cmpl $0x61, %eax
jne 0x14421
leaq 0xc337(%rip), %rbx      # 0x206a0
movq %rbx, %rdi
callq 0x2080
movq %rax, %r14
incq %r14
movl $0x104, %eax            # imm = 0x104
cmpq %rax, %r14
cmovaeq %rax, %r14
leaq -0x140(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2130
movb $0x0, -0x141(%rbp,%r14)
xorl %eax, %eax
cmpb $0x2f, (%r15)
sete %al
leaq (%rax,%rbp), %rdi
addq $-0x140, %rdi           # imm = 0xFEC0
movl $0x2f, %esi
callq 0x2060
testq %rax, %rax
je 0x144e3
movq %rax, %r14
leaq 0x3ad0(%rip), %r15      # 0x17ea2
leaq -0x140(%rbp), %r12
movb $0x0, (%r14)
movq %r12, %rdi
movq %r15, %rsi
callq 0x13e29
testb %al, %al
jne 0x143fc
movq %r12, %rdi
callq 0x13e8b
testb %al, %al
je 0x144e3
movb $0x2f, (%r14)
incq %r14
movq %r14, %rdi
movl $0x2f, %esi
callq 0x2060
movq %rax, %r14
testq %rax, %rax
jne 0x143d9
jmp 0x144e3
movb (%r12), %al
cmpb $0x61, %al
je 0x144e0
movzbl %al, %ecx
cmpl $0x77, %ecx
je 0x144e0
movl 0xbe18(%rip), %r15d     # 0x20254
testl %r15d, %r15d
jle 0x144d8
movq %r12, -0x38(%rbp)
movq %r14, -0x30(%rbp)
movq 0xbdf4(%rip), %r14      # 0x20248
leaq 0xc245(%rip), %rbx      # 0x206a0
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x8(%r14,%r12), %rsi
testq %rsi, %rsi
je 0x1447d
movq 0x18(%r14,%r12), %rdx
movq -0x30(%rbp), %rdi
callq 0x20a0
testl %eax, %eax
jne 0x144bf
movq %rbx, -0x140(%rbp)
movq (%r14,%r12), %rdx
leaq -0x140(%rbp), %rdi
leaq 0x488c(%rip), %rsi      # 0x18d22
movq -0x30(%rbp), %rcx
xorl %eax, %eax
callq 0x12e1e
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0x13e29
testb %al, %al
jne 0x144e3
movq 0xbd90(%rip), %r14      # 0x20248
movl 0xbd95(%rip), %r15d     # 0x20254
incq %r13
movslq %r15d, %rax
addq $0x20, %r12
cmpq %rax, %r13
jl 0x14461
movq -0x38(%rbp), %rax
movb (%rax), %al
movq -0x30(%rbp), %r14
xorl %ecx, %ecx
cmpb $0x65, %al
cmoveq %rcx, %r14
movq %r14, %rbx
movq %rbx, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq