// File: /workspace/github_c_makefile_star2_O2/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, %r14
movq %rdi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, (%rdi)
andq $0x0, 0x30(%rdi)
orq $-0x1, 0x10(%rdi)
leaq 0x8bc9a(%rip), %rax     # 0x9cbe0
movl $0x8642, %edi           # imm = 0x8642
callq *(%rax)
leaq 0x486a6(%rip), %rdi     # 0x595fa
leaq 0x486ab(%rip), %rsi     # 0x59606
callq 0x1c5c1
movl %eax, 0x18(%rbx)
movq %rdx, 0x20(%rbx)
leaq 0x539b7(%rip), %rcx     # 0x64925
movl %eax, %edi
movq %rdx, %rsi
movq %rcx, %rdx
callq 0x1cbce
movl %eax, 0x1c(%rsp)
movl 0x18(%rbx), %ebp
movq 0x20(%rbx), %r15
leaq 0x71a73(%rip), %rdx     # 0x82a00
movl %ebp, %edi
movq %r15, %rsi
callq 0x1cbce
movl %eax, 0x18(%rsp)
leaq 0x4c0b6(%rip), %rdx     # 0x5d058
movl %ebp, %edi
movq %r15, %rsi
callq 0x1cb7d
movl %eax, 0x28(%rbx)
leaq 0x4865c(%rip), %rdx     # 0x59612
movl %ebp, %edi
movq %r15, %rsi
callq 0x1cb7d
movl %eax, 0x2c(%rbx)
leaq 0x40(%rsp), %r15
andq $0x0, (%r15)
leaq 0x4864b(%rip), %rsi     # 0x5961e
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x6510
movq (%r15), %rdi
callq 0x6330
movq %rax, %r12
movq (%r15), %rdi
callq 0x62f0
testq %r12, %r12
je 0x112a8
pushq $0xc
popq %rsi
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x65f0
movq %rax, 0x28(%rsp)
movq (%rbx), %rbp
movq 0x8(%rbx), %r13
leaq 0x48625(%rip), %r15     # 0x59641
movq %r12, %rdi
callq 0x6340
testq %rax, %rax
je 0x1128f
cmpb $0x2e, 0x13(%rax)
je 0x1101c
movq %r14, 0x20(%rsp)
addq $0x13, %rax
movq %rax, %rdi
callq 0x6420
movl %eax, %r14d
movl $0x4206, %edi           # imm = 0x4206
movl %eax, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x63e0
testq %rax, %rax
jne 0x1127e
movq %rbp, 0x38(%rsp)
movq %r15, %rdi
movq 0x20(%rsp), %rsi
movl %r14d, 0x14(%rsp)
movl %r14d, %edx
xorl %eax, %eax
callq 0x43d0d
movq %rax, %rdi
leaq 0x48355(%rip), %rsi     # 0x593dd
callq 0x61d0
andq $0x0, 0x30(%rsp)
testq %rax, %rax
je 0x110b9
movq %rax, %r15
movq %rax, %rdi
leaq 0x4852b(%rip), %rsi     # 0x595d0
leaq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x60e0
movq %r15, %rdi
callq 0x6630
callq 0x6750
cvtsi2ss %eax, %xmm1
movss 0x47fc2(%rip), %xmm0    # 0x5908c
mulss %xmm0, %xmm1
mulss 0x47f6e(%rip), %xmm1    # 0x59044
movss %xmm1, 0x4(%rsp)
callq 0x6750
cvtsi2ss %eax, %xmm0
mulss 0x47f9f(%rip), %xmm0    # 0x5908c
cvtss2sd %xmm0, %xmm0
movsd 0x4829f(%rip), %xmm1    # 0x59398
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x10(%rsp)
callq 0x6750
cvtsi2ss %eax, %xmm0
mulss 0x47f70(%rip), %xmm0    # 0x5908c
cvtss2sd %xmm0, %xmm0
movsd 0x48270(%rip), %xmm1    # 0x59398
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm2
movss 0x4(%rsp), %xmm0
movss 0x10(%rsp), %xmm1
callq 0x38295
andl $0x0, 0xc(%rsp)
movl %eax, 0x4(%rsp)
andl $0x0, 0x8(%rsp)
pushq $0x1
popq %rbp
movl %ebp, %edi
leaq 0xc(%rsp), %rsi
leaq 0x8bbb4(%rip), %rax     # 0x9cd18
callq *(%rax)
movl 0xc(%rsp), %edi
leaq 0x8bbaf(%rip), %rax     # 0x9cd20
callq *(%rax)
movl %ebp, %edi
leaq 0x8(%rsp), %rsi
leaq 0x8bba7(%rip), %rax     # 0x9cd28
callq *(%rax)
movl 0x8(%rsp), %esi
movl $0x8892, %edi           # imm = 0x8892
leaq 0x8bab5(%rip), %rax     # 0x9cc48
callq *(%rax)
movl $0x5dc0, %esi           # imm = 0x5DC0
movl $0x8892, %edi           # imm = 0x8892
movq 0x28(%rsp), %rdx
movl $0x88e8, %ecx           # imm = 0x88E8
leaq 0x8bb80(%rip), %rax     # 0x9cd30
callq *(%rax)
movl 0x1c(%rsp), %r14d
movl %r14d, %edi
leaq 0x8bb7f(%rip), %rbp     # 0x9cd40
callq *(%rbp)
movl 0x18(%rsp), %r15d
movl %r15d, %edi
callq *(%rbp)
movl %r14d, %edi
pushq $0x2
popq %rsi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
pushq $0xc
popq %rbp
movl %ebp, %r8d
xorl %r9d, %r9d
leaq 0x8bb4c(%rip), %r14     # 0x9cd38
callq *(%r14)
movl %r15d, %edi
pushq $0x1
popq %rsi
movl $0x1405, %edx           # imm = 0x1405
xorl %ecx, %ecx
movl %ebp, %r8d
pushq $0x8
popq %r9
callq *(%r14)
movl $0x8892, %edi           # imm = 0x8892
xorl %esi, %esi
leaq 0x8ba34(%rip), %rax     # 0x9cc48
callq *(%rax)
xorl %edi, %edi
leaq 0x8bb01(%rip), %rax     # 0x9cd20
callq *(%rax)
leaq 0x1(%r13), %r14
movq %r14, %rsi
shlq $0x5, %rsi
movq 0x38(%rsp), %rdi
callq 0x6700
movq %rax, %rbp
shlq $0x5, %r13
movq 0x30(%rsp), %rax
movl 0xc(%rsp), %ecx
movl 0x8(%rsp), %edx
movl 0x14(%rsp), %esi
movl %esi, (%rbp,%r13)
movq %rax, 0x8(%rbp,%r13)
movl 0x4(%rsp), %eax
movl %eax, 0x10(%rbp,%r13)
movl %ecx, 0x14(%rbp,%r13)
movl %edx, 0x18(%rbp,%r13)
movb $0x0, 0x1c(%rbp,%r13)
movq %r14, %r13
movq 0x20(%rsp), %r14
jmp 0x11015
leaq 0x483a7(%rip), %rdi     # 0x5962c
callq 0x6740
jmp 0x11062
movq %r13, 0x8(%rbx)
movq %rbp, (%rbx)
movq %r12, %rdi
callq 0x6660
movq 0x28(%rsp), %rdi
callq 0x62f0
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq