// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movl %ecx, %ebp
movl %edx, %r14d
movq %rdi, %rbx
movl %edx, %ecx
movq 0x50(%rsi), %rax
movq 0x58(%rsi), %rdx
subq %rax, %rdx
sarq $0x4, %rdx
movabsq $-0x3333333333333333, %rdi # imm = 0xCCCCCCCCCCCCCCCD
imulq %rdi, %rdx
cmpq %rcx, %rdx
jbe 0xb59b
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
cmpb $0x0, 0x2d(%rax,%rcx)
je 0xb59b
movq 0x38(%rsi), %r9
movq 0x40(%rsi), %rdx
movq %rdx, %rsi
subq %r9, %rsi
sarq $0x3, %rsi
imulq %rdi, %rsi
sarq $0x2, %rsi
testq %rsi, %rsi
jle 0xb50e
leaq (%rsi,%rsi,4), %r10
shlq $0x5, %r10
addq %r9, %r10
incq %rsi
addq $0x50, %r9
movq %r9, %r8
movzbl -0x30(%r8), %r9d
testl %r9d, %r9d
je 0xb482
cmpl $0x91, %r9d
je 0xb482
movzbl -0x2e(%r8), %r9d
cmpl %r14d, %r9d
jne 0xb482
cmpl %ebp, -0x2c(%r8)
je 0xb5f4
movzbl -0x8(%r8), %r9d
testl %r9d, %r9d
je 0xb4a9
cmpl $0x91, %r9d
je 0xb4a9
movzbl -0x6(%r8), %r9d
cmpl %r14d, %r9d
jne 0xb4a9
cmpl %ebp, -0x4(%r8)
je 0xb5fa
movzbl 0x20(%r8), %r9d
testl %r9d, %r9d
je 0xb4d0
cmpl $0x91, %r9d
je 0xb4d0
movzbl 0x22(%r8), %r9d
cmpl %r14d, %r9d
jne 0xb4d0
cmpl %ebp, 0x24(%r8)
je 0xb621
movzbl 0x48(%r8), %r9d
testl %r9d, %r9d
je 0xb4f7
cmpl $0x91, %r9d
je 0xb4f7
movzbl 0x4a(%r8), %r9d
cmpl %r14d, %r9d
jne 0xb4f7
cmpl %ebp, 0x4c(%r8)
je 0xb600
decq %rsi
addq $0xa0, %r8
cmpq $0x1, %rsi
jg 0xb45b
movq %r10, %r9
movq %rdx, %rsi
subq %r9, %rsi
sarq $0x3, %rsi
imulq %rdi, %rsi
cmpq $0x1, %rsi
je 0xb563
cmpq $0x2, %rsi
je 0xb54e
movq %rdx, %r8
cmpq $0x3, %rsi
jne 0xb621
movzbl 0x20(%r9), %esi
testl %esi, %esi
je 0xb54a
cmpl $0x91, %esi
jne 0xb606
addq $0x28, %r9
movzbl 0x20(%r9), %esi
testl %esi, %esi
je 0xb55f
cmpl $0x91, %esi
jne 0xb5db
addq $0x28, %r9
movzbl 0x20(%r9), %esi
movq %rdx, %r8
testl %esi, %esi
je 0xb621
movq %rdx, %r8
cmpl $0x91, %esi
je 0xb621
movzbl 0x22(%r9), %esi
xorl %r14d, %esi
movl 0x24(%r9), %edi
xorl %ebp, %edi
orl %esi, %edi
cmovneq %rdx, %r9
jmp 0xb61e
leaq 0x4836(%rip), %rdi      # 0xfdd8
movl %r14d, %esi
xorl %eax, %eax
callq 0x21d0
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x47d2(%rip), %rsi      # 0xfd8c
leaq 0x47cc(%rip), %rdx      # 0xfd8d
movq %rbx, %rdi
callq 0x6b90
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzbl 0x22(%r9), %esi
cmpl %r14d, %esi
jne 0xb55f
cmpl %ebp, 0x24(%r9)
je 0xb61e
jmp 0xb55f
addq $-0x50, %r8
jmp 0xb621
addq $-0x28, %r8
jmp 0xb621
addq $0x28, %r8
jmp 0xb621
movzbl 0x22(%r9), %esi
cmpl %r14d, %esi
jne 0xb54a
cmpl %ebp, 0x24(%r9)
jne 0xb54a
movq %r9, %r8
leaq 0x10(%rbx), %r12
movq %r12, (%rbx)
cmpq %rdx, %r8
je 0xb641
movq (%r8), %rsi
movq 0x8(%r8), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x6b00
jmp 0xb5c9
addq %rcx, %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x6b00
cmpq $0x0, 0x8(%rbx)
jne 0xb776
movl $0x1, %eax
cmpl $0xa, %r14d
jb 0xb6b1
movl $0x4, %eax
movl $0xd1b71759, %ecx       # imm = 0xD1B71759
movl %r14d, %edx
cmpl $0x63, %edx
jbe 0xb6aa
cmpl $0x3e7, %edx            # imm = 0x3E7
jbe 0xb6af
cmpl $0x2710, %edx           # imm = 0x2710
jb 0xb6b1
movl %edx, %esi
imulq %rcx, %rsi
shrq $0x2d, %rsi
addl $0x4, %eax
cmpl $0x1869f, %edx          # imm = 0x1869F
movl %esi, %edx
ja 0xb679
addl $-0x3, %eax
jmp 0xb6b1
addl $-0x2, %eax
jmp 0xb6b1
decl %eax
movl %eax, %esi
leaq 0x38(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x28(%rsp), %rdi
xorl %edx, %edx
callq 0x2200
leaq 0x28(%rsp), %r15
movq (%r15), %rdi
movl 0x8(%r15), %esi
movl %r14d, %edx
callq 0x6a93
leaq 0x46ed(%rip), %rcx      # 0xfdd0
movl $0x7, %r8d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21f0
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb71c
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
jmp 0xb723
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
leaq 0x8(%rsp), %r14
movq %rdx, 0x8(%r14)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2220
movq (%r14), %rdi
cmpq %r15, %rdi
je 0xb75f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0xb776
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
testl %ebp, %ebp
je 0xb5c9
leaq 0x28(%rsp), %rdi
movl %ebp, %esi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x7538
leaq 0x468b(%rip), %rcx      # 0xfe23
leaq 0x28(%rsp), %rdi
movl $0x2, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x21f0
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xb7d3
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
jmp 0xb7da
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rdx, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x2090
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0xb81b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21c0
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb5c9
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0xb5c9
movq %rax, %r14
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
jne 0xb87d
jmp 0xb88f
jmp 0xb88c
movq %rax, %r14
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0xb86f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0xb86f
movq %rax, %r14
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb88f
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0xb88f
movq %rax, %r14
movq (%rbx), %rdi
cmpq %r12, %rdi
je 0xb8a3
movq (%r12), %rsi
incq %rsi
callq 0x21c0
movq %r14, %rdi
callq 0x22c0
nop