// File: /workspace/github_c_makefile_star2_O3/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, %rbp
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r14
testq %rdx, %rdx
je 0x14068
leaq 0x10(%r15), %rax
xorl %ebx, %ebx
movq %r12, %rcx
xorl %edx, %edx
movq -0x8(%rax), %rdi
subq -0x10(%rax), %rdi
movq %rdi, %rsi
shrq $0xc, %rsi
cmpb $0x1, (%rax)
jne 0x14031
cmpq $0x1001000, %rdi        # imm = 0x1001000
jb 0x14031
addq $0x1000, %rbx           # imm = 0x1000
addq %rsi, %rdx
jmp 0x14034
addq %rsi, %rbx
addq $0xa0, %rax
decq %rcx
jne 0x14008
movq %rdx, %xmm0
punpckldq 0x51224(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x5122c(%rip), %xmm0    # 0x65280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x50(%rsp)
jmp 0x14074
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
xorl %ebx, %ebx
movq %rbx, %xmm0
punpckldq 0x511ef(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x511f7(%rip), %xmm0    # 0x65280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
callq 0x6660
callq 0x60a0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x512ce(%rip), %xmm0    # 0x65388
cvttsd2si %xmm0, %rdx
andq %rcx, %rdx
orq %rax, %rdx
movq %rdx, (%rbp)
movq %rdx, %xmm1
punpckldq 0x5119a(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x511a2(%rip), %xmm1    # 0x65280
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0x6430
movsd 0x51291(%rip), %xmm4    # 0x65388
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd %xmm4, %xmm0
cvttsd2si %xmm0, %r13
sarq $0x3f, %rcx
andq %rcx, %r13
orq %rax, %r13
testq %r12, %r12
je 0x1457d
movq %r14, 0x90(%rsp)
movq %r13, %rax
imulq %r13, %rax
movq %rax, %rcx
subq %rbx, %rcx
movq %rcx, %xmm1
punpckldq 0x51133(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x5113b(%rip), %xmm1    # 0x65280
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq %r15, 0x40(%rsp)
leaq 0x38(%r15), %rcx
xorl %edx, %edx
movsd 0x5122c(%rip), %xmm1    # 0x65390
movq %r12, %rsi
movapd 0x50(%rsp), %xmm3
movq -0x30(%rcx), %r8
subq -0x38(%rcx), %r8
movq %rdx, -0x8(%rcx)
cmpq $0x1, %rsi
jne 0x14187
movq %rax, (%rcx)
movq %rax, %rdx
jmp 0x141eb
movq %r8, %rdi
shrq $0xc, %rdi
cmpq $0x1001000, %r8         # imm = 0x1001000
setae %r8b
andb -0x28(%rcx), %r8b
cmpb $0x1, %r8b
jne 0x141e5
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, %rdx
addq $0x800, %rdx            # imm = 0x800
andq $-0x1000, %rdx          # imm = 0xF000
jmp 0x141e8
addq %rdi, %rdx
movq %rdx, (%rcx)
addq $0xa0, %rcx
decq %rsi
jne 0x1416d
movq %rbp, 0x60(%rsp)
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
movl %r13d, %esi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x39c39
xorl %r14d, %r14d
movq (%rbx), %r15
movd %r13d, %xmm0
pshufd $0x50, %xmm0, %xmm0     # xmm0 = xmm0[0,0,1,1]
movdqa %xmm0, 0xb0(%rsp)
leaq 0x3c(%rsp), %rcx
movq 0x40(%rsp), %rdi
movq %r12, 0x98(%rsp)
leaq (%r14,%r14,4), %rax
shlq $0x5, %rax
movq 0x40(%rsp), %rdx
leaq (%rdx,%rax), %r13
cmpb $0x1, 0x10(%rdx,%rax)
movq %rdi, 0xa0(%rsp)
jne 0x14284
movl $0x811c9dc5, %r11d      # imm = 0x811C9DC5
xorl %eax, %eax
movsbl (%rdi,%rax), %edx
xorl %r11d, %edx
imull $0x1000193, %edx, %r11d # imm = 0x1000193
incq %rax
cmpq $0x10, %rax
jne 0x14268
movb $0x1, %dil
jmp 0x142ec
movq 0x20(%r13), %rbx
testq %rbx, %rbx
je 0x142bd
movq %rbx, %rdi
callq 0x62f0
movb $0x2, %dil
testq %rax, %rax
jle 0x142e1
addq %rbx, %rax
movl $0x811c9dc5, %r11d      # imm = 0x811C9DC5
movsbl (%rbx), %edx
xorl %r11d, %edx
imull $0x1000193, %edx, %r11d # imm = 0x1000193
incq %rbx
cmpq %rax, %rbx
jb 0x142a6
jmp 0x142e7
movl $0x811c9dc5, %r11d      # imm = 0x811C9DC5
xorl %eax, %eax
movsbl (%rdi,%rax), %edx
xorl %r11d, %edx
imull $0x1000193, %edx, %r11d # imm = 0x1000193
incq %rax
cmpq $0x10, %rax
jne 0x142c5
movb $0x3, %dil
jmp 0x142ec
movl $0x811c9dc5, %r11d      # imm = 0x811C9DC5
leaq 0x3c(%rsp), %rcx
movq 0x18(%r13), %rax
testq %rax, %rax
je 0x14318
movb $0x4, %bpl
cmpb $0x78, 0x2(%rax)
je 0x1431a
movb $0x5, %bpl
cmpb $0x77, 0x1(%rax)
je 0x1431a
cmpb $0x72, (%rax)
movzbl %dil, %ebp
movl $0x6, %eax
cmovel %eax, %ebp
jmp 0x1431a
movl %edi, %ebp
movl %r11d, %r10d
shrl $0x8, %r10d
movq 0x30(%r13), %rbx
pxor %xmm0, %xmm0
movdqa 0xb0(%rsp), %xmm2
cmpq 0x38(%r13), %rbx
movq 0x60(%rsp), %rax
jae 0x14426
xorpd %xmm1, %xmm1
movaps 0xb0(%rsp), %xmm3
movaps %xmm3, %xmm2
movq %r14, 0xa8(%rsp)
movaps %xmm3, 0xc0(%rsp)
movapd %xmm1, 0x80(%rsp)
movaps %xmm2, 0x50(%rsp)
movq (%rax), %rdi
movq %rbx, %rsi
leaq 0x4c(%rsp), %rdx
movq %r15, %r12
movl %r10d, %r15d
movl %r11d, %r14d
callq 0x15466
movdqa 0x50(%rsp), %xmm2
movl %r14d, %r11d
movl %r15d, %r10d
movq %r12, %r15
leaq 0x3c(%rsp), %rcx
movq 0xa8(%rsp), %r14
movl 0x3c(%rsp), %eax
movl 0x70(%rsp), %edi
imull %eax, %edi
movl 0x4c(%rsp), %edx
addl %edx, %edi
movslq %edi, %rdi
movb %r11b, (%r12,%rdi,4)
movb %r10b, 0x1(%r12,%rdi,4)
movb %r14b, 0x2(%r12,%rdi,4)
movb %bpl, 0x3(%r12,%rdi,4)
movd %eax, %xmm0
movq 0x60(%rsp), %rax
movd %edx, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
pcmpgtd %xmm1, %xmm2
movdqa %xmm1, %xmm0
pand %xmm2, %xmm0
pandn 0xc0(%rsp), %xmm2
por %xmm0, %xmm2
movdqa %xmm1, %xmm0
movdqa 0x80(%rsp), %xmm3
pcmpgtd %xmm3, %xmm0
pand %xmm0, %xmm1
pandn %xmm3, %xmm0
por %xmm1, %xmm0
incq %rbx
movdqa %xmm0, %xmm1
movdqa %xmm2, %xmm3
cmpq 0x38(%r13), %rbx
jb 0x14358
movb %r11b, 0x40(%r13)
movb %r10b, 0x41(%r13)
movb %r14b, 0x42(%r13)
movb %bpl, 0x43(%r13)
psubd %xmm2, %xmm0
psubd 0x50ebe(%rip), %xmm0    # 0x65300
punpcklqdq %xmm0, %xmm2    # xmm2 = xmm2[0],xmm0[0]
cvtdq2ps %xmm2, %xmm0
movups %xmm0, 0x44(%r13)
incq %r14
movq 0xa0(%rsp), %rdi
addq $0xa0, %rdi
movq 0x98(%rsp), %rbp
cmpq %rbp, %r14
jne 0x14240
movq 0x40(%rsp), %r15
addq $0x64, %r15
leaq 0x5114e(%rip), %r12     # 0x655cf
leaq 0x51150(%rip), %r13     # 0x655d8
movq 0x90(%rsp), %r14
cmpb $0x1, -0x54(%r15)
jne 0x1449c
movq %r12, %rdi
jmp 0x144b2
movq -0x44(%r15), %rdi
testq %rdi, %rdi
je 0x144af
callq 0x60f0
movq %rax, %rdi
jmp 0x144b2
movq %r13, %rdi
callq 0x64e0
movq %rax, %rbx
movsd -0x20(%r15), %xmm0
movaps %xmm0, 0x50(%rsp)
movsd -0x18(%r15), %xmm0
movaps %xmm0, 0x80(%rsp)
leaq 0x110(%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 %rbx, %rdi
movss 0x50b0e(%rip), %xmm0    # 0x65010
callq 0x4eae9
movaps 0x80(%rsp), %xmm4
xorps %xmm1, %xmm1
cvtss2sd %xmm4, %xmm1
mulsd 0x50e7a(%rip), %xmm1    # 0x65398
xorps %xmm2, %xmm2
cvtss2sd %xmm0, %xmm2
divsd %xmm2, %xmm1
cvtsd2ss %xmm1, %xmm1
movaps %xmm1, %xmm2
mulss 0x50ad8(%rip), %xmm2    # 0x65010
shufps $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
unpcklpd %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
mulps %xmm1, %xmm0
subps %xmm0, %xmm4
movaps %xmm4, %xmm0
mulps 0x50c00(%rip), %xmm0    # 0x65150
addps 0x50(%rsp), %xmm0
movq %rbx, -0xc(%r15)
movss %xmm2, -0x4(%r15)
mulps %xmm1, %xmm3
movsd %xmm0, %xmm3            # xmm3 = xmm0[0],xmm3[1]
movupd %xmm3, (%r15)
addq $0xa0, %r15
decq %rbp
jne 0x14490
jmp 0x1458f
leaq 0x68(%rsp), %rdi
movl %r13d, %esi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x39c39
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x3f17c
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x39568
movq %r14, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq