// File: /workspace/github_cpp_makefile_star2_O2/ksherlock[P]wdc-utils/zrdz_disassembler.cpp
// Function: zrdz_disassembler::location_name[abi:cxx11](unsigned int, unsigned int) const
std::string zrdz_disassembler::location_name(unsigned section, uint32_t offset) const {

	if (section >= _sections.size()) {
		warnx("Invalid section %d", section);
		return "$";
	}
	auto &e = _sections[section];
	if (!e.valid) {
		warnx("Invalid section %d", section);
		return "$";
	}

	// todo -- need to verify relative/absolute value

	auto iter = std::find_if(_symbols.begin(), _symbols.end(), [=](const symbol &s){
		if (s.type == equ_type) return false;
		if (s.type == S_UND) return false;
		return s.section == section && s.offset == offset;
	});

	if (iter != _symbols.end()) return iter->name;

	//fallback to section name + offset
	std::string tmp = e.name;
	if (tmp.empty()) tmp = "section" + std::to_string(section);
	if (offset) tmp += "+$" + to_x(offset, 4);
	return tmp;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, %r9d
movq %rdi, %r14
movl %edx, %r8d
movq 0x50(%rsi), %r15
movq 0x58(%rsi), %rax
subq %r15, %rax
pushq $0x50
popq %rdi
cqto
idivq %rdi
cmpq %r8, %rax
jbe 0x868e
imulq $0x50, %r8, %rax
cmpb $0x0, 0x2d(%r15,%rax)
je 0x868e
movl %r9d, 0x24(%rsp)
movq %r14, 0x10(%rsp)
addq %rax, %r15
movq 0x38(%rsi), %rbp
movq 0x40(%rsi), %r14
movq %rcx, 0x18(%rsp)
movq %rcx, %rax
shlq $0x20, %rax
orq %r8, %rax
leaq 0x28(%rsp), %r13
movq %rax, (%r13)
movq %r14, 0x8(%rsp)
subq %rbp, %r14
pushq $0x28
popq %rcx
movq %r14, %rax
cqto
idivq %rcx
movq %rax, %rbx
sarq $0x2, %rbx
testq %rbx, %rbx
jle 0x86c5
movq %r13, %rdi
movq %rbp, %rsi
callq 0x8e02
testb %al, %al
jne 0x871f
leaq 0x28(%rbp), %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x8e02
testb %al, %al
jne 0x8722
leaq 0x50(%rbp), %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x8e02
testb %al, %al
jne 0x8722
leaq 0x78(%rbp), %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x8e02
testb %al, %al
jne 0x8722
addq $0xa0, %rbp
decq %rbx
addq $-0xa0, %r14
jmp 0x861a
leaq 0x3793(%rip), %rdi      # 0xbe28
movl %r9d, %esi
xorl %eax, %eax
callq 0x21e0
leaq 0x3736(%rip), %rsi      # 0xbddc
leaq 0x28(%rsp), %rdx
movq %r14, %rdi
callq 0x48d4
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
cmpq $0x1, %rax
je 0x87f5
cmpq $0x2, %rax
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r13
je 0x8705
cmpq $0x3, %rax
jne 0x8746
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x8e02
testb %al, %al
jne 0x871a
addq $0x28, %rbp
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x8e02
testb %al, %al
je 0x8801
movq %rbp, %r12
jmp 0x872c
movq %rbp, %r12
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r13
movq 0x8(%rsp), %rax
cmpq %rax, %r12
je 0x8746
movq %r14, %rdi
movq %r12, %rsi
callq 0x2260
jmp 0x86b3
movq %r14, %rdi
movq %r15, %rsi
callq 0x2260
cmpq $0x0, 0x8(%r14)
jne 0x879e
leaq 0x48(%rsp), %rdi
movl 0x24(%rsp), %esi
callq 0x4b15
leaq 0x36b3(%rip), %rsi      # 0xbe20
leaq 0x28(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x4bad
leaq 0x28(%rsp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2220
movq %rbx, %rdi
callq 0x20d0
leaq 0x48(%rsp), %rdi
callq 0x20d0
testl %r13d, %r13d
je 0x86b3
leaq 0x48(%rsp), %rdi
pushq $0x4
popq %rdx
movl %r13d, %esi
xorl %ecx, %ecx
callq 0x56d0
leaq 0x36b3(%rip), %rsi      # 0xbe73
leaq 0x28(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x4bad
leaq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x2380
leaq 0x28(%rsp), %rdi
callq 0x20d0
leaq 0x48(%rsp), %rdi
callq 0x20d0
jmp 0x86b3
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r13
jmp 0x8805
addq $0x28, %rbp
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x8e02
testb %al, %al
movq 0x8(%rsp), %rax
cmoveq %rax, %rbp
movq %rbp, %r12
jmp 0x8731
jmp 0x8838
jmp 0x8847
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x20d0
jmp 0x883b
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x20d0
jmp 0x884a
movq %rax, %rbx
movq 0x10(%rsp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x2300