// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rdi, %rbx
cmpb $0x0, 0xbb40(%rip)      # 0x1a150
je 0xe6d8
leaq 0xc093(%rip), %r15      # 0x1a6b0
leaq -0x140(%rbp), %rdi
movq %r15, (%rdi)
leaq 0x4194(%rip), %rsi      # 0x127c2
leaq 0xbb1b(%rip), %rdx      # 0x1a150
movq %rbx, %rcx
xorl %eax, %eax
callq 0xd450
movq %r15, %rdi
movq %r14, %rsi
callq 0xe2dc
testb %al, %al
jne 0xe6ea
movzbl (%r14), %eax
cmpl $0x77, %eax
je 0xe660
cmpl $0x61, %eax
jne 0xe6db
leaq 0xc049(%rip), %rsi      # 0x1a6b0
leaq -0x140(%rbp), %r14
movl $0x104, %edx            # imm = 0x104
movq %r14, %rdi
movq %rsi, %rbx
callq 0xd24d
xorl %eax, %eax
cmpb $0x2f, (%r14)
sete %al
leaq (%rax,%rbp), %r13
addq $-0x140, %r13           # imm = 0xFEC0
pushq $0x2f
popq %r15
leaq 0x32a5(%rip), %r12      # 0x11942
movq %r13, %rdi
movl %r15d, %esi
callq 0x2070
testq %rax, %rax
je 0xe6e7
movq %rax, %r13
movb $0x0, (%rax)
movq %r14, %rdi
movq %r12, %rsi
callq 0xe2dc
testb %al, %al
jne 0xe6ce
movq %r14, %rdi
callq 0xe33c
testb %al, %al
je 0xe6e7
movb $0x2f, (%r13)
incq %r13
jmp 0xe69d
movb (%r14), %al
cmpb $0x61, %al
je 0xe6e7
movzbl %al, %eax
cmpl $0x77, %eax
jne 0xe6ff
movq %rbx, %r15
movq %r15, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, -0x30(%rbp)
movl 0xbb5a(%rip), %r15d     # 0x1a264
xorl %r12d, %r12d
xorl %r13d, %r13d
movslq %r15d, %rax
cmpq %rax, %r13
jge 0xe788
movq 0xbb39(%rip), %r14      # 0x1a258
movq 0x8(%r14,%r12), %rsi
testq %rsi, %rsi
je 0xe73a
movq 0x18(%r14,%r12), %rdx
movq %rbx, %rdi
callq 0x20c0
testl %eax, %eax
jne 0xe77f
leaq 0xbf6f(%rip), %r15      # 0x1a6b0
movq %r15, -0x140(%rbp)
movq (%r14,%r12), %rdx
leaq -0x140(%rbp), %rdi
leaq 0x4068(%rip), %rsi      # 0x127c2
movq %rbx, %rcx
xorl %eax, %eax
callq 0xd450
movq %r15, %rdi
movq -0x30(%rbp), %rsi
callq 0xe2dc
testb %al, %al
jne 0xe6ea
movl 0xbae5(%rip), %r15d     # 0x1a264
incq %r13
addq $0x20, %r12
jmp 0xe710
xorl %eax, %eax
movq -0x30(%rbp), %rcx
cmpb $0x65, (%rcx)
movq %rbx, %r15
cmoveq %rax, %r15
jmp 0xe6ea