// File: kobanium[P]Ray/src/mcts/Rating.cpp
// Function: RatingMove(game_info_t*, int, std::mersenne_twister_engine<unsigned long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ul, 29ul, 6148914691236517205ul, 17ul, 8202884508482404352ul, 37ul, 18444473444759240704ul, 43ul, 6364136223846793005ul>&)
int
RatingMove( game_info_t *game, int color, std::mt19937_64 &mt )
{
  long long *rate = game->rate[color - 1];
  long long *sum_rate_row = game->sum_rate_row[color - 1];
  long long *sum_rate = &game->sum_rate[color - 1];
  int y, pos;
  long long rand_num;

  // レートの部分更新
  PartialRating(game, color, sum_rate, sum_rate_row, rate);

  // 合法手を選択するまでループ
  while (true) {
    if (*sum_rate == 0) {
      pos = PASS;
      break;
    }

    rand_num = (mt() % (*sum_rate)) + 1;

    // 縦方向の位置を求める
    y = board_start;
    while (rand_num > sum_rate_row[y]){
      rand_num -= sum_rate_row[y++];
    }

    // 横方向の位置を求める
    pos = POS(board_start, y);
    do {
      rand_num -= rate[pos];
      if (rand_num <= 0) break;
      pos++;
    } while (true);

    // 選ばれた手が合法手ならループを抜け出し
    // そうでなければその箇所のレートを0にし, 手を選びなおす
    if (IsLegalNotEye(game, pos, color)) {
      break;
    } else {
      *sum_rate -= rate[pos];
      sum_rate_row[y] -= rate[pos];
      rate[pos] = 0;
    }
  }

  return pos;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
addq $0x9a848, %rax          # imm = 0x9A848
movl -0xc(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x1a48, %rcx, %rcx     # imm = 0x1A48
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
addq $0x9dcd8, %rax          # imm = 0x9DCD8
movl -0xc(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
addq $0x9dea8, %rax          # imm = 0x9DEA8
movl -0xc(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movq -0x30(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %r8
callq 0x83530
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x83411
movl $0x0, -0x38(%rbp)
jmp 0x8351c
movq -0x18(%rbp), %rdi
callq 0x65a00
movq -0x30(%rbp), %rcx
xorl %edx, %edx
divq (%rcx)
addq $0x1, %rdx
movq %rdx, -0x40(%rbp)
leaq 0x6461e(%rip), %rax     # 0xe7a50
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x40(%rbp), %rax
movq -0x28(%rbp), %rcx
movslq -0x34(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jle 0x8346c
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x34(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movq -0x40(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x40(%rbp)
jmp 0x83437
leaq 0x645dd(%rip), %rax     # 0xe7a50
movl (%rax), %eax
movl -0x34(%rbp), %ecx
leaq 0x645cd(%rip), %rdx     # 0xe7a4c
imull (%rdx), %ecx
addl %ecx, %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x40(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jg 0x834a7
jmp 0x834b8
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0x83487
jmp 0x834b8
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %esi
movl -0xc(%rbp), %edx
callq 0x9d460
testb $0x1, %al
jne 0x834cd
jmp 0x834cf
jmp 0x8351c
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x30(%rbp), %rax
movq (%rax), %rcx
subq %rdx, %rcx
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
subq %rsi, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0x833fb
movl -0x38(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)