// File: /workspace/github_c_makefile_star2_O3/arkanis[P]process-map/map.c
// Function: LoadTaskTraces
TaskTraces LoadTaskTraces(char* pid) {
	TaskTraces traces = { 0 };
	traces.focusedTaskIndex = -1;
	
	glEnable(GL_PROGRAM_POINT_SIZE);
	traces.particleShader = LoadShader("particle.vs", "particle.fs");
	int shaderPosLocation = GetShaderLocationAttrib(traces.particleShader, "pos");
	int shaderTimeLocation = GetShaderLocationAttrib(traces.particleShader, "time");
	traces.shaderColorLocation = GetShaderLocation(traces.particleShader, "color");
	traces.shaderCurrentTimeLocation = GetShaderLocation(traces.particleShader, "currentTime");
	
	char* tasksDirPath = NULL;
	asprintf(&tasksDirPath, "/proc/%s/task", pid);
	DIR* tasksDir = opendir(tasksDirPath);
	free(tasksDirPath);
	if (tasksDir == NULL)
		return traces;
	
	TaskSample* emptyParticleBuffer = calloc(particleCount, sizeof(emptyParticleBuffer[0]));
	
	struct dirent *entry;
	while ((entry = readdir(tasksDir)) != NULL) {
		if (entry->d_name[0] == '.')
			continue;
		
		pid_t tid = atoi(entry->d_name);
		
		if ( ptrace(PTRACE_SEIZE, tid, NULL, NULL) != 0 )
			perror("ptrace(PTRACE_SEIZE)");
		
		FILE* taskNameFile = fopen(TextFormat("/proc/%s/task/%d/comm", pid, tid), "rb");
		char* taskName = NULL;
		if (taskNameFile != NULL) {
			fscanf(taskNameFile, "%m[^\n]", &taskName);
			fclose(taskNameFile);
		}
		
		float hue        =       360 * (rand() / (float)RAND_MAX);
		float saturation = 0.5 + 0.5 * (rand() / (float)RAND_MAX);
		float value      = 0.5 + 0.5 * (rand() / (float)RAND_MAX);
		Color color = ColorFromHSV(hue, saturation, value);
		
		GLuint vao = 0, vbo = 0;
		glGenVertexArrays(1, &vao);
		glBindVertexArray(vao);
			glGenBuffers(1, &vbo);
			glBindBuffer(GL_ARRAY_BUFFER, vbo);
				glBufferData(GL_ARRAY_BUFFER, particleCount * sizeof(emptyParticleBuffer[0]), emptyParticleBuffer, GL_DYNAMIC_DRAW);
				glEnableVertexAttribArray(shaderPosLocation);
				glEnableVertexAttribArray(shaderTimeLocation);
				glVertexAttribPointer(shaderPosLocation,  2, GL_FLOAT, GL_FALSE, sizeof(TaskSample), 0);
				glVertexAttribPointer(shaderTimeLocation, 1, GL_UNSIGNED_INT, GL_FALSE, sizeof(TaskSample), (void*)offsetof(TaskSample, t));
			glBindBuffer(GL_ARRAY_BUFFER, 0);
		glBindVertexArray(0);
		
		traces.taskCount++;
		traces.tasks = realloc(traces.tasks, traces.taskCount * sizeof(traces.tasks[0]));
		traces.tasks[traces.taskCount - 1] = (Task){
			.pid = tid,
			.name = taskName,
			.color = color,
			.particleVAO = vao,
			.particleVBO = vbo,
			.dead = false
		};
	}
	closedir(tasksDir);
	free(emptyParticleBuffer);
	
	return traces;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r15
movq %rdi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rdi)
xorl %r12d, %r12d
movq %r12, 0x30(%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, (%rdi)
movq $-0x1, 0x10(%rdi)
leaq 0x9108b(%rip), %rax     # 0xa5bb0
movl $0x8642, %edi           # imm = 0x8642
callq *(%rax)
leaq 0x50abf(%rip), %rdi     # 0x655f2
leaq 0x50ac4(%rip), %rsi     # 0x655fe
callq 0x223d0
movl %eax, 0x18(%rbx)
movq %rdx, 0x20(%rbx)
leaq 0x5bff8(%rip), %rcx     # 0x70b45
movl %eax, %edi
movq %rdx, %rsi
movq %rcx, %rdx
callq 0x22a62
movl %eax, 0x10(%rsp)
movl 0x18(%rbx), %ebp
movq 0x20(%rbx), %r14
leaq 0x7a164(%rip), %rdx     # 0x8ecd0
movl %ebp, %edi
movq %r14, %rsi
callq 0x22a62
movl %eax, 0xc(%rsp)
leaq 0x545e7(%rip), %rdx     # 0x69168
movl %ebp, %edi
movq %r14, %rsi
callq 0x22a0d
movl %eax, 0x28(%rbx)
leaq 0x50a75(%rip), %rdx     # 0x6560a
movl %ebp, %edi
movq %r14, %rsi
callq 0x22a0d
movl %eax, 0x2c(%rbx)
leaq 0x40(%rsp), %r14
movq %r12, (%r14)
leaq 0x50a65(%rip), %rsi     # 0x65616
movq %r14, %rdi
movq %r15, 0x38(%rsp)
movq %r15, %rdx
xorl %eax, %eax
callq 0x6500
movq (%r14), %rdi
callq 0x6320
movq %rax, %r15
movq (%r14), %rdi
callq 0x62e0
testq %r15, %r15
je 0x14e97
movl $0x7d0, %edi            # imm = 0x7D0
movl $0xc, %esi
callq 0x65e0
movq %rax, 0x18(%rsp)
movq (%rbx), %r14
movq %r15, %rdi
callq 0x6330
testq %rax, %rax
je 0x14e82
movq %r15, 0x30(%rsp)
movq %rbx, 0x28(%rsp)
movl 0xc(%rsp), %r12d
cmpb $0x2e, 0x13(%rax)
je 0x14e5e
addq $0x13, %rax
movq %rax, %rdi
callq 0x6410
movl %eax, %r13d
movl $0x4206, %edi           # imm = 0x4206
movl %eax, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x63d0
testq %rax, %rax
jne 0x14e71
leaq 0x509e8(%rip), %rdi     # 0x65639
movq 0x38(%rsp), %rsi
movl %r13d, %edx
xorl %eax, %eax
callq 0x4dfb7
movq %rax, %rdi
leaq 0x5076b(%rip), %rsi     # 0x653d5
callq 0x61d0
movq $0x0, 0x20(%rsp)
testq %rax, %rax
je 0x14c9e
movq %rax, %rbp
movq %rax, %rdi
leaq 0x5093e(%rip), %rsi     # 0x655c8
leaq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x60e0
movq %rbp, %rdi
callq 0x6620
callq 0x6740
cvtsi2ss %eax, %xmm1
movss 0x503dd(%rip), %xmm0    # 0x6508c
mulss %xmm0, %xmm1
mulss 0x50389(%rip), %xmm1    # 0x65044
movss %xmm1, (%rsp)
callq 0x6740
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x503b8(%rip), %xmm0    # 0x6508c
cvtss2sd %xmm0, %xmm0
movsd 0x506c0(%rip), %xmm1    # 0x653a0
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x14(%rsp)
callq 0x6740
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x50386(%rip), %xmm0    # 0x6508c
cvtss2sd %xmm0, %xmm0
movsd 0x5068e(%rip), %xmm1    # 0x653a0
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm2
movss (%rsp), %xmm0
movss 0x14(%rsp), %xmm1
callq 0x412e2
movl %eax, (%rsp)
xorl %eax, %eax
movl %eax, 0x8(%rsp)
movl %eax, 0x4(%rsp)
movl $0x1, %edi
leaq 0x8(%rsp), %rsi
leaq 0x90f9c(%rip), %rax     # 0xa5ce8
callq *(%rax)
movl 0x8(%rsp), %edi
leaq 0x90f97(%rip), %rbx     # 0xa5cf0
callq *(%rbx)
movl $0x1, %edi
leaq 0x4(%rsp), %rsi
leaq 0x90f8c(%rip), %rax     # 0xa5cf8
callq *(%rax)
movl 0x4(%rsp), %esi
movl $0x8892, %edi           # imm = 0x8892
leaq 0x90e9a(%rip), %rbp     # 0xa5c18
callq *(%rbp)
movl $0x5dc0, %esi           # imm = 0x5DC0
movl $0x8892, %edi           # imm = 0x8892
movq 0x18(%rsp), %rdx
movl $0x88e8, %ecx           # imm = 0x88E8
leaq 0x90f64(%rip), %rax     # 0xa5d00
callq *(%rax)
movl 0x10(%rsp), %r15d
movl %r15d, %edi
leaq 0x90f63(%rip), %rbx     # 0xa5d10
callq *(%rbx)
movl %r12d, %edi
callq *(%rbx)
movl %r15d, %edi
movl $0x2, %esi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
movl $0xc, %r8d
xorl %r9d, %r9d
leaq 0x90f35(%rip), %rbx     # 0xa5d08
callq *(%rbx)
movl $0x8, %r9d
movl %r12d, %edi
movl $0x1, %esi
movl $0x1405, %edx           # imm = 0x1405
xorl %ecx, %ecx
movl $0xc, %r8d
callq *(%rbx)
movl $0x8892, %edi           # imm = 0x8892
xorl %esi, %esi
callq *(%rbp)
xorl %edi, %edi
leaq 0x90eeb(%rip), %rax     # 0xa5cf0
callq *(%rax)
movq 0x28(%rsp), %rbx
movq 0x8(%rbx), %r15
leaq 0x1(%r15), %rsi
movq %rsi, 0x8(%rbx)
shlq $0x5, %rsi
movq %r14, %rdi
callq 0x66f0
movq %rax, %r14
shlq $0x5, %r15
movq 0x20(%rsp), %rax
movl 0x8(%rsp), %ecx
movl 0x4(%rsp), %edx
movl %r13d, (%r14,%r15)
movq %rax, 0x8(%r14,%r15)
movl (%rsp), %eax
movl %eax, 0x10(%r14,%r15)
movl %ecx, 0x14(%r14,%r15)
movl %edx, 0x18(%r14,%r15)
movb $0x0, 0x1c(%r14,%r15)
movq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x6330
testq %rax, %rax
jne 0x14c16
jmp 0x14e82
leaq 0x507ac(%rip), %rdi     # 0x65624
callq 0x6730
jmp 0x14c4a
movq %r14, (%rbx)
movq %r15, %rdi
callq 0x6650
movq 0x18(%rsp), %rdi
callq 0x62e0
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq