// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movb $0x0, %al
callq 0x80e0
movq %rax, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jae 0x9c5c
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x9b75
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jae 0x9bf2
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0xc480
cmpl $0x0, %eax
je 0x9bc1
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x8260
jmp 0x9be5
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x9be7
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x9b7c
movl $0x0, -0x38(%rbp)
movslq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x9c3d
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x9bf9
movq -0x30(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x8240
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x9b29
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x9c96
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x8410
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x9c70
movl $0x0, -0x54(%rbp)
movl $0x1, -0x58(%rbp)
movslq -0x58(%rbp), %rax
movq -0x48(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x9cf5
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x58(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x20(%rax)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x9ca4
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x88c0
movq %rax, -0x70(%rbp)
movl $0x0, -0x54(%rbp)
movq -0x70(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x9d50
movl $0x1, -0x54(%rbp)
movq -0x48(%rbp), %rdi
callq 0x8240
cmpq $0x0, -0x70(%rbp)
je 0x9d4e
movq -0x48(%rbp), %rdi
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x8410
movq -0x70(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x70(%rbp)
jmp 0x9d28
jmp 0x9d50
jmp 0x9d52
cmpl $0x0, -0x54(%rbp)
jne 0x9c9d
movb $0x0, %al
callq 0x80e0
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x9ef7
movq -0x80(%rbp), %rdi
callq 0x8240
movl $0x0, -0x88(%rbp)
movl -0x88(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jae 0x9e6b
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x88(%rbp), %rcx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
movq -0x90(%rbp), %rcx
cmpl 0x50(%rcx), %eax
jae 0x9e55
movq -0x90(%rbp), %rax
movq 0x58(%rax), %rax
movslq -0x94(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x84(%rbp), %eax
jne 0x9e42
movq -0x78(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x8410
movq -0x80(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x8410
jmp 0x9e44
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x9de1
jmp 0x9e57
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x9da4
movl $0x1, -0xa4(%rbp)
movslq -0xa4(%rbp), %rax
movq -0x80(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x9ee1
movq -0x80(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xa4(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
movq -0x80(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0xa4(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rcx
movq -0xb0(%rbp), %rax
movq %rcx, 0x20(%rax)
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x9e75
jmp 0x9ee3
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x9d7f
movq -0x78(%rbp), %rdi
callq 0x88a0
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x48(%rax)
movq -0x8(%rbp), %rax
movl 0x48(%rax), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x93f0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x50(%rax)
movl $0x0, -0xbc(%rbp)
movslq -0xbc(%rbp), %rax
movq -0x78(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x9f85
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0xbc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movslq -0xbc(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x9f30
movq -0x10(%rbp), %rdi
callq 0x8210
movq -0x78(%rbp), %rdi
callq 0x8210
addq $0xd0, %rsp
popq %rbp
retq