// File: /workspace/github_c_makefile_star2_O1/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 $0x88, %rsp
movl %r9d, %r13d
movq %r8, %rbx
movl %ecx, %r14d
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, 0x50(%rsp)
movq %r8, %rbp
testq %r8, %r8
jne 0x6b29
movq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0x6ff5
movq %rax, %rbp
movl %r14d, %ecx
movq $0x0, 0x38(%rsp)
movq %r15, %rax
xorl %edx, %edx
movq %rcx, 0x60(%rsp)
divq %rcx
movq %rax, 0x18(%rsp)
movl %r14d, %edi
movq %rbp, %rsi
movl %r15d, %edx
movl %r13d, 0x68(%rsp)
movl %r13d, %ecx
callq 0x70ab
movq %rax, 0x48(%rsp)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x6fd6
xorl %eax, %eax
callq 0x66e4
movq %rax, %r13
movq %rbp, 0x28(%rsp)
cmpq $0x0, (%rbp)
sete %al
movl %r14d, 0x20(%rsp)
testl %r14d, %r14d
sete %bpl
orb %bpl, %al
movq %rbx, 0x78(%rsp)
movq %r12, 0x70(%rsp)
je 0x6bc0
movq $0x0, 0x18(%rsp)
xorl %r15d, %r15d
movq 0x30(%rsp), %r14
jmp 0x6dce
movl 0x18(%rsp), %ebx
xorl %esi, %esi
xorl %edx, %edx
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %r14
movq %rbx, 0x80(%rsp)
movq %rdx, 0x38(%rsp)
testq %r15, %r15
movq %rsi, 0x58(%rsp)
je 0x6d91
movq 0x8(%rax), %rax
leaq (%rsi,%rsi,4), %rcx
movq 0x20(%rax,%rcx,8), %r12
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2120
movl $0x1, %eax
movl $0x0, 0x24(%rsp)
cmpb $0x0, (%r14,%rbp)
jne 0x6d82
movq %rax, 0x40(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x67b9
movb $0x1, (%r14,%rbp)
movq %rbp, 0x18(%rsp)
leaq 0x1(%rbp), %rax
cmpq %r15, %rax
jae 0x6cd5
movq 0x40(%rsp), %r14
xorl %ebp, %ebp
movb $0x1, %bl
cmpq %rbp, %r14
je 0x6ca4
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2806
testl %eax, %eax
je 0x6c77
movq 0x50(%rsp), %rax
movq (%rax,%rbp,8), %rax
cmpq %r12, (%rax,%r14,8)
setbe %bl
jmp 0x6ca4
movq 0x50(%rsp), %rcx
movq 0x18(%rsp), %rax
movq (%rcx,%rax,8), %rax
movq (%rax,%rbp,8), %rax
cmpq %r12, %rax
jb 0x6ca4
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 0x6cb0
incq %rbp
cmpq %r15, %rbp
jb 0x6c4f
testb %bl, %bl
je 0x6cc9
movq %r13, %rdi
movq %r14, %rsi
callq 0x67b9
movq 0x30(%rsp), %rax
movb $0x1, (%rax,%r14)
incq %r14
cmpq %r15, %r14
jb 0x6c4d
movq %r13, %rdi
callq 0x69ff
cmpq $0x2, %rax
setae %cl
movq 0x80(%rsp), %rbx
cmpq %rbx, %rax
setb %dl
testb %dl, %cl
je 0x6d23
movl 0x68(%rsp), %ecx
movl %ecx, (%rsp)
movq 0x48(%rsp), %rdi
movl %eax, %esi
movq %r13, %rdx
movl 0x24(%rsp), %r14d
movl %r14d, %ecx
movq 0x58(%rsp), %r8
movl %r12d, %r9d
callq 0x71d7
movq 0x18(%rsp), %rbp
jmp 0x6d68
movq 0x38(%rsp), %rsi
cmpl 0x20(%rsp), %esi
setae %cl
cmpq %rbx, %rax
setne %dl
orb %cl, %dl
movq 0x18(%rsp), %rbp
movl 0x24(%rsp), %r14d
jne 0x6d68
incl %esi
movq %rsi, 0x38(%rsp)
movq 0x48(%rsp), %rdi
movq 0x58(%rsp), %r8
movl %r8d, 0x14(%rdi)
movl %eax, %esi
movq %r13, %rdx
movl %r14d, %ecx
movl %r12d, %r9d
callq 0x7343
incl %r14d
movl %r14d, 0x24(%rsp)
movq %r13, %rdi
callq 0x67b1
movq 0x30(%rsp), %r14
movq 0x40(%rsp), %rax
incq %rbp
incq %rax
cmpq %r15, %rbp
jne 0x6c16
movq 0x58(%rsp), %rsi
incq %rsi
movl 0x20(%rsp), %ecx
movq 0x38(%rsp), %rdx
cmpl %ecx, %edx
sete %bpl
movq 0x28(%rsp), %rax
cmpq %rsi, (%rax)
jbe 0x6dba
cmpl %ecx, %edx
jne 0x6bda
movl %esi, %eax
movq %rax, 0x18(%rsp)
movq 0x78(%rsp), %rbx
movq %rsi, %r15
movq 0x70(%rsp), %r12
movq %r13, %rdi
callq 0x679b
movq %r14, %rdi
callq 0x2030
movq 0x28(%rsp), %rax
cmpq %r15, (%rax)
movq 0x48(%rsp), %r15
jbe 0x6f67
movb %bpl, 0x24(%rsp)
movl $0x8, %edx
movq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x90fd
movq %rax, %r12
cmpl $0x0, 0x20(%rsp)
je 0x6ea3
movl $0x1, %eax
movq %rax, 0x40(%rsp)
xorl %r14d, %r14d
movq 0x50(%rsp), %rbp
movq %r15, %rdi
movl %r14d, %esi
callq 0x8c08
movq %rax, %rdi
callq 0x8d4f
leaq 0x1(%r14), %rcx
movq %rcx, 0x30(%rsp)
cmpq %rbx, %rcx
movq %rbx, %rdx
jae 0x6e91
movl (%rax), %ebx
movq 0x40(%rsp), %r13
movq %r15, %rdi
movl %r13d, %esi
callq 0x8c08
movq %rax, %rdi
callq 0x8d4f
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 0x6e56
incq 0x40(%rsp)
movq 0x30(%rsp), %r14
cmpq %rdx, %r14
movq %rdx, %rbx
jne 0x6e2b
movq 0x28(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rcx, (%rax)
movq %rbx, %rsi
jbe 0x6f4b
movl $0x0, 0x6c(%rsp)
cmpl $0x0, 0x20(%rsp)
je 0x6f30
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x18(%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 0x6f28
movq %r15, %rbp
movq (%r12,%r13,8), %rax
cmpq %r14, (%rax,%rbp,8)
jne 0x6f20
movq 0x48(%rsp), %rdi
movl %r13d, %esi
movl %ebp, %edx
leaq 0x6c(%rsp), %rcx
movq 0x18(%rsp), %r8
movl %r14d, %r9d
callq 0x747a
movq 0x60(%rsp), %rsi
incq %rbp
cmpq %rbp, %rsi
jne 0x6ef5
incq %r15
cmpq %rsi, %rbx
jne 0x6ee7
movq 0x18(%rsp), %rdx
incq %rdx
movq 0x28(%rsp), %rax
movq %rdx, 0x18(%rsp)
cmpq %rdx, (%rax)
ja 0x6eb9
movq %r12, %rdi
callq 0x9193
movq 0x78(%rsp), %rbx
movq 0x70(%rsp), %r12
movq 0x48(%rsp), %r15
movb 0x24(%rsp), %bpl
testb %bpl, %bpl
je 0x6fb3
movq %r15, %rdi
callq 0x754c
movq %r15, %rdi
callq 0x7735
movq %r15, %rdi
callq 0x79c7
movq %r15, %rdi
movq %r12, %rsi
callq 0x7a12
testq %rbx, %rbx
jne 0x6f9e
movq 0x28(%rsp), %rdi
callq 0x6558
movq %r15, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa036(%rip), %rax      # 0x10ff0
movq (%rax), %rdi
leaq 0x5b30(%rip), %rsi      # 0xcaf4
movq 0x38(%rsp), %rdx
movl 0x20(%rsp), %ecx
xorl %eax, %eax
callq 0x2170
jmp 0x6f6c
leaq 0x636b(%rip), %rdi      # 0xd348
leaq 0x4b1b(%rip), %rsi      # 0xbaff
leaq 0x4b2d(%rip), %rcx      # 0xbb18
movl $0x59, %edx
callq 0x2110