// File: /workspace/github_c_makefile_star2_O3/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
testq %rsi, %rsi
je 0x14ac7
movq %rsi, %r14
movq %rdi, %rbp
movq %rdx, %xmm0
punpckldq 0x50c6c(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x50c74(%rip), %xmm0    # 0x65280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x120(%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, 0x40(%rsp)
movq %rsi, 0x60(%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, 0x38(%rsp)
subq (%rbp,%r12), %rbx
movq 0x20(%rbp,%r12), %rsi
leaq 0x88(%rsp), %rdi
callq 0x154e4
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 0x60(%rsp), %rsi
movq %rax, %r15
callq 0x65f0
movq 0x40(%rsp), %rdi
callq 0x62e0
movq %r13, 0x78(%rsp)
testq %r13, %r13
je 0x14aa1
addq %rbp, %r12
movq 0x38(%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 0x146f7
movq %rbx, 0x70(%rsp)
testq %rcx, %rcx
je 0x14aa1
movaps 0x120(%rsp), %xmm0
callq 0x6430
movaps %xmm0, 0x40(%rsp)
movss 0x4c(%r12), %xmm0
movss 0x50936(%rip), %xmm1    # 0x65080
mulss %xmm1, %xmm0
cvttss2si %xmm0, %esi
movss 0x50(%r12), %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %edx
leaq 0x88(%rsp), %rdi
xorl %ecx, %ecx
callq 0x39c39
movapd 0x40(%rsp), %xmm0
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
subsd 0x50bfe(%rip), %xmm0    # 0x65388
cvttsd2si %xmm0, %rax
andq %rdx, %rax
orq %rcx, %rax
shlq $0x3, %rax
testq %rax, %rax
js 0x147a8
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x147b7
shrq %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movq 0x38(%rsp), %rsi
movq 0x88(%rsp), %rdi
movq 0x30(%r12), %rax
shlq $0x6, %rax
movq %rax, 0xd8(%rsp)
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
xorl %r8d, %r8d
movq 0x70(%rsp), %rdx
movq %r15, 0xe0(%rsp)
movaps %xmm0, 0x110(%rsp)
movq %rdi, 0xf0(%rsp)
leaq (%r8,%r8,2), %rax
movq 0x8(%r15,%rax,8), %rcx
movq %rcx, %r14
subq %rsi, %r14
jb 0x14a3e
cmpq %rdx, %rcx
jae 0x14a3e
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
movaps %xmm0, %xmm1
cmpq %rax, %r14
movq 0xd0(%rsp), %rbp
leaq 0xfc(%rsp), %r13
jae 0x14919
movq 0x80(%rsp), %rax
leal 0x1(%rax), %ebx
xorps %xmm0, %xmm0
movaps 0x110(%rsp), %xmm3
movaps %xmm3, 0x60(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
leaq 0x5c(%rsp), %rcx
callq 0x15466
movss 0x507e3(%rip), %xmm3    # 0x65080
movss 0x5c(%rsp), %xmm0
movss 0xfc(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
cvtdq2ps %xmm1, %xmm2
movss 0x44(%r12), %xmm0
mulss %xmm3, %xmm0
movaps %xmm2, %xmm1
subss %xmm0, %xmm1
cvttss2si %xmm1, %eax
movaps %xmm2, %xmm0
shufps $0x55, %xmm2, %xmm0     # xmm0 = xmm0[1,1],xmm2[1,1]
movss 0x48(%r12), %xmm1
mulss %xmm3, %xmm1
subss %xmm1, %xmm0
cvttss2si %xmm0, %ecx
imull 0x90(%rsp), %ecx
addl %eax, %ecx
movslq %ecx, %rax
movq 0xf0(%rsp), %rcx
movl %ebx, (%rcx,%rax,4)
movaps %xmm2, %xmm1
minps 0x60(%rsp), %xmm1
maxps 0x40(%rsp), %xmm2
incq %r14
movaps %xmm1, %xmm3
movaps %xmm2, %xmm0
decq %r15
jne 0x14878
subps %xmm1, %xmm2
movaps %xmm2, 0x60(%rsp)
movaps 0x509e8(%rip), %xmm0    # 0x65310
mulps %xmm0, %xmm1
movaps %xmm1, 0x40(%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 0x170(%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 0x50663(%rip), %xmm0    # 0x65010
callq 0x4eae9
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
movq %r15, -0x20(%r14,%rbx)
movaps 0x60(%rsp), %xmm3
addps 0x50956(%rip), %xmm3    # 0x65320
mulps 0x5093f(%rip), %xmm3    # 0x65310
xorps %xmm2, %xmm2
cvtss2sd %xmm3, %xmm2
mulsd 0x509b8(%rip), %xmm2    # 0x65398
divsd %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtsd2ss %xmm2, %xmm1
movaps %xmm1, %xmm2
mulss 0x5061a(%rip), %xmm2    # 0x65010
shufps $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
mulps %xmm0, %xmm1
subps %xmm1, %xmm3
mulps 0x50749(%rip), %xmm3    # 0x65150
addps 0x40(%rsp), %xmm3
movss %xmm2, -0x18(%r14,%rbx)
movlhps %xmm1, %xmm3            # xmm3 = xmm3[0],xmm1[0]
movups %xmm3, -0x14(%r14,%rbx)
movq 0xe0(%rsp), %r15
movq 0x70(%rsp), %rdx
movq 0x38(%rsp), %rsi
movaps 0x110(%rsp), %xmm0
movq 0x80(%rsp), %r8
incq %r8
cmpq 0x78(%rsp), %r8
jne 0x147f9
movq 0x98(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x88(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0xfc(%rsp), %rdi
callq 0x3f17c
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 0x14646
jmp 0x14ace
xorl %r15d, %r15d
xorl %esi, %esi
xorl %edi, %edi
callq 0x65f0
movq %r15, %rdi
callq 0x62e0
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq