// File: /workspace/github_c_makefile_star2_O1/LPD-EPFL[P]mctop/src/mctop_topology.c
// Function: mctop_mem_latencies_add
void
mctop_mem_latencies_add(mctop_t* topo, uint64_t** mem_lat_table)
{
  /* mem. latencies */
  if (mem_lat_table != NULL)
    {
      topo->has_mem = topo->has_mem | LATENCY;
      topo->node_to_socket = calloc_assert(topo->n_sockets, sizeof(uint));

      for (int s = 0; s < topo->n_sockets; s++)
	{
	  socket_t* socket = &topo->sockets[s];
	  uint hwc_use_id = 0;
	  uint min_avg = -1;
	  for (int i = 0; i < socket->n_hwcs; i++)
	    {
	      uint hwc_id = socket->hwcs[i]->id;
	      uint avg = 0;
	      for (int s = 0; s < topo->n_sockets; s++)
		{
		  avg += mem_lat_table[hwc_id][s];
		}
	      avg /= topo->n_sockets;
	      if (avg > 0 && avg < min_avg)
		{
		  min_avg = avg;
		  hwc_use_id = hwc_id;
		}
	    }

	  uint64_t* lats = mem_lat_table[hwc_use_id];
	  socket->mem_latencies = malloc_assert(topo->n_sockets * sizeof(uint));
	  uint min_lat = -1, local_node = 0;
	  for (int n = 0; n < topo->n_sockets; n++)
	    {
	      if (lats[n] < min_lat)
		{
		  local_node = n;
		  min_lat = lats[n];
		}
	      socket->mem_latencies[n] = lats[n];
	    }
	  socket->local_node = local_node;
	  topo->node_to_socket[socket->local_node] = s;
	}
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rsi, %rsi
je 0x7b49
movq %rsi, %rbx
movq %rdi, %rbp
orb $0x1, 0x30(%rdi)
movl 0x18(%rdi), %edi
movl $0x4, %esi
callq 0x2150
testq %rax, %rax
je 0x7b77
movq %rax, 0x38(%rbp)
movl 0x18(%rbp), %r13d
testq %r13, %r13
je 0x7b49
movq 0x20(%rbp), %rcx
movq %rcx, 0x10(%rsp)
leaq (,%r13,4), %rcx
movq %rcx, 0x8(%rsp)
xorl %r12d, %r12d
movq %rax, (%rsp)
imulq $0xa8, %r12, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %r15
movl 0x28(%rcx,%rax), %esi
testq %rsi, %rsi
je 0x7ae4
movq 0x30(%r15), %rdi
xorl %r8d, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %r10d, %r10d
movq (%rdi,%r8,8), %rax
movl (%rax), %r11d
movq (%rbx,%r11,8), %rax
xorl %edx, %edx
xorl %ecx, %ecx
addl (%rax,%rdx,8), %ecx
incq %rdx
cmpq %rdx, %r13
jne 0x7aaf
movl %ecx, %eax
xorl %edx, %edx
divl %r13d
cmpl %ecx, %r13d
setbe %cl
cmpl %r9d, %eax
setb %dl
testb %dl, %cl
cmovnel %r11d, %r10d
cmovnel %eax, %r9d
incq %r8
cmpq %rsi, %r8
jne 0x7aa0
movl %r10d, %eax
jmp 0x7ae6
xorl %eax, %eax
movq (%rbx,%rax,8), %r14
movq 0x8(%rsp), %rdi
callq 0x21d0
testq %rax, %rax
je 0x7b58
movq %rax, 0x78(%r15)
movl 0x18(%rbp), %ecx
testq %rcx, %rcx
je 0x7b2d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edi, %edi
xorl %edx, %edx
movq (%r14,%rdi,8), %r8
movl %esi, %r9d
cmpq %r9, %r8
cmovbl %r8d, %esi
cmovbl %edi, %edx
movl %r8d, (%rax,%rdi,4)
incq %rdi
cmpq %rdi, %rcx
jne 0x7b0e
jmp 0x7b2f
xorl %edx, %edx
movl %edx, 0x6c(%r15)
movl %edx, %eax
movq (%rsp), %rcx
movl %r12d, (%rcx,%rax,4)
incq %r12
cmpq %r13, %r12
jne 0x7a77
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x57e9(%rip), %rdi      # 0xd348
leaq 0x3f99(%rip), %rsi      # 0xbaff
leaq 0x3fab(%rip), %rcx      # 0xbb18
movl $0x59, %edx
callq 0x2110
leaq 0x57ca(%rip), %rdi      # 0xd348
leaq 0x3f7a(%rip), %rsi      # 0xbaff
leaq 0x3fa8(%rip), %rcx      # 0xbb34
movl $0x69, %edx
callq 0x2110