// File: /workspace/github_cpp_makefile_star2_O3/FreeSlave[P]vhlt/hlrad/qrad.cpp
// Function: LoadRadFiles(char const*, char const*, char const*)
void            LoadRadFiles(const char* const mapname, const char* const user_rad, const char* argv0)
{
    char global_lights[_MAX_PATH];
    char mapname_lights[_MAX_PATH];

    char mapfile[_MAX_PATH];
    char mapdir[_MAX_PATH];
    char appdir[_MAX_PATH];

    // Get application directory (only an approximation on posix systems)
    // try looking in the directory we were run from
    {
        char tmp[_MAX_PATH];
        memset(tmp, 0, sizeof(tmp));
#ifdef SYSTEM_WIN32
        GetModuleFileName(NULL, tmp, _MAX_PATH);
#else
        safe_strncpy(tmp, argv0, _MAX_PATH);
#endif
        ExtractFilePath(tmp, appdir);
    }

    // Get map directory
    ExtractFilePath(mapname, mapdir);
#ifdef ZHLT_DEFAULTEXTENSION_FIX
	ExtractFile(mapname, mapfile);
#else
    ExtractFileBase(mapname, mapfile);
#endif

    // Look for lights.rad in mapdir
    safe_strncpy(global_lights, mapdir, _MAX_PATH);
    safe_strncat(global_lights, lights_rad, _MAX_PATH);
    if (q_exists(global_lights))
    {
        ReadLightFile(global_lights);
    }
    else
    {
        // Look for lights.rad in appdir
        safe_strncpy(global_lights, appdir, _MAX_PATH);
        safe_strncat(global_lights, lights_rad, _MAX_PATH);
        if (q_exists(global_lights))
        {
            ReadLightFile(global_lights);
        }
        else
        {
            // Look for lights.rad in current working directory
            safe_strncpy(global_lights, lights_rad, _MAX_PATH);
            if (q_exists(global_lights))
            {
                ReadLightFile(global_lights);
            }
        }
    }
   
    // Look for mapname.rad in mapdir
    safe_strncpy(mapname_lights, mapdir, _MAX_PATH);
    safe_strncat(mapname_lights, mapfile, _MAX_PATH);
#ifdef ZHLT_DEFAULTEXTENSION_FIX
	safe_strncat(mapname_lights, ext_rad, _MAX_PATH);
#else
    DefaultExtension(mapname_lights, ext_rad);
#endif
    if (q_exists(mapname_lights))
    {
        ReadLightFile(mapname_lights);
    }


    if (user_rad)
    {
        char user_lights[_MAX_PATH];
        char userfile[_MAX_PATH];

        ExtractFile(user_rad, userfile);

        // Look for user.rad from command line (raw)
        safe_strncpy(user_lights, user_rad, _MAX_PATH);
        if (q_exists(user_lights))
        {
            ReadLightFile(user_lights);
        }
        else
        {
            // Try again with .rad enforced as extension
            DefaultExtension(user_lights, ext_rad);
            if (q_exists(user_lights))
            {
                ReadLightFile(user_lights);
            }
            else
            {
                // Look for user.rad in mapdir
                safe_strncpy(user_lights, mapdir, _MAX_PATH);
                safe_strncat(user_lights, userfile, _MAX_PATH);
                DefaultExtension(user_lights, ext_rad);
                if (q_exists(user_lights))
                {
                    ReadLightFile(user_lights);
                }
                else
                {
                    // Look for user.rad in appdir
                    safe_strncpy(user_lights, appdir, _MAX_PATH);
                    safe_strncat(user_lights, userfile, _MAX_PATH);
                    DefaultExtension(user_lights, ext_rad);
                    if (q_exists(user_lights))
                    {
                        ReadLightFile(user_lights);
                    }
                    else
                    {
                        // Look for user.rad in current working directory
                        safe_strncpy(user_lights, userfile, _MAX_PATH);
                        DefaultExtension(user_lights, ext_rad);
                        if (q_exists(user_lights))
                        {
                            ReadLightFile(user_lights);
                        }
                    }
                }
            }
        }
    }

#ifdef HLRAD_INFO_TEXLIGHTS
    ReadInfoTexlights(); // AJM
#endif
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x7008, %rsp           # imm = 0x7008
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x1000(%rsp), %r12
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
xorl %esi, %esi
callq 0x4190
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
movq %r15, %rsi
callq 0x88b9
leaq 0x3000(%rsp), %rsi
movq %r12, %rdi
callq 0x851d
leaq 0x4000(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x851d
leaq 0x5000(%rsp), %rsi
movq %r14, %rdi
callq 0x85c2
leaq 0x1000(%rsp), %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
movq %r15, %rsi
callq 0x88b9
movq 0x208ea(%rip), %rsi     # 0x48440
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x88d0
movq %r14, %rdi
callq 0x976b
testb %al, %al
jne 0x27bd4
leaq 0x1000(%rsp), %r14
leaq 0x3000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x88b9
movq 0x208ad(%rip), %rsi     # 0x48440
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x88d0
movq %r14, %rdi
callq 0x976b
testb %al, %al
jne 0x27bd4
movq 0x2088d(%rip), %rsi     # 0x48440
leaq 0x1000(%rsp), %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x88b9
movq %r14, %rdi
callq 0x976b
testb %al, %al
je 0x27be1
leaq 0x1000(%rsp), %rdi
callq 0x27d7d
leaq 0x6000(%rsp), %r14
leaq 0x4000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x88b9
leaq 0x5000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x88d0
movq 0x2082e(%rip), %rsi     # 0x48448
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x88d0
movq %r14, %rdi
callq 0x976b
testb %al, %al
je 0x27c40
leaq 0x6000(%rsp), %rdi
callq 0x27d7d
testq %rbx, %rbx
je 0x27d69
leaq 0x2000(%rsp), %rsi
movq %rbx, %rdi
callq 0x85c2
movq %rsp, %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
movq %rbx, %rsi
callq 0x88b9
movq %r14, %rdi
callq 0x976b
testb %al, %al
jne 0x27d61
movq 0x207c5(%rip), %rsi     # 0x48448
movq %rsp, %rbx
movq %rbx, %rdi
callq 0x83ad
movq %rbx, %rdi
callq 0x976b
testb %al, %al
jne 0x27d61
movq %rsp, %rbx
leaq 0x4000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x88b9
leaq 0x2000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x88d0
movq 0x20776(%rip), %rsi     # 0x48448
movq %rbx, %rdi
callq 0x83ad
movq %rbx, %rdi
callq 0x976b
testb %al, %al
jne 0x27d61
movq %rsp, %rbx
leaq 0x3000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x88b9
leaq 0x2000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x88d0
movq 0x2072e(%rip), %rsi     # 0x48448
movq %rbx, %rdi
callq 0x83ad
movq %rbx, %rdi
callq 0x976b
testb %al, %al
jne 0x27d61
movq %rsp, %rbx
leaq 0x2000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x88b9
movq 0x206fb(%rip), %rsi     # 0x48448
movq %rbx, %rdi
callq 0x83ad
movq %rbx, %rdi
callq 0x976b
testb %al, %al
je 0x27d69
movq %rsp, %rdi
callq 0x27d7d
callq 0x27888
addq $0x7008, %rsp           # imm = 0x7008
popq %rbx
popq %r12
popq %r14
popq %r15
retq