// File: /workspace/github_cpp_makefile_star2_O2/sc0ty[P]CODExtract/src/codfile/codfile.cpp
// Function: CODFile::LoadParent()
bool CODFile::LoadParent()
{
    if (!isOpen) throw "Load file first";

    // parent already loaded
    if (!isChildCOD()) return false;

    /* extract parent filename from cod file */

    // data segment header
    TDATASEGHEADER *dh = (TDATASEGHEADER *)DataSeg;

    // sanity check
    if (dh->exported_string_offset > HeadSeg->data_size)
        throw "Incorrect file format";

    // export names ptr array
    u16 *expn = (u16 *)(DataSeg + sizeof(TDATASEGHEADER) +
                        sizeof(u16)*dh->num_classes +
                        sizeof(u16)*2*dh->num_modules);

    // check if ptr is in data section
    if ( *expn >= HeadSeg->data_size)
        throw "Incorrect file format";

    /* create parent filename */

    string fn = FName;
    fn = &DataSeg[*expn];
	size_t s;

	// find every $ characters
	while ((s = fn.find('$')) != string::npos)
	{
		int c;
		istringstream iss(fn.substr(s+1, 2));
		iss >> hex >> c;
		fn.replace(s, 3, 1, (char)c);
	}

	// extract directory from path
    size_t sz = FName.find_last_of("\\/");
	if (sz != string::npos) fn = FName.substr(0, sz+1) + fn;
    fn += ".cod";

    // load parent cod file
    LoadFile(fn.c_str());

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
cmpb $0x0, 0x48(%rdi)
je 0x30fc
movq %rdi, %r12
callq 0x316c
testb %al, %al
je 0x30ea
movq 0x28(%r12), %rcx
movq 0x38(%r12), %rdi
movzwl 0x28(%rcx), %ecx
cmpw %cx, 0x6(%rdi)
ja 0x310d
movb %al, -0x29(%rbp)
movzbl 0x5(%rdi), %edx
leaq (%rdi,%rdx,2), %rsi
movzbl 0x4(%rdi), %r14d
cmpw %cx, 0x34(%rsi,%r14,4)
jae 0x310d
leaq (%rdi,%rdx,2), %r15
addq $0x34, %r15
leaq 0x8(%r12), %rsi
leaq -0x60(%rbp), %rbx
movq %rbx, %rdi
movq %rsi, -0x38(%rbp)
callq 0x2260
movzwl (%r15,%r14,4), %esi
movq %r12, -0x40(%rbp)
addq 0x38(%r12), %rsi
movq %rbx, %rdi
callq 0x2320
leaq -0x60(%rbp), %r12
leaq -0x80(%rbp), %r15
leaq -0x200(%rbp), %r14
pushq $0x1
popq %r13
movq %r12, %rdi
pushq $0x24
popq %rsi
xorl %edx, %edx
callq 0x2240
cmpq $-0x1, %rax
je 0x3056
movq %rax, %rbx
leaq 0x1(%rax), %rdx
movq %r15, %rdi
movq %r12, %rsi
pushq $0x2
popq %rcx
callq 0x21d0
movq %r14, %rdi
movq %r15, %rsi
pushq $0x8
popq %rdx
callq 0x22d0
movq %r15, %rdi
callq 0x20d0
movq -0x200(%rbp), %rax
movq -0x18(%rax), %rax
movl -0x1e8(%rbp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, -0x1e8(%rbp,%rax)
movq %r14, %rdi
leaq -0x30(%rbp), %rsi
callq 0x2110
movsbl -0x30(%rbp), %r8d
movq %r12, %rdi
movq %rbx, %rsi
pushq $0x3
popq %rdx
movq %r13, %rcx
callq 0x22b0
movq %r14, %rdi
callq 0x20e0
jmp 0x2fca
leaq 0x1167(%rip), %rsi      # 0x41c4
pushq $-0x1
popq %rdx
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
callq 0x2180
cmpq $-0x1, %rax
movq -0x40(%rbp), %r14
je 0x30c2
incq %rax
leaq -0x80(%rbp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x21d0
leaq -0x200(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x35f4
leaq -0x60(%rbp), %rdi
leaq -0x200(%rbp), %rbx
movq %rbx, %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x20d0
leaq -0x80(%rbp), %rdi
callq 0x20d0
leaq 0x10fe(%rip), %rsi      # 0x41c7
leaq -0x60(%rbp), %rdi
callq 0x2310
movq -0x60(%rbp), %rsi
movq %r14, %rdi
callq 0x2cba
leaq -0x60(%rbp), %rdi
callq 0x20d0
movb -0x29(%rbp), %al
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x2100
leaq 0x10a9(%rip), %rcx      # 0x41b4
jmp 0x311c
pushq $0x8
popq %rdi
callq 0x2100
leaq 0x1082(%rip), %rcx      # 0x419e
movq %rcx, (%rax)
movq 0x2ea2(%rip), %rsi      # 0x5fc8
movq %rax, %rdi
xorl %edx, %edx
callq 0x22e0
jmp 0x313d
jmp 0x3138
jmp 0x3138
jmp 0x3138
movq %rax, %rbx
jmp 0x315a
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x20d0
jmp 0x315a
movq %rax, %rbx
leaq -0x200(%rbp), %rdi
callq 0x20e0
leaq -0x60(%rbp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x22f0
nop