// File: /workspace/github_c_makefile_star2_O1/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 $0xc8, %rsp
movq %rdx, %r14
movq %rsi, %r15
xorpd %xmm1, %xmm1
testq %rdx, %rdx
je 0x128c0
leaq 0x10(%r15), %rax
xorl %ebx, %ebx
movq %r14, %r9
xorl %edx, %edx
movq -0x8(%rax), %r8
subq -0x10(%rax), %r8
movq %r8, %rsi
shrq $0xc, %rsi
cmpb $0x1, (%rax)
jne 0x1288f
cmpq $0x1001000, %r8         # imm = 0x1001000
jb 0x1288f
addq $0x1000, %rbx           # imm = 0x1000
addq %rsi, %rdx
jmp 0x12892
addq %rsi, %rbx
addq $0xa0, %rax
decq %r9
jne 0x12866
movq %rdx, %xmm0
punpckldq 0x55906(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x5590e(%rip), %xmm0    # 0x681c0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
jmp 0x128c6
xorl %ebx, %ebx
xorpd %xmm3, %xmm3
movq %rcx, 0x48(%rsp)
movq %rbx, %xmm2
punpckldq 0x558d8(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x558e0(%rip), %xmm2    # 0x681c0
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
movq %rdi, 0x90(%rsp)
movapd %xmm3, 0x50(%rsp)
jb 0x12906
sqrtsd %xmm0, %xmm0
jmp 0x1290b
callq 0x6790
callq 0x6660
callq 0x60a0
cvttsd2si %xmm0, %rax
subsd 0x55966(%rip), %xmm0    # 0x68288
cvttsd2si %xmm0, %rcx
movq %rax, %rdx
sarq $0x3f, %rdx
andq %rcx, %rdx
orq %rax, %rdx
movq %rdx, %xmm1
punpckldq 0x5586f(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x55877(%rip), %xmm1    # 0x681c0
movq 0x48(%rsp), %rax
movq %rdx, (%rax)
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0x6430
movsd 0x5591e(%rip), %xmm4    # 0x68288
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd %xmm4, %xmm0
cvttsd2si %xmm0, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
orq %rax, %rdx
movq %rdx, 0x40(%rsp)
testq %r14, %r14
movapd 0x50(%rsp), %xmm3
je 0x12a6b
movq 0x40(%rsp), %rax
imulq %rax, %rax
movq %rax, %rcx
subq %rbx, %rcx
movq %rcx, %xmm1
punpckldq 0x557fb(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x55803(%rip), %xmm1    # 0x681c0
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
leaq 0x38(%r15), %rcx
xorl %esi, %esi
movsd 0x558b9(%rip), %xmm1    # 0x68290
movq %r14, %rdx
movq -0x30(%rcx), %r8
subq -0x38(%rcx), %r8
movq %rsi, -0x8(%rcx)
cmpq $0x1, %rdx
jne 0x129f1
movq %rax, (%rcx)
jmp 0x12a58
movq %r8, %rdi
shrq $0xc, %rdi
cmpq $0x1001000, %r8         # imm = 0x1001000
setae %r8b
andb -0x28(%rcx), %r8b
cmpb $0x1, %r8b
jne 0x12a52
xorps %xmm2, %xmm2
cvtsi2sd %rdi, %xmm2
divsd %xmm3, %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %rdi
movq %rdi, %r8
sarq $0x3f, %r8
subsd %xmm4, %xmm2
cvttsd2si %xmm2, %r9
andq %r8, %r9
orq %rdi, %r9
addq %r9, %rsi
addq $0x800, %rsi            # imm = 0x800
andq $-0x1000, %rsi          # imm = 0xF000
movq %rsi, (%rcx)
jmp 0x12a58
addq %rsi, %rdi
movq %rdi, (%rcx)
movq (%rcx), %rsi
addq $0xa0, %rcx
decq %rdx
jne 0x129da
leaq 0x68(%rsp), %rdi
movq 0x40(%rsp), %rdx
movl %edx, %esi
xorl %ecx, %ecx
callq 0x3cabc
testq %r14, %r14
je 0x12e4c
movq 0x68(%rsp), %rax
movq %rax, 0xb0(%rsp)
xorl %ebx, %ebx
leaq 0x3c(%rsp), %rdx
leaq 0x38(%rsp), %rcx
movq %r15, %rbp
movq %r14, 0xa0(%rsp)
movq %r15, 0x98(%rsp)
leaq (%rbx,%rbx,4), %rax
shlq $0x5, %rax
leaq (%r15,%rax), %rdi
cmpb $0x1, 0x10(%r15,%rax)
jne 0x12aec
movl $0x811c9dc5, %r9d       # imm = 0x811C9DC5
xorl %eax, %eax
movsbl (%rbp,%rax), %esi
xorl %r9d, %esi
imull $0x1000193, %esi, %r9d  # imm = 0x1000193
incq %rax
cmpq $0x10, %rax
jne 0x12acf
movb $0x1, %sil
jmp 0x12b62
movq 0x20(%rdi), %r13
testq %r13, %r13
je 0x12b2a
movq %rdi, %r14
movq %r13, %rdi
callq 0x62f0
movb $0x2, %sil
testq %rax, %rax
jle 0x12b4f
addq %r13, %rax
movl $0x811c9dc5, %r9d       # imm = 0x811C9DC5
movsbl (%r13), %edx
xorl %r9d, %edx
imull $0x1000193, %edx, %r9d  # imm = 0x1000193
incq %r13
cmpq %rax, %r13
jb 0x12b11
jmp 0x12b55
movl $0x811c9dc5, %r9d       # imm = 0x811C9DC5
xorl %eax, %eax
movsbl (%rbp,%rax), %esi
xorl %r9d, %esi
imull $0x1000193, %esi, %r9d  # imm = 0x1000193
incq %rax
cmpq $0x10, %rax
jne 0x12b32
movb $0x3, %sil
jmp 0x12b62
movl $0x811c9dc5, %r9d       # imm = 0x811C9DC5
leaq 0x3c(%rsp), %rdx
leaq 0x38(%rsp), %rcx
movq %r14, %rdi
movq 0x18(%rdi), %rax
testq %rax, %rax
movq %rbp, 0xa8(%rsp)
je 0x12b97
movb $0x4, %r8b
cmpb $0x78, 0x2(%rax)
je 0x12b9a
movb $0x5, %r8b
cmpb $0x77, 0x1(%rax)
je 0x12b9a
cmpb $0x72, (%rax)
movzbl %sil, %r8d
movl $0x6, %eax
cmovel %eax, %r8d
jmp 0x12b9a
movl %esi, %r8d
movl %r9d, %r12d
shrl $0x8, %r12d
movq 0x30(%rdi), %r13
xorl %r15d, %r15d
movq 0x40(%rsp), %rax
movl %eax, %r14d
movl %eax, %ebp
movl $0x0, %r11d
cmpq 0x38(%rdi), %r13
movq 0x48(%rsp), %rsi
jae 0x12ca0
xorl %r11d, %r11d
movq 0x40(%rsp), %rax
movl %eax, %ebp
xorl %r15d, %r15d
movl %eax, %r14d
movq %rdi, 0xc0(%rsp)
movq %rbx, 0xb8(%rsp)
movl %r8d, 0x50(%rsp)
movl %r14d, 0x80(%rsp)
movq (%rsi), %rdi
movq %r13, %rsi
movl %r9d, %ebx
movl %r15d, %r14d
movl %ebp, %r15d
movl %r11d, %ebp
callq 0x13dde
movl %ebp, %r11d
movl %r15d, %ebp
movl %r14d, %r15d
movl 0x80(%rsp), %r14d
movl %ebx, %r9d
leaq 0x38(%rsp), %rcx
leaq 0x3c(%rsp), %rdx
movq 0xb0(%rsp), %rsi
movl 0x38(%rsp), %eax
movl 0x70(%rsp), %r8d
imull %eax, %r8d
movl 0x3c(%rsp), %edi
addl %edi, %r8d
movslq %r8d, %r8
cmpl %r14d, %edi
cmovll %edi, %r14d
movb %r9b, (%rsi,%r8,4)
cmpl %r15d, %edi
cmovgl %edi, %r15d
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %rbx
movb %r12b, 0x1(%rsi,%r8,4)
cmpl %ebp, %eax
cmovll %eax, %ebp
movb %bl, 0x2(%rsi,%r8,4)
cmpl %r11d, %eax
cmovgl %eax, %r11d
movl 0x50(%rsp), %eax
movb %al, 0x3(%rsi,%r8,4)
movq 0x48(%rsp), %rsi
movl 0x50(%rsp), %r8d
incq %r13
cmpq 0x38(%rdi), %r13
jb 0x12bec
xorps %xmm0, %xmm0
cvtsi2ss %r14d, %xmm0
movb %r9b, 0x40(%rdi)
xorps %xmm1, %xmm1
cvtsi2ss %ebp, %xmm1
movb %r12b, 0x41(%rdi)
movb %bl, 0x42(%rdi)
subl %r14d, %r15d
incl %r15d
xorps %xmm2, %xmm2
cvtsi2ss %r15d, %xmm2
subl %ebp, %r11d
incl %r11d
xorps %xmm3, %xmm3
cvtsi2ss %r11d, %xmm3
movb %r8b, 0x43(%rdi)
movss %xmm0, 0x44(%rdi)
movss %xmm1, 0x48(%rdi)
movss %xmm2, 0x4c(%rdi)
movss %xmm3, 0x50(%rdi)
incq %rbx
movq 0xa8(%rsp), %rbp
addq $0xa0, %rbp
movq 0xa0(%rsp), %r14
cmpq %r14, %rbx
movq 0x98(%rsp), %r15
jne 0x12ab3
addq $0x70, %r15
leaq 0x557ab(%rip), %rbx     # 0x684cf
leaq 0x557ad(%rip), %r12     # 0x684d8
cmpb $0x1, -0x60(%r15)
jne 0x12d37
movq %rbx, %rdi
jmp 0x12d4d
movq -0x50(%r15), %rdi
testq %rdi, %rdi
je 0x12d4a
callq 0x60f0
movq %rax, %rdi
jmp 0x12d4d
movq %r12, %rdi
callq 0x64e0
movq %rax, %r13
movsd -0x2c(%r15), %xmm0
movaps %xmm0, 0x50(%rsp)
movsd -0x24(%r15), %xmm0
movaps %xmm0, 0x80(%rsp)
leaq 0x100(%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 %r13, %rdi
movss 0x5527b(%rip), %xmm0    # 0x68018
callq 0x51b09
movaps 0x80(%rsp), %xmm4
xorps %xmm1, %xmm1
cvtss2sd %xmm4, %xmm1
mulsd 0x554df(%rip), %xmm1    # 0x68298
xorps %xmm2, %xmm2
cvtss2sd %xmm0, %xmm2
divsd %xmm2, %xmm1
xorps %xmm2, %xmm2
cvtsd2ss %xmm1, %xmm2
movaps %xmm0, %xmm1
mulss %xmm2, %xmm1
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
mulss %xmm2, %xmm0
mulss 0x55236(%rip), %xmm2    # 0x68018
movaps %xmm4, %xmm3
movaps %xmm4, %xmm7
subss %xmm1, %xmm3
movss 0x5521c(%rip), %xmm4    # 0x68010
mulss %xmm4, %xmm3
movaps 0x50(%rsp), %xmm5
addss %xmm5, %xmm3
shufps $0x55, %xmm5, %xmm5     # xmm5 = xmm5[1,1,1,1]
movaps %xmm5, %xmm6
movaps %xmm7, %xmm5
shufps $0x55, %xmm7, %xmm5     # xmm5 = xmm5[1,1],xmm7[1,1]
subss %xmm0, %xmm5
mulss %xmm4, %xmm5
addss %xmm6, %xmm5
movq %r13, -0x18(%r15)
movss %xmm2, -0x10(%r15)
movss %xmm3, -0xc(%r15)
movss %xmm5, -0x8(%r15)
movss %xmm1, -0x4(%r15)
movss %xmm0, (%r15)
addq $0xa0, %r15
decq %r14
jne 0x12d2b
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
movq 0x90(%rsp), %rbx
movq %rbx, %rdi
callq 0x4207c
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x3c3b4
movq %rbx, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq