// File: /workspace/github_c_makefile_star2_O1/arkanis[P]process-map/map.c
// Function: UpdateTaskTraces
void UpdateTaskTraces(TaskTraces* traces, AddrRange ranges[], size_t rangeCount, size_t mapHilbertCurveOrder) {
	traces->currentTime++;
	// We use a hilbert curve order that can map each address to an x/y coordinate with byte accuracy (up from 4k per
	// pixel on the map). Because of that the addressOnMap below is calculated in bytes instead of 4k pages and the
	// calculated coordinates are later divided by 64 (side length of a 4k square).
	size_t byteAccurateHilberCurveOrder = mapHilbertCurveOrder + 6;
	
	for (size_t t = 0; t < traces->taskCount; t++) {
		if (traces->tasks[t].dead)
			continue;
		if ( ptrace(PTRACE_INTERRUPT, traces->tasks[t].pid, NULL, NULL) != 0 ) {
			traces->tasks[t].dead = true;
		  	perror("ptrace(PTRACE_INTERRUPT)");
		}
	}
	for (size_t t = 0; t < traces->taskCount; t++) {
		if (traces->tasks[t].dead)
			continue;
		Task* task = &traces->tasks[t];
		
		struct user_regs_struct regs;
		if ( waitpid(task->pid, NULL, 0) == -1 )                     perror("waitpid()");
		if ( ptrace(PTRACE_GETREGS, task->pid, NULL, &regs) != 0 )   perror("ptrace(PTRACE_GETREGS)");
		if ( ptrace(PTRACE_CONT, task->pid, NULL, NULL) != 0 )       perror("ptrace(PTRACE_CONT)");
		
		uint64_t addressOnMap = 0;
		for (size_t r = 0; r < rangeCount; r++) {
			if (regs.rip >= ranges[r].startAddr && regs.rip < ranges[r].endAddr) {
				addressOnMap = ranges[r].startPage * pageSize + (regs.rip - ranges[r].startAddr);
				break;
			}
		}
		
		int x, y;
		HilberCurveDistToPos(byteAccurateHilberCurveOrder, addressOnMap, &x, &y);
		TaskSample sample = (TaskSample){ .x = x / 64.0f, .y = y / 64.0f, .t = traces->currentTime };
		
		size_t currentParticleIndex = traces->currentTime % particleCount;
		glBindBuffer(GL_ARRAY_BUFFER, task->particleVBO);
			glBufferSubData(GL_ARRAY_BUFFER, currentParticleIndex * sizeof(TaskSample), sizeof(TaskSample), &sample);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rcx, 0x10(%rsp)
movq %rdi, %r14
incl 0x30(%rdi)
movq %rdx, %r15
movq %rsi, 0x28(%rsp)
cmpq $0x0, 0x8(%rdi)
je 0x138e6
movl $0x1c, %r12d
leaq 0x54cb0(%rip), %r13     # 0x6854f
xorl %ebp, %ebp
movq (%r14), %rax
cmpb $0x0, (%rax,%r12)
jne 0x138c5
movl -0x1c(%rax,%r12), %esi
movl $0x4207, %edi           # imm = 0x4207
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x63d0
testq %rax, %rax
jne 0x138d4
incq %rbp
addq $0x20, %r12
cmpq 0x8(%r14), %rbp
jb 0x138a1
jmp 0x138e6
movq (%r14), %rax
movb $0x1, (%rax,%r12)
movq %r13, %rdi
callq 0x6730
jmp 0x138c5
cmpq $0x0, 0x8(%r14)
je 0x13a7e
addq $0x6, 0x10(%rsp)
leaq 0x9531a(%rip), %rbp     # 0xa8c18
xorl %r13d, %r13d
movq (%r14), %r12
movq %r13, %rax
shlq $0x5, %rax
cmpb $0x0, 0x1c(%r12,%rax)
jne 0x13a3c
addq %rax, %r12
movl (%r12), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x6480
cmpl $-0x1, %eax
je 0x13a4b
movl (%r12), %esi
movl $0xc, %edi
xorl %edx, %edx
leaq 0x30(%rsp), %rcx
xorl %eax, %eax
callq 0x63d0
testq %rax, %rax
jne 0x13a5c
movl (%r12), %esi
movl $0x7, %edi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x63d0
testq %rax, %rax
jne 0x13a6d
testq %r15, %r15
je 0x1399b
movq 0xb0(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %r15, %rdx
movq %rcx, %rsi
subq (%rax), %rsi
jb 0x13990
cmpq 0x8(%rax), %rcx
jb 0x1399f
addq $0xa0, %rax
decq %rdx
jne 0x13982
xorl %esi, %esi
jmp 0x139aa
movq 0x30(%rax), %rax
shlq $0xc, %rax
addq %rax, %rsi
movq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x13dde
xorps %xmm0, %xmm0
cvtsi2ssl 0xc(%rsp), %xmm0
movss 0x546c9(%rip), %xmm1    # 0x68098
mulss %xmm1, %xmm0
movss %xmm0, 0x1c(%rsp)
xorps %xmm0, %xmm0
cvtsi2ssl 0x8(%rsp), %xmm0
mulss %xmm1, %xmm0
movss %xmm0, 0x20(%rsp)
movl 0x30(%r14), %ebx
movl %ebx, 0x24(%rsp)
imulq $0x10624dd3, %rbx, %rax # imm = 0x10624DD3
shrq $0x27, %rax
imull $0x7d0, %eax, %eax      # imm = 0x7D0
subl %eax, %ebx
movl 0x18(%r12), %esi
movl $0x8892, %edi           # imm = 0x8892
callq *(%rbp)
shll $0x2, %ebx
leal (%rbx,%rbx,2), %esi
movl $0xc, %edx
movl $0x8892, %edi           # imm = 0x8892
leaq 0x1c(%rsp), %rcx
leaq 0x951f0(%rip), %rax     # 0xa8c20
callq *(%rax)
movl $0x8892, %edi           # imm = 0x8892
xorl %esi, %esi
callq *(%rbp)
incq %r13
cmpq 0x8(%r14), %r13
jb 0x13901
jmp 0x13a7e
leaq 0x54b16(%rip), %rdi     # 0x68568
callq 0x6730
jmp 0x13930
leaq 0x54b0f(%rip), %rdi     # 0x68572
callq 0x6730
jmp 0x13950
leaq 0x54b15(%rip), %rdi     # 0x68589
callq 0x6730
jmp 0x1396d
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq