// File: /workspace/github_cpp_makefile_star2_O2/kobanium[P]Ray/src/mcts/UctRating.cpp
// Function: CalculateMoveScoreWithBTFM(game_info_t const*, int, unsigned int const*, int)
double
CalculateMoveScoreWithBTFM( const game_info_t *game, const int pos, const unsigned int *tactical_features, const int distance_index )
{
  const int moves = game->moves;
  const int pm1 = (moves > 1) ? game->record[moves - 1].pos : PASS;
  const int pm2 = (moves > 2) ? game->record[moves - 2].pos : PASS;
  const int pm3 = (moves > 3) ? game->record[moves - 3].pos : PASS;
  const int pm4 = (moves > 4) ? game->record[moves - 4].pos : PASS;
  int dis = 0;
  std::vector<fm_t*> active_features;
  pattern_hash_t hash_pat;

  if (pos == PASS) {
    if (moves > 1 && pm1 == PASS) {
      active_features.push_back(&uct_pass[UCT_PASS_AFTER_PASS]);
    } else {
      active_features.push_back(&uct_pass[UCT_PASS_AFTER_MOVE]);
    }
  } else {
    if (pm1 != PASS) {
      dis = DIS(pos, pm1);
      if (dis >= MOVE_DISTANCE_MAX - 1) {
        dis = MOVE_DISTANCE_MAX - 1;
      }
      active_features.push_back(&uct_move_distance_1[dis + distance_index]);
    }

    if (pm2 != PASS) {
      dis = DIS(pos, pm2);
      if (dis >= MOVE_DISTANCE_MAX - 1) {
        dis = MOVE_DISTANCE_MAX - 1;
      }
      active_features.push_back(&uct_move_distance_2[dis + distance_index]);
    }

    if (pm3 != PASS) {
      dis = DIS(pos, pm3);
      if (dis >= MOVE_DISTANCE_MAX - 1) {
        dis = MOVE_DISTANCE_MAX - 1;
      }
      active_features.push_back(&uct_move_distance_3[dis + distance_index]);
    }

    if (pm4 != PASS) {
      dis = DIS(pos, pm4);
      if (dis >= MOVE_DISTANCE_MAX - 1) {
        dis = MOVE_DISTANCE_MAX - 1;
      }
      active_features.push_back(&uct_move_distance_4[dis + distance_index]);
    }

    const unsigned int *features = &tactical_features[pos * UCT_INDEX_MAX];

    if (features[UCT_CAPTURE_INDEX]        > 0) active_features.push_back(&uct_capture[features[UCT_CAPTURE_INDEX]]);
    if (features[UCT_SAVE_EXTENSION_INDEX] > 0) active_features.push_back(&uct_save_extension[features[UCT_SAVE_EXTENSION_INDEX]]);
    if (features[UCT_ATARI_INDEX]          > 0) active_features.push_back(&uct_atari[features[UCT_ATARI_INDEX]]);
    if (features[UCT_EXTENSION_INDEX]      > 0) active_features.push_back(&uct_extension[features[UCT_EXTENSION_INDEX]]);
    if (features[UCT_DAME_INDEX]           > 0) active_features.push_back(&uct_dame[features[UCT_DAME_INDEX]]);
    if (features[UCT_CONNECT_INDEX]        > 0) active_features.push_back(&uct_connect[features[UCT_CONNECT_INDEX]]);
    if (features[UCT_THROW_IN_INDEX]       > 0) active_features.push_back(&uct_throw_in[features[UCT_THROW_IN_INDEX]]);

    PatternHash(&game->pat[pos], &hash_pat);
    const int pat3_idx = Pat3(game->pat, pos);
    const int md2_idx = md2_index[MD2(game->pat, pos)];
    const int md3_idx = SearchIndex(md3_index, hash_pat.list[MD_3]);
    const int md4_idx = SearchIndex(md4_index, hash_pat.list[MD_4]);
    const int md5_idx = SearchIndex(md5_index, hash_pat.list[MD_5 + MD_MAX]);

    // 盤上の位置
    active_features.push_back(&uct_pos_id[board_pos_id[pos]]);

    // パターン
    if (md5_idx != -1) {
      active_features.push_back(&uct_md5[md5_idx]);
    } else if (md4_idx != -1) {
      active_features.push_back(&uct_md4[md4_idx]);
    } else if (md3_idx != -1) {
      active_features.push_back(&uct_md3[md3_idx]);
    } else if (md2_idx != -1) {
      active_features.push_back(&uct_md2[md2_idx]);
    } else {
      active_features.push_back(&uct_pat3[pat3_idx]);
    }
  }

  if (moves > 1 && game->ko_move == game->moves - 1) {
    active_features.push_back(&uct_ko_exist);
  }

  const int feature_num = active_features.size();
  const double gamma = Gamma(active_features);
  double theta = feature_num > 1 ? 1.0 : 0.0;

  for (int i = 0; i < feature_num - 1; i++) {
    for (int j = i + 1; j < feature_num; j++) {
      theta *= Theta(active_features, i, j);
    }
  }

  return gamma + theta;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %r8d
movq %rdx, %r14
movq %rdi, %rbx
movl 0x43b0(%rdi), %r13d
cmpl $0x2, %r13d
jl 0x31bb2
leal -0x1(%r13), %eax
shlq $0x4, %rax
movl 0x4(%rbx,%rax), %eax
cmpl $0x2, %r13d
jne 0x31fed
movl $0x0, 0x14(%rsp)
xorl %r12d, %r12d
jmp 0x31bbf
movl $0x0, 0x14(%rsp)
xorl %r12d, %r12d
xorl %eax, %eax
xorl %r15d, %r15d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
testl %esi, %esi
je 0x31fbd
movq %r13, 0x40(%rsp)
movq %rbx, 0x18(%rsp)
movl %esi, 0x3c(%rsp)
movslq %esi, %r13
pushq $0xf
popq %rbx
testl %eax, %eax
movl %r8d, %ebp
je 0x31c5d
leaq 0x1aac7084(%rip), %rcx  # 0x1aaf8c80
movl (%rcx,%r13,4), %edx
cltq
subl (%rcx,%rax,4), %edx
movl %edx, %ecx
negl %ecx
cmovsl %edx, %ecx
leaq 0x1aac7d9d(%rip), %rdx  # 0x1aaf99b0
movl (%rdx,%r13,4), %esi
subl (%rdx,%rax,4), %esi
movl %esi, %eax
negl %eax
cmovsl %esi, %eax
imulq $0x4c, %rcx, %rcx
addq 0x2f35c(%rip), %rcx     # 0x60f88
movl (%rcx,%rax,4), %eax
cmpl $0xf, %eax
cmovgel %ebx, %eax
addl %r8d, %eax
cltq
imulq $0x30, %rax, %rax
leaq 0x8074beb(%rip), %rcx   # 0x80a6830
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
movl %ebp, %r8d
testl %r15d, %r15d
je 0x31ccb
leaq 0x1aac7017(%rip), %rax  # 0x1aaf8c80
movl (%rax,%r13,4), %ecx
movslq %r15d, %rdx
subl (%rax,%rdx,4), %ecx
movl %ecx, %eax
negl %eax
cmovsl %ecx, %eax
leaq 0x1aac7d2f(%rip), %rcx  # 0x1aaf99b0
movl (%rcx,%r13,4), %esi
subl (%rcx,%rdx,4), %esi
movl %esi, %ecx
negl %ecx
cmovsl %esi, %ecx
imulq $0x4c, %rax, %rax
addq 0x2f2ee(%rip), %rax     # 0x60f88
movl (%rax,%rcx,4), %eax
cmpl $0xf, %eax
cmovgel %ebx, %eax
addl %r8d, %eax
cltq
imulq $0x30, %rax, %rax
leaq 0x807577d(%rip), %rcx   # 0x80a7430
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
movl %ebp, %r8d
testl %r12d, %r12d
je 0x31d39
leaq 0x1aac6fa9(%rip), %rax  # 0x1aaf8c80
movl (%rax,%r13,4), %ecx
movslq %r12d, %rdx
subl (%rax,%rdx,4), %ecx
movl %ecx, %eax
negl %eax
cmovsl %ecx, %eax
leaq 0x1aac7cc1(%rip), %rcx  # 0x1aaf99b0
movl (%rcx,%r13,4), %esi
subl (%rcx,%rdx,4), %esi
movl %esi, %ecx
negl %ecx
cmovsl %esi, %ecx
imulq $0x4c, %rax, %rax
addq 0x2f280(%rip), %rax     # 0x60f88
movl (%rax,%rcx,4), %eax
cmpl $0xf, %eax
cmovgel %ebx, %eax
addl %r8d, %eax
cltq
imulq $0x30, %rax, %rax
leaq 0x807630f(%rip), %rcx   # 0x80a8030
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
movl %ebp, %r8d
movl 0x14(%rsp), %edx
testl %edx, %edx
movl 0x3c(%rsp), %ebp
je 0x31dac
leaq 0x1aac6f34(%rip), %rax  # 0x1aaf8c80
movl (%rax,%r13,4), %ecx
movslq %edx, %rdx
subl (%rax,%rdx,4), %ecx
movl %ecx, %eax
negl %eax
cmovsl %ecx, %eax
leaq 0x1aac7c4c(%rip), %rcx  # 0x1aaf99b0
movl (%rcx,%r13,4), %esi
subl (%rcx,%rdx,4), %esi
movl %esi, %ecx
negl %ecx
cmovsl %esi, %ecx
imulq $0x4c, %rax, %rax
addq 0x2f20b(%rip), %rax     # 0x60f88
movl (%rax,%rcx,4), %eax
cmpl $0xf, %eax
cmovll %eax, %ebx
addl %r8d, %ebx
movslq %ebx, %rax
imulq $0x30, %rax, %rax
leaq 0x8076e99(%rip), %rcx   # 0x80a8c30
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
imull $0x7, %ebp, %eax
movslq %eax, %rbx
movl (%r14,%rbx,4), %eax
testq %rax, %rax
je 0x31ddb
imulq $0x30, %rax, %rax
leaq 0x8077a6a(%rip), %rcx   # 0x80a9830
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
movl 0x4(%r14,%rbx,4), %eax
testq %rax, %rax
je 0x31e05
imulq $0x30, %rax, %rax
leaq 0x8077f50(%rip), %rcx   # 0x80a9d40
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
movl 0x8(%r14,%rbx,4), %eax
testq %rax, %rax
je 0x31e2f
imulq $0x30, %rax, %rax
leaq 0x8078016(%rip), %rcx   # 0x80a9e30
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
movl 0xc(%r14,%rbx,4), %eax
testq %rax, %rax
je 0x31e59
imulq $0x30, %rax, %rax
leaq 0x807834c(%rip), %rcx   # 0x80aa190
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
movl 0x10(%r14,%rbx,4), %eax
testq %rax, %rax
je 0x31e83
imulq $0x30, %rax, %rax
leaq 0x8078472(%rip), %rcx   # 0x80aa2e0
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
movl 0x14(%r14,%rbx,4), %eax
testq %rax, %rax
je 0x31ead
imulq $0x30, %rax, %rax
leaq 0x8078538(%rip), %rcx   # 0x80aa3d0
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
movl 0x18(%r14,%rbx,4), %eax
testq %rax, %rax
movq 0x18(%rsp), %rbx
je 0x31edc
imulq $0x30, %rax, %rax
leaq 0x8078569(%rip), %rcx   # 0x80aa430
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
imulq $0x18, %r13, %rax
leaq (%rbx,%rax), %rdi
addq $0x4748, %rdi           # imm = 0x4748
leaq 0x48(%rsp), %rsi
callq 0x39038
leaq 0x4748(%rbx), %r15
movq %r15, %rdi
movl %ebp, %esi
callq 0x37bc6
movl %eax, %r14d
movq %r15, %rdi
movl %ebp, %esi
callq 0x37bd2
movl %eax, %eax
leaq 0x8078574(%rip), %rcx   # 0x80aa490
movslq (%rcx,%rax,4), %rbx
movq 0x50(%rsp), %rsi
leaq 0xc078564(%rip), %rdi   # 0xc0aa490
callq 0x3926f
movl %eax, %ebp
movq 0x58(%rsp), %rsi
leaq 0xd078551(%rip), %rdi   # 0xd0aa490
callq 0x3926f
movl %eax, %r15d
movq 0x60(%rsp), %rsi
leaq 0xe07853d(%rip), %rdi   # 0xe0aa490
callq 0x3926f
movl %eax, %r12d
leaq 0x1aac5fee(%rip), %rax  # 0x1aaf7f50
movslq (%rax,%r13,4), %rax
imulq $0x30, %rax, %rax
leaq 0xf07851f(%rip), %rcx   # 0xf0aa490
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
cmpl $-0x1, %r12d
movq 0x40(%rsp), %r13
je 0x32034
movslq %r12d, %rax
imulq $0x30, %rax, %rax
leaq 0xf0790ed(%rip), %rcx   # 0xf0ab090
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
jmp 0x320d5
cmpl $0x2, %r13d
jl 0x3205f
testl %eax, %eax
jne 0x3205f
leaq 0x807482a(%rip), %rax   # 0x80a6800
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
jmp 0x320e0
movl $0x0, 0x14(%rsp)
leal -0x2(%r13), %ecx
shlq $0x4, %rcx
movl 0x4(%rbx,%rcx), %r15d
cmpl $0x4, %r13d
jb 0x3207a
leal -0x3(%r13), %ecx
shlq $0x4, %rcx
movl 0x4(%rbx,%rcx), %r12d
cmpl $0x4, %r13d
je 0x31bc2
leal -0x4(%r13), %ecx
shlq $0x4, %rcx
movl 0x4(%rbx,%rcx), %ecx
movl %ecx, 0x14(%rsp)
jmp 0x31bc2
cmpl $-0x1, %r15d
je 0x32082
movslq %r15d, %rax
imulq $0x30, %rax, %rax
leaq 0x11e3fc48(%rip), %rcx  # 0x11e71c90
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
jmp 0x320d5
leaq 0x807476a(%rip), %rax   # 0x80a67d0
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
jmp 0x320da
xorl %r12d, %r12d
jmp 0x31bc2
cmpl $-0x1, %ebp
je 0x320ac
movslq %ebp, %rax
imulq $0x30, %rax, %rax
leaq 0x14c067fb(%rip), %rcx  # 0x14c38890
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
jmp 0x320d5
cmpl $-0x1, %ebx
je 0x321da
imulq $0x30, %rbx, %rax
leaq 0x179cd3d0(%rip), %rcx  # 0x179ff490
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
movq 0x18(%rsp), %rbx
cmpl $0x2, %r13d
jl 0x32109
movl 0x43b0(%rbx), %eax
decl %eax
cmpl %eax, 0x43c8(%rbx)
jne 0x32109
leaq 0x1aa93f99(%rip), %rax  # 0x1aac6090
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rdx
movq %rdx, %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movsd 0x11ee3(%rip), %xmm4    # 0x44008
movq %rax, %rsi
cmpq %rdx, %rsi
je 0x3213a
movq (%rsi), %rdi
mulsd (%rdi), %xmm4
addq $0x8, %rsi
jmp 0x32128
cmpl $0x2, %ecx
jge 0x32145
xorpd %xmm0, %xmm0
jmp 0x3214d
movsd 0x11ebb(%rip), %xmm0    # 0x44008
pushq $0x1
popq %rdx
cmovgel %ecx, %edx
decl %edx
pushq $0x1
popq %rsi
xorl %edi, %edi
movsd 0x13d8e(%rip), %xmm1    # 0x45ef0
movq %rsi, %r8
cmpq %rdx, %rdi
je 0x321b1
movq %r8, %r9
cmpl %ecx, %r9d
jge 0x321a9
movq (%rax,%rdi,8), %r10
movq (%rax,%r9,8), %r11
xorpd %xmm2, %xmm2
movq %rsi, %rbx
cmpq $0x6, %rbx
je 0x3219c
movsd (%r10,%rbx,8), %xmm3
mulsd (%r11,%rbx,8), %xmm3
addsd %xmm3, %xmm2
incq %rbx
jmp 0x32181
mulsd %xmm1, %xmm2
mulsd %xmm2, %xmm0
incq %r9
jmp 0x3216d
incq %rdi
incq %r8
jmp 0x32165
addsd %xmm0, %xmm4
movsd %xmm4, 0x18(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x3261e
movsd 0x18(%rsp), %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq %r14d, %rax
imulq $0x30, %rax, %rax
leaq 0x1a793ea8(%rip), %rcx  # 0x1a7c6090
addq %rax, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0x32652
movq 0x18(%rsp), %rbx
jmp 0x320da
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
jmp 0x32239
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3261e
movq %rbx, %rdi
callq 0x6740