// File: /workspace/github_c_makefile_star2_O2/LPD-EPFL[P]mctop/src/mctop_topology.c
// Function: mctop_fix_children_links
void
mctop_fix_children_links(mctop_t* topo)
{
  for (int s = 0; s < topo->n_sockets; s++)
    {
      socket_t* socket = topo->sockets + s;
      darray_t* children = darray_create();
      darray_t* parents = darray_create();
      darray_t* contents = darray_create();
      for (int h = 0; h < socket->n_hwcs; h++)
      	{
      	  hw_context_t* hwc = socket->hwcs[h];
      	  darray_add_uniq(children, (uintptr_t) hwc);
      	}

      for (int lvl = 0; lvl < topo->socket_level; lvl++)
	{
	  /* get parents */
	  DARRAY_FOR_EACH(children, i)
	    {
	      hwc_gs_t* gs = (hwc_gs_t*) DARRAY_GET_N(children, i);
	      gs->socket = socket;
	      darray_add_uniq(parents, (uintptr_t) gs->parent);
	    }
	  DARRAY_FOR_EACH(parents, p)
	    {
	      hwc_gs_t* gsp = (hwc_gs_t*) DARRAY_GET_N(parents, p);
	      DARRAY_FOR_EACH(children, c)
		{
		  hwc_gs_t* gsc = (hwc_gs_t*) DARRAY_GET_N(children, c);
		  gsc->socket = socket;
		  if (gsc->parent == gsp)
		    {
		      darray_add(contents, (uintptr_t) gsc);
		    }
		}

	      gsp->n_children = darray_get_num_elems(contents);
	      gsp->children = malloc_assert(gsp->n_children * sizeof(hwc_gs_t*));
	      DARRAY_FOR_EACH(contents, c)
		{
		  gsp->children[c] = (hwc_gs_t*) DARRAY_GET_N(contents, c);
		}

	      if (gsp->children[0]->type == CORE) /* set n_cores for socket */
		{
		  socket->n_cores = gsp->n_children;
		}

	      darray_empty(contents);
	    }

	  darray_copy(children, parents);
	  darray_empty(parents);
	}

      darray_free(children);
      darray_free(parents);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
xorl %ecx, %ecx
movq %rdi, 0x18(%rsp)
movl 0x18(%rbx), %eax
cmpq %rax, %rcx
jae 0x6cae
movq %rcx, 0x10(%rsp)
imulq $0xa8, %rcx, %rbp
addq 0x20(%rbx), %rbp
xorl %eax, %eax
callq 0x5e6c
movq %rax, %r14
xorl %eax, %eax
callq 0x5e6c
movq %rax, %r15
xorl %eax, %eax
callq 0x5e6c
movq %rax, %r12
xorl %ebx, %ebx
movl 0x28(%rbp), %eax
cmpq %rax, %rbx
jae 0x6b97
movq 0x30(%rbp), %rax
movq (%rax,%rbx,8), %rsi
movq %r14, %rdi
callq 0x5ff2
incq %rbx
jmp 0x6b7a
xorl %eax, %eax
movq %r15, 0x20(%rsp)
movq 0x18(%rsp), %rbx
cmpl 0x14(%rbx), %eax
jae 0x6c91
movl %eax, 0xc(%rsp)
xorl %ebx, %ebx
cmpq %rbx, (%r14)
jbe 0x6bd4
movq 0x10(%r14), %rax
movq (%rax,%rbx,8), %rax
movq %rbp, 0x10(%rax)
movq 0x18(%rax), %rsi
movq %r15, %rdi
callq 0x5ff2
incq %rbx
jmp 0x6bb2
xorl %ebx, %ebx
cmpq %rbx, (%r15)
jbe 0x6c73
movq 0x10(%r15), %rax
movq (%rax,%rbx,8), %r13
xorl %r15d, %r15d
cmpq %r15, (%r14)
jbe 0x6c0e
movq 0x10(%r14), %rax
movq (%rax,%r15,8), %rsi
movq %rbp, 0x10(%rsi)
cmpq %r13, 0x18(%rsi)
jne 0x6c09
movq %r12, %rdi
callq 0x5f31
incq %r15
jmp 0x6bea
movq %r12, %rdi
callq 0x619d
movl %eax, 0x3c(%r13)
movl %eax, %edi
shlq $0x3, %rdi
callq 0x6856
movq %rax, 0x40(%r13)
movq (%r12), %rax
xorl %ecx, %ecx
movq 0x20(%rsp), %r15
cmpq %rcx, %rax
je 0x6c4f
movq 0x10(%r12), %rdx
movq (%rdx,%rcx,8), %rdx
movq 0x40(%r13), %rsi
movq %rdx, (%rsi,%rcx,8)
incq %rcx
jmp 0x6c34
movq 0x40(%r13), %rax
movq (%rax), %rax
cmpl $0x1, 0x8(%rax)
jne 0x6c63
movl 0x3c(%r13), %eax
movl %eax, 0x38(%rbp)
movq %r12, %rdi
callq 0x5f2c
incq %rbx
jmp 0x6bd6
movq %r14, %rdi
movq %r15, %rsi
callq 0x5ee4
movq %r15, %rdi
callq 0x5f2c
movl 0xc(%rsp), %eax
incl %eax
jmp 0x6b9e
movq %r14, %rdi
callq 0x5f16
movq %r15, %rdi
callq 0x5f16
movq 0x10(%rsp), %rcx
incq %rcx
jmp 0x6b3e
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq