// File: /workspace/github_c_makefile_star2_O2/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 0x85eb
leaq 0xb0(%rsp), %rdi
movq %rbx, %rsi
callq 0x2050
jmp 0x8621
leaq 0x1a0(%rsp), %rdi
pushq $0x64
popq %rsi
callq 0x2290
testl %eax, %eax
jne 0x8d53
leaq 0x2162(%rip), %rsi      # 0xa76c
leaq 0xb0(%rsp), %rdi
leaq 0x1a0(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
leaq 0x3a01(%rip), %rsi      # 0xc029
leaq 0xb0(%rsp), %rdi
callq 0x2240
movq %rax, %r14
testq %rax, %rax
jne 0x8699
leaq 0x39fc(%rip), %rsi      # 0xc040
leaq 0xb0(%rsp), %rbx
leaq 0x1a0(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x22b0
leaq 0x39c4(%rip), %rsi      # 0xc029
movq %rbx, %rdi
callq 0x2240
testq %rax, %rax
je 0x8d64
movq %rax, %r14
movq 0x7948(%rip), %rax      # 0xffc8
movq (%rax), %rdi
leaq 0x39f2(%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 0x39ce(%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 0x8cf5
movq %r15, 0x98(%rsp)
movl 0x8(%rsp), %edi
pushq $0x8
popq %rbx
movq %rdi, %rsi
movq %rbx, %rdx
callq 0x84a4
movq %rax, 0x50(%rsp)
movl 0x8(%rsp), %edi
movq %rdi, %rsi
movq %rbx, %rdx
callq 0x851b
movq %rax, 0x58(%rsp)
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
movq %rbx, %rdx
callq 0x84a4
movq %rax, 0x48(%rsp)
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
movq %rbx, %rdx
callq 0x84a4
movq %rax, 0x40(%rsp)
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
movq %rbx, %rdx
callq 0x84a4
movq %rax, 0x38(%rsp)
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
movq %rbx, %rdx
callq 0x84a4
movq %rax, 0x30(%rsp)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x2150
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x8d87
pushq $0x1
popq %r15
leaq 0x290(%rsp), %r12
leaq 0x210(%rsp), %r13
leaq 0x70(%rsp), %rbp
xorl %ebx, %ebx
movl 0x8(%rsp), %eax
imull %eax, %eax
cmpl %eax, %ebx
jae 0x87fa
movq %r14, %rdi
leaq 0x3923(%rip), %rsi      # 0xc0e2
movq %r12, %rdx
movq %r13, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x2070
cmpl $0x3, %eax
jne 0x87f7
movl 0x70(%rsp), %eax
movl 0x290(%rsp), %ecx
movq 0x50(%rsp), %rdx
movq (%rdx,%rcx,8), %rcx
movl 0x210(%rsp), %edx
movq %rax, (%rcx,%rdx,8)
incl %ebx
jmp 0x87aa
xorl %r15d, %r15d
pushq $0x8
popq %rax
leaq 0x210(%rsp), %r12
movq $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movl %eax, %r13d
movq 0x10(%rsp), %rbp
testl %r15d, %r15d
je 0x8d19
movq %r14, %rdi
leaq 0x38b9(%rip), %rsi      # 0xc0eb
movq %r12, %rdx
leaq 0x68(%rsp), %rcx
xorl %eax, %eax
callq 0x2070
cmpl $0x2, %eax
jne 0x8b30
leaq 0x79df(%rip), %rbx      # 0x10230
xorl %r13d, %r13d
cmpq $0x8, %r13
je 0x8872
movq (%rbx), %rsi
movq %r12, %rdi
callq 0x2160
testl %eax, %eax
je 0x8878
incq %r13
addq $0x8, %rbx
jmp 0x8854
pushq $0x8
popq %rax
movl %eax, %r13d
cmpl $0x7, %r13d
pushq $0x7
popq %rax
cmovael %eax, %r13d
cmpb $0x0, (%rbp,%r13)
jne 0x89d9
movb $0x1, (%rbp,%r13)
movl %r13d, %eax
leaq 0x36f5(%rip), %rcx      # 0xbf94
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0xc(%rsp), %esi
movq %r14, %rdi
movq 0x48(%rsp), %rdx
jmp 0x88de
movl 0xc(%rsp), %esi
movq %r14, %rdi
movq 0x30(%rsp), %rdx
jmp 0x88de
movl 0xc(%rsp), %esi
movq %r14, %rdi
movq 0x40(%rsp), %rdx
jmp 0x88de
movl 0xc(%rsp), %esi
movq %r14, %rdi
movq 0x38(%rsp), %rdx
callq 0x8da4
movl %eax, %r15d
jmp 0x881f
xorl %r15d, %r15d
jmp 0x881f
movl 0x68(%rsp), %ebp
movl %ebp, %edi
callq 0x78f7
movq %rax, %r15
xorl %ebx, %ebx
cmpq %rbx, %rbp
je 0x899a
movq %r14, %rdi
leaq 0x38ad(%rip), %rsi      # 0xc1c3
leaq 0x290(%rsp), %rdx
leaq 0xac(%rsp), %rcx
leaq 0x2f4(%rsp), %r8
leaq 0x70(%rsp), %r9
xorl %eax, %eax
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0x3c4(%rsp), %r10
pushq %r10
leaq 0x70(%rsp), %r10
pushq %r10
leaq 0x370(%rsp), %r10
pushq %r10
callq 0x2070
addq $0x20, %rsp
cmpl $0x8, %eax
jne 0x89b3
movq 0x70(%rsp), %rax
movq 0x8(%r15), %rcx
movq %rax, (%rcx,%rbx,8)
movq 0x60(%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
jmp 0x8903
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
movq 0x10(%rsp), %rbp
je 0x89ae
callq 0x7947
movq %r15, %rax
jmp 0x89c5
movq %r15, %rdi
callq 0x7947
movq 0x10(%rsp), %rbp
movq 0x20(%rsp), %rax
xorl %r15d, %r15d
movq %rax, 0x20(%rsp)
testq %rax, %rax
setne %r15b
jmp 0x881f
movq 0x7610(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x7846(%rip), %rax      # 0x10230
movq (%rax,%r13,8), %rdx
leaq 0x36fc(%rip), %rsi      # 0xc0f1
leaq 0xb0(%rsp), %rcx
xorl %eax, %eax
callq 0x2170
jmp 0x888f
movl 0xc(%rsp), %ebx
imull 0x8(%rsp), %ebx
xorl %ebp, %ebp
cmpl %ebp, %ebx
je 0x8b20
movq %r14, %rdi
leaq 0x36bc(%rip), %rsi      # 0xc0e2
leaq 0x290(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x60(%rsp), %r8
xorl %eax, %eax
callq 0x2070
cmpl $0x3, %eax
jne 0x8b22
movl 0x60(%rsp), %eax
movl 0x290(%rsp), %ecx
movq 0x58(%rsp), %rdx
movq (%rdx,%rcx,8), %rcx
movl 0x70(%rsp), %edx
movq %rax, (%rcx,%rdx,8)
incl %ebp
jmp 0x8a14
movl 0xc(%rsp), %edi
callq 0x7cc4
movq %rax, 0x28(%rsp)
xorl %ebp, %ebp
movl 0xc(%rsp), %eax
movl %eax, 0x1c(%rsp)
cmpl %eax, %ebp
ja 0x8b12
movl %ebp, %r15d
xorl %ebx, %ebx
cmpq $0x6, %rbx
je 0x8b03
movq %r14, %rdi
leaq 0x3741(%rip), %rsi      # 0xc1de
leaq 0x290(%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
popq %rcx
popq %rdx
cmpl $0x6, %eax
jne 0x8b0e
movq 0x28(%rsp), %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r15,8), %rax
xorl %ecx, %ecx
cmpq $0x5, %rcx
je 0x8afe
movsd 0x70(%rsp,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
incq %rcx
jmp 0x8ae8
incq %rbx
jmp 0x8a8d
incl %ebp
movl 0x1c(%rsp), %eax
jmp 0x8a80
movl 0x1c(%rsp), %eax
xorl %r15d, %r15d
cmpl %eax, %ebp
seta %r15b
jmp 0x881a
movl %ebx, %ebp
xorl %r15d, %r15d
cmpl %ebx, %ebp
setae %r15b
jmp 0x881a
xorl %eax, %eax
cmpb $0x0, (%rbp)
movq 0x58(%rsp), %rdx
cmoveq %rax, %rdx
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %ecx
movl 0x6c(%rsp), %r9d
movq 0x50(%rsp), %rdi
xorl %r8d, %r8d
callq 0x6278
movq %rax, %rbx
cmpb $0x0, 0x1(%rbp)
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %r12
je 0x8bd5
cmpb $0x0, 0x2(%rbp)
je 0x8bd5
cmpb $0x0, 0x3(%rbp)
je 0x8b87
movb 0x4(%rbp), %al
cmpb $0x0, 0x3(%rbp)
je 0x8bb5
testb %al, %al
jne 0x8bb9
jmp 0x8b8d
cmpb $0x0, 0x3(%rbp)
je 0x8bd5
cmpb $0x0, 0x4(%rbp)
je 0x8bd5
movq 0x7456(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x357d(%rip), %rsi      # 0xc121
leaq 0xb0(%rsp), %rdx
xorl %eax, %eax
callq 0x2170
jmp 0x8bd5
testb %al, %al
je 0x8bd5
movq %rbx, %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %r8
callq 0x7974
testq %r12, %r12
je 0x8be5
movq %rbx, %rdi
movq %r12, %rsi
callq 0x796f
cmpb $0x0, 0x6(%rbp)
je 0x8bf6
movq %rbx, %rdi
movq %r15, %rsi
callq 0x7d87
movl 0x8(%rsp), %esi
movq 0x50(%rsp), %rdi
callq 0x8588
movl 0x8(%rsp), %esi
movq 0x58(%rsp), %rdi
callq 0x8588
movl 0x8(%rsp), %esi
movq 0x48(%rsp), %rdi
callq 0x8588
movl 0x8(%rsp), %esi
movq 0x40(%rsp), %rdi
callq 0x8588
movl 0x8(%rsp), %esi
movq 0x38(%rsp), %rdi
callq 0x8588
movl 0x8(%rsp), %esi
movq 0x30(%rsp), %rdi
callq 0x8588
testq %r15, %r15
je 0x8c5b
movl 0xc(%rsp), %esi
movq %r15, %rdi
callq 0x7d39
movq %r14, %rdi
callq 0x20e0
movq %rbp, %rdi
callq 0x2030
cmpl $0x0, 0x30(%rbx)
je 0x8cb0
xorl %r14d, %r14d
xorl %r15d, %r15d
movl 0x10(%rbx), %eax
cmpq %rax, %r15
jae 0x8cb0
movq 0x40(%rbx), %r12
movq 0x10(%r12,%r14), %r13
movl 0x6c(%r13), %ebp
movl (%r12,%r14), %edi
callq 0x2250
cmpl %eax, %ebp
jne 0x8ca2
incq %r15
addq $0x30, %r14
jmp 0x8c77
pushq $0x1
popq %rax
movl %eax, 0x28(%r12,%r14)
movl %eax, 0x68(%r13)
jmp 0x8c99
callq 0x20d0
subq 0x98(%rsp), %rax
cvtsi2sd %rax, %xmm0
divsd 0x134e(%rip), %xmm0     # 0xa018
mulsd 0x2b8e(%rip), %xmm0     # 0xb860
leaq 0x34b8(%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 0x72f4(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x33a3(%rip), %rsi      # 0xc0a9
xorl %ebx, %ebx
leaq 0xb0(%rsp), %rdx
xorl %eax, %eax
callq 0x2170
jmp 0x8ce0
movq 0x72d0(%rip), %rax      # 0xfff0
movq (%rax), %rdi
movl %r13d, %eax
leaq 0x7503(%rip), %rcx      # 0x10230
movq (%rcx,%rax,8), %rcx
leaq 0x3429(%rip), %rsi      # 0xc161
xorl %ebx, %ebx
leaq 0xb0(%rsp), %rdx
xorl %eax, %eax
callq 0x2170
movq 0x28(%rsp), %r15
jmp 0x8bf6
leaq 0x16cc(%rip), %rdi      # 0xa426
callq 0x2260
jmp 0x8603
movq 0x7285(%rip), %rax      # 0xfff0
movq (%rax), %rcx
leaq 0x32e3(%rip), %rdi      # 0xc058
pushq $0x23
popq %rsi
pushq $0x1
popq %rdx
callq 0x22d0
xorl %ebx, %ebx
jmp 0x8ce0
leaq 0x35ba(%rip), %rdi      # 0xc348
leaq 0x1d6a(%rip), %rsi      # 0xaaff
leaq 0x1d98(%rip), %rcx      # 0xab34
pushq $0x69
popq %rdx
callq 0x2110