// File: /workspace/github_c_makefile_star2_O2/arkanis[P]process-map/map.c
// Function: PlotAddrRanges
Texture2D PlotAddrRanges(AddrRange ranges[], size_t rangeCount, Font rangeNameFont, size_t* hilbertCurveOrder) {
	// First figure out how many pages we need to fit into the map, from that we can calculate the size of the map square.
	uint64_t minRequiredPages = 0, oversizedGapPages = 0;
	for (size_t i = 0; i < rangeCount; i++) {
		size_t rangePageCount = (ranges[i].endAddr - ranges[i].startAddr) / pageSize;
		if (ranges[i].isGap) {
			if (rangePageCount > cappedGapPageCount) {
				minRequiredPages += cappedGapPageCount;
				oversizedGapPages += rangePageCount;
			} else {
				minRequiredPages += rangePageCount;
			}
		} else {
			minRequiredPages += rangePageCount;
		}
	}
	
	// Caclulate texture size and the order of the hilbert curve used to map the 1D address ranges onto a 2D plane.
	// sqrt(minRequiredPages) is the size of the square needed to fit minRequiredPages pixels.
	// log2(squareSize) is the order of the hilber curve (and the power-of-two exponent of the square size). It needs to be an integer so we round up with ceil().
	*hilbertCurveOrder = ceil(log2(sqrt(minRequiredPages)));
	size_t mapSideLength = pow(2, *hilbertCurveOrder);
	
	// We now know the size of the map, distribute the left over map space among the oversized gaps (proportinally to their respective relative size)
	uint64_t leftOverFreePages = mapSideLength*mapSideLength - minRequiredPages;
	uint64_t prevRangeEndPage = 0;
	for (size_t i = 0; i < rangeCount; i++) {
		size_t rangePageCount = (ranges[i].endAddr - ranges[i].startAddr) / pageSize;
		ranges[i].startPage = prevRangeEndPage;
		if (i == rangeCount - 1) {
			// Make sure the end of the last range aligns with the end of the map
			ranges[i].endPage = mapSideLength*mapSideLength;
		} else if (ranges[i].isGap && rangePageCount > cappedGapPageCount) {
			rangePageCount = cappedGapPageCount + leftOverFreePages * ((double)rangePageCount / oversizedGapPages);
			ranges[i].endPage = ranges[i].startPage + rangePageCount;
			// Round the endPage towards to the closest 16M boundary to avoid jagged edges on the map 
			ranges[i].endPage = (ranges[i].endPage + cappedGapPageCount / 2) / cappedGapPageCount * cappedGapPageCount;
		} else {
			ranges[i].endPage = ranges[i].startPage + rangePageCount;
		}
		
		prevRangeEndPage = ranges[i].endPage;
	}
	
	// Create a map image where the address ranges are mapped into the image via a hilbert curve.
	// Each range gets its own color and all pixels of that range are filled with its color.
	// The color has to be unique since we use them to draw the borders between ranges in the fragment shader and to
	// highlight all pixels of the selected range.
	// Each color (a 32 bit value) is constructed as follows: rr rr ii tt
	// rr rr: 16 bits of pseudo-random data taken from the hashed range name or the ranges start and end address.
	//        The fragment shader uses those bits to calculate a display color for the range.
	// ii:    8 bits of the range index just to make sure consecutive ranges have different colors even if they share
	//        the same name (and with that the same hash).
	// tt:    The type of the range (unused area, gap, file backed range, anonymous range). The fragment shader draws
	//        different types with different colors (e.g. gaps as light gray instead of a random color).
	// While we iterate over each pixel of a range also calculate the bounding box for later (text positioning, draw culling, etc.).
	Image mapImage = GenImageColor(mapSideLength, mapSideLength, BLANK);
	Color* mapImagePixels = mapImage.data;
	for (size_t i = 0; i < rangeCount; i++) {
		Color color;
		if (ranges[i].isGap) {
			uint32_t hash = fnv1a(&ranges[i], offsetof(AddrRange, isGap));
			color = (Color){ .r = hash, .g = hash >> 8, .b = i, .a = 1 };  // 1 = gap
		} else if (ranges[i].filePath) {
			uint32_t hash = fnv1a(ranges[i].filePath, strlen(ranges[i].filePath));
			color = (Color){ .r = hash, .g = hash >> 8, .b = i, .a = 2 };  // 2 = file backed range
		} else {
			uint32_t hash = fnv1a(&ranges[i], offsetof(AddrRange, isGap));
			color = (Color){ .r = hash, .g = hash >> 8, .b = i, .a = 3 };  // 3 = anonymous range
		}
		
		if (ranges[i].perms) {
			if (ranges[i].perms[2] == 'x') {
				color.a = 4;  // 4 = executable range
			} else if (ranges[i].perms[1] == 'w') {
				color.a = 5;  // 5 = writable range
			} else if (ranges[i].perms[0] == 'r') {
				color.a = 6;  // 6 = readable range
			}
		}
		
		int xMin = mapSideLength, xMax = 0, yMin = mapSideLength, yMax = 0;
		for (uint64_t page = ranges[i].startPage; page < ranges[i].endPage; page++) {
			int x, y;
			HilberCurveDistToPos(*hilbertCurveOrder, page, &x, &y);
			mapImagePixels[y * mapImage.width + x] = color;
			
			if (x < xMin) xMin = x;
			if (x > xMax) xMax = x;
			if (y < yMin) yMin = y;
			if (y > yMax) yMax = y;
		}
		
		ranges[i].color = color;
		ranges[i].boundingBox = (Rectangle){ .x = xMin, .y = yMin, .width = xMax - xMin + 1, .height = yMax - yMin + 1 };
	}
	
	// Create the label text for each region and calculate the labels position and font size
	for (size_t i = 0; i < rangeCount; i++) {
		char* text = NULL;
		if (ranges[i].isGap)
			text = strdup("unmapped");
		else if (ranges[i].filePath)
			text = strdup(basename(ranges[i].filePath));
		else
			text = strdup("anonymous");
		
		ranges[i].label = LabelFor(text, ranges[i].boundingBox, rangeNameFont);
	}

	Texture2D mapTexture = LoadTextureFromImage(mapImage);
	UnloadImage(mapImage);
	return mapTexture;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rcx, 0x48(%rsp)
movq %rdx, %r13
movq %rdi, 0x58(%rsp)
movq %rsi, 0x40(%rsp)
leaq 0x10(%rsi), %rax
xorl %r14d, %r14d
movq %rdx, %rcx
xorl %r15d, %r15d
subq $0x1, %rcx
jb 0x10668
movq -0x8(%rax), %rsi
subq -0x10(%rax), %rsi
movq %rsi, %rdx
shrq $0xc, %rdx
cmpb $0x1, (%rax)
jne 0x1065d
cmpq $0x1001000, %rsi        # imm = 0x1001000
jb 0x1065d
addq $0x1000, %r15           # imm = 0x1000
addq %rdx, %r14
jmp 0x10660
addq %rdx, %r15
addq $0xa0, %rax
jmp 0x1062e
movq %r15, %xmm0
punpckldq 0x48c4b(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x48c53(%rip), %xmm0    # 0x592d0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
sqrtsd %xmm1, %xmm0
callq 0x6670
callq 0x60a0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x48cdd(%rip), %xmm0    # 0x59388
cvttsd2si %xmm0, %rdx
andq %rcx, %rdx
orq %rax, %rdx
movq 0x48(%rsp), %rax
movq %rdx, (%rax)
movq %rdx, %xmm1
punpckldq 0x48bf5(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x48bfd(%rip), %xmm1    # 0x592d0
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0x6440
movsd 0x48c9c(%rip), %xmm4    # 0x59388
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd %xmm4, %xmm0
cvttsd2si %xmm0, %rbx
sarq $0x3f, %rcx
andq %rcx, %rbx
orq %rax, %rbx
movq %rbx, %rax
imulq %rbx, %rax
movq %rax, %rcx
subq %r15, %rcx
movq %rcx, %xmm1
movq 0x48b9f(%rip), %xmm3    # 0x592c0
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movapd 0x48ba3(%rip), %xmm5    # 0x592d0
subpd %xmm5, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq %r14, %xmm2
punpckldq %xmm3, %xmm2    # xmm2 = xmm2[0],xmm3[0],xmm2[1],xmm3[1]
subpd %xmm5, %xmm2
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
movq 0x40(%rsp), %r15
leaq 0x38(%r15), %rcx
xorl %edx, %edx
movsd 0x48c27(%rip), %xmm2    # 0x59390
movq %r13, %rdi
movq %r13, %rsi
subq $0x1, %rsi
jb 0x10803
movq -0x30(%rcx), %r8
subq -0x38(%rcx), %r8
movq %rdx, -0x8(%rcx)
cmpq $0x1, %rdi
jne 0x10793
movq %rax, (%rcx)
movq %rax, %rdx
jmp 0x107f4
movq %r8, %rdi
shrq $0xc, %rdi
cmpq $0x1001000, %r8         # imm = 0x1001000
setae %r8b
andb -0x28(%rcx), %r8b
cmpb $0x1, %r8b
jne 0x107ee
cvtsi2sd %rdi, %xmm3
divsd %xmm1, %xmm3
mulsd %xmm0, %xmm3
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %rdi
movq %rdi, %r8
sarq $0x3f, %r8
subsd %xmm4, %xmm3
cvttsd2si %xmm3, %r9
andq %r8, %r9
orq %rdi, %r9
addq %r9, %rdx
addq $0x800, %rdx            # imm = 0x800
andq $-0x1000, %rdx          # imm = 0xF000
jmp 0x107f1
addq %rdi, %rdx
movq %rdx, (%rcx)
addq $0xa0, %rcx
movq %rsi, %rdi
jmp 0x1076f
leaq 0x68(%rsp), %r14
xorl %r12d, %r12d
movq %r14, %rdi
movl %ebx, %esi
movl %ebx, %edx
xorl %ecx, %ecx
callq 0x316e6
movq (%r14), %rbp
movd %ebx, %xmm0
pshufd $0x50, %xmm0, %xmm0     # xmm0 = xmm0[0,0,1,1]
movdqa %xmm0, 0xa0(%rsp)
movq %r13, 0x60(%rsp)
cmpq %r13, %r12
je 0x109d2
imulq $0xa0, %r12, %rax
leaq (%r15,%rax), %r13
cmpb $0x1, 0x10(%r15,%rax)
movq %r12, 0x50(%rsp)
jne 0x10866
movq %r13, %rdi
pushq $0x10
popq %rsi
callq 0x117e5
movl %eax, %r12d
movb $0x1, %al
jmp 0x10899
movq 0x20(%r13), %r14
testq %r14, %r14
je 0x10889
movq %r14, %rdi
callq 0x6300
movq %r14, %rdi
movq %rax, %rsi
callq 0x117e5
movl %eax, %r12d
movb $0x2, %al
jmp 0x10899
movq %r13, %rdi
pushq $0x10
popq %rsi
callq 0x117e5
movl %eax, %r12d
movb $0x3, %al
movq 0x18(%r13), %rcx
testq %rcx, %rcx
je 0x108c0
movb $0x4, %bl
cmpb $0x78, 0x2(%rcx)
je 0x108c2
movb $0x5, %bl
cmpb $0x77, 0x1(%rcx)
je 0x108c2
cmpb $0x72, (%rcx)
movzbl %al, %ebx
pushq $0x6
popq %rax
cmovel %eax, %ebx
jmp 0x108c2
movl %eax, %ebx
movl %r12d, %r14d
shrl $0x8, %r14d
movq 0x30(%r13), %r15
xorpd %xmm2, %xmm2
movdqa 0xa0(%rsp), %xmm0
cmpq 0x38(%r13), %r15
jae 0x10993
movq 0x48(%rsp), %rax
movq (%rax), %rdi
movq %r15, %rsi
leaq 0x80(%rsp), %rdx
leaq 0x3c(%rsp), %rcx
movaps %xmm2, 0xc0(%rsp)
movdqa %xmm0, 0xb0(%rsp)
callq 0x11803
movl 0x3c(%rsp), %eax
movl 0x70(%rsp), %ecx
imull %eax, %ecx
movl 0x80(%rsp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movb %r12b, (%rbp,%rcx,4)
movb %r14b, 0x1(%rbp,%rcx,4)
movq 0x50(%rsp), %rsi
movb %sil, 0x2(%rbp,%rcx,4)
movb %bl, 0x3(%rbp,%rcx,4)
movd %eax, %xmm0
movd %edx, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movdqa 0xb0(%rsp), %xmm3
movdqa %xmm3, %xmm0
pcmpgtd %xmm1, %xmm0
movdqa %xmm1, %xmm2
pand %xmm0, %xmm2
pandn %xmm3, %xmm0
por %xmm2, %xmm0
movdqa %xmm1, %xmm2
movdqa 0xc0(%rsp), %xmm3
pcmpgtd %xmm3, %xmm2
pand %xmm2, %xmm1
pandn %xmm3, %xmm2
por %xmm1, %xmm2
incq %r15
jmp 0x108da
movb %r12b, 0x40(%r13)
movb %r14b, 0x41(%r13)
movq 0x50(%rsp), %r12
movb %r12b, 0x42(%r13)
movb %bl, 0x43(%r13)
psubd %xmm0, %xmm2
psubd 0x4892c(%rip), %xmm2    # 0x592e0
punpcklqdq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0]
cvtdq2ps %xmm0, %xmm0
movups %xmm0, 0x44(%r13)
incq %r12
movq 0x60(%rsp), %r13
movq 0x40(%rsp), %r15
jmp 0x10833
addq $0x58, %r15
leaq 0x48bfa(%rip), %rbx     # 0x595d7
leaq 0x80(%rsp), %r14
leaq 0x48bf4(%rip), %r12     # 0x595e0
leaq 0x110(%rsp), %rbp
subq $0x1, %r13
jb 0x10a85
cmpb $0x1, -0x48(%r15)
jne 0x10a0a
movq %rbx, %rdi
jmp 0x10a20
movq -0x38(%r15), %rdi
testq %rdi, %rdi
je 0x10a1d
callq 0x60f0
movq %rax, %rdi
jmp 0x10a20
movq %r12, %rdi
callq 0x64f0
movsd -0x14(%r15), %xmm0
movsd -0xc(%r15), %xmm1
movupd (%rbp), %xmm2
movdqu 0x10(%rbp), %xmm3
movupd 0x20(%rbp), %xmm4
movupd %xmm4, 0x20(%rsp)
movdqu %xmm3, 0x10(%rsp)
movupd %xmm2, (%rsp)
movq %r14, %rdi
movq %rax, %rsi
callq 0x11734
movdqu 0x80(%rsp), %xmm0
movupd 0x90(%rsp), %xmm1
movupd %xmm1, 0x10(%r15)
movdqu %xmm0, (%r15)
addq $0xa0, %r15
jmp 0x109f4
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
movq 0x58(%rsp), %rbx
movq %rbx, %rdi
callq 0x36594
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x3105c
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq