// File: /workspace/github_c_makefile_star2_O3/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 %r9d, %r12d
movl %ecx, %ebp
movq %rdx, 0x78(%rsp)
movq %rsi, %r15
movq %rdi, 0x50(%rsp)
movq %r8, %rsi
movq %r8, 0x80(%rsp)
testq %r8, %r8
jne 0x6a87
movq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0x6f42
movq %rax, %rsi
movl %ebp, %ecx
xorl %r14d, %r14d
movq %r15, %rax
xorl %edx, %edx
movq %rcx, 0x60(%rsp)
divq %rcx
movq %rax, 0x70(%rsp)
movl %ebp, %edi
movl %r15d, %edx
movq %rsi, %rbx
movl %r12d, 0x68(%rsp)
movl %r12d, %ecx
callq 0x6ff8
movq %rax, 0x48(%rsp)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0x6f23
xorl %eax, %eax
callq 0x6640
movq %rax, %r13
movq %rbx, 0x28(%rsp)
movq (%rbx), %rax
testq %rax, %rax
sete %cl
testl %ebp, %ebp
sete %r12b
orb %r12b, %cl
movl %ebp, 0x18(%rsp)
je 0x6b05
movq $0x0, 0x20(%rsp)
xorl %ebx, %ebx
jmp 0x6d15
movl 0x70(%rsp), %ecx
movq %rcx, 0x88(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x28(%rsp), %rcx
testq %r15, %r15
je 0x6cf6
movq %r14, 0x40(%rsp)
movq 0x8(%rcx), %rax
movq %rbx, 0x58(%rsp)
leaq (%rbx,%rbx,4), %rcx
movq 0x20(%rax,%rcx,8), %r12
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2120
xorl %esi, %esi
movl $0x1, %r14d
xorl %ebp, %ebp
cmpb $0x0, (%rbx,%rsi)
je 0x6b68
incq %rsi
movq %rsi, %rax
jmp 0x6cd4
movl %ebp, 0x1c(%rsp)
movq %r14, 0x30(%rsp)
movq %r13, %rdi
movq %rsi, %r14
callq 0x6715
movb $0x1, (%rbx,%r14)
movq %r14, 0x20(%rsp)
leaq 0x1(%r14), %rax
movq %rax, 0x90(%rsp)
cmpq %r15, %rax
jae 0x6c28
movq 0x30(%rsp), %r14
xorl %ebp, %ebp
movb $0x1, %bl
cmpq %rbp, %r14
je 0x6bf7
movq %r13, %rdi
movq %rbp, %rsi
callq 0x281d
testl %eax, %eax
je 0x6bca
movq 0x50(%rsp), %rax
movq (%rax,%rbp,8), %rax
cmpq %r12, (%rax,%r14,8)
setbe %bl
jmp 0x6bf7
movq 0x50(%rsp), %rcx
movq 0x20(%rsp), %rax
movq (%rcx,%rax,8), %rax
movq (%rax,%rbp,8), %rax
cmpq %r12, %rax
jb 0x6bf7
movq (%rcx,%r14,8), %rcx
movq (%rcx,%rbp,8), %rcx
cmpq %r12, %rcx
setb %dl
cmpq %rcx, %rax
sete %bl
orb %dl, %bl
testb %bl, %bl
je 0x6c03
incq %rbp
cmpq %r15, %rbp
jb 0x6ba2
testb %bl, %bl
je 0x6c1c
movq %r13, %rdi
movq %r14, %rsi
callq 0x6715
movq 0x38(%rsp), %rax
movb $0x1, (%rax,%r14)
incq %r14
cmpq %r15, %r14
jb 0x6ba0
movq %r13, %rdi
callq 0x6951
cmpq $0x2, %rax
setae %cl
movq 0x88(%rsp), %rsi
cmpq %rsi, %rax
setb %dl
testb %dl, %cl
je 0x6c74
movl 0x68(%rsp), %ecx
movl %ecx, (%rsp)
movq 0x48(%rsp), %rdi
movl %eax, %esi
movq %r13, %rdx
movl 0x1c(%rsp), %ebp
movl %ebp, %ecx
movq 0x58(%rsp), %r8
movl %r12d, %r9d
callq 0x711d
movq 0x30(%rsp), %r14
jmp 0x6cba
movq 0x40(%rsp), %rdx
cmpl 0x18(%rsp), %edx
setae %cl
cmpq %rsi, %rax
setne %al
orb %cl, %al
movq 0x30(%rsp), %r14
movl 0x1c(%rsp), %ebp
jne 0x6cba
incl %edx
movq %rdx, 0x40(%rsp)
movq 0x48(%rsp), %rdi
movq 0x58(%rsp), %r8
movl %r8d, 0x14(%rdi)
movq 0x70(%rsp), %rsi
movq %r13, %rdx
movl %ebp, %ecx
movl %r12d, %r9d
callq 0x7289
incl %ebp
movq %r13, %rdi
callq 0x670d
movq 0x90(%rsp), %rax
movq %rax, %rsi
movq 0x38(%rsp), %rbx
incq %r14
cmpq %r15, %rax
jne 0x6b57
movq 0x28(%rsp), %rcx
movq (%rcx), %rax
movl 0x18(%rsp), %ebp
movq 0x40(%rsp), %r14
movq 0x58(%rsp), %rbx
incq %rbx
cmpl %ebp, %r14d
sete %r12b
cmpq %rbx, %rax
jbe 0x6d0e
cmpl %ebp, %r14d
jne 0x6b1b
movl %ebx, %eax
movq %rax, 0x20(%rsp)
movq %r13, %rdi
callq 0x66f7
movq 0x38(%rsp), %rdi
callq 0x2030
movq 0x28(%rsp), %rax
cmpq %rbx, (%rax)
movq 0x48(%rsp), %r15
jbe 0x6eae
movb %r12b, 0x1c(%rsp)
movq %r14, 0x40(%rsp)
movl $0x8, %edx
movq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x9061
movq %rax, %r12
cmpl $0x0, 0x18(%rsp)
je 0x6df1
movl $0x1, %eax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
movq 0x50(%rsp), %rbp
movq %r15, %rdi
movl %r14d, %esi
callq 0x8b7c
movq %rax, %rdi
callq 0x8cb4
leaq 0x1(%r14), %rcx
movq %rcx, 0x38(%rsp)
cmpq %rbx, %rcx
movq %rbx, %rdx
jae 0x6ddf
movl (%rax), %ebx
movq 0x30(%rsp), %r13
movq %r15, %rdi
movl %r13d, %esi
callq 0x8b7c
movq %rax, %rdi
callq 0x8cb4
movq 0x60(%rsp), %rdx
movl (%rax), %eax
movq (%rbp,%rbx,8), %rcx
movq (%rcx,%rax,8), %rax
movq (%r12,%r13,8), %rcx
movq %rax, (%rcx,%r14,8)
movq (%r12,%r14,8), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
cmpq %r13, %rdx
jne 0x6da4
incq 0x30(%rsp)
movq 0x38(%rsp), %r14
cmpq %rdx, %r14
movq %rdx, %rbx
jne 0x6d79
movq 0x28(%rsp), %rcx
movq (%rcx), %rax
cmpq 0x20(%rsp), %rax
movq %rbx, %rsi
jbe 0x6e9c
movl $0x0, 0x6c(%rsp)
cmpl $0x0, 0x18(%rsp)
je 0x6e86
movq 0x8(%rcx), %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rcx,4), %rcx
movq 0x20(%rax,%rcx,8), %r14
movl $0x1, %r15d
xorl %ebx, %ebx
movq %rbx, %r13
incq %rbx
cmpq %rsi, %rbx
jae 0x6e71
movq %r15, %rbp
movq (%r12,%r13,8), %rax
cmpq %r14, (%rax,%rbp,8)
jne 0x6e69
movq 0x48(%rsp), %rdi
movl %r13d, %esi
movl %ebp, %edx
leaq 0x6c(%rsp), %rcx
movq 0x20(%rsp), %r8
movl %r14d, %r9d
callq 0x73c0
movq 0x60(%rsp), %rsi
incq %rbp
cmpq %rbp, %rsi
jne 0x6e3e
incq %r15
cmpq %rsi, %rbx
jne 0x6e30
movq 0x28(%rsp), %rcx
movq (%rcx), %rax
movq 0x48(%rsp), %r15
movq 0x20(%rsp), %rdi
incq %rdi
movq %rdi, 0x20(%rsp)
cmpq %rdi, %rax
ja 0x6e07
movq %r12, %rdi
callq 0x90f7
movq 0x40(%rsp), %r14
movb 0x1c(%rsp), %r12b
testb %r12b, %r12b
je 0x6f02
movq %r15, %rdi
callq 0x7492
movq %r15, %rdi
callq 0x7680
movq %r15, %rdi
callq 0x7919
movq %r15, %rdi
movq 0x78(%rsp), %rsi
callq 0x7964
cmpq $0x0, 0x80(%rsp)
jne 0x6eed
movq 0x28(%rsp), %rdi
callq 0x64cd
movq %r15, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x90e7(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x4be1(%rip), %rsi      # 0xbaf4
movl %r14d, %edx
movl 0x18(%rsp), %ecx
xorl %eax, %eax
callq 0x2170
jmp 0x6eb3
leaq 0x541e(%rip), %rdi      # 0xc348
leaq 0x3bce(%rip), %rsi      # 0xaaff
leaq 0x3be0(%rip), %rcx      # 0xab18
movl $0x59, %edx
callq 0x2110