// File: /workspace/github_c_makefile_star2_O3/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)
incl 0x30(%rdi)
movq %rsi, 0x18(%rsp)
cmpq $0x0, 0x8(%rdi)
je 0x15137
movq %rdi, %r14
movq %rdx, %r15
addq $0x6, 0x10(%rsp)
movl $0x1c, %r12d
leaq 0x506e5(%rip), %r13     # 0x6564f
xorl %ebp, %ebp
movq (%r14), %rax
cmpb $0x0, (%rax,%r12)
jne 0x14f90
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 0x14fa2
incq %rbp
movq 0x8(%r14), %rax
addq $0x20, %r12
cmpq %rax, %rbp
jb 0x14f6c
jmp 0x14fb4
movq (%r14), %rax
movb $0x1, (%rax,%r12)
movq %r13, %rdi
callq 0x6730
jmp 0x14f90
testq %rax, %rax
je 0x15137
leaq 0x90c54(%rip), %rbp     # 0xa5c18
xorl %r13d, %r13d
movq (%r14), %r12
movq %r13, %rcx
shlq $0x5, %rcx
cmpb $0x0, 0x1c(%r12,%rcx)
jne 0x150f6
addq %rcx, %r12
movl (%r12), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x6480
cmpl $-0x1, %eax
je 0x15104
movl (%r12), %esi
movl $0xc, %edi
xorl %edx, %edx
leaq 0x30(%rsp), %rcx
xorl %eax, %eax
callq 0x63d0
testq %rax, %rax
jne 0x15115
movl (%r12), %esi
movl $0x7, %edi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x63d0
testq %rax, %rax
jne 0x15126
testq %r15, %r15
je 0x15061
movq 0xb0(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %r15, %rdx
movq %rcx, %rsi
subq (%rax), %rsi
jb 0x15056
cmpq 0x8(%rax), %rcx
jb 0x15065
addq $0xa0, %rax
decq %rdx
jne 0x15048
xorl %esi, %esi
jmp 0x15070
movq 0x30(%rax), %rax
shlq $0xc, %rax
addq %rax, %rsi
movq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x15466
movss 0x8(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
cvtdq2ps %xmm1, %xmm0
mulps 0x50293(%rip), %xmm0    # 0x65330
movlps %xmm0, 0x20(%rsp)
movl 0x30(%r14), %ebx
movl %ebx, 0x28(%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 0x20(%rsp), %rcx
leaq 0x90b3a(%rip), %rax     # 0xa5c20
callq *(%rax)
movl $0x8892, %edi           # imm = 0x8892
xorl %esi, %esi
callq *(%rbp)
movq 0x8(%r14), %rax
incq %r13
cmpq %rax, %r13
jb 0x14fc7
jmp 0x15137
leaq 0x5055d(%rip), %rdi     # 0x65668
callq 0x6730
jmp 0x14ff6
leaq 0x50556(%rip), %rdi     # 0x65672
callq 0x6730
jmp 0x15016
leaq 0x5055c(%rip), %rdi     # 0x65689
callq 0x6730
jmp 0x15033
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq