// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x2e0, %rsp            # imm = 0x2E0
movq %rdi, -0x10(%rbp)
callq 0x20a0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0xccc7
leaq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0x455e(%rip), %rsi      # 0x1121c
movb $0x0, %al
callq 0x2280
jmp 0xcd02
leaq -0xf0(%rbp), %rdi
movl $0x64, %esi
callq 0x2260
cmpl $0x0, %eax
je 0xcce9
leaq 0x32de(%rip), %rdi      # 0xffc2
callq 0x2230
leaq -0x80(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
leaq 0x372b(%rip), %rsi      # 0x10426
movb $0x0, %al
callq 0x2280
leaq -0x80(%rbp), %rdi
leaq 0x44f8(%rip), %rsi      # 0x11205
callq 0x2210
movq %rax, -0xf8(%rbp)
cmpq $0x0, -0xf8(%rbp)
jne 0xcda0
leaq -0x80(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
leaq 0x44ea(%rip), %rsi      # 0x1121f
movb $0x0, %al
callq 0x2280
leaq -0x80(%rbp), %rdi
leaq 0x44be(%rip), %rsi      # 0x11205
callq 0x2210
movq %rax, -0xf8(%rbp)
cmpq $0x0, -0xf8(%rbp)
jne 0xcd82
movq 0x8294(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
leaq 0x44c9(%rip), %rsi      # 0x11237
movb $0x0, %al
callq 0x2140
movq $0x0, -0x8(%rbp)
jmp 0xd5c9
movq 0x8247(%rip), %rax      # 0x14fd0
movq (%rax), %rdi
leaq -0x80(%rbp), %rdx
leaq 0x44c4(%rip), %rsi      # 0x1125b
movb $0x0, %al
callq 0x2140
jmp 0xcda0
movl $0x8, -0xfc(%rbp)
movq -0xf8(%rbp), %rdi
leaq -0x180(%rbp), %rdx
leaq -0x180(%rbp), %rcx
addq $0x1e, %rcx
leaq -0x180(%rbp), %r9
addq $0x3c, %r9
leaq -0x180(%rbp), %r10
addq $0x5a, %r10
leaq 0x4493(%rip), %rsi      # 0x11273
leaq -0x100(%rbp), %r8
leaq -0x104(%rbp), %r11
leaq -0x108(%rbp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movb $0x0, %al
callq 0x2050
movl %eax, -0x184(%rbp)
cmpl $0x7, -0x184(%rbp)
je 0xce42
movq 0x81d8(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
leaq -0x80(%rbp), %rdx
leaq 0x445a(%rip), %rsi      # 0x11288
movb $0x0, %al
callq 0x2140
movq $0x0, -0x8(%rbp)
jmp 0xd5c9
movl -0x100(%rbp), %eax
movl %eax, %edi
movl -0x100(%rbp), %eax
movl %eax, %esi
movl $0x8, %edx
callq 0xcaa0
movq %rax, -0x190(%rbp)
movl -0x100(%rbp), %eax
movl %eax, %edi
movl -0x100(%rbp), %eax
movl %eax, %esi
movl $0x8, %edx
callq 0xcb60
movq %rax, -0x198(%rbp)
movl -0x100(%rbp), %eax
movl %eax, %edi
movl -0x104(%rbp), %eax
movl %eax, %esi
movl $0x8, %edx
callq 0xcaa0
movq %rax, -0x1a0(%rbp)
movl -0x100(%rbp), %eax
movl %eax, %edi
movl -0x104(%rbp), %eax
movl %eax, %esi
movl $0x8, %edx
callq 0xcaa0
movq %rax, -0x1a8(%rbp)
movl -0x100(%rbp), %eax
movl %eax, %edi
movl -0x104(%rbp), %eax
movl %eax, %esi
movl $0x8, %edx
callq 0xcaa0
movq %rax, -0x1b0(%rbp)
movl -0x100(%rbp), %eax
movl %eax, %edi
movl -0x104(%rbp), %eax
movl %eax, %esi
movl $0x8, %edx
callq 0xcaa0
movq %rax, -0x1b8(%rbp)
movq $0x0, -0x1c0(%rbp)
movq $0x0, -0x1c8(%rbp)
movl $0x8, %edi
movl $0x1, %esi
callq 0xd5e0
movq %rax, -0x1d0(%rbp)
movl $0x1, -0x1d4(%rbp)
movl $0x0, -0x1d8(%rbp)
movl -0x1d8(%rbp), %eax
movl -0x100(%rbp), %ecx
imull -0x100(%rbp), %ecx
cmpl %ecx, %eax
jae 0xcfd1
movq -0xf8(%rbp), %rdi
leaq 0x4354(%rip), %rsi      # 0x112c1
leaq -0x1dc(%rbp), %rdx
leaq -0x1e0(%rbp), %rcx
leaq -0x1e4(%rbp), %r8
movb $0x0, %al
callq 0x2050
cmpl $0x3, %eax
je 0xcf9a
movl $0x0, -0x1d4(%rbp)
jmp 0xcfd1
movl -0x1e4(%rbp), %eax
movl %eax, %edx
movq -0x190(%rbp), %rax
movl -0x1dc(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x1e0(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x1d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d8(%rbp)
jmp 0xcf48
jmp 0xcfd3
cmpl $0x0, -0x1d4(%rbp)
je 0xd1d4
movq -0xf8(%rbp), %rdi
leaq -0x270(%rbp), %rdx
leaq 0x42d5(%rip), %rsi      # 0x112ca
leaq -0x274(%rbp), %rcx
movb $0x0, %al
callq 0x2050
cmpl $0x2, %eax
jne 0xd1cd
leaq -0x270(%rbp), %rdi
callq 0xd640
movl %eax, -0xfc(%rbp)
movq -0x1d0(%rbp), %rax
movl -0xfc(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
je 0xd060
movq 0x7fc0(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
movl -0xfc(%rbp), %eax
movl %eax, %ecx
leaq 0x81f6(%rip), %rax      # 0x15240
movq (%rax,%rcx,8), %rdx
leaq -0x80(%rbp), %rcx
leaq 0x4277(%rip), %rsi      # 0x112d0
movb $0x0, %al
callq 0x2140
movq -0x1d0(%rbp), %rax
movl -0xfc(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movl -0xfc(%rbp), %eax
movq %rax, -0x2b8(%rbp)
subq $0x8, %rax
ja 0xd1cb
movq -0x2b8(%rbp), %rax
leaq 0x40d6(%rip), %rcx      # 0x1116c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xf8(%rbp), %rdi
movl -0x100(%rbp), %esi
movl -0x104(%rbp), %edx
movq -0x198(%rbp), %rcx
callq 0xd6a0
movl %eax, -0x1d4(%rbp)
jmp 0xd1cb
movq -0xf8(%rbp), %rdi
movl -0x104(%rbp), %esi
movq -0x1a0(%rbp), %rdx
callq 0xd730
movl %eax, -0x1d4(%rbp)
jmp 0xd1cb
movq -0xf8(%rbp), %rdi
movl -0x104(%rbp), %esi
movq -0x1a8(%rbp), %rdx
callq 0xd730
movl %eax, -0x1d4(%rbp)
jmp 0xd1cb
movq -0xf8(%rbp), %rdi
movl -0x104(%rbp), %esi
movq -0x1b0(%rbp), %rdx
callq 0xd730
movl %eax, -0x1d4(%rbp)
jmp 0xd1cb
movq -0xf8(%rbp), %rdi
movl -0x104(%rbp), %esi
movq -0x1b8(%rbp), %rdx
callq 0xd730
movl %eax, -0x1d4(%rbp)
jmp 0xd1cb
movq -0x1c8(%rbp), %rdi
movq -0xf8(%rbp), %rsi
movl -0x274(%rbp), %edx
callq 0xd7c0
movq %rax, -0x1c8(%rbp)
cmpq $0x0, -0x1c8(%rbp)
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x1d4(%rbp)
jmp 0xd1cb
movl -0x104(%rbp), %edi
callq 0xb700
movq %rax, -0x1c0(%rbp)
movq -0xf8(%rbp), %rdi
movl -0x104(%rbp), %esi
movq -0x1c0(%rbp), %rdx
callq 0xd900
movl %eax, -0x1d4(%rbp)
jmp 0xd1cb
movl $0x0, -0x1d4(%rbp)
jmp 0xd1cf
jmp 0xd1d4
jmp 0xcfd3
movq $0x0, -0x280(%rbp)
cmpl $0x0, -0x1d4(%rbp)
je 0xd3d1
movq -0x1d0(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
je 0xd20b
movq -0x198(%rbp), %rax
movq %rax, -0x2c0(%rbp)
jmp 0xd216
xorl %eax, %eax
movq %rax, -0x2c0(%rbp)
jmp 0xd216
movq -0x2c0(%rbp), %rax
movq %rax, -0x288(%rbp)
movq -0x190(%rbp), %rdi
movl -0x100(%rbp), %eax
movl %eax, %esi
movq -0x288(%rbp), %rdx
movl -0x104(%rbp), %ecx
movl -0x108(%rbp), %r9d
xorl %eax, %eax
movl %eax, %r8d
callq 0x8af0
movq %rax, -0x280(%rbp)
movq -0x1d0(%rbp), %rax
movzbl 0x1(%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x2c1(%rbp)
je 0xd2b7
movq -0x1d0(%rbp), %rax
movzbl 0x2(%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x2c1(%rbp)
je 0xd2b7
movq -0x1d0(%rbp), %rax
movzbl 0x3(%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x2c1(%rbp)
je 0xd2b7
movq -0x1d0(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x2c1(%rbp)
movb -0x2c1(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x28c(%rbp)
movq -0x1d0(%rbp), %rax
movzbl 0x1(%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x2c2(%rbp)
je 0xd327
movq -0x1d0(%rbp), %rax
movzbl 0x2(%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x2c2(%rbp)
je 0xd327
movq -0x1d0(%rbp), %rax
movzbl 0x3(%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x2c2(%rbp)
je 0xd327
movq -0x1d0(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x2c2(%rbp)
movb -0x2c2(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x290(%rbp)
cmpl $0x0, -0x28c(%rbp)
je 0xd36b
movq -0x280(%rbp), %rdi
movq -0x1a0(%rbp), %rsi
movq -0x1a8(%rbp), %rdx
movq -0x1b0(%rbp), %rcx
movq -0x1b8(%rbp), %r8
callq 0xb1a0
jmp 0xd392
cmpl $0x0, -0x290(%rbp)
je 0xd390
movq 0x7c7d(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
leaq -0x80(%rbp), %rdx
leaq 0x3f77(%rip), %rsi      # 0x11300
movb $0x0, %al
callq 0x2140
jmp 0xd392
cmpq $0x0, -0x1c8(%rbp)
je 0xd3af
movq -0x280(%rbp), %rdi
movq -0x1c8(%rbp), %rsi
callq 0xb180
movq -0x1d0(%rbp), %rax
cmpb $0x0, 0x6(%rax)
je 0xd3cf
movq -0x280(%rbp), %rdi
movq -0x1c0(%rbp), %rsi
callq 0xb820
jmp 0xd400
movq 0x7c20(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
leaq -0x80(%rbp), %rdx
movl -0xfc(%rbp), %eax
movl %eax, %ecx
leaq 0x7e52(%rip), %rax      # 0x15240
movq (%rax,%rcx,8), %rcx
leaq 0x3f47(%rip), %rsi      # 0x11340
movb $0x0, %al
callq 0x2140
movq -0x190(%rbp), %rdi
movl -0x100(%rbp), %eax
movl %eax, %esi
callq 0xcc30
movq -0x198(%rbp), %rdi
movl -0x100(%rbp), %eax
movl %eax, %esi
callq 0xcc30
movq -0x1a0(%rbp), %rdi
movl -0x100(%rbp), %eax
movl %eax, %esi
callq 0xcc30
movq -0x1a8(%rbp), %rdi
movl -0x100(%rbp), %eax
movl %eax, %esi
callq 0xcc30
movq -0x1b0(%rbp), %rdi
movl -0x100(%rbp), %eax
movl %eax, %esi
callq 0xcc30
movq -0x1b8(%rbp), %rdi
movl -0x100(%rbp), %eax
movl %eax, %esi
callq 0xcc30
cmpq $0x0, -0x1c0(%rbp)
je 0xd494
movq -0x1c0(%rbp), %rdi
movl -0x104(%rbp), %esi
callq 0xb7a0
movq -0xf8(%rbp), %rdi
callq 0x20b0
movq -0x1d0(%rbp), %rdi
callq 0x2030
movq -0x280(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0xd57e
movl $0x0, -0x294(%rbp)
movl -0x294(%rbp), %eax
movq -0x280(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xd57c
movq -0x280(%rbp), %rax
movq 0x40(%rax), %rax
movl -0x294(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x2a0(%rbp)
movq -0x2a0(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x2a8(%rbp)
movq -0x2a8(%rbp), %rax
movl 0x6c(%rax), %eax
movl %eax, -0x2c8(%rbp)
movq -0x2a0(%rbp), %rax
movl (%rax), %edi
callq 0x2220
movl %eax, %ecx
movl -0x2c8(%rbp), %eax
cmpl %ecx, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0xd566
movq -0x2a0(%rbp), %rax
movl $0x1, 0x28(%rax)
movq -0x2a8(%rbp), %rax
movl $0x1, 0x68(%rax)
jmp 0xd568
movl -0x294(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x294(%rbp)
jmp 0xd4c7
jmp 0xd57e
callq 0x20a0
subq -0x18(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x1a84(%rip), %xmm1     # 0xf018
divsd %xmm1, %xmm0
movsd %xmm0, -0x2b0(%rbp)
movsd 0x3488(%rip), %xmm0     # 0x10a30
mulsd -0x2b0(%rbp), %xmm0
leaq 0x3db9(%rip), %rdi      # 0x11370
movb $0x1, %al
callq 0x20d0
movq -0x280(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x2e0, %rsp            # imm = 0x2E0
popq %rbp
retq
nopw %cs:(%rax,%rax)