// File: kobanium[P]Ray/src/mcts/UctRating.cpp
// Function: InputMD2(char const*, fm_t*)
static void
InputMD2( const char *filename, fm_t params[] )
{
  FILE *fp;
  int index, counter = 0;
  unsigned int md2_transp16[16];

  for (unsigned int md2 = 0; md2 < static_cast<unsigned int>(MD2_MAX); md2++) {
    md2_index[md2] = -1;
  }
#if defined (_WIN32)
  errno_t err;

  err = fopen_s(&fp, filename, "r");
  if (err != 0) {
    std::cerr << "can not open -" << filename << "-" << std::endl;
  }

  while (fscanf_s(fp, "%d%le", &index, &params[counter].w) != EOF) {
    for (int i = 0; i < BTFM_DIMENSION; i++) {
      if (fscanf_s(fp, "%le", &params[counter].v[i]) == EOF) {
        std::cerr << "Read Error : " << filename << std::endl;
        exit(1);
      }
    }
    MD2Transpose16(index, md2_transp16);

    for (int i = 0; i < 16; i++) {
      const unsigned int idx = md2_transp16[i];
      md2_index[idx] = counter;
    }
    counter++;
  }
#else
  fp = fopen(filename, "r");
  if (fp == NULL) {
    std::cerr << "can not open -" << filename << "-" << std::endl;
  }

  while (fscanf(fp, "%d%le", &index, &params[counter].w) != EOF) {
    for (int i = 0; i < BTFM_DIMENSION; i++) {
      if (fscanf(fp, "%le", &params[counter].v[i]) == EOF) {
        std::cerr << "Read Error : " << filename << std::endl;
        exit(1);
      }
    }
    MD2Transpose16(index, md2_transp16);

    for (int i = 0; i < 16; i++) {
      const unsigned int idx = md2_transp16[i];
      md2_index[idx] = counter;
    }
    counter++;
  }
#endif
  fclose(fp);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x64(%rbp)
cmpl $0x1000000, -0x64(%rbp) # imm = 0x1000000
jae 0x87025
movl -0x64(%rbp), %eax
movl %eax, %ecx
leaq 0x80a95fd(%rip), %rax   # 0x8130610
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x86ffe
movq -0x8(%rbp), %rdi
leaq 0x264fb(%rip), %rsi     # 0xad52b
callq 0x7680
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x8707d
movq 0x5ffa9(%rip), %rdi     # 0xe6ff0
leaq 0x260b2(%rip), %rsi     # 0xad100
callq 0x7530
movq %rax, %rdi
movq -0x8(%rbp), %rsi
callq 0x7530
movq %rax, %rdi
leaq 0x25092(%rip), %rsi     # 0xac0fb
callq 0x7530
movq %rax, %rdi
movq 0x5fef0(%rip), %rsi     # 0xe6f68
callq 0x75b0
jmp 0x8707f
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rcx
movslq -0x20(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rcx
leaq 0x28082(%rip), %rsi     # 0xaf11b
leaq -0x1c(%rbp), %rdx
movb $0x0, %al
callq 0x7a00
cmpl $-0x1, %eax
je 0x87183
movl $0x0, -0x68(%rbp)
cmpl $0x5, -0x68(%rbp)
jge 0x87134
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rdx
movslq -0x20(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdx
addq $0x8, %rdx
movslq -0x68(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdx
leaq 0x2803a(%rip), %rsi     # 0xaf11d
movb $0x0, %al
callq 0x7a00
cmpl $-0x1, %eax
jne 0x87127
movq 0x5fefa(%rip), %rdi     # 0xe6ff0
leaq 0x26015(%rip), %rsi     # 0xad112
callq 0x7530
movq %rax, %rdi
movq -0x8(%rbp), %rsi
callq 0x7530
movq %rax, %rdi
movq 0x5fe50(%rip), %rsi     # 0xe6f68
callq 0x75b0
movl $0x1, %edi
callq 0x76a0
jmp 0x87129
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x870b4
movl -0x1c(%rbp), %edi
leaq -0x60(%rbp), %rsi
callq 0x93060
movl $0x0, -0x6c(%rbp)
cmpl $0x10, -0x6c(%rbp)
jge 0x87175
movslq -0x6c(%rbp), %rax
movl -0x60(%rbp,%rax,4), %eax
movl %eax, -0x70(%rbp)
movl -0x20(%rbp), %edx
movl -0x70(%rbp), %eax
movl %eax, %ecx
leaq 0x80a94a9(%rip), %rax   # 0x8130610
movl %edx, (%rax,%rcx,4)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x87147
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x8707f
movq -0x18(%rbp), %rdi
callq 0x7540
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)