// File: /workspace/github_c_makefile_star2_O3/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 0x6640
movq %rax, %rbp
cmpl $0x0, 0x18(%r14)
je 0x774e
xorl %r12d, %r12d
xorl %ebx, %ebx
movq 0x20(%r14), %r13
imulq $0xa8, %r12, %rax
movq 0x30(%r13,%rax), %rcx
movq (%rcx), %r15
testq %rbx, %rbx
je 0x76c7
movq %r15, 0x20(%rbx)
addq %rax, %r13
cmpl $0x2, 0x28(%r13)
jb 0x770f
movl $0x1, %ebx
movq 0x30(%r13), %rax
movq (%rax,%rbx,8), %rsi
movq %r15, %rdi
callq 0x8ebb
movq 0x30(%r13), %rcx
movq (%rcx,%rbx,8), %rsi
testl %eax, %eax
je 0x76fc
movq %rbp, %rdi
callq 0x6715
jmp 0x7703
movq %rsi, 0x20(%r15)
movq %rsi, %r15
incq %rbx
movl 0x28(%r13), %eax
cmpq %rax, %rbx
jb 0x76d6
movq (%rbp), %rax
testq %rax, %rax
je 0x7733
movq 0x10(%rbp), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rbx
movq %rbx, 0x20(%r15)
incq %rdx
movq %rbx, %r15
cmpq %rdx, %rax
jne 0x771e
jmp 0x7736
movq %r15, %rbx
movq %rbp, %rdi
callq 0x670d
incq %r12
movl 0x18(%r14), %eax
cmpq %rax, %r12
jb 0x76ab
movq 0x40(%r14), %rbx
testq %rbx, %rbx
je 0x778b
movq 0x18(%rbx), %rsi
movq %rbp, %rdi
callq 0x27e4
movq 0x20(%rbx), %rbx
jmp 0x7752
movq %rax, %r15
movq %rbp, %rdi
callq 0x670d
testq %r15, %r15
je 0x778b
movq 0x18(%r15), %rsi
movq %rbp, %rdi
callq 0x27e4
movq 0x20(%r15), %r15
jmp 0x7774
movq (%rbp), %rax
cmpq $0x2, %rax
jb 0x77b3
movq 0x10(%rbp), %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 0x779e
movq %rbp, %rdi
xorl %esi, %esi
callq 0x6955
cmpq $0x0, 0x18(%rax)
jne 0x7769
movq %rbp, 0x8(%rsp)
xorl %eax, %eax
callq 0x6640
movq %rax, %r15
movl 0x14(%r14), %ebp
incl %ebp
cmpl (%r14), %ebp
jae 0x7898
movq %r14, 0x10(%rsp)
movq 0x8(%rsp), %rdi
callq 0x670d
movl 0x18(%r14), %ecx
testl %ecx, %ecx
je 0x785c
xorl %r13d, %r13d
movq 0x20(%r14), %rbx
imulq $0xa8, %r13, %rdx
movl 0x50(%rbx,%rdx), %eax
testl %eax, %eax
je 0x7852
addq %rdx, %rbx
xorl %r14d, %r14d
movq 0x58(%rbx), %rcx
movq (%rcx,%r14,8), %r12
cmpl %ebp, 0x4(%r12)
jne 0x783f
movq %r15, %rdi
movq %r12, %rsi
callq 0x27e4
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x27e4
movl 0x50(%rbx), %eax
incq %r14
movl %eax, %ecx
cmpq %rcx, %r14
jb 0x7815
movq 0x10(%rsp), %r14
movl 0x18(%r14), %ecx
incq %r13
movl %ecx, %eax
cmpq %rax, %r13
jb 0x77fc
movq 0x8(%rsp), %rax
movq (%rax), %rax
cmpq $0x2, %rax
jb 0x788d
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %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 0x7878
incl %ebp
cmpl (%r14), %ebp
jb 0x77e7
movq %r15, %rdi
callq 0x6951
movl %eax, 0x48(%r14)
movl %eax, %edi
shlq $0x3, %rdi
callq 0x21d0
testq %rax, %rax
je 0x78fa
movq %rax, 0x50(%r14)
movq (%r15), %rax
testq %rax, %rax
movq 0x8(%rsp), %rdi
je 0x78df
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 0x78c7
callq 0x66f7
movq %r15, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x66f7
leaq 0x4a47(%rip), %rdi      # 0xc348
leaq 0x31f7(%rip), %rsi      # 0xaaff
leaq 0x3209(%rip), %rcx      # 0xab18
movl $0x59, %edx
callq 0x2110