// File: kobanium[P]Ray/src/board/GoBoard.cpp
// Function: PoPutStone(game_info_t*, int, int)
void
PoPutStone( game_info_t *game, const int pos, const int color )
{
  const int *string_id = game->string_id;
  const int other = GetOppositeColor(color);
  char *board = game->board;
  string_t *string = game->string;
  int connection = 0, prisoner = 0;
  int neighbor[4], connect[4] = { 0 };
  int *update_pos = game->update_pos[color - 1];
  int &update_num = game->update_num[color - 1];

  // この手番の着手で打ち上げた石の数を0にする
  game->capture_pos[color - 1][0] = CAPTURE_END;
  game->capture_num[color - 1] = 0;
  game->capture_dir[color - 1] = 0;

  // 着手制限の限界を超えていなければ記録
  if (game->moves < MAX_RECORDS) {
    game->record[game->moves].color = color;
    game->record[game->moves].pos = pos;
  }

  // 着手がパスなら手数を進めて終了
  if (pos == PASS) {
    game->moves++;
    return;
  }

  // 碁盤に石を置く
  board[pos] = static_cast<char>(color);

  // 候補酒から除外
  game->candidates[pos] = false;

  // 着手箇所のレートを0に戻す
  game->sum_rate[0] -= game->rate[0][pos];
  game->sum_rate_row[0][board_y[pos]] -= game->rate[0][pos];
  game->rate[0][pos] = 0;
  game->sum_rate[1] -= game->rate[1][pos];
  game->sum_rate_row[1][board_y[pos]] -= game->rate[1][pos];
  game->rate[1][pos] = 0;

  // 着手箇所の戦術的特徴を全て消す
  ClearTacticalFeatures(&game->tactical_features[pos * ALL_MAX]);

  // パターンの更新(MD2)  
  UpdateMD2Stone(game->pat, color, pos);

  // 着手箇所の上下左右の座標の導出
  GetNeighbor4(neighbor, pos);

  // 着手箇所の上下左右の確認
  // 自分の連があれば, その連の呼吸点を1つ減らし, 接続候補に入れる
  // 敵の連であれば, その連の呼吸点を1つ減らし, 呼吸点が0になったら取り除く  
  for (int i = 0; i < 4; i++) {
    if (board[neighbor[i]] == color) {
      PoRemoveLiberty(game, &string[string_id[neighbor[i]]], pos, color);
      connect[connection++] = string_id[neighbor[i]];
    } else if (board[neighbor[i]] == other) {
      PoRemoveLiberty(game, &string[string_id[neighbor[i]]], pos, color);
      if (string[string_id[neighbor[i]]].libs == 0) {
        prisoner += PoRemoveString(game, &string[string_id[neighbor[i]]], color);
      }
    }
  }

  // 打ち上げた石をアゲハマに追加
  game->prisoner[color] += prisoner;

  // 接続候補がなければ, 新しい連を作成して, 劫かどうかの確認をする
  // 接続候補が1つならば, その連に石を追加する
  // 接続候補が2つ以上ならば, 連同士を繋ぎ合わせて, 石を追加する  
  if (connection == 0) {
    MakeString(game, pos, color);
    if (prisoner == 1 &&
        string[string_id[pos]].libs == 1) {
      game->ko_move = game->moves;
      game->ko_pos = string[string_id[pos]].lib[0];
    }
  } else if (connection == 1) {
    AddStone(game, pos, color, connect[0]);
  } else {
    ConnectString(game, pos, color, connection, connect);
  }

  if (string[string_id[pos]].libs < 3) {
    int lib = string[string_id[pos]].lib[0];
    while (lib != LIBERTY_END) {
      update_pos[update_num++] = lib;
      lib = string[string_id[pos]].lib[lib];
    }
  }

  // 手数を進める
  game->moves++;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq -0x8(%rbp), %rax
addq $0x95720, %rax          # imm = 0x95720
movq %rax, -0x18(%rbp)
movl -0x10(%rbp), %edi
callq 0x25810
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rax
addq $0x43f8, %rax           # imm = 0x43F8
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
addq $0x9620, %rax           # imm = 0x9620
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x72b0
movq -0x8(%rbp), %rax
addq $0x99d00, %rax          # imm = 0x99D00
movl -0x10(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x5a4, %rcx, %rcx      # imm = 0x5A4
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
addq $0x99cf8, %rax          # imm = 0x99CF8
movl -0x10(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rax
addq $0x98738, %rax          # imm = 0x98738
movl -0x10(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0xae0, %rcx, %rcx      # imm = 0xAE0
addq %rcx, %rax
movl $0x2b7, (%rax)          # imm = 0x2B7
movq -0x8(%rbp), %rax
movl -0x10(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl $0x0, 0x98728(%rax,%rcx,4)
movq -0x8(%rbp), %rax
movl -0x10(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl $0x0, 0x98730(%rax,%rcx,4)
movq -0x8(%rbp), %rax
cmpl $0x43b, 0x43b0(%rax)    # imm = 0x43B
jge 0x9e321
movl -0x10(%rbp), %ecx
movq -0x8(%rbp), %rax
movq -0x8(%rbp), %rdx
movslq 0x43b0(%rdx), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0xc(%rbp), %ecx
movq -0x8(%rbp), %rax
movq -0x8(%rbp), %rdx
movslq 0x43b0(%rdx), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
cmpl $0x0, -0xc(%rbp)
jne 0x9e33f
movq -0x8(%rbp), %rax
movl 0x43b0(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x43b0(%rax)
jmp 0x9e70a
movl -0x10(%rbp), %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movslq -0xc(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movb $0x0, 0x96ce0(%rax,%rcx)
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq 0x9a848(%rax,%rcx,8), %rdx
movq -0x8(%rbp), %rax
movq 0x9dea8(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x9dea8(%rax)
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq 0x9a848(%rax,%rcx,8), %rsi
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rdx
leaq 0x1aae178d(%rip), %rcx  # 0x1ab7fb30
movslq (%rcx,%rdx,4), %rcx
movq 0x9dcd8(%rax,%rcx,8), %rdx
subq %rsi, %rdx
movq %rdx, 0x9dcd8(%rax,%rcx,8)
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq $0x0, 0x9a848(%rax,%rcx,8)
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq 0x9c290(%rax,%rcx,8), %rdx
movq -0x8(%rbp), %rax
movq 0x9deb0(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x9deb0(%rax)
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq 0x9c290(%rax,%rcx,8), %rsi
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rdx
leaq 0x1aae171e(%rip), %rcx  # 0x1ab7fb30
movslq (%rcx,%rdx,4), %rcx
movq 0x9ddc0(%rax,%rcx,8), %rdx
subq %rsi, %rdx
movq %rdx, 0x9ddc0(%rax,%rcx,8)
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq $0x0, 0x9c290(%rax,%rcx,8)
movq -0x8(%rbp), %rdi
addq $0x97372, %rdi          # imm = 0x97372
imull $0x6, -0xc(%rbp), %eax
cltq
addq %rax, %rdi
callq 0x79350
movq -0x8(%rbp), %rdi
addq $0x4748, %rdi           # imm = 0x4748
movl -0x10(%rbp), %esi
movl -0xc(%rbp), %edx
callq 0x90cc0
leaq -0x50(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x9bc90
movl $0x0, -0x74(%rbp)
cmpl $0x4, -0x74(%rbp)
jge 0x9e5a5
movq -0x28(%rbp), %rax
movslq -0x74(%rbp), %rcx
movslq -0x50(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl -0x10(%rbp), %eax
jne 0x9e4f3
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x18(%rbp), %rax
movslq -0x74(%rbp), %rcx
movslq -0x50(%rbp,%rcx,4), %rcx
movslq (%rax,%rcx,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
addq %rax, %rsi
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
callq 0x9ab60
movq -0x18(%rbp), %rax
movslq -0x74(%rbp), %rcx
movslq -0x50(%rbp,%rcx,4), %rcx
movl (%rax,%rcx,4), %ecx
movl -0x34(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x34(%rbp)
cltq
movl %ecx, -0x60(%rbp,%rax,4)
jmp 0x9e595
movq -0x28(%rbp), %rax
movslq -0x74(%rbp), %rcx
movslq -0x50(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl -0x1c(%rbp), %eax
jne 0x9e593
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x18(%rbp), %rax
movslq -0x74(%rbp), %rcx
movslq -0x50(%rbp,%rcx,4), %rcx
movslq (%rax,%rcx,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
addq %rax, %rsi
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
callq 0x9ab60
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0x74(%rbp), %rdx
movslq -0x50(%rbp,%rdx,4), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
cmpl $0x0, 0x4(%rax)
jne 0x9e591
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x18(%rbp), %rax
movslq -0x74(%rbp), %rcx
movslq -0x50(%rbp,%rcx,4), %rcx
movslq (%rax,%rcx,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
addq %rax, %rsi
movl -0x10(%rbp), %edx
callq 0x9b130
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x9e593
jmp 0x9e595
jmp 0x9e597
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x9e47f
movl -0x38(%rbp), %edx
movq -0x8(%rbp), %rax
movslq -0x10(%rbp), %rcx
addl 0x43b4(%rax,%rcx,4), %edx
movl %edx, 0x43b4(%rax,%rcx,4)
cmpl $0x0, -0x34(%rbp)
jne 0x9e637
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
callq 0x9a1c0
cmpl $0x1, -0x38(%rbp)
jne 0x9e635
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0xc(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
cmpl $0x1, 0x4(%rax)
jne 0x9e635
movq -0x8(%rbp), %rax
movl 0x43b0(%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x43c8(%rax)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0xc(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
movswl 0x8(%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x43c4(%rax)
jmp 0x9e669
cmpl $0x1, -0x34(%rbp)
jne 0x9e651
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movl -0x60(%rbp), %ecx
callq 0x9a530
jmp 0x9e667
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movl -0x34(%rbp), %ecx
leaq -0x60(%rbp), %r8
callq 0x9a760
jmp 0x9e669
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0xc(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
cmpl $0x3, 0x4(%rax)
jge 0x9e6f7
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0xc(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
movswl 0x8(%rax), %eax
movl %eax, -0x78(%rbp)
cmpl $0x2b7, -0x78(%rbp)     # imm = 0x2B7
je 0x9e6f5
movl -0x78(%rbp), %edx
movq -0x68(%rbp), %rax
movq -0x70(%rbp), %rsi
movl (%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, (%rsi)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0xc(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
movslq -0x78(%rbp), %rcx
movswl 0x8(%rax,%rcx,2), %eax
movl %eax, -0x78(%rbp)
jmp 0x9e6aa
jmp 0x9e6f7
movq -0x8(%rbp), %rax
movl 0x43b0(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x43b0(%rax)
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)