// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %rax
movq %rdi, %rbx
xorl %eax, %eax
movl %eax, 0x8(%rdi)
movb $0x0, 0xc(%rdi)
movq %rax, 0x10(%rdi)
movq $0x30, 0x1c(%rdi)
movl %eax, 0x24(%rdi)
movb $0x1, 0x28(%rdi)
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
movq %rcx, 0x2c(%rdi)
movl $0x1, 0x34(%rdi)
leaq 0xa4fe(%rip), %rcx      # 0x13d58
movq %rcx, (%rdi)
movups (%rdx), %xmm0
movups %xmm0, 0x38(%rdi)
movq 0x10(%rdx), %rcx
movq %rcx, 0x48(%rdi)
movq %rax, 0x10(%rdx)
xorps %xmm0, %xmm0
movups %xmm0, (%rdx)
leaq 0x50(%rdi), %rcx
movq %rcx, (%rsp)
movq %rax, 0x60(%rdi)
movups %xmm0, 0x50(%rdi)
movl $0xffffffff, 0x68(%rdi) # imm = 0xFFFFFFFF
movq (%rsi), %r15
movq 0x8(%rsi), %r14
cmpq %r14, %r15
je 0x991e
leaq 0x20(%r15), %r13
movzbl 0x20(%r15), %esi
movq 0x50(%rbx), %rbp
movq 0x58(%rbx), %rax
subq %rbp, %rax
sarq $0x4, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
cmpq %rsi, %rax
ja 0x98e0
incq %rsi
movq (%rsp), %r12
movq %r12, %rdi
callq 0xc302
movzbl (%r13), %esi
movq (%r12), %rbp
leaq (%rsi,%rsi,4), %r12
shll $0x4, %r12d
leaq (%r12,%rbp), %rdi
movb $0x1, 0x2d(%rbp,%r12)
movl 0x28(%r15), %eax
movl %eax, 0x48(%rbp,%r12)
movq %r15, %rsi
callq 0x2220
movq (%r13), %rax
movq %rax, 0x20(%rbp,%r12)
movl 0x8(%r13), %eax
movl %eax, 0x28(%rbp,%r12)
addq $0x30, %r15
jmp 0x9894
movq 0x38(%rbx), %r15
movq 0x40(%rbx), %r13
cmpq %r13, %r15
je 0x99b4
movabsq $-0x3333333333333333, %rbp # imm = 0xCCCCCCCCCCCCCCCD
leaq 0x637c(%rip), %r12      # 0xfcbc
movb 0x20(%r15), %al
testb %al, %al
je 0x99ab
testb $0x8, 0x21(%r15)
jne 0x99ab
movzbl 0x22(%r15), %esi
movq 0x50(%rbx), %rdi
movq 0x58(%rbx), %rcx
subq %rdi, %rcx
sarq $0x4, %rcx
imulq %rbp, %rcx
cmpq %rsi, %rcx
jbe 0x998f
leaq (%rsi,%rsi,4), %rcx
shll $0x4, %ecx
cmpb $0x0, 0x2d(%rdi,%rcx)
je 0x999e
cmpb $-0x6f, %al
je 0x99ab
addq %rcx, %rdi
addq $0x30, %rdi
movq %r15, %rsi
callq 0xc344
jmp 0x99ab
movq (%r15), %rdx
movq %r12, %rdi
xorl %eax, %eax
callq 0x21d0
jmp 0x99ab
movq (%r15), %rdx
movq %r12, %rdi
xorl %eax, %eax
callq 0x21d0
addq $0x28, %r15
cmpq %r13, %r15
jne 0x9940
movq 0x50(%rbx), %r15
movq 0x58(%rbx), %r14
cmpq %r14, %r15
je 0x9a55
movq 0x30(%r15), %r13
movq 0x38(%r15), %r12
cmpq %r12, %r13
je 0x9a3e
movq %r12, %rbp
subq %r13, %rbp
movq %rbp, %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0xba0c
cmpq $0x281, %rbp            # imm = 0x281
jl 0x9a33
leaq 0x280(%r13), %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0xc0fb
cmpq %r12, %rbp
je 0x9a3e
movq %rbp, %rdi
callq 0xc242
addq $0x28, %rbp
jmp 0x9a20
movq %r13, %rdi
movq %r12, %rsi
callq 0xc0fb
addq $0x50, %r15
cmpq %r14, %r15
jne 0x99c5
movq 0x50(%rbx), %r15
movq 0x58(%rbx), %rax
jmp 0x9a58
movq %r15, %rax
subq %r15, %rax
sarq $0x4, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %rcx
cmpq $0x5, %rcx
movq (%rsp), %r14
jae 0x9a87
movl $0x5, %esi
movq %r14, %rdi
callq 0xc302
movq (%r14), %r15
movq 0x8(%r15), %rdx
leaq 0x624b(%rip), %rcx      # 0xfcdd
movl $0x5, %r8d
movq %r15, %rdi
xorl %esi, %esi
callq 0x2280
movq (%r14), %rdi
movq 0x58(%rdi), %rdx
addq $0x50, %rdi
leaq 0x622f(%rip), %rcx      # 0xfce3
movl $0x4, %r8d
xorl %esi, %esi
callq 0x2280
movq (%r14), %rdi
movq 0xa8(%rdi), %rdx
addq $0xa0, %rdi
leaq 0x620f(%rip), %rcx      # 0xfce8
movl $0x5, %r8d
xorl %esi, %esi
callq 0x2280
movq (%r14), %rdi
movq 0xf8(%rdi), %rdx
addq $0xf0, %rdi
leaq 0x61eb(%rip), %rcx      # 0xfce9
movl $0x4, %r8d
xorl %esi, %esi
callq 0x2280
movq (%r14), %rdi
movq 0x148(%rdi), %rdx
addq $0x140, %rdi            # imm = 0x140
leaq 0x61cb(%rip), %rcx      # 0xfcee
movl $0x5, %r8d
xorl %esi, %esi
callq 0x2280
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9b45
jmp 0x9b45
jmp 0x9b45
movq %rax, %r15
leaq 0x38(%rbx), %r12
movq (%rsp), %rdi
callq 0xc3a4
movq %r12, %rdi
callq 0x6662
movq %rbx, %rdi
callq 0x73ec
movq %r15, %rdi
callq 0x22c0
nop