// File: /workspace/github_cpp_makefile_star2_O1/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 0x899d
leaq 0x3000(%rsp), %rsi
movq %r12, %rdi
callq 0x85f4
leaq 0x4000(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x85f4
leaq 0x5000(%rsp), %rsi
movq %r14, %rdi
callq 0x86a7
leaq 0x1000(%rsp), %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
movq %r15, %rsi
callq 0x899d
movq 0x20a22(%rip), %rsi     # 0x49440
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x89b4
movq %r14, %rdi
callq 0x9856
testb %al, %al
jne 0x28a9c
leaq 0x1000(%rsp), %r14
leaq 0x3000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x899d
movq 0x209e5(%rip), %rsi     # 0x49440
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x89b4
movq %r14, %rdi
callq 0x9856
testb %al, %al
jne 0x28a9c
movq 0x209c5(%rip), %rsi     # 0x49440
leaq 0x1000(%rsp), %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x899d
movq %r14, %rdi
callq 0x9856
testb %al, %al
je 0x28aa9
leaq 0x1000(%rsp), %rdi
callq 0x28c45
leaq 0x6000(%rsp), %r14
leaq 0x4000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x899d
leaq 0x5000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x89b4
movq 0x20966(%rip), %rsi     # 0x49448
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x89b4
movq %r14, %rdi
callq 0x9856
testb %al, %al
je 0x28b08
leaq 0x6000(%rsp), %rdi
callq 0x28c45
testq %rbx, %rbx
je 0x28c31
leaq 0x2000(%rsp), %rsi
movq %rbx, %rdi
callq 0x86a7
movq %rsp, %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
movq %rbx, %rsi
callq 0x899d
movq %r14, %rdi
callq 0x9856
testb %al, %al
jne 0x28c29
movq 0x208fd(%rip), %rsi     # 0x49448
movq %rsp, %rbx
movq %rbx, %rdi
callq 0x8479
movq %rbx, %rdi
callq 0x9856
testb %al, %al
jne 0x28c29
movq %rsp, %rbx
leaq 0x4000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x899d
leaq 0x2000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x89b4
movq 0x208ae(%rip), %rsi     # 0x49448
movq %rbx, %rdi
callq 0x8479
movq %rbx, %rdi
callq 0x9856
testb %al, %al
jne 0x28c29
movq %rsp, %rbx
leaq 0x3000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x899d
leaq 0x2000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x89b4
movq 0x20866(%rip), %rsi     # 0x49448
movq %rbx, %rdi
callq 0x8479
movq %rbx, %rdi
callq 0x9856
testb %al, %al
jne 0x28c29
movq %rsp, %rbx
leaq 0x2000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x899d
movq 0x20833(%rip), %rsi     # 0x49448
movq %rbx, %rdi
callq 0x8479
movq %rbx, %rdi
callq 0x9856
testb %al, %al
je 0x28c31
movq %rsp, %rdi
callq 0x28c45
callq 0x28750
addq $0x7008, %rsp           # imm = 0x7008
popq %rbx
popq %r12
popq %r14
popq %r15
retq