// File: /workspace/github_c_makefile_star2_O1/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 0x9574c(%rip), %rax     # 0xa8bb0
movl $0x8642, %edi           # imm = 0x8642
callq *(%rax)
leaq 0x55080(%rip), %rdi     # 0x684f2
leaq 0x55085(%rip), %rsi     # 0x684fe
callq 0x2414d
movl %eax, 0x18(%rbx)
movq %rdx, 0x20(%rbx)
leaq 0x60269(%rip), %rcx     # 0x736f5
movl %eax, %edi
movq %rdx, %rsi
movq %rcx, %rdx
callq 0x247e8
movl %eax, 0x10(%rsp)
movl 0x18(%rbx), %ebp
movq 0x20(%rbx), %r14
leaq 0x7e345(%rip), %rdx     # 0x917f0
movl %ebp, %edi
movq %r14, %rsi
callq 0x247e8
movl %eax, 0xc(%rsp)
leaq 0x589d8(%rip), %rdx     # 0x6be98
movl %ebp, %edi
movq %r14, %rsi
callq 0x24793
movl %eax, 0x28(%rbx)
leaq 0x55036(%rip), %rdx     # 0x6850a
movl %ebp, %edi
movq %r14, %rsi
callq 0x24793
movl %eax, 0x2c(%rbx)
leaq 0x40(%rsp), %r14
movq %r12, (%r14)
leaq 0x55026(%rip), %rsi     # 0x68516
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 0x137d6
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 0x137c1
movq %r15, 0x30(%rsp)
movq %rbx, 0x28(%rsp)
movl 0xc(%rsp), %r12d
cmpb $0x2e, 0x13(%rax)
je 0x1379d
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 0x137b0
leaq 0x54fa9(%rip), %rdi     # 0x68539
movq 0x38(%rsp), %rsi
movl %r13d, %edx
xorl %eax, %eax
callq 0x50f37
movq %rax, %rdi
leaq 0x54d2c(%rip), %rsi     # 0x682d5
callq 0x61d0
movq $0x0, 0x20(%rsp)
testq %rax, %rax
je 0x135dd
movq %rax, %rbp
movq %rax, %rdi
leaq 0x54eff(%rip), %rsi     # 0x684c8
leaq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x60e0
movq %rbp, %rdi
callq 0x6620
callq 0x6740
cvtsi2ss %eax, %xmm1
movss 0x54aa6(%rip), %xmm0    # 0x68094
mulss %xmm0, %xmm1
mulss 0x54a56(%rip), %xmm1    # 0x68050
movss %xmm1, (%rsp)
callq 0x6740
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x54a81(%rip), %xmm0    # 0x68094
cvtss2sd %xmm0, %xmm0
movsd 0x54c81(%rip), %xmm1    # 0x682a0
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x14(%rsp)
callq 0x6740
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x54a4f(%rip), %xmm0    # 0x68094
cvtss2sd %xmm0, %xmm0
movsd 0x54c4f(%rip), %xmm1    # 0x682a0
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm2
movss (%rsp), %xmm0
movss 0x14(%rsp), %xmm1
callq 0x43fcd
movl %eax, (%rsp)
xorl %eax, %eax
movl %eax, 0x8(%rsp)
movl %eax, 0x4(%rsp)
movl $0x1, %edi
leaq 0x8(%rsp), %rsi
leaq 0x9565d(%rip), %rax     # 0xa8ce8
callq *(%rax)
movl 0x8(%rsp), %edi
leaq 0x95658(%rip), %rbx     # 0xa8cf0
callq *(%rbx)
movl $0x1, %edi
leaq 0x4(%rsp), %rsi
leaq 0x9564d(%rip), %rax     # 0xa8cf8
callq *(%rax)
movl 0x4(%rsp), %esi
movl $0x8892, %edi           # imm = 0x8892
leaq 0x9555b(%rip), %rbp     # 0xa8c18
callq *(%rbp)
movl $0x5dc0, %esi           # imm = 0x5DC0
movl $0x8892, %edi           # imm = 0x8892
movq 0x18(%rsp), %rdx
movl $0x88e8, %ecx           # imm = 0x88E8
leaq 0x95625(%rip), %rax     # 0xa8d00
callq *(%rax)
movl 0x10(%rsp), %r15d
movl %r15d, %edi
leaq 0x95624(%rip), %rbx     # 0xa8d10
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 0x955f6(%rip), %rbx     # 0xa8d08
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 0x955ac(%rip), %rax     # 0xa8cf0
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 0x13555
jmp 0x137c1
leaq 0x54d6d(%rip), %rdi     # 0x68524
callq 0x6730
jmp 0x13589
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