// File: /workspace/github_c_makefile_star2_O2/arkanis[P]process-map/map.c
// Function: PlotRangeElfSymbols
void PlotRangeElfSymbols(AddrRange ranges[], size_t rangeCount, size_t hilbertCurveOrder, Font labelFont) {
	// Load the ELF symbols for each region. If there are any.
	char* prevRangeFilePath = NULL;
	ElfSymbols prevRangeSymbols = { 0 };
	for (size_t i = 0; i < rangeCount; i++) {
		AddrRange* range = &ranges[i];
		uint64_t rangeStartOffset = range->fileOffset;
		uint64_t rangeEndOffset = range->fileOffset + (range->endAddr - range->startAddr);
		
		// Reuse the ELF symbols of the previous range if the filePath is the same.
		// Just some cheap caching to avoid loading the same ELF for successive ranges.
		ElfSymbols symbols = { 0 };
		if ( range->filePath && prevRangeFilePath && strcmp(range->filePath, prevRangeFilePath) == 0 ) {
			symbols = prevRangeSymbols;
		} else {
			symbols = LoadElfSymbols(range->filePath);
			UnloadElfSymbols(&prevRangeSymbols);
			prevRangeSymbols = symbols;
		}
		
		// Check if any of the ELFs symbols are within the area mapped in the current region. Skip region if not.
		size_t symbolsInRange = 0;
		for (size_t s = 0; s < symbols.count; s++) {
			if ( symbols.at[s].fileOffset >= rangeStartOffset && symbols.at[s].fileOffset < rangeEndOffset )
				symbolsInRange++;
		}
		
		if (symbolsInRange == 0)
			continue;
		
		// Create a symbol map texture that covers only this address ranges area
		size_t mapSideLength = pow(2, hilbertCurveOrder);
		size_t symbolMapHilberCurveOrder = hilbertCurveOrder + 3;
		size_t symbolMapScale = pow(2, 3);
		Image symbolMapImage = GenImageColor(range->boundingBox.width * symbolMapScale, range->boundingBox.height * symbolMapScale, BLANK);
		uint32_t* symbolMapPixels = symbolMapImage.data;
		
		size_t pixelsPerPage = symbolMapScale * symbolMapScale;
		size_t bytesPerPixel = pageSize / pixelsPerPage;
		uint64_t rangeStartPixel = range->startPage * pixelsPerPage;
		
		for (size_t s = 0; s < symbols.count; s++) {
			if ( !(symbols.at[s].fileOffset >= rangeStartOffset && symbols.at[s].fileOffset < rangeEndOffset) )
				continue;
			
			// Symbol is in the file area mapped into this address range
			uint64_t symbolStartPixel = rangeStartPixel + (symbols.at[s].fileOffset - rangeStartOffset) / bytesPerPixel;
			uint64_t symbolEndPixel = symbolStartPixel + symbols.at[s].size / bytesPerPixel;
			
			// Assign the pixels 
			float xMin = mapSideLength * symbolMapScale, xMax = 0, yMin = mapSideLength * symbolMapScale, yMax = 0;
			for (uint64_t pixel = symbolStartPixel; pixel < symbolEndPixel; pixel++) {
				int globalX, globalY;
				HilberCurveDistToPos(symbolMapHilberCurveOrder, pixel, &globalX, &globalY);
				int x = globalX - range->boundingBox.x * symbolMapScale;
				int y = globalY - range->boundingBox.y * symbolMapScale;
				
				symbolMapPixels[y * symbolMapImage.width + x] = s + 1;
				
				if (globalX < xMin) xMin = globalX;
				if (globalX > xMax) xMax = globalX;
				if (globalY < yMin) yMin = globalY;
				if (globalY > yMax) yMax = globalY;
			}
			
			Rectangle symbolBoundingBox = (Rectangle){
				.x = xMin / symbolMapScale, .y = yMin / symbolMapScale,
				.width = (xMax - xMin + 1) / symbolMapScale,
				.height = (yMax - yMin + 1) / symbolMapScale
			};
			
			range->symbolCount++;
			range->symbolLabels = realloc(range->symbolLabels, range->symbolCount * sizeof(range->symbolLabels[0]));
			range->symbolLabels[range->symbolCount - 1] = LabelFor(strdup(symbols.at[s].name), symbolBoundingBox, labelFont);
		}
		
		range->symbolMap = LoadTextureFromImage(symbolMapImage);
	}
	UnloadElfSymbols(&prevRangeSymbols);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdx, %rbx
movq %rsi, 0x98(%rsp)
movq %rdi, 0x90(%rsp)
xorps %xmm0, %xmm0
leaq 0xe0(%rsp), %rax
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rdx, %xmm0
punpckldq 0x487ab(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x487b3(%rip), %xmm0    # 0x592d0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x100(%rsp)
addq $0x3, %rbx
xorl %eax, %eax
cmpq 0x98(%rsp), %rax
je 0x10ef4
movq %rax, 0x48(%rsp)
imulq $0xa0, %rax, %rax
movq 0x90(%rsp), %rcx
leaq (%rcx,%rax), %r15
movq 0x8(%rcx,%rax), %r13
movq 0x28(%rcx,%rax), %rbp
subq (%rcx,%rax), %r13
addq %rbp, %r13
movq 0x20(%rcx,%rax), %rsi
leaq 0x50(%rsp), %rdi
callq 0x1187b
movups 0x50(%rsp), %xmm0
movaps %xmm0, 0x120(%rsp)
movq 0x60(%rsp), %r12
movq 0x68(%rsp), %r14
leaq 0xe0(%rsp), %rdi
callq 0x11a99
movapd 0x120(%rsp), %xmm0
movapd %xmm0, 0xe0(%rsp)
movq %r12, 0xf0(%rsp)
movq %r14, 0xf8(%rsp)
movq %r12, 0x38(%rsp)
leaq 0x8(%r12), %rax
movq %r14, 0x30(%rsp)
xorl %ecx, %ecx
subq $0x1, %r14
jb 0x10bfc
movq (%rax), %rsi
cmpq %rbp, %rsi
setae %dil
cmpq %r13, %rsi
setb %sil
andb %dil, %sil
movzbl %sil, %esi
addq %rsi, %rcx
addq $0x18, %rax
jmp 0x10bd5
testq %rcx, %rcx
movq 0x48(%rsp), %rax
je 0x10eec
movapd 0x100(%rsp), %xmm0
callq 0x6440
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x4875c(%rip), %xmm0    # 0x59388
cvttsd2si %xmm0, %r14
andq %rcx, %r14
orq %rax, %r14
movss 0x4c(%r15), %xmm0
movss 0x4843b(%rip), %xmm1    # 0x59080
mulss %xmm1, %xmm0
cvttss2si %xmm0, %esi
movss 0x50(%r15), %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %edx
leaq 0xc8(%rsp), %rdi
xorl %ecx, %ecx
callq 0x316e6
shlq $0x3, %r14
testq %r14, %r14
js 0x10c7a
cvtsi2ss %r14, %xmm0
jmp 0x10c86
shrq %r14
cvtsi2ss %r14, %xmm0
addss %xmm0, %xmm0
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %rdx
movq 0xc8(%rsp), %r14
movq 0x30(%r15), %rax
shlq $0x6, %rax
movq %rax, 0xa0(%rsp)
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
xorl %esi, %esi
movq %r13, 0xb0(%rsp)
movq %rbp, 0xa8(%rsp)
movaps %xmm0, 0x110(%rsp)
cmpq %rdx, %rsi
je 0x10ead
imulq $0x18, %rsi, %rdi
movq 0x8(%rcx,%rdi), %rax
movq %rax, %r12
subq %rbp, %r12
jb 0x10ea5
cmpq %r13, %rax
jae 0x10ea5
addq %rcx, %rdi
shrq $0x6, %r12
addq 0xa0(%rsp), %r12
movq %rdi, 0xb8(%rsp)
movq 0x10(%rdi), %rbp
shrq $0x6, %rbp
addq %r12, %rbp
movq %rsi, 0xc0(%rsp)
leal 0x1(%rsi), %r13d
xorps %xmm2, %xmm2
movaps %xmm0, %xmm1
cmpq %rbp, %r12
jae 0x10dc0
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x50(%rsp), %rdx
leaq 0x44(%rsp), %rcx
movaps %xmm2, 0x70(%rsp)
movaps %xmm1, 0x80(%rsp)
callq 0x11803
movss 0x44(%rsp), %xmm0
movss 0x50(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
cvtdq2ps %xmm1, %xmm0
movss 0x44(%r15), %xmm1
movss 0x48314(%rip), %xmm3    # 0x59080
mulss %xmm3, %xmm1
movaps %xmm0, %xmm2
subss %xmm1, %xmm2
cvttss2si %xmm2, %eax
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
movss 0x48(%r15), %xmm2
mulss %xmm3, %xmm2
subss %xmm2, %xmm1
cvttss2si %xmm1, %ecx
imull 0xd0(%rsp), %ecx
addl %eax, %ecx
movslq %ecx, %rax
movl %r13d, (%r14,%rax,4)
movaps %xmm0, %xmm1
minps 0x80(%rsp), %xmm1
maxps 0x70(%rsp), %xmm0
incq %r12
movaps %xmm0, %xmm2
jmp 0x10d21
subps %xmm1, %xmm2
movaps 0x48526(%rip), %xmm0    # 0x592f0
mulps %xmm0, %xmm1
movaps %xmm1, 0x80(%rsp)
addps 0x48524(%rip), %xmm2    # 0x59300
mulps %xmm0, %xmm2
movaps %xmm2, 0x70(%rsp)
movq 0x90(%r15), %rsi
movq 0x98(%r15), %rdi
incq %rsi
movq %rsi, 0x90(%r15)
shlq $0x5, %rsi
callq 0x6700
movq %rax, %r12
movq %rax, 0x98(%r15)
movq 0x90(%r15), %r13
shlq $0x5, %r13
movq 0xb8(%rsp), %rax
movq (%rax), %rdi
callq 0x64f0
leaq 0x170(%rsp), %rcx
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x50(%rsp), %rdi
movq %rax, %rsi
movaps 0x80(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
callq 0x11734
movups 0x50(%rsp), %xmm0
movups 0x60(%rsp), %xmm1
movups %xmm1, -0x10(%r12,%r13)
movups %xmm0, -0x20(%r12,%r13)
movq 0xb0(%rsp), %r13
movq 0xa8(%rsp), %rbp
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %rdx
movaps 0x110(%rsp), %xmm0
movq 0xc0(%rsp), %rsi
incq %rsi
jmp 0x10cc6
movq 0xd8(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0xc8(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x50(%rsp), %rdi
callq 0x36594
movl 0x60(%rsp), %eax
movl %eax, 0x88(%r15)
movupd 0x50(%rsp), %xmm0
movupd %xmm0, 0x78(%r15)
movq 0x48(%rsp), %rax
incq %rax
jmp 0x10b38
leaq 0xe0(%rsp), %rdi
callq 0x11a99
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq