// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x8b38
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x9140
movq %rax, -0x28(%rbp)
movl $0x1, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
xorl %edx, %edx
divq %rcx
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x1c(%rbp), %edi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rax
movl %eax, %edx
movl -0x2c(%rbp), %ecx
callq 0x9250
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
shlq $0x0, %rdi
callq 0x93f0
movq %rax, -0x48(%rbp)
movb $0x0, %al
callq 0x80e0
movq %rax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movslq -0x54(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x8e88
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x8ba8
jmp 0x8e88
movl $0x0, -0x58(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x54(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movslq -0x64(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x8bf2
movq -0x48(%rbp), %rax
movslq -0x64(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x8bd1
movq $0x0, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movslq -0x74(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x8e78
movq -0x48(%rbp), %rax
movslq -0x74(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x8c22
jmp 0x8e6a
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rdi
movslq -0x74(%rbp), %rsi
callq 0x8260
movq -0x48(%rbp), %rax
movslq -0x74(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
movslq -0x78(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x8da4
movl $0x1, -0x7c(%rbp)
movl $0x0, -0x80(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x7c(%rbp)
movb %al, -0xc9(%rbp)
je 0x8c8b
movslq -0x80(%rbp), %rax
cmpq -0x10(%rbp), %rax
setb %al
movb %al, -0xc9(%rbp)
movb -0xc9(%rbp), %al
testb $0x1, %al
jne 0x8c9a
jmp 0x8d75
movl -0x80(%rbp), %eax
cmpl -0x78(%rbp), %eax
je 0x8d65
movq -0x50(%rbp), %rdi
movslq -0x80(%rbp), %rsi
callq 0x8460
cmpl $0x0, %eax
je 0x8ce0
movq -0x8(%rbp), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0x60(%rbp), %rax
setbe %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x7c(%rbp)
jmp 0x8d63
movq -0x8(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movb $0x1, %al
cmpq -0x60(%rbp), %rcx
movb %al, -0xca(%rbp)
jb 0x8d55
movq -0x8(%rbp), %rax
movslq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movb $0x1, %al
cmpq -0x60(%rbp), %rcx
movb %al, -0xca(%rbp)
jb 0x8d55
movq -0x8(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x8(%rbp), %rcx
movslq -0x78(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x80(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
sete %al
movb %al, -0xca(%rbp)
movb -0xca(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x7c(%rbp)
jmp 0x8d65
jmp 0x8d67
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x8c6c
cmpl $0x0, -0x7c(%rbp)
je 0x8d94
movq -0x50(%rbp), %rdi
movslq -0x78(%rbp), %rsi
callq 0x8260
movq -0x48(%rbp), %rax
movslq -0x78(%rbp), %rcx
movb $0x1, (%rax,%rcx)
jmp 0x8d96
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x8c50
movq -0x50(%rbp), %rdi
callq 0x88a0
movq %rax, -0x88(%rbp)
cmpq $0x1, -0x88(%rbp)
jbe 0x8df9
movq -0x88(%rbp), %rax
movl -0x34(%rbp), %ecx
cmpq %rcx, %rax
jae 0x8df9
movq -0x40(%rbp), %rdi
movq -0x88(%rbp), %rax
movl %eax, %esi
movq -0x50(%rbp), %rdx
movl -0x58(%rbp), %ecx
movl -0x54(%rbp), %r8d
movq -0x60(%rbp), %rax
movl %eax, %r9d
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
callq 0x9440
jmp 0x8e58
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x8e56
movq -0x88(%rbp), %rax
movl -0x34(%rbp), %ecx
cmpq %rcx, %rax
jne 0x8e56
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x54(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x40(%rbp), %rdi
movq -0x88(%rbp), %rax
movl %eax, %esi
movq -0x50(%rbp), %rdx
movl -0x58(%rbp), %ecx
movl -0x54(%rbp), %r8d
movq -0x60(%rbp), %rax
movl %eax, %r9d
movq -0x18(%rbp), %r10
movl -0x2c(%rbp), %eax
movq %r10, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x95c0
jmp 0x8e58
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
callq 0x8240
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x8c01
jmp 0x8e7a
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x8b8a
movq -0x50(%rbp), %rdi
callq 0x8210
movq -0x48(%rbp), %rdi
callq 0x2030
movslq -0x54(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x90d5
movl -0x1c(%rbp), %eax
movl %eax, %edi
movl -0x1c(%rbp), %eax
movl %eax, %esi
movl $0x8, %edx
callq 0xcb60
movq %rax, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x8fcd
movq -0x40(%rbp), %rdi
movl -0x94(%rbp), %esi
callq 0xba60
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0xbe60
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
movl -0xa8(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x8fb7
movq -0x40(%rbp), %rdi
movl -0xa8(%rbp), %esi
callq 0xba60
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0xbe60
movl (%rax), %eax
movl %eax, -0xb4(%rbp)
movq -0x8(%rbp), %rax
movl -0xa4(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0xb4(%rbp), %ecx
movq (%rax,%rcx,8), %rdx
movq -0x90(%rbp), %rax
movslq -0xa8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x94(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x90(%rbp), %rax
movslq -0x94(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0xa8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x8f18
jmp 0x8fb9
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x8ed0
jmp 0x8fcf
movslq -0x54(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x90c4
movl $0x0, -0xb8(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x54(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
movq %rax, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x90b4
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x909e
movq -0x90(%rbp), %rax
movslq -0xc4(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0xc8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0xc0(%rbp), %rax
jne 0x908b
movq -0x40(%rbp), %rdi
movl -0xc4(%rbp), %esi
movl -0xc8(%rbp), %edx
movl -0x54(%rbp), %r8d
movq -0xc0(%rbp), %rax
movl %eax, %r9d
leaq -0xb8(%rbp), %rcx
callq 0x9780
jmp 0x908d
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0x9030
jmp 0x90a0
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x9012
jmp 0x90b6
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x8fcf
movq -0x90(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xcc30
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
je 0x90fb
movq 0xbf14(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
movl -0x38(%rbp), %edx
movl -0x1c(%rbp), %ecx
leaq 0x7bd2(%rip), %rsi      # 0x10cc6
movb $0x0, %al
callq 0x2140
movq -0x40(%rbp), %rdi
callq 0x9880
movq -0x40(%rbp), %rdi
callq 0x9b00
movq -0x40(%rbp), %rdi
callq 0x9fa0
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xa020
cmpl $0x0, -0x30(%rbp)
je 0x9132
movq -0x28(%rbp), %rdi
callq 0x7de0
movq -0x40(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
nop