// File: /workspace/github_cpp_makefile_star2_O1/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, -0x30(%rbp)
cmpb $0x0, 0xb2a3(%rip)      # 0x1f140
je 0x13fb3
leaq 0xb7f6(%rip), %rbx      # 0x1f6a0
leaq -0x140(%rbp), %rdi
movq %rbx, (%rdi)
leaq 0x3d07(%rip), %rsi      # 0x17bc2
leaq 0xb27e(%rip), %rdx      # 0x1f140
movq -0x30(%rbp), %rcx
xorl %eax, %eax
callq 0x128d0
movq %rbx, %rdi
movq %r14, %r15
movq %r14, %rsi
callq 0x13996
testb %al, %al
jne 0x14080
movzbl (%r15), %eax
cmpl $0x77, %eax
je 0x13ef8
movq %r15, %r14
cmpl $0x61, %eax
jne 0x13fb3
leaq 0xb7a1(%rip), %rbx      # 0x1f6a0
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 0x14080
movq %rax, %r14
leaq 0x2dda(%rip), %r15      # 0x16d42
leaq -0x140(%rbp), %r12
movb $0x0, (%r14)
movq %r12, %rdi
movq %r15, %rsi
callq 0x13996
testb %al, %al
jne 0x13f92
movq %r12, %rdi
callq 0x139f8
testb %al, %al
je 0x14080
movb $0x2f, (%r14)
incq %r14
movq %r14, %rdi
movl $0x2f, %esi
callq 0x2060
movq %rax, %r14
testq %rax, %rax
jne 0x13f6f
jmp 0x14080
movzbl (%r14), %eax
cmpl $0x61, %eax
je 0x13fc1
cmpl $0x77, %eax
jne 0x13fca
movq -0x30(%rbp), %rbx
jmp 0x14080
movq %r14, -0x38(%rbp)
cmpl $0x0, 0xb27f(%rip)      # 0x1f254
setg %r12b
jle 0x1406f
leaq 0xb6ba(%rip), %rbx      # 0x1f6a0
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0xb255(%rip), %r14      # 0x1f248
movq 0x8(%r14,%r13), %rsi
testq %rsi, %rsi
je 0x14014
movq 0x18(%r14,%r13), %rdx
movq -0x30(%rbp), %rdi
callq 0x20a0
movl $0x6, %ecx
testl %eax, %eax
jne 0x14047
movq %rbx, -0x140(%rbp)
movq (%r14,%r13), %rdx
leaq -0x140(%rbp), %rdi
leaq 0x3b95(%rip), %rsi      # 0x17bc2
movq -0x30(%rbp), %rcx
xorl %eax, %eax
callq 0x128d0
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0x13996
movzbl %al, %ecx
cmpl $0x6, %ecx
je 0x14050
testl %ecx, %ecx
jne 0x14069
incq %r15
movslq 0xb1fa(%rip), %rax      # 0x1f254
addq $0x20, %r13
cmpq %rax, %r15
setl %r12b
jl 0x13fec
jmp 0x1406f
testb $0x1, %r12b
jne 0x14080
xorl %eax, %eax
movq -0x38(%rbp), %rcx
cmpb $0x65, (%rcx)
movq -0x30(%rbp), %rbx
cmoveq %rax, %rbx
movq %rbx, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq