// File: /workspace/github_c_makefile_star2_O3/LPD-EPFL[P]mctop/src/mctop_load.c
// Function: mctop_load
mctop_t*
mctop_load(const char* mct_file)
{
  clock_t cstart = clock();

#if defined(__sparc__)
  lgrp_cookie_initialize();
#endif

  char file_open[100], hostname[100];
  if (mct_file != NULL)
    {
      sprintf(file_open, "%s", mct_file);
    }
  else
    {
      if (gethostname(hostname, 100) != 0)
	{
	  perror("MCTOP Error: Could not get hostname!");
	}
      sprintf(file_open, "./desc/%s.mct", hostname);
    }

  FILE* ifile = fopen(file_open, "r");
  if (ifile == NULL)
    {
      sprintf(file_open, "/usr/share/mctop/%s.mct", hostname);
      ifile = fopen(file_open, "r");
      if (ifile == NULL)
	{
	  fprintf(stderr, "MCTOP Error: Cannot find MCT file!\n");
	  return NULL;
	}
      else
	{
	  fprintf(stdout, "MCTOP Info: Opened %s!\n", file_open);
	}
    }

  mctop_dtype_t type = MCTOP_DTYPE_N;

  uint n_hwcs, n_sockets, is_smt;
  char discard[4][30];
  int ret = fscanf(ifile, "%s %s %u %s %u %s %u",
		   discard[0], discard[1], &n_hwcs, discard[2], &n_sockets, discard[3], &is_smt);
  if (ret != 7)
    {
      fprintf(stderr, "MCTOP Error: Incorrect MCT file %s! Reason: Header line\n", file_open);
      return NULL;
    }

  uint64_t** lat_table = (uint64_t**) table_malloc(n_hwcs, n_hwcs, sizeof(uint64_t));
  uint64_t** mem_lat_table = (uint64_t**) table_calloc(n_hwcs, n_hwcs, sizeof(uint64_t));
  double** mem_bw_table_r = (double**) table_malloc(n_hwcs, n_sockets, sizeof(double));
  double** mem_bw_table1_r = (double**) table_malloc(n_hwcs, n_sockets, sizeof(double));
  double** mem_bw_table_w = (double**) table_malloc(n_hwcs, n_sockets, sizeof(double));
  double** mem_bw_table1_w = (double**) table_malloc(n_hwcs, n_sockets, sizeof(double));  
  double*** pow_measurements = NULL;
  mctop_cache_info_t* cache_info = NULL;

  uint8_t* have_data = calloc_assert(MCTOP_DTYPE_N, sizeof(uint8_t));

  uint correct = 1;
  for (uint x = 0; x < (n_hwcs * n_hwcs); x++)
    {
      uint xc, yc, lat;
      if (fscanf(ifile, "%u %u %u", &xc, &yc, &lat) != 3)
	{
	  correct = 0;
	  break;
	}
      lat_table[xc][yc] = lat;
    }

  while (correct)
    {
      char dtype[128];
      int param;
      if (fscanf(ifile, "%s %d", dtype, &param) == 2)
	{
	  type = mctop_dtype_get(dtype);
	  if (have_data[type])
	    {
	      fprintf(stderr, "MCTOP Warning: Duplicate data for %s in in %s!\n", 
		      mctop_dtypes[type], file_open);
	    }
	  have_data[type] = 1;
	  switch (type)
	    {
	    case MEM_LAT:
	      correct = mctop_load_mem_lat(ifile, n_hwcs, n_sockets, mem_lat_table);
	      break;
	    case MEM_BW_READ:
	      correct = mctop_load_mem_bw(ifile, n_sockets, mem_bw_table_r);
	      break;
	    case MEM_BW1_READ:
	      correct = mctop_load_mem_bw(ifile, n_sockets, mem_bw_table1_r);
	      break;
	    case MEM_BW_WRITE:
	      correct = mctop_load_mem_bw(ifile, n_sockets, mem_bw_table_w);
	      break;
	    case MEM_BW1_WRITE:
	      correct = mctop_load_mem_bw(ifile, n_sockets, mem_bw_table1_w);
	      break;
	    case CACHE:
	      cache_info = mctop_load_cache_info(cache_info, ifile, param);
	      correct = (cache_info != NULL);
	      break;
	    case POWER:
	      pow_measurements = mctop_power_measurements_create(n_sockets);
	      correct = mctop_load_pow_info(ifile, n_sockets, pow_measurements);
	      break;
	    case UKNOWN:
	    case MCTOP_DTYPE_N:	/* just for compilation warning */
	      correct = 0;
	      break;
	    }
	}
      else
	{
	  break;
	}
    }

  mctop_t* topo = NULL;
  if (correct)
    {
      uint64_t** mlat = have_data[MEM_LAT] ? mem_lat_table : NULL;
      topo = mctop_construct(lat_table, n_hwcs, mlat, n_sockets, NULL, is_smt);
      uint has_mem_bw_all = have_data[MEM_BW_READ] && have_data[MEM_BW1_READ] &&
      	have_data[MEM_BW_WRITE] && have_data[MEM_BW1_WRITE];
      uint has_mem_bw_any = have_data[MEM_BW_READ] && have_data[MEM_BW1_READ] &&
	have_data[MEM_BW_WRITE] && have_data[MEM_BW1_WRITE];
      if (has_mem_bw_all)
	{
	  mctop_mem_bandwidth_add(topo, mem_bw_table_r, mem_bw_table1_r, mem_bw_table_w, mem_bw_table1_w);
	}
      else if (has_mem_bw_any)
	{
	  fprintf(stderr, "MCTOP Warning: Incomplete memory bandwidth data in %s! Ignore.\n", file_open);
	}

      if (cache_info != NULL)
	{
	  mctop_cache_info_add(topo, cache_info);
	}

      if (have_data[POWER])
	{
	  mctop_pow_info_add(topo, pow_measurements);
	}
    }
  else
    {
      fprintf(stderr, "MCTOP Error: Incorrect MCT file %s! Reason: %s\n", file_open, mctop_dtypes[type]);
    }

  table_free((void**) lat_table, n_hwcs);
  table_free((void**) mem_lat_table, n_hwcs);
  table_free((void**) mem_bw_table_r, n_hwcs);
  table_free((void**) mem_bw_table1_r, n_hwcs);
  table_free((void**) mem_bw_table_w, n_hwcs);
  table_free((void**) mem_bw_table1_w, n_hwcs);
  if (pow_measurements != NULL)
    {
      mctop_power_measurements_free(pow_measurements, n_sockets);
    }
  fclose(ifile);

  free(have_data);

#ifdef __x86_64__
  if (topo->has_mem)
    {
      for (uint i = 0; i < topo->n_hwcs; i++)
	{
	  hw_context_t* hwc = &topo->hwcs[i];
	  socket_t* socket = hwc->socket;
	  if (unlikely(socket->local_node != numa_node_of_cpu(hwc->id)))
	    {
	      hwc->local_node_wrong = 1;
	      socket->local_node_wrong = 1;
	    }
	}
    }
#endif

  double dur = (clock() - cstart) / (double) CLOCKS_PER_SEC;
  printf("MCTOP Info: Topology loaded in %.3f ms\n", 1000 * dur);
  return topo;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movq %rdi, %rbx
callq 0x20d0
movq %rax, %r15
testq %rbx, %rbx
je 0x915b
leaq 0xb0(%rsp), %rdi
movq %rbx, %rsi
callq 0x2050
jmp 0x9193
leaq 0x1a0(%rsp), %rdi
movl $0x64, %esi
callq 0x2290
testl %eax, %eax
jne 0x9961
leaq 0x15f0(%rip), %rsi      # 0xa76c
leaq 0xb0(%rsp), %rdi
leaq 0x1a0(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
leaq 0x2e8f(%rip), %rsi      # 0xc029
leaq 0xb0(%rsp), %rdi
callq 0x2240
movq %rax, %r14
testq %rax, %rax
jne 0x920b
leaq 0x2e8a(%rip), %rsi      # 0xc040
leaq 0xb0(%rsp), %rbx
leaq 0x1a0(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x22b0
leaq 0x2e52(%rip), %rsi      # 0xc029
movq %rbx, %rdi
callq 0x2240
testq %rax, %rax
je 0x9972
movq %rax, %r14
movq 0x6dd6(%rip), %rax      # 0xffc8
movq (%rax), %rdi
leaq 0x2e80(%rip), %rsi      # 0xc07c
leaq 0xb0(%rsp), %rdx
xorl %eax, %eax
callq 0x2170
leaq 0x13e(%rsp), %rcx
leaq 0x15c(%rsp), %r9
leaq 0x17a(%rsp), %r10
subq $0x8, %rsp
leaq 0x74(%rsp), %r11
leaq 0x14(%rsp), %rbx
leaq 0x2e5c(%rip), %rsi      # 0xc094
leaq 0x128(%rsp), %rdx
leaq 0x10(%rsp), %r8
movq %r14, %rdi
xorl %eax, %eax
pushq %r11
pushq %r10
pushq %rbx
callq 0x2070
addq $0x20, %rsp
cmpl $0x7, %eax
jne 0x98fe
movq %r15, 0x98(%rsp)
movl 0x8(%rsp), %edi
movl $0x8, %edx
movq %rdi, %rsi
callq 0x8fe8
movq %rax, 0x58(%rsp)
movl 0x8(%rsp), %edi
movl $0x8, %edx
movq %rdi, %rsi
callq 0x9061
movq %rax, 0x60(%rsp)
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
movl $0x8, %edx
callq 0x8fe8
movq %rax, 0x50(%rsp)
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
movl $0x8, %edx
callq 0x8fe8
movq %rax, 0x48(%rsp)
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
movl $0x8, %edx
callq 0x8fe8
movq %rax, 0x40(%rsp)
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
movl $0x8, %edx
callq 0x8fe8
movq %rax, 0x38(%rsp)
movl $0x8, %edi
movl $0x1, %esi
callq 0x2150
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x997e
movl 0x8(%rsp), %eax
imull %eax, %eax
testl %eax, %eax
je 0x9382
leaq 0x2dc2(%rip), %rbx      # 0xc0e2
leaq 0x210(%rsp), %r15
leaq 0x3a0(%rsp), %r12
leaq 0x70(%rsp), %r13
xorl %ebp, %ebp
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x2070
cmpl $0x3, %eax
jne 0x9922
movl 0x70(%rsp), %eax
movl 0x210(%rsp), %ecx
movq 0x58(%rsp), %rdx
movq (%rdx,%rcx,8), %rcx
movl 0x3a0(%rsp), %edx
movq %rax, (%rcx,%rdx,8)
incl %ebp
movl 0x8(%rsp), %eax
imull %eax, %eax
cmpl %eax, %ebp
jb 0x9337
movq %r14, 0x10(%rsp)
leaq 0x3a0(%rsp), %r12
xorl %ebx, %ebx
movq $0x0, 0x28(%rsp)
movq 0x18(%rsp), %r13
movq %rbx, %r14
movq 0x10(%rsp), %rdi
leaq 0x2d3d(%rip), %rsi      # 0xc0eb
movq %r12, %rdx
leaq 0x68(%rsp), %rcx
xorl %eax, %eax
callq 0x2070
cmpl $0x2, %eax
jne 0x9716
leaq 0x6e63(%rip), %rbx      # 0x10230
xorl %ebp, %ebp
movq (%rbx), %rsi
movq %r12, %rdi
callq 0x2160
testl %eax, %eax
je 0x93f0
incq %rbp
addq $0x8, %rbx
cmpq $0x8, %rbp
jne 0x93cf
movl $0x8, %ebp
cmpl $0x7, %ebp
movl $0x7, %eax
cmovael %eax, %ebp
cmpb $0x0, (%r13,%rbp)
movq %r14, %rbx
jne 0x96b0
movb $0x1, (%r13,%rbp)
leaq 0x2b7d(%rip), %rcx      # 0xbf94
movslq (%rcx,%rbp,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0xc(%rsp), %eax
imull 0x8(%rsp), %eax
movl $0x1, %r15d
testl %eax, %eax
je 0x95d3
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
movq 0x10(%rsp), %rdi
leaq 0x2c97(%rip), %rsi      # 0xc0e2
leaq 0x210(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x30(%rsp), %r8
xorl %eax, %eax
callq 0x2070
cmpl $0x3, %eax
jne 0x94d3
movq 0x20(%rsp), %rax
leal -0x1(%rax), %r13d
xorl %ebx, %ebx
movl 0x30(%rsp), %eax
movl 0x210(%rsp), %ecx
movq 0x60(%rsp), %rdx
movq (%rdx,%rcx,8), %rcx
movl 0x70(%rsp), %edx
movq %rax, (%rcx,%rdx,8)
cmpl %ebx, %r13d
je 0x9709
movq 0x10(%rsp), %rdi
leaq 0x2c3d(%rip), %rsi      # 0xc0e2
leaq 0x210(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x30(%rsp), %r8
xorl %eax, %eax
callq 0x2070
incl %ebx
cmpl $0x3, %eax
je 0x9474
xorl %r13d, %r13d
cmpl 0x20(%rsp), %ebx
setae %r13b
movq %r14, %rbx
movl %r13d, %r15d
jmp 0x95a6
movl 0xc(%rsp), %esi
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %rdx
jmp 0x95cb
movl 0xc(%rsp), %esi
movq 0x10(%rsp), %rdi
movq 0x50(%rsp), %rdx
jmp 0x95cb
movl 0xc(%rsp), %edi
callq 0x892c
movq %rax, %rbx
xorl %r15d, %r15d
movl 0xc(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x10(%rsp), %r14
movl %r15d, %r15d
xorl %r13d, %r13d
movq %r14, %rdi
leaq 0x2cb1(%rip), %rsi      # 0xc1de
leaq 0x210(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x78(%rsp), %r8
leaq 0x80(%rsp), %r9
xorl %eax, %eax
leaq 0x90(%rsp), %r10
pushq %r10
leaq 0x90(%rsp), %r10
pushq %r10
callq 0x2070
addq $0x10, %rsp
cmpl $0x6, %eax
jne 0x959c
movq (%rbx,%r13,8), %rcx
movq (%rcx,%r15,8), %rcx
xorl %edx, %edx
movsd 0x70(%rsp,%rdx,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
incq %rdx
cmpq $0x5, %rdx
jne 0x9575
incq %r13
cmpq $0x6, %r13
jne 0x9523
incl %r15d
cmpl 0x20(%rsp), %r15d
jbe 0x951d
xorl %r15d, %r15d
cmpl $0x6, %eax
sete %r15b
movq 0x18(%rsp), %r13
jmp 0x95d3
movl 0xc(%rsp), %esi
movq 0x10(%rsp), %rdi
movq 0x38(%rsp), %rdx
jmp 0x95cb
movl 0xc(%rsp), %esi
movq 0x10(%rsp), %rdi
movq 0x48(%rsp), %rdx
callq 0x999d
movl %eax, %r15d
testl %r15d, %r15d
jne 0x939f
jmp 0x98f4
movl 0x68(%rsp), %r13d
movl %r13d, %edi
callq 0x849f
movq %rax, %r15
testq %r13, %r13
je 0x9694
xorl %ebx, %ebx
movq 0x10(%rsp), %rdi
leaq 0x2bbb(%rip), %rsi      # 0xc1c3
leaq 0x210(%rsp), %rdx
leaq 0xac(%rsp), %rcx
leaq 0x274(%rsp), %r8
leaq 0x70(%rsp), %r9
xorl %eax, %eax
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0x344(%rsp), %r10
pushq %r10
leaq 0x40(%rsp), %r10
pushq %r10
leaq 0x2f0(%rsp), %r10
pushq %r10
callq 0x2070
addq $0x20, %rsp
cmpl $0x8, %eax
jne 0x96e0
movq 0x70(%rsp), %rax
movq 0x8(%r15), %rcx
movq %rax, (%rcx,%rbx,8)
movq 0x30(%rsp), %rax
movq 0x10(%r15), %rcx
movq %rax, (%rcx,%rbx,8)
movq 0xa0(%rsp), %rax
movq 0x18(%r15), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
cmpq %rbx, %r13
jne 0x95fc
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x96a3
callq 0x8544
movq %r15, %rax
movq 0x18(%rsp), %r13
movq %r14, %rbx
jmp 0x96f5
movq 0x6939(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x6b6f(%rip), %rax      # 0x10230
movq (%rax,%rbp,8), %rdx
leaq 0x2a25(%rip), %rsi      # 0xc0f1
leaq 0xb0(%rsp), %rcx
xorl %eax, %eax
callq 0x2170
jmp 0x940a
movq %r15, %rdi
callq 0x8544
movq 0x18(%rsp), %r13
movq %r14, %rbx
movq 0x28(%rsp), %rax
xorl %r15d, %r15d
movq %rax, 0x28(%rsp)
testq %rax, %rax
setne %r15b
jmp 0x95d3
movq 0x18(%rsp), %r13
movq %r14, %rbx
jmp 0x95d3
xorl %eax, %eax
cmpb $0x0, (%r13)
movq 0x60(%rsp), %rdx
cmoveq %rax, %rdx
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %ecx
movl 0x6c(%rsp), %r9d
movq 0x58(%rsp), %rdi
xorl %r8d, %r8d
callq 0x6a44
movq %rax, %rbx
cmpb $0x0, 0x1(%r13)
je 0x97b9
cmpb $0x0, 0x2(%r13)
je 0x97b9
cmpb $0x0, 0x3(%r13)
je 0x9769
movb 0x4(%r13), %al
cmpb $0x0, 0x3(%r13)
je 0x9799
testb %al, %al
jne 0x979d
jmp 0x9770
cmpb $0x0, 0x3(%r13)
je 0x97b9
cmpb $0x0, 0x4(%r13)
je 0x97b9
movq 0x6872(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x2999(%rip), %rsi      # 0xc121
leaq 0xb0(%rsp), %rdx
xorl %eax, %eax
callq 0x2170
jmp 0x97b9
testb %al, %al
je 0x97b9
movq %rbx, %rdi
movq 0x50(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %r8
callq 0x8571
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x97cb
movq %rbx, %rdi
callq 0x856c
cmpb $0x0, 0x6(%r13)
je 0x97e2
movq %rbx, %rdi
movq %r14, %r15
movq %r14, %rsi
callq 0x8a0f
jmp 0x97e5
movq %r14, %r15
movq 0x10(%rsp), %r14
movl 0x8(%rsp), %esi
movq 0x58(%rsp), %rdi
callq 0x90f7
movl 0x8(%rsp), %esi
movq 0x60(%rsp), %rdi
callq 0x90f7
movl 0x8(%rsp), %esi
movq 0x50(%rsp), %rdi
callq 0x90f7
movl 0x8(%rsp), %esi
movq 0x48(%rsp), %rdi
callq 0x90f7
movl 0x8(%rsp), %esi
movq 0x40(%rsp), %rdi
callq 0x90f7
movl 0x8(%rsp), %esi
movq 0x38(%rsp), %rdi
callq 0x90f7
testq %r15, %r15
je 0x984f
movl 0xc(%rsp), %esi
movq %r15, %rdi
callq 0x89c1
movq %r14, %rdi
callq 0x20e0
movq %r13, %rdi
callq 0x2030
cmpl $0x0, 0x30(%rbx)
je 0x98ac
cmpl $0x0, 0x10(%rbx)
je 0x98ac
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x40(%rbx), %r12
movq 0x10(%r12,%r14), %r13
movl 0x6c(%r13), %ebp
movl (%r12,%r14), %edi
callq 0x2250
cmpl %eax, %ebp
jne 0x989c
incq %r15
movl 0x10(%rbx), %eax
addq $0x30, %r14
cmpq %rax, %r15
jb 0x9871
jmp 0x98ac
movl $0x1, %eax
movl %eax, 0x28(%r12,%r14)
movl %eax, 0x68(%r13)
jmp 0x988b
callq 0x20d0
subq 0x98(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0x74f(%rip), %xmm0      # 0xa018
mulsd 0x1f8f(%rip), %xmm0     # 0xb860
leaq 0x28b9(%rip), %rdi      # 0xc191
movb $0x1, %al
callq 0x2100
movq %rbx, %rax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %r15
movq 0x10(%rsp), %r14
jmp 0x992f
movq 0x66eb(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x279a(%rip), %rsi      # 0xc0a9
xorl %ebx, %ebx
leaq 0xb0(%rsp), %rdx
xorl %eax, %eax
callq 0x2170
jmp 0x98df
movl $0x8, %ebp
xorl %r15d, %r15d
movq 0x18(%rsp), %r13
movq 0x66ba(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x68f0(%rip), %rax      # 0x10230
movq (%rax,%rbp,8), %rcx
leaq 0x2816(%rip), %rsi      # 0xc161
xorl %ebx, %ebx
leaq 0xb0(%rsp), %rdx
xorl %eax, %eax
callq 0x2170
jmp 0x97ea
leaq 0xabe(%rip), %rdi       # 0xa426
callq 0x2260
jmp 0x9175
callq 0x2a1e
xorl %ebx, %ebx
jmp 0x98df
leaq 0x29c3(%rip), %rdi      # 0xc348
leaq 0x1173(%rip), %rsi      # 0xaaff
leaq 0x11a1(%rip), %rcx      # 0xab34
movl $0x69, %edx
callq 0x2110