// File: 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:
subq $0x8018, %rsp           # imm = 0x8018
movq %rdi, 0x8010(%rsp)
movq %rsi, 0x8008(%rsp)
movq %rdx, 0x8000(%rsp)
leaq 0x2000(%rsp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x41b0
leaq 0x2000(%rsp), %rdi
movq 0x8000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x95f0
leaq 0x2000(%rsp), %rdi
leaq 0x3000(%rsp), %rsi
callq 0x91d0
movq 0x8010(%rsp), %rdi
leaq 0x4000(%rsp), %rsi
callq 0x91d0
movq 0x8010(%rsp), %rdi
leaq 0x5000(%rsp), %rsi
callq 0x9230
leaq 0x7000(%rsp), %rdi
leaq 0x4000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x95f0
leaq 0x7000(%rsp), %rdi
movq 0x2f08b(%rip), %rsi     # 0x7e450
movl $0x1000, %edx           # imm = 0x1000
callq 0x9630
leaq 0x7000(%rsp), %rdi
callq 0xaad0
testb $0x1, %al
jne 0x4f3e2
jmp 0x4f3f4
leaq 0x7000(%rsp), %rdi
callq 0x4f6c0
jmp 0x4f486
leaq 0x7000(%rsp), %rdi
leaq 0x3000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x95f0
leaq 0x7000(%rsp), %rdi
movq 0x2f033(%rip), %rsi     # 0x7e450
movl $0x1000, %edx           # imm = 0x1000
callq 0x9630
leaq 0x7000(%rsp), %rdi
callq 0xaad0
testb $0x1, %al
jne 0x4f43a
jmp 0x4f449
leaq 0x7000(%rsp), %rdi
callq 0x4f6c0
jmp 0x4f484
leaq 0x7000(%rsp), %rdi
movq 0x2eff8(%rip), %rsi     # 0x7e450
movl $0x1000, %edx           # imm = 0x1000
callq 0x95f0
leaq 0x7000(%rsp), %rdi
callq 0xaad0
testb $0x1, %al
jne 0x4f475
jmp 0x4f482
leaq 0x7000(%rsp), %rdi
callq 0x4f6c0
jmp 0x4f484
jmp 0x4f486
leaq 0x6000(%rsp), %rdi
leaq 0x4000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x95f0
leaq 0x6000(%rsp), %rdi
leaq 0x5000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x9630
leaq 0x6000(%rsp), %rdi
movq 0x2ef8f(%rip), %rsi     # 0x7e458
movl $0x1000, %edx           # imm = 0x1000
callq 0x9630
leaq 0x6000(%rsp), %rdi
callq 0xaad0
testb $0x1, %al
jne 0x4f4e6
jmp 0x4f4f3
leaq 0x6000(%rsp), %rdi
callq 0x4f6c0
cmpq $0x0, 0x8008(%rsp)
je 0x4f6aa
movq 0x8008(%rsp), %rdi
movq %rsp, %rsi
callq 0x9230
leaq 0x1000(%rsp), %rdi
movq 0x8008(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x95f0
leaq 0x1000(%rsp), %rdi
callq 0xaad0
testb $0x1, %al
jne 0x4f53f
jmp 0x4f551
leaq 0x1000(%rsp), %rdi
callq 0x4f6c0
jmp 0x4f6a8
leaq 0x1000(%rsp), %rdi
movq 0x2eef8(%rip), %rsi     # 0x7e458
callq 0x9100
leaq 0x1000(%rsp), %rdi
callq 0xaad0
testb $0x1, %al
jne 0x4f578
jmp 0x4f58a
leaq 0x1000(%rsp), %rdi
callq 0x4f6c0
jmp 0x4f6a6
leaq 0x1000(%rsp), %rdi
leaq 0x4000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x95f0
leaq 0x1000(%rsp), %rdi
movq %rsp, %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x9630
leaq 0x1000(%rsp), %rdi
movq 0x2ee90(%rip), %rsi     # 0x7e458
callq 0x9100
leaq 0x1000(%rsp), %rdi
callq 0xaad0
testb $0x1, %al
jne 0x4f5e0
jmp 0x4f5f2
leaq 0x1000(%rsp), %rdi
callq 0x4f6c0
jmp 0x4f6a4
leaq 0x1000(%rsp), %rdi
leaq 0x3000(%rsp), %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x95f0
leaq 0x1000(%rsp), %rdi
movq %rsp, %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x9630
leaq 0x1000(%rsp), %rdi
movq 0x2ee28(%rip), %rsi     # 0x7e458
callq 0x9100
leaq 0x1000(%rsp), %rdi
callq 0xaad0
testb $0x1, %al
jne 0x4f648
jmp 0x4f657
leaq 0x1000(%rsp), %rdi
callq 0x4f6c0
jmp 0x4f6a2
leaq 0x1000(%rsp), %rdi
movq %rsp, %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x95f0
leaq 0x1000(%rsp), %rdi
movq 0x2eddd(%rip), %rsi     # 0x7e458
callq 0x9100
leaq 0x1000(%rsp), %rdi
callq 0xaad0
testb $0x1, %al
jne 0x4f693
jmp 0x4f6a0
leaq 0x1000(%rsp), %rdi
callq 0x4f6c0
jmp 0x4f6a2
jmp 0x4f6a4
jmp 0x4f6a6
jmp 0x4f6a8
jmp 0x4f6aa
callq 0x4f040
addq $0x8018, %rsp           # imm = 0x8018
retq
nopw (%rax,%rax)