// File: kobanium[P]Ray/src/mcts/Rating.cpp
// Function: UpdateAroundPreviousMove(game_info_t*, int, long long*, long long*, long long*, int*, bool*, int)
static void
UpdateAroundPreviousMove( game_info_t *game, int color, long long *sum_rate, long long *sum_rate_row, long long *rate, int *update, bool *flag, int index )
{
  double bias[4] = {1.0, 1.0, 1.0, 1.0};

  // 盤端での特殊処理
  if (index == 1) {
    const int pos = game->record[game->moves - 1].pos;
    if ((border_dis_x[pos] == 1 && border_dis_y[pos] == 2) ||
        (border_dis_x[pos] == 2 && border_dis_y[pos] == 1)) {
      for (int i = 0; i < 4; i++) {
        if ((border_dis_x[update[i]] == 1 && border_dis_y[update[i]] == 2) ||
            (border_dis_x[update[i]] == 2 && border_dis_y[update[i]] == 1)) {
          bias[i] = 1000.0;
          break;
        }
      }
    }
  }

  for (int i = 0; i < 4; i++) {
    const int pos = update[i];
    if (game->candidates[pos]){
      if (flag[pos] && bias[i] == 1.0) continue;
      const bool self_atari_flag = CheckSelfAtariForSimulation(game, color, pos);

      // 元あったレートを消去
      *sum_rate -= rate[pos];
      sum_rate_row[board_y[pos]] -= rate[pos];

      if (!self_atari_flag){
        rate[pos] = 0;
      } else {
        CheckCaptureAndAtariForSimulation(game, color, pos);
        const double gamma = po_pattern[MD2(game->pat, pos)]
          * po_previous_distance[index]
          * CalculateTacticalFeatures(&game->tactical_features[pos * ALL_MAX])
          * bias[i];
        rate[pos] = static_cast<long long>(gamma) + 1;

        // 新たに計算したレートを代入
        *sum_rate += rate[pos];
        sum_rate_row[board_y[pos]] += rate[pos];
      }
    }
    ClearTacticalFeatures(&game->tactical_features[pos * ALL_MAX]);
    flag[pos] = true;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq 0x2b340(%rip), %rax     # 0xaf0f0
movq %rax, -0x50(%rbp)
movq 0x2b33d(%rip), %rax     # 0xaf0f8
movq %rax, -0x48(%rbp)
movq 0x2b33a(%rip), %rax     # 0xaf100
movq %rax, -0x40(%rbp)
movq 0x2b337(%rip), %rax     # 0xaf108
movq %rax, -0x38(%rbp)
cmpl $0x1, 0x18(%rbp)
jne 0x83ee9
movq -0x8(%rbp), %rax
movq -0x8(%rbp), %rcx
movl 0x43b0(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x54(%rbp)
movslq -0x54(%rbp), %rcx
leaq 0x1ab4ca55(%rip), %rax  # 0x1abd0860
cmpl $0x1, (%rax,%rcx,4)
jne 0x83e22
movslq -0x54(%rbp), %rcx
leaq 0x1ab4d774(%rip), %rax  # 0x1abd1590
cmpl $0x2, (%rax,%rcx,4)
je 0x83e4c
movslq -0x54(%rbp), %rcx
leaq 0x1ab4ca33(%rip), %rax  # 0x1abd0860
cmpl $0x2, (%rax,%rcx,4)
jne 0x83ee7
movslq -0x54(%rbp), %rcx
leaq 0x1ab4d74e(%rip), %rax  # 0x1abd1590
cmpl $0x1, (%rax,%rcx,4)
jne 0x83ee7
movl $0x0, -0x58(%rbp)
cmpl $0x4, -0x58(%rbp)
jge 0x83ee5
movq -0x30(%rbp), %rax
movslq -0x58(%rbp), %rcx
movslq (%rax,%rcx,4), %rcx
leaq 0x1ab4c9f0(%rip), %rax  # 0x1abd0860
cmpl $0x1, (%rax,%rcx,4)
jne 0x83e8f
movq -0x30(%rbp), %rax
movslq -0x58(%rbp), %rcx
movslq (%rax,%rcx,4), %rcx
leaq 0x1ab4d707(%rip), %rax  # 0x1abd1590
cmpl $0x2, (%rax,%rcx,4)
je 0x83ec1
movq -0x30(%rbp), %rax
movslq -0x58(%rbp), %rcx
movslq (%rax,%rcx,4), %rcx
leaq 0x1ab4c9be(%rip), %rax  # 0x1abd0860
cmpl $0x2, (%rax,%rcx,4)
jne 0x83ed5
movq -0x30(%rbp), %rax
movslq -0x58(%rbp), %rcx
movslq (%rax,%rcx,4), %rcx
leaq 0x1ab4d6d5(%rip), %rax  # 0x1abd1590
cmpl $0x1, (%rax,%rcx,4)
jne 0x83ed5
movslq -0x58(%rbp), %rax
movsd 0x2b1fb(%rip), %xmm0    # 0xaf0c8
movsd %xmm0, -0x50(%rbp,%rax,8)
jmp 0x83ee5
jmp 0x83ed7
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x83e53
jmp 0x83ee7
jmp 0x83ee9
movl $0x0, -0x5c(%rbp)
cmpl $0x4, -0x5c(%rbp)
jge 0x840c4
movq -0x30(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movslq -0x60(%rbp), %rcx
testb $0x1, 0x96ce0(%rax,%rcx)
je 0x84091
movq 0x10(%rbp), %rax
movslq -0x60(%rbp), %rcx
testb $0x1, (%rax,%rcx)
je 0x83f4b
movslq -0x5c(%rbp), %rax
movsd -0x50(%rbp,%rax,8), %xmm0
movsd 0x280ca(%rip), %xmm1    # 0xac008
ucomisd %xmm1, %xmm0
jne 0x83f4b
jp 0x83f4b
jmp 0x840b6
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x60(%rbp), %edx
callq 0xa2830
andb $0x1, %al
movb %al, -0x61(%rbp)
movq -0x28(%rbp), %rax
movslq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x18(%rbp), %rax
movq (%rax), %rcx
subq %rdx, %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movslq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x20(%rbp), %rax
movslq -0x60(%rbp), %rdx
leaq 0x1aafbb9d(%rip), %rcx  # 0x1ab7fb30
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rcx,8), %rdx
subq %rsi, %rdx
movq %rdx, (%rax,%rcx,8)
testb $0x1, -0x61(%rbp)
jne 0x83fbd
movq -0x28(%rbp), %rax
movslq -0x60(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0x8408f
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x60(%rbp), %edx
callq 0xa3180
movq -0x8(%rbp), %rdi
addq $0x4748, %rdi           # imm = 0x4748
movl -0x60(%rbp), %esi
callq 0x93d60
movl %eax, %eax
movl %eax, %ecx
leaq 0x67eb6(%rip), %rax     # 0xebea0
movss (%rax,%rcx,4), %xmm0
movslq 0x18(%rbp), %rcx
leaq 0x4067ea6(%rip), %rax   # 0x40ebea0
mulss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x8(%rbp), %rdi
addq $0x97372, %rdi          # imm = 0x97372
imull $0x6, -0x60(%rbp), %eax
cltq
addq %rax, %rdi
callq 0x85150
movaps %xmm0, %xmm1
movsd -0x78(%rbp), %xmm0
mulsd %xmm1, %xmm0
movslq -0x5c(%rbp), %rax
mulsd -0x50(%rbp,%rax,8), %xmm0
movsd %xmm0, -0x70(%rbp)
cvttsd2si -0x70(%rbp), %rdx
addq $0x1, %rdx
movq -0x28(%rbp), %rax
movslq -0x60(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movslq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x18(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movslq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x20(%rbp), %rax
movslq -0x60(%rbp), %rsi
leaq 0x1aafbaad(%rip), %rcx  # 0x1ab7fb30
movslq (%rcx,%rsi,4), %rcx
addq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x84091
movq -0x8(%rbp), %rdi
addq $0x97372, %rdi          # imm = 0x97372
imull $0x6, -0x60(%rbp), %eax
cltq
addq %rax, %rdi
callq 0x79350
movq 0x10(%rbp), %rax
movslq -0x60(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x83ef0
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)