// File: /workspace/github_c_makefile_star2_O2/LPD-EPFL[P]mctop/src/mctop_topology.c
// Function: mctop_construct
mctop_t*
mctop_construct(uint64_t** lat_table_norm, const size_t N,
		 uint64_t** mem_lat_table, const uint n_sockets,
		 cdf_cluster_t* cc, const int is_smt)
{
  int free_cc = 0;
  if (cc == NULL)
    {
      cc = mctop_infer_clustering(lat_table_norm, N);
      free_cc = 1;
    }

  const uint hwc_per_socket = N / n_sockets;
  uint n_sockets_found = 0;
  mctop_t* topo = mctop_create(n_sockets, cc, N, is_smt);

  uint8_t* processed = malloc_assert(N * sizeof(uint8_t));
  darray_t* group = darray_create();
  int lvl;
  for (lvl = 0; lvl < cc->n_clusters; lvl++)
    {
      if (n_sockets_found == n_sockets)
	{
	  break;
	}

      uint seq_id = 0;
      uint64_t target_lat = cc->clusters[lvl].median;
      for (int i = 0; i < N; i++)
	{
	  processed[i] = 0;
	}

      size_t n_groups = 0;
      for (int x = 0; x < N; x++)
	{
	  if (processed[x])
	    {
	      continue;
	    }

	  n_groups++;
	  darray_add(group, x);
	  processed[x] = 1;
	  for (int y = x + 1; y < N; y++)
	    {
	      int belongs = 1;
	      for (int w = 0; belongs && w < N; w++)
	      	{
	      	  if (w != y) /* w is not y that is being checked */
	      	    {
		      if (darray_exists(group, w)) /* if w already in group => y must either  */
			{ /* belong with y due to a smaller latency or share the latency as y */
			  belongs = (lat_table_norm[w][y] <= target_lat);
			}
		      else	/* otherwise if w is in larger lat with x and y, then both x */
			{	/* and y should have the same distance to w */
			  belongs = (lat_table_norm[x][w] < target_lat ||
				     lat_table_norm[y][w] < target_lat ||
				     lat_table_norm[x][w] == lat_table_norm[y][w]);
			}
		    }
	      	}

	      if (belongs)
		{
		  darray_add(group, y);
		  processed[y] = 1;
		}
	    }
	  size_t group_size = darray_get_num_elems(group);
	  if (group_size > 1 && group_size < hwc_per_socket) /* within socket */
	    {
	      mctop_hwc_group_create(topo, group_size, group, seq_id, lvl, target_lat, is_smt);
	    }
	  else if (n_sockets_found < n_sockets && group_size == hwc_per_socket) /* socket */
	    {
	      n_sockets_found++;
	      topo->socket_level = lvl;
	      mctop_socket_create(topo, group_size, group, seq_id, lvl, target_lat, mem_lat_table, is_smt);
	    }

	  seq_id++;
	  darray_empty(group);
	}
    }

  darray_free(group);
  free(processed);

  if (lvl < cc->n_clusters)
    {
      uint64_t** lat_table_socket = (uint64_t**) table_calloc(n_sockets, n_sockets, sizeof(uint64_t));
      for (int x = 0; x < n_sockets; x++)
	{
	  socket_t* sx = mctop_get_socket(topo, x);
	  uint hwc_x = mctop_socket_get_first_hwc(sx)->id;
	  for (int y = x + 1; y < n_sockets; y++)
	    {
	      socket_t* sy = mctop_get_socket(topo, y);
	      uint hwc_y = mctop_socket_get_first_hwc(sy)->id;
	      lat_table_socket[x][y] = lat_table_socket[y][x] = lat_table_norm[hwc_x][hwc_y];
	    }
	}


      for (; lvl < cc->n_clusters; lvl++)
	{
	  uint seq_id = 0;
	  uint64_t target_lat = cc->clusters[lvl].median;
	  for (int x = 0; x < n_sockets; x++)
	    {
	      for (int y = x + 1; y < n_sockets; y++)
		{
		  if (lat_table_socket[x][y] == target_lat)
		    {
		      mctop_siblings_create(topo, x, y, &seq_id, lvl, target_lat);
		    }
		}
	    }
	}
      table_free((void**) lat_table_socket, n_sockets);
    }

  if (n_sockets_found != n_sockets)
    {
      fprintf(stderr, "MCTOP Warning: Found %u sockets instead of %u that was expected.\n"
	      "            The generated topology is probably INCORRECT!.\n", n_sockets_found, n_sockets);
    }

  mctop_fix_children_links(topo);
  mctop_fix_horizontal_links(topo);
  mctop_fix_n_hwcs_per_core_smt(topo);
  mctop_mem_latencies_add(topo, mem_lat_table);

  if (free_cc)
    {
      cdf_cluster_free(cc);
    }

  return topo;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %ecx, %r14d
movq %rdx, 0x70(%rsp)
movq %rsi, %rbp
movq %rdi, 0x18(%rsp)
movq %r8, %rbx
movq %r8, 0x78(%rsp)
testq %r8, %r8
jne 0x62bc
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movl %r9d, %ebx
callq 0x66e3
movl %ebx, %r9d
movq %rax, %rbx
movl %r14d, %ecx
movq %rbp, %rax
xorl %edx, %edx
movq %rcx, 0x50(%rsp)
divq %rcx
movq %rax, %r15
movl %r14d, %edi
movq %rbx, %rsi
movl %ebp, %edx
movl %r9d, 0x60(%rsp)
movl %r9d, %ecx
callq 0x6796
movq %rax, 0x30(%rsp)
movq %rbp, %rdi
callq 0x6856
movq %rax, %r12
xorl %eax, %eax
callq 0x5e6c
xorl %edx, %edx
movq %rax, %r13
movq %r15, 0x80(%rsp)
movl %r15d, %eax
movq %rax, 0x88(%rsp)
movl $0x0, 0x14(%rsp)
movq %rbx, 0x68(%rsp)
movl %r14d, 0x24(%rsp)
movq %r12, 0x48(%rsp)
cmpq %rdx, (%rbx)
movq %rdx, 0x28(%rsp)
jbe 0x6536
cmpl %r14d, 0x14(%rsp)
je 0x6536
movq 0x8(%rbx), %rax
imulq $0x28, %rdx, %rcx
movq 0x20(%rax,%rcx), %rax
movq %rax, 0x38(%rsp)
xorl %eax, %eax
cmpq %rax, %rbp
je 0x6366
movb $0x0, (%r12,%rax)
incq %rax
jmp 0x6357
xorl %ebx, %ebx
xorl %r14d, %r14d
cmpq %rbp, %rbx
je 0x6524
cmpb $0x0, (%r12,%rbx)
jne 0x651c
movl %r14d, 0x44(%rsp)
movq %r13, %r14
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5f31
movb $0x1, (%r12,%rbx)
movq %rbx, 0x58(%rsp)
movq %rbx, %r13
leaq 0x1(%r13), %rax
cmpq %rbp, %rax
jae 0x6464
movq %rax, 0x90(%rsp)
leaq 0x1(%r13), %r15
movb $0x1, %r12b
xorl %ebx, %ebx
testb $0x1, %r12b
je 0x6436
cmpq %rbp, %rbx
jae 0x6436
movb $0x1, %r12b
cmpq %rbx, %r15
je 0x6431
movq %r14, %rdi
movq %rbx, %rsi
callq 0x601f
testl %eax, %eax
je 0x63f8
movq 0x18(%rsp), %rax
movq (%rax,%rbx,8), %rax
movq 0x38(%rsp), %rcx
cmpq %rcx, 0x8(%rax,%r13,8)
setbe %r12b
jmp 0x6431
movq 0x18(%rsp), %rax
movq 0x58(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax,%rbx,8), %rax
cmpq 0x38(%rsp), %rax
jb 0x6431
movq 0x18(%rsp), %rcx
movq 0x8(%rcx,%r13,8), %rcx
movq (%rcx,%rbx,8), %rcx
cmpq 0x38(%rsp), %rcx
setb %dl
cmpq %rcx, %rax
sete %r12b
orb %dl, %r12b
incq %rbx
jmp 0x63bd
movq 0x90(%rsp), %rbx
movq %rbx, %r13
testb $0x1, %r12b
je 0x639f
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5f31
movq 0x48(%rsp), %rax
movb $0x1, (%rax,%rbx)
jmp 0x639c
movq %r14, %r13
movq %r14, %rdi
callq 0x619d
cmpq $0x2, %rax
setae %cl
movq 0x88(%rsp), %rsi
cmpq %rsi, %rax
setb %dl
testb %dl, %cl
je 0x64bc
movl 0x60(%rsp), %ecx
movl %ecx, (%rsp)
movq 0x30(%rsp), %rdi
movl %eax, %esi
movq %r13, %rdx
movl 0x44(%rsp), %r14d
movl %r14d, %ecx
movq 0x28(%rsp), %r8
movq 0x38(%rsp), %r9
callq 0x6880
movq 0x48(%rsp), %r12
movq 0x58(%rsp), %rbx
jmp 0x650c
movl 0x14(%rsp), %edx
cmpl 0x24(%rsp), %edx
setae %cl
cmpq %rsi, %rax
setne %al
orb %cl, %al
movq 0x48(%rsp), %r12
movq 0x58(%rsp), %rbx
movl 0x44(%rsp), %r14d
jne 0x650c
incl %edx
movl %edx, 0x14(%rsp)
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %r8
movl %r8d, 0x14(%rdi)
movq 0x80(%rsp), %rsi
movq %r13, %rdx
movl %r14d, %ecx
movq 0x38(%rsp), %r9
callq 0x697f
incl %r14d
movq %r13, %rdi
callq 0x5f2c
movq 0x28(%rsp), %rdx
incq %rbx
jmp 0x636b
incq %rdx
movq 0x68(%rsp), %rbx
movl 0x24(%rsp), %r14d
jmp 0x632a
movq %r13, %rdi
callq 0x5f16
movq %r12, %rdi
callq 0x2030
movq 0x28(%rsp), %rax
cmpq %rax, (%rbx)
movq 0x30(%rsp), %rbp
jbe 0x6673
pushq $0x8
popq %rdx
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x851b
movq %rbx, %rsi
movq %rax, %rbx
pushq $0x1
popq %r15
xorl %r14d, %r14d
cmpq %rsi, %r14
je 0x65e0
movq %rbp, %rdi
movl %r14d, %esi
callq 0x7ed0
movq %rax, %rdi
callq 0x801a
movl (%rax), %r13d
movq %r15, %r12
movq 0x50(%rsp), %rsi
cmpq %rsi, %r12
jae 0x65d8
movq %rbp, %rdi
movl %r12d, %esi
callq 0x7ed0
movq %rax, %rdi
callq 0x801a
movl (%rax), %eax
movq 0x18(%rsp), %rcx
movq (%rcx,%r13,8), %rcx
movq (%rcx,%rax,8), %rax
movq (%rbx,%r12,8), %rcx
movq %rax, (%rcx,%r14,8)
movq (%rbx,%r14,8), %rcx
movq %rax, (%rcx,%r12,8)
incq %r12
jmp 0x6597
incq %r14
incq %r15
jmp 0x6579
leaq 0x64(%rsp), %r14
movq 0x28(%rsp), %r8
movq 0x68(%rsp), %r15
cmpq %r8, (%r15)
jbe 0x6663
andl $0x0, 0x64(%rsp)
movq 0x8(%r15), %rax
imulq $0x28, %r8, %rcx
movq 0x20(%rax,%rcx), %r15
xorl %r12d, %r12d
pushq $0x1
popq %r13
cmpq %rsi, %r12
je 0x6659
movq %r13, 0x18(%rsp)
cmpq %rsi, %r13
jae 0x664c
movq (%rbx,%r12,8), %rax
cmpq %r15, (%rax,%r13,8)
jne 0x6647
movq 0x30(%rsp), %rdi
movl %r12d, %esi
movl %r13d, %edx
movq %r14, %rcx
movq %r8, %rbp
movl %r15d, %r9d
callq 0x6a7d
movq %rbp, %r8
movq 0x50(%rsp), %rsi
incq %r13
jmp 0x6617
incq %r12
movq 0x18(%rsp), %r13
incq %r13
jmp 0x660d
incq %r8
movq 0x30(%rsp), %rbp
jmp 0x65ea
movq %rbx, %rdi
callq 0x8588
movq %r15, %rbx
movl 0x24(%rsp), %r14d
movl 0x14(%rsp), %edx
cmpl %r14d, %edx
jne 0x66c6
movq %rbp, %rdi
callq 0x6b26
movq %rbp, %rdi
callq 0x6cbd
movq %rbp, %rdi
callq 0x6ef4
movq %rbp, %rdi
movq 0x70(%rsp), %rsi
callq 0x6f3b
cmpq $0x0, 0x78(%rsp)
jne 0x66b1
movq %rbx, %rdi
callq 0x5d08
movq %rbp, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9923(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x541d(%rip), %rsi      # 0xbaf4
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2170
jmp 0x667c