// File: /workspace/github_cpp_makefile_star2_O1/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 0xb35a(%rip), %rcx      # 0x14d58
movq %rcx, (%rdi)
movq (%rdx), %rcx
movq %rcx, 0x38(%rdi)
movq 0x8(%rdx), %rcx
movq %rcx, 0x40(%rdi)
movq 0x10(%rdx), %rcx
movq %rcx, 0x48(%rdi)
movq %rax, 0x10(%rdx)
xorps %xmm0, %xmm0
movups %xmm0, (%rdx)
leaq 0x50(%rdi), %r12
movq %rax, 0x60(%rdi)
movups %xmm0, 0x50(%rdi)
movl $0xffffffff, 0x68(%rdi) # imm = 0xFFFFFFFF
movq (%rsi), %r15
movq 0x8(%rsi), %r14
cmpq %r14, %r15
movq %r12, (%rsp)
je 0x9ac3
movq 0x58(%rbx), %rax
subq 0x50(%rbx), %rax
movzbl 0x20(%r15), %esi
sarq $0x4, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
cmpq %rsi, %rax
ja 0x9a74
incq %rsi
movq %r12, %rdi
callq 0xc556
leaq 0x20(%r15), %r13
movzbl 0x20(%r15), %eax
movq (%r12), %rbp
leaq (%rax,%rax,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)
movq (%rsp), %r12
addq $0x30, %r15
cmpq %r14, %r15
jne 0x9a45
movq 0x38(%rbx), %r15
movq 0x40(%rbx), %r13
cmpq %r13, %r15
je 0x9b59
movabsq $-0x3333333333333333, %rbp # imm = 0xCCCCCCCCCCCCCCCD
leaq 0x61d7(%rip), %r12      # 0xfcbc
movb 0x20(%r15), %al
testb %al, %al
je 0x9b50
testb $0x8, 0x21(%r15)
jne 0x9b50
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 0x9b34
leaq (%rsi,%rsi,4), %rcx
shll $0x4, %ecx
cmpb $0x0, 0x2d(%rdi,%rcx)
je 0x9b43
cmpb $-0x6f, %al
je 0x9b50
addq %rcx, %rdi
addq $0x30, %rdi
movq %r15, %rsi
callq 0xc598
jmp 0x9b50
movq (%r15), %rdx
movq %r12, %rdi
xorl %eax, %eax
callq 0x21d0
jmp 0x9b50
movq (%r15), %rdx
movq %r12, %rdi
xorl %eax, %eax
callq 0x21d0
addq $0x28, %r15
cmpq %r13, %r15
jne 0x9ae5
movq 0x50(%rbx), %rbp
movq 0x58(%rbx), %r14
cmpq %r14, %rbp
je 0x9bed
movq 0x30(%rbp), %r12
movq 0x38(%rbp), %r15
cmpq %r15, %r12
je 0x9be4
movq %r15, %r13
subq %r12, %r13
movq %r13, %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 %r12, %rdi
movq %r15, %rsi
callq 0xbbe6
cmpq $0x281, %r13            # imm = 0x281
jl 0x9bd9
leaq 0x280(%r12), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0xc34f
cmpq %r15, %r13
je 0x9be4
movq %r13, %rdi
callq 0xc496
addq $0x28, %r13
jmp 0x9bc6
movq %r12, %rdi
movq %r15, %rsi
callq 0xc34f
addq $0x50, %rbp
jmp 0x9b61
movq 0x58(%rbx), %rax
subq 0x50(%rbx), %rax
sarq $0x4, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %rcx
cmpq $0x5, %rcx
movq (%rsp), %r14
jae 0x9c1e
movl $0x5, %esi
movq %r14, %rdi
callq 0xc556
movq (%r14), %rdi
movq 0x8(%rdi), %rdx
leaq 0x60b1(%rip), %rcx      # 0xfcdd
movl $0x5, %r8d
xorl %esi, %esi
callq 0x2280
movq (%r14), %rdi
movq 0x58(%rdi), %rdx
addq $0x50, %rdi
leaq 0x6098(%rip), %rcx      # 0xfce3
movl $0x4, %r8d
xorl %esi, %esi
callq 0x2280
movq (%r14), %rdi
movq 0xa8(%rdi), %rdx
addq $0xa0, %rdi
leaq 0x6078(%rip), %rcx      # 0xfce8
movl $0x5, %r8d
xorl %esi, %esi
callq 0x2280
movq (%r14), %rdi
movq 0xf8(%rdi), %rdx
addq $0xf0, %rdi
leaq 0x6054(%rip), %rcx      # 0xfce9
movl $0x4, %r8d
xorl %esi, %esi
callq 0x2280
movq (%r14), %rdi
movq 0x148(%rdi), %rdx
addq $0x140, %rdi            # imm = 0x140
leaq 0x6034(%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 0x9cdc
jmp 0x9cdc
jmp 0x9cdc
movq %rax, %r15
leaq 0x38(%rbx), %r12
movq (%rsp), %rdi
callq 0xc5f2
movq %r12, %rdi
callq 0x6794
movq %rbx, %rdi
callq 0x752c
movq %r15, %rdi
callq 0x22c0