// File: /workspace/github_cpp_makefile_star2_O2/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 0x41a0
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
movq %r15, %rsi
callq 0x7721
leaq 0x3000(%rsp), %rsi
movq %r12, %rdi
callq 0x750a
leaq 0x4000(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x750a
leaq 0x5000(%rsp), %rsi
movq %r14, %rdi
callq 0x7556
leaq 0x1000(%rsp), %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
movq %r15, %rsi
callq 0x7721
movq 0x20dba(%rip), %rsi     # 0x45450
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x7738
movq %r14, %rdi
callq 0x82be
testb %al, %al
jne 0x24714
leaq 0x1000(%rsp), %r14
leaq 0x3000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x7721
movq 0x20d7d(%rip), %rsi     # 0x45450
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x7738
movq %r14, %rdi
callq 0x82be
testb %al, %al
jne 0x24714
movq 0x20d5d(%rip), %rsi     # 0x45450
leaq 0x1000(%rsp), %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x7721
movq %r14, %rdi
callq 0x82be
testb %al, %al
je 0x24721
leaq 0x1000(%rsp), %rdi
callq 0x248bd
leaq 0x6000(%rsp), %r14
leaq 0x4000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x7721
leaq 0x5000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x7738
movq 0x20cfe(%rip), %rsi     # 0x45458
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x7738
movq %r14, %rdi
callq 0x82be
testb %al, %al
je 0x24780
leaq 0x6000(%rsp), %rdi
callq 0x248bd
testq %rbx, %rbx
je 0x248a9
leaq 0x2000(%rsp), %rsi
movq %rbx, %rdi
callq 0x7556
movq %rsp, %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7721
movq %r14, %rdi
callq 0x82be
testb %al, %al
jne 0x248a1
movq 0x20c95(%rip), %rsi     # 0x45458
movq %rsp, %rbx
movq %rbx, %rdi
callq 0x7471
movq %rbx, %rdi
callq 0x82be
testb %al, %al
jne 0x248a1
movq %rsp, %rbx
leaq 0x4000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x7721
leaq 0x2000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x7738
movq 0x20c46(%rip), %rsi     # 0x45458
movq %rbx, %rdi
callq 0x7471
movq %rbx, %rdi
callq 0x82be
testb %al, %al
jne 0x248a1
movq %rsp, %rbx
leaq 0x3000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x7721
leaq 0x2000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x7738
movq 0x20bfe(%rip), %rsi     # 0x45458
movq %rbx, %rdi
callq 0x7471
movq %rbx, %rdi
callq 0x82be
testb %al, %al
jne 0x248a1
movq %rsp, %rbx
leaq 0x2000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x7721
movq 0x20bcb(%rip), %rsi     # 0x45458
movq %rbx, %rdi
callq 0x7471
movq %rbx, %rdi
callq 0x82be
testb %al, %al
je 0x248a9
movq %rsp, %rdi
callq 0x248bd
callq 0x24429
addq $0x7008, %rsp           # imm = 0x7008
popq %rbx
popq %r12
popq %r14
popq %r15
retq