// File: /workspace/github_cpp_makefile_star2_O2/ksherlock[P]wdc-utils/zrdz_disassembler.cpp
// Function: zrdz_disassembler::zrdz_disassembler(std::vector<section, std::allocator<section>>&&, std::vector<symbol, std::allocator<symbol>>&&)
zrdz_disassembler::zrdz_disassembler(std::vector<section> &&sections, std::vector<symbol> &&symbols) : 
	disassembler(wdc), _symbols(std::move(symbols))
{

	// do not sort _symbols ... order matters for lookup by entry number.
#if 0
	// sort by section / address.
	std::sort(_symbols.begin(), _symbols.end(), [](const symbol &a, const symbol &b){
		if (a.section < b.section) return true;
		if (a.section == b.section && a.offset < b.offset) return true;
		return false;
	});
#endif

	for (auto &s : sections) {
		if (s.number >= _sections.size()) _sections.resize(s.number + 1);
		_sections[s.number].valid = true;
		_sections[s.number].pc = s.org;
		_sections[s.number] = std::move(s);
	}

	for (auto &s : _symbols) {

		if (s.type == S_UND) continue;
		if (s.flags & SF_VAR) continue;

		if (s.section >= _sections.size()) {
			warnx("invalid section %d for symbol %s", s.section, s.name.c_str());
			continue;
		}
		auto &e = _sections[s.section];
		if (!e.valid) {
			warnx("invalid section %d for symbol %s", s.section, s.name.c_str());
			continue;
		}
		if (s.type == equ_type) continue;
		e.symbols.emplace_back(s);
	}

	// sort labels...
	for (auto &e : _sections) {
		if (e.symbols.empty()) continue;
		std::sort(e.symbols.begin(), e.symbols.end(), [](const symbol &a, const symbol &b) {
			return a.offset > b.offset;
		});
	}

	if (_sections.size() < 5) _sections.resize(5);
	_sections[SECT_PAGE0].name = "page0";
	_sections[SECT_CODE].name = "code";
	_sections[SECT_KDATA].name = "kdata";
	_sections[SECT_DATA].name = "data";
	_sections[SECT_UDATA].name = "udata";

#if 0
	// now place equates at the end...
	for (auto &s : _symbols) {
		if (s.type != equ_type) continue;
		auto &sec = _sections[s.section];
		sec.symbols.emplace_back(s);
	}	
#endif

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbx
pushq $0x1
popq %rsi
callq 0x8e26
leaq 0xa98a(%rip), %rax      # 0x11d58
movq %rax, (%rbx)
leaq 0x38(%rbx), %rax
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, 0x38(%rbx)
movq 0x10(%r15), %rax
movq %rax, 0x48(%rbx)
xorps %xmm0, %xmm0
andq $0x0, 0x10(%r15)
movups %xmm0, (%r15)
andq $0x0, 0x60(%rbx)
movups %xmm0, 0x50(%rbx)
orl $-0x1, 0x68(%rbx)
leaq 0x50(%rbx), %r15
movq (%r13), %r12
movq 0x8(%r13), %r14
pushq $0x50
popq %r13
movq %r15, 0x8(%rsp)
cmpq %r14, %r12
je 0x7470
movzbl 0x20(%r12), %esi
movq 0x50(%rbx), %rcx
movq 0x58(%rbx), %rax
subq %rcx, %rax
cqto
idivq %r13
cmpq %rsi, %rax
ja 0x744c
incq %rsi
movq %r15, %rdi
callq 0x8e64
movzbl 0x20(%r12), %esi
movq (%r15), %rcx
imulq $0x50, %rsi, %rax
leaq (%rcx,%rax), %rdi
movb $0x1, 0x2d(%rcx,%rax)
movl 0x28(%r12), %edx
movl %edx, 0x48(%rcx,%rax)
movq %r12, %rsi
callq 0x8e9a
addq $0x30, %r12
jmp 0x7418
movq 0x38(%rbx), %r12
movq 0x40(%rbx), %rbp
leaq 0x488d(%rip), %r13      # 0xbd0c
cmpq %rbp, %r12
je 0x74f9
movb 0x20(%r12), %cl
testb %cl, %cl
je 0x74f3
testb $0x8, 0x21(%r12)
jne 0x74f3
movzbl 0x22(%r12), %esi
movq 0x50(%rbx), %rdi
movq 0x58(%rbx), %rax
subq %rdi, %rax
cqto
pushq $0x50
popq %r8
idivq %r8
cmpq %rsi, %rax
jbe 0x74d5
imulq $0x50, %rsi, %rax
cmpb $0x0, 0x2d(%rdi,%rax)
je 0x74e5
cmpb $-0x6f, %cl
je 0x74f3
addq %rax, %rdi
addq $0x30, %rdi
movq %r12, %rsi
callq 0x8ec4
jmp 0x74f3
movq (%r12), %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x21e0
jmp 0x74f3
movq (%r12), %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x21e0
addq $0x28, %r12
jmp 0x747f
movq 0x50(%rbx), %r14
movq 0x58(%rbx), %r15
cmpq %r15, %r14
je 0x757d
movq 0x30(%r14), %r13
movq 0x38(%r14), %r12
cmpq %r12, %r13
je 0x7577
movq %r12, %rbp
subq %r13, %rbp
movq %rbp, %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x89fd
cmpq $0x281, %rbp            # imm = 0x281
jl 0x756c
leaq 0x280(%r13), %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x8d1e
jmp 0x7565
movq %rbp, %rdi
callq 0x8db1
addq $0x28, %rbp
cmpq %r12, %rbp
jne 0x7559
jmp 0x7577
movq %r13, %rdi
movq %r12, %rsi
callq 0x8d1e
addq $0x50, %r14
jmp 0x7501
movq 0x50(%rbx), %rdi
movq 0x58(%rbx), %rax
subq %rdi, %rax
cqto
pushq $0x50
popq %rcx
idivq %rcx
cmpq $0x5, %rax
movq 0x8(%rsp), %r14
jae 0x75a9
pushq $0x5
popq %rsi
movq %r14, %rdi
callq 0x8e64
movq (%r14), %rdi
leaq 0x477d(%rip), %rsi      # 0xbd2d
callq 0x2370
movq (%r14), %rdi
addq $0x50, %rdi
leaq 0x4770(%rip), %rsi      # 0xbd33
callq 0x2370
movl $0xa0, %edi
addq (%r14), %rdi
leaq 0x4761(%rip), %rsi      # 0xbd38
callq 0x2370
movl $0xf0, %edi
addq (%r14), %rdi
leaq 0x474e(%rip), %rsi      # 0xbd39
callq 0x2370
movl $0x140, %edi            # imm = 0x140
addq (%r14), %rdi
leaq 0x473f(%rip), %rsi      # 0xbd3e
callq 0x2370
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x761b
jmp 0x761b
jmp 0x761b
jmp 0x761b
movq %rax, %r12
movq 0x8(%rsp), %rdi
callq 0x8f00
movq 0x10(%rsp), %rdi
callq 0x485a
movq %rbx, %rdi
callq 0x56c6
movq %r12, %rdi
callq 0x2300