// File: /workspace/github_c_makefile_star2_O1/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 $0x128, %rsp            # imm = 0x128
testq %rsi, %rsi
je 0x13406
movq %rsi, %r14
movq %rdi, %rbp
movq %rdx, %xmm0
punpckldq 0x552e7(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x552ef(%rip), %xmm0    # 0x681c0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x110(%rsp)
addq $0x3, %rdx
movq %rdx, 0xd0(%rsp)
xorl %eax, %eax
xorl %edi, %edi
xorl %esi, %esi
xorl %r15d, %r15d
movq %r14, 0xb0(%rsp)
movq %rbp, 0xa8(%rsp)
movq %r15, 0x50(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, %r15
movq %rax, 0xc8(%rsp)
leaq (%rax,%rax,4), %r12
shlq $0x5, %r12
movq 0x8(%rbp,%r12), %rbx
movq 0x28(%rbp,%r12), %rax
movq %rax, 0x48(%rsp)
subq (%rbp,%r12), %rbx
movq 0x20(%rbp,%r12), %rsi
leaq 0x88(%rsp), %rdi
callq 0x13e56
movq 0x88(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0x98(%rsp), %rax
movq 0xa0(%rsp), %r13
movq %r15, %rdi
movq 0x40(%rsp), %rsi
movq %rax, %r15
callq 0x65f0
movq 0x50(%rsp), %rdi
callq 0x62e0
movq %r13, 0x78(%rsp)
testq %r13, %r13
je 0x133e0
addq %rbp, %r12
movq 0x48(%rsp), %r9
addq %r9, %rbx
leaq 0x8(%r15), %rax
movq 0x78(%rsp), %rdx
xorl %ecx, %ecx
movq (%rax), %rsi
cmpq %r9, %rsi
setae %dil
cmpq %rbx, %rsi
setb %sil
andb %dil, %sil
movzbl %sil, %esi
addq %rsi, %rcx
addq $0x18, %rax
decq %rdx
jne 0x12fbc
movq %rbx, 0x70(%rsp)
testq %rcx, %rcx
je 0x133e0
movaps 0x110(%rsp), %xmm0
callq 0x6430
movaps %xmm0, 0x50(%rsp)
movss 0x4c(%r12), %xmm0
movss 0x55075(%rip), %xmm1    # 0x68084
mulss %xmm1, %xmm0
cvttss2si %xmm0, %esi
movss 0x50(%r12), %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %edx
leaq 0x88(%rsp), %rdi
xorl %ecx, %ecx
callq 0x3cabc
movapd 0x50(%rsp), %xmm0
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
subsd 0x55239(%rip), %xmm0    # 0x68288
cvttsd2si %xmm0, %rax
andq %rdx, %rax
orq %rcx, %rax
shlq $0x3, %rax
testq %rax, %rax
js 0x1306d
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x1307c
shrq %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x34(%rsp)
movq 0x48(%rsp), %rsi
movq 0x88(%rsp), %rdi
movq 0x30(%r12), %rax
shlq $0x6, %rax
movq %rax, 0xd8(%rsp)
xorl %r8d, %r8d
movq 0x70(%rsp), %rdx
movq %r15, 0xe0(%rsp)
movq %rdi, 0xf0(%rsp)
leaq (%r8,%r8,2), %rax
movq 0x8(%r15,%rax,8), %rcx
movq %rcx, %r14
subq %rsi, %r14
jb 0x1337d
cmpq %rdx, %rcx
jae 0x1337d
movq %r8, 0x80(%rsp)
leaq (%r15,%rax,8), %rax
shrq $0x6, %r14
addq 0xd8(%rsp), %r14
movq %rax, 0xe8(%rsp)
movq 0x10(%rax), %r15
shrq $0x6, %r15
leaq (%r15,%r14), %rax
xorps %xmm2, %xmm2
movss 0x34(%rsp), %xmm0
movaps %xmm0, %xmm1
movaps %xmm0, %xmm4
xorps %xmm3, %xmm3
cmpq %rax, %r14
movq 0xd0(%rsp), %rbp
leaq 0xfc(%rsp), %r13
jae 0x13209
movq 0x80(%rsp), %rax
leal 0x1(%rax), %ebx
xorps %xmm5, %xmm5
movss 0x34(%rsp), %xmm0
movaps %xmm0, %xmm6
xorps %xmm7, %xmm7
movss %xmm0, 0x38(%rsp)
movss %xmm7, 0x3c(%rsp)
movss %xmm6, 0x40(%rsp)
movss %xmm5, 0x50(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
leaq 0x6c(%rsp), %rcx
callq 0x13dde
movss 0x54f0a(%rip), %xmm4    # 0x68084
xorps %xmm2, %xmm2
cvtsi2ssl 0xfc(%rsp), %xmm2
movss 0x44(%r12), %xmm0
mulss %xmm4, %xmm0
movaps %xmm2, %xmm1
subss %xmm0, %xmm1
cvttss2si %xmm1, %eax
xorps %xmm3, %xmm3
cvtsi2ssl 0x6c(%rsp), %xmm3
movss 0x48(%r12), %xmm0
mulss %xmm4, %xmm0
movaps %xmm3, %xmm1
subss %xmm0, %xmm1
cvttss2si %xmm1, %ecx
imull 0x90(%rsp), %ecx
addl %eax, %ecx
movslq %ecx, %rax
movq 0xf0(%rsp), %rcx
movl %ebx, (%rcx,%rax,4)
movaps %xmm2, %xmm1
minss 0x38(%rsp), %xmm1
maxss 0x3c(%rsp), %xmm2
movaps %xmm3, %xmm4
minss 0x40(%rsp), %xmm4
maxss 0x50(%rsp), %xmm3
incq %r14
movaps %xmm3, %xmm5
movaps %xmm4, %xmm6
movaps %xmm2, %xmm7
movaps %xmm1, %xmm0
decq %r15
jne 0x13147
subss %xmm1, %xmm2
movss 0x54e7b(%rip), %xmm0    # 0x68090
mulss %xmm0, %xmm1
movss %xmm1, 0x50(%rsp)
movss 0x54ddd(%rip), %xmm1    # 0x68004
addss %xmm1, %xmm2
mulss %xmm0, %xmm2
movss %xmm2, 0x38(%rsp)
subss %xmm4, %xmm3
mulss %xmm0, %xmm4
movss %xmm4, 0x40(%rsp)
addss %xmm1, %xmm3
mulss %xmm0, %xmm3
movss %xmm3, 0x3c(%rsp)
movq 0x90(%r12), %rsi
movq 0x98(%r12), %rdi
incq %rsi
movq %rsi, 0x90(%r12)
shlq $0x5, %rsi
callq 0x66f0
movq %rax, %r14
movq %rax, 0x98(%r12)
movq 0x90(%r12), %rbx
shlq $0x5, %rbx
movq 0xe8(%rsp), %rax
movq (%rax), %rdi
callq 0x64e0
movq %rax, %r15
leaq 0x160(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
xorps %xmm1, %xmm1
movq %r15, %rdi
movss 0x54d4a(%rip), %xmm0    # 0x68018
callq 0x51b09
movss 0x38(%rsp), %xmm4
xorps %xmm1, %xmm1
cvtss2sd %xmm4, %xmm1
mulsd 0x54fb0(%rip), %xmm1    # 0x68298
xorps %xmm2, %xmm2
cvtss2sd %xmm0, %xmm2
divsd %xmm2, %xmm1
cvtsd2ss %xmm1, %xmm1
movaps %xmm0, %xmm2
mulss %xmm1, %xmm2
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
mulss %xmm1, %xmm0
mulss 0x54d0a(%rip), %xmm1    # 0x68018
subss %xmm2, %xmm4
movss 0x54cf6(%rip), %xmm3    # 0x68010
mulss %xmm3, %xmm4
addss 0x50(%rsp), %xmm4
movaps %xmm4, %xmm5
movss 0x3c(%rsp), %xmm4
subss %xmm0, %xmm4
mulss %xmm3, %xmm4
addss 0x40(%rsp), %xmm4
movq %r15, -0x20(%r14,%rbx)
movss %xmm1, -0x18(%r14,%rbx)
movss %xmm5, -0x14(%r14,%rbx)
movss %xmm4, -0x10(%r14,%rbx)
movss %xmm2, -0xc(%r14,%rbx)
movss %xmm0, -0x8(%r14,%rbx)
movq 0xe0(%rsp), %r15
movq 0x70(%rsp), %rdx
movq 0x48(%rsp), %rsi
movq 0x80(%rsp), %r8
incq %r8
cmpq 0x78(%rsp), %r8
jne 0x130b8
movq 0x98(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x88(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0xfc(%rsp), %rdi
callq 0x4207c
movl 0x10c(%rsp), %eax
movl %eax, 0x88(%r12)
movupd 0xfc(%rsp), %xmm0
movupd %xmm0, 0x78(%r12)
movq 0xb0(%rsp), %r14
movq 0xa8(%rsp), %rbp
movq 0xc8(%rsp), %rax
incq %rax
cmpq %r14, %rax
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
jne 0x12f0b
jmp 0x1340d
xorl %r15d, %r15d
xorl %esi, %esi
xorl %edi, %edi
callq 0x65f0
movq %r15, %rdi
callq 0x62e0
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq