// File: /workspace/github_c_makefile_star2_O1/LPD-EPFL[P]mctop/src/mctop_topology.c
// Function: mctop_fix_horizontal_links
void
mctop_fix_horizontal_links(mctop_t* topo)
{
  darray_t* smt_hwcs = darray_create();
  hw_context_t* hwc_prev_socket = NULL;
  for (int s = 0; s < topo->n_sockets; s++)
    {
      socket_t* socket = topo->sockets + s;
      hw_context_t* hwc_cur = socket->hwcs[0];
      if (hwc_prev_socket != NULL)
	{
	  hwc_prev_socket->next = hwc_cur;
	}
      int r;
      for (r = 1;  r < socket->n_hwcs; r++)
	{
	  if (mctop_hwcs_are_same_core(hwc_cur, socket->hwcs[r]))
	    {
	      darray_add(smt_hwcs, (uintptr_t) socket->hwcs[r]);
	    }
	  else
	    {
	      hwc_cur->next = socket->hwcs[r];
	      hwc_cur = hwc_cur->next;
	    }
	}

      DARRAY_FOR_EACH(smt_hwcs, i)
	{
	  hw_context_t* hwc = (hw_context_t*) DARRAY_GET_N(smt_hwcs, i);
	  hwc_cur->next = hwc;
	  hwc_cur = hwc_cur->next;
	}
      hwc_prev_socket = hwc_cur;

      darray_empty(smt_hwcs);
    }

  darray_t* hwgs = smt_hwcs;	// just a synonym
  hw_context_t* hwc_cur = topo->hwcs;
  while (hwc_cur != NULL)
    {
      darray_add_uniq(hwgs, (uintptr_t) hwc_cur->parent);
      hwc_cur = hwc_cur->next;
    }

  int do_next_lvl = 0;

  do
    {
      DARRAY_FOR_EACH_FROM(hwgs, i, 1)
	{
	  hwc_gs_t* gsp = (hwc_gs_t*) DARRAY_GET_N(hwgs, i - 1);
	  hwc_gs_t* gsc = (hwc_gs_t*) DARRAY_GET_N(hwgs, i);
	  gsp->next = gsc;
	}
      hwc_gs_t* first = (hwc_gs_t*) darray_get_elem_n(hwgs, 0);
  
      do_next_lvl = 0;
      if (first->parent != NULL)
	{
	  do_next_lvl = 1;
	  darray_empty(hwgs);
	  while (first != NULL)
	    {
	      darray_add_uniq(hwgs, (uintptr_t) first->parent);
	      first = first->next;
	    }
	}
    }
  while (do_next_lvl);

  /* siblings */
  darray_t* siblings_all = darray_create();
  darray_t* siblings = smt_hwcs; // just a synonym
  for (int l = topo->socket_level + 1; l < topo->n_levels; l++)
    {
      darray_empty(siblings);
      for (int s = 0; s < topo->n_sockets; s++)
	{
	  socket_t* socket = topo->sockets + s;
	  for (int i = 0; i < socket->n_siblings; i++)
	    {
	      sibling_t* sibling = socket->siblings[i];
	      if (sibling->level == l)
		{
		  darray_add_uniq(siblings_all, (uintptr_t) sibling);
		  darray_add_uniq(siblings, (uintptr_t) sibling);
		}
	    }
	}
      DARRAY_FOR_EACH_FROM(siblings, i, 1)
	{
	  sibling_t* left = (sibling_t*) DARRAY_GET_N(siblings, i - 1);
	  sibling_t* right = (sibling_t*) DARRAY_GET_N(siblings, i);
	  left->next = right;
	}
    }

  topo->n_siblings = darray_get_num_elems(siblings_all);
  topo->siblings = malloc_assert(topo->n_siblings * sizeof(sibling_t*));
  DARRAY_FOR_EACH(siblings_all, i)
    {
      sibling_t* sibling =  (sibling_t*) DARRAY_GET_N(siblings_all, i);
      topo->siblings[i] = sibling;
    }

  darray_free(smt_hwcs);
  darray_free(siblings_all);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
xorl %eax, %eax
callq 0x66e4
movq %rax, %r13
cmpl $0x0, 0x18(%r14)
movq %rax, 0x8(%rsp)
je 0x780f
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x20(%r14), %r13
imulq $0xa8, %r12, %rax
movq 0x30(%r13,%rax), %rcx
movq (%rcx), %r15
testq %rbp, %rbp
je 0x7781
movq %r15, 0x20(%rbp)
addq %rax, %r13
cmpl $0x2, 0x28(%r13)
jb 0x77cb
movl $0x1, %ebp
movq 0x30(%r13), %rax
movq (%rax,%rbp,8), %rsi
movq %r15, %rdi
callq 0x8f56
movq 0x30(%r13), %rcx
movq (%rcx,%rbp,8), %rsi
testl %eax, %eax
je 0x77b8
movq 0x8(%rsp), %rdi
callq 0x67b9
jmp 0x77bf
movq %rsi, 0x20(%r15)
movq %rsi, %r15
incq %rbp
movl 0x28(%r13), %eax
cmpq %rax, %rbp
jb 0x7790
movq 0x8(%rsp), %r13
movq (%r13), %rax
testq %rax, %rax
je 0x77f4
movq 0x10(%r13), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rbp
movq %rbp, 0x20(%r15)
incq %rdx
movq %rbp, %r15
cmpq %rdx, %rax
jne 0x77df
jmp 0x77f7
movq %r15, %rbp
movq %r13, %rdi
callq 0x67b1
incq %r12
movl 0x18(%r14), %eax
cmpq %rax, %r12
jb 0x7765
movq 0x40(%r14), %rbx
testq %rbx, %rbx
je 0x784c
movq 0x18(%rbx), %rsi
movq %r13, %rdi
callq 0x27c3
movq 0x20(%rbx), %rbx
jmp 0x7813
movq %rax, %r15
movq %r13, %rdi
callq 0x67b1
testq %r15, %r15
je 0x784c
movq 0x18(%r15), %rsi
movq %r13, %rdi
callq 0x27c3
movq 0x20(%r15), %r15
jmp 0x7835
movq (%r13), %rax
cmpq $0x2, %rax
jb 0x7874
movq 0x10(%r13), %rcx
movl $0x1, %edx
movq -0x8(%rcx,%rdx,8), %rsi
movq (%rcx,%rdx,8), %rdi
movq %rdi, 0x20(%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x785f
movq %r13, %rdi
xorl %esi, %esi
callq 0x6a03
cmpq $0x0, 0x18(%rax)
jne 0x782a
xorl %eax, %eax
callq 0x66e4
movq %rax, %r15
movl 0x14(%r14), %ebp
incl %ebp
cmpl (%r14), %ebp
jae 0x7948
movq %r14, 0x10(%rsp)
movq %r13, %rdi
callq 0x67b1
cmpl $0x0, 0x18(%r14)
je 0x7910
xorl %r13d, %r13d
movq 0x20(%r14), %rbx
imulq $0xa8, %r13, %rax
cmpl $0x0, 0x50(%rbx,%rax)
je 0x78ff
addq %rax, %rbx
xorl %r14d, %r14d
movq 0x58(%rbx), %rax
movq (%rax,%r14,8), %r12
cmpl %ebp, 0x4(%r12)
jne 0x78f4
movq %r15, %rdi
movq %r12, %rsi
callq 0x27c3
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x27c3
incq %r14
movl 0x50(%rbx), %eax
cmpq %rax, %r14
jb 0x78cd
incq %r13
movq 0x10(%rsp), %r14
movl 0x18(%r14), %eax
cmpq %rax, %r13
jb 0x78b5
movq 0x8(%rsp), %r13
movq (%r13), %rax
cmpq $0x2, %rax
jb 0x793d
movq 0x10(%r13), %rcx
movl $0x1, %edx
movq -0x8(%rcx,%rdx,8), %rsi
movq (%rcx,%rdx,8), %rdi
movq %rdi, 0x20(%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x7928
incl %ebp
cmpl (%r14), %ebp
jb 0x78a3
movq %r15, %rdi
callq 0x69ff
movl %eax, 0x48(%r14)
movl %eax, %edi
shlq $0x3, %rdi
callq 0x21d0
testq %rax, %rax
je 0x79a8
movq %rax, 0x50(%r14)
movq (%r15), %rax
testq %rax, %rax
je 0x798a
xorl %ecx, %ecx
movq 0x10(%r15), %rdx
movq (%rdx,%rcx,8), %rdx
movq 0x50(%r14), %rsi
movq %rdx, (%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x7972
movq %r13, %rdi
callq 0x679b
movq %r15, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x679b
leaq 0x5999(%rip), %rdi      # 0xd348
leaq 0x4149(%rip), %rsi      # 0xbaff
leaq 0x415b(%rip), %rcx      # 0xbb18
movl $0x59, %edx
callq 0x2110