// File: /workspace/github_c_makefile_star2_O2/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, %r13
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x5e6c
movq %rax, %r14
pushq $0x1
popq %rbx
xorl %eax, %eax
movq %r13, 0x8(%rsp)
movl 0x18(%r13), %ecx
cmpq %rcx, %r12
jae 0x6d84
movq 0x20(%r13), %rbp
imulq $0xa8, %r12, %rcx
movq 0x30(%rbp,%rcx), %rdx
movq (%rdx), %r15
testq %rax, %rax
je 0x6d0e
movq %r15, 0x20(%rax)
addq %rcx, %rbp
movq %rbx, %r13
movl 0x28(%rbp), %eax
cmpq %rax, %r13
jae 0x6d4e
movq 0x30(%rbp), %rax
movq (%rax,%r13,8), %rsi
movq %r15, %rdi
callq 0x8219
movq 0x30(%rbp), %rcx
movq (%rcx,%r13,8), %rsi
testl %eax, %eax
je 0x6d42
movq %r14, %rdi
callq 0x5f31
jmp 0x6d49
movq %rsi, 0x20(%r15)
movq %rsi, %r15
incq %r13
jmp 0x6d14
movq (%r14), %rax
xorl %ecx, %ecx
movq 0x8(%rsp), %r13
cmpq %rcx, %rax
je 0x6d71
movq 0x10(%r14), %rdx
movq (%rdx,%rcx,8), %rdx
movq %rdx, 0x20(%r15)
incq %rcx
movq %rdx, %r15
jmp 0x6d58
movq %r14, %rdi
callq 0x5f2c
incq %r12
movq %r15, %rax
jmp 0x6ce5
leaq 0x40(%r13), %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x6da2
movq 0x18(%rbx), %rsi
movq %r14, %rdi
callq 0x5ff2
addq $0x20, %rbx
jmp 0x6d88
pushq $0x1
popq %r12
movq (%r14), %rax
movq %r12, %rcx
cmpq %rcx, %rax
jbe 0x6dc7
movq 0x10(%r14), %rdx
movq -0x8(%rdx,%rcx,8), %rsi
movq (%rdx,%rcx,8), %rdx
movq %rdx, 0x20(%rsi)
incq %rcx
jmp 0x6dac
movq %r14, %rdi
xorl %esi, %esi
callq 0x61a1
cmpq $0x0, 0x18(%rax)
je 0x6dfa
movq %rax, %r15
movq %r14, %rdi
callq 0x5f2c
testq %r15, %r15
je 0x6da6
movq 0x18(%r15), %rsi
movq %r14, %rdi
callq 0x5ff2
movq 0x20(%r15), %r15
jmp 0x6de3
xorl %eax, %eax
callq 0x5e6c
movq %rax, %r15
movl 0x14(%r13), %ebp
incl %ebp
cmpl (%r13), %ebp
jae 0x6e9c
movq %r14, %rdi
callq 0x5f2c
xorl %ecx, %ecx
movl 0x18(%r13), %eax
cmpq %rax, %rcx
jae 0x6e7b
movq %rcx, 0x10(%rsp)
imulq $0xa8, %rcx, %rbx
addq 0x20(%r13), %rbx
xorl %r13d, %r13d
movl 0x50(%rbx), %eax
cmpq %rax, %r13
jae 0x6e6c
movq 0x58(%rbx), %rax
movq (%rax,%r13,8), %r12
cmpl %ebp, 0x4(%r12)
jne 0x6e67
movq %r15, %rdi
movq %r12, %rsi
callq 0x5ff2
movq %r14, %rdi
movq %r12, %rsi
callq 0x5ff2
incq %r13
jmp 0x6e3a
movq 0x10(%rsp), %rcx
incq %rcx
movq 0x8(%rsp), %r13
jmp 0x6e1e
movq (%r14), %rax
pushq $0x1
popq %rcx
cmpq %rcx, %rax
jbe 0x6e08
movq 0x10(%r14), %rdx
movq -0x8(%rdx,%rcx,8), %rsi
movq (%rdx,%rcx,8), %rdx
movq %rdx, 0x20(%rsi)
incq %rcx
jmp 0x6e81
movq %r15, %rdi
callq 0x619d
movl %eax, 0x48(%r13)
movl %eax, %edi
shlq $0x3, %rdi
callq 0x6856
movq %rax, 0x50(%r13)
movq (%r15), %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x6ed6
movq 0x10(%r15), %rdx
movq (%rdx,%rcx,8), %rdx
movq 0x50(%r13), %rsi
movq %rdx, (%rsi,%rcx,8)
incq %rcx
jmp 0x6ebc
movq %r14, %rdi
callq 0x5f16
movq %r15, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5f16