// File: /workspace/github_cpp_makefile_star2_O3/kobanium[P]Ray/src/board/GoBoard.cpp
// Function: PutStone(game_info_t*, int, int)
void
PutStone( 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 *capture_pos = game->capture_pos[color - 1];

  int tmp = capture_pos[0];
  while (tmp < CAPTURE_END) {
    const int next = capture_pos[tmp];
    capture_pos[tmp] = 0;
    tmp = next;
  }

  // この手番の着手で打ち上げた石の数を0にする
  game->capture_pos[color - 1][0] = CAPTURE_END;
  game->capture_num[color - 1] = 0;
  game->capture_dir[color - 1] = 0;

  // 着手箇所の戦術的特徴を全て消す
  ClearTacticalFeatures(&game->tactical_features[pos * ALL_MAX]);

  game->previous2_hash = game->previous1_hash;
  game->previous1_hash = game->current_hash;

  if (game->ko_move != 0 &&
      game->ko_move == game->moves - 1) {
    game->current_hash ^= hash_bit[game->ko_pos][HASH_KO];
  }

  // 着手箇所と色を記録
  if (game->moves < MAX_RECORDS) {
    game->record[game->moves].color = color;
    game->record[game->moves].pos = pos;
    game->move_hash ^= move_bit[game->moves][pos][color];
  }

  // 着手がパスなら手数を進めて終了
  if (pos == PASS) {
    if (game->moves < MAX_RECORDS) {
      game->record[game->moves].hash = game->positional_hash;
    }
    game->current_hash ^= hash_bit[game->pass_count++][HASH_PASS];
    if (game->pass_count >= BOARD_MAX) { 
      game->pass_count = 0;
    }
    game->moves++;
    return;
  }

  // 石を置く
  board[pos] = static_cast<char>(color);

  // 候補手から除外
  game->candidates[pos] = false;

  game->current_hash ^= hash_bit[pos][color];
  game->positional_hash ^= hash_bit[pos][color];

  // パターンの更新(MD5)
  UpdatePatternStone(game->pat, color, pos);

  // 着手点の上下左右の座標を導出
  GetNeighbor4(neighbor, pos);

  // 着手箇所の上下左右の確認
  // 自分の連があれば, その連の呼吸点を1つ減らし, 接続候補に入れる
  // 敵の連であれば, その連の呼吸点を1つ減らし, 呼吸点が0になったら取り除く
  for (int i = 0; i < 4; i++) {
    if (board[neighbor[i]] == color) {
      RemoveLiberty(game, &string[string_id[neighbor[i]]], pos);
      connect[connection++] = string_id[neighbor[i]];
    } else if (board[neighbor[i]] == other) {
      RemoveLiberty(game, &string[string_id[neighbor[i]]], pos);
      if (string[string_id[neighbor[i]]].libs == 0) {
        prisoner += RemoveString(game, &string[string_id[neighbor[i]]], color);
        game->capture_dir[color - 1]++;
      }
    }
  }

  // 打ち上げた石をアゲハマに追加
  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];
      game->current_hash ^= hash_bit[game->ko_pos][HASH_KO];
    }
  } else if (connection == 1) {
    AddStone(game, pos, color, connect[0]);
  } else {
    ConnectString(game, pos, color, connection, connect);
  }

  // ハッシュ値の記録
  if (game->moves < MAX_RECORDS) {
    game->record[game->moves].hash = game->positional_hash;
  }
  
  // 手数を1つだけ進める
  game->moves++;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %r14d
movl %esi, %r15d
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
leal -0x1(%r14), %eax
movslq %eax, %rcx
movq %rcx, 0x18(%rsp)
imulq $0xae0, %rcx, %rax      # imm = 0xAE0
addq %rdi, %rax
addq $0x98738, %rax          # imm = 0x98738
movl (%rax), %ecx
cmpl $0x2b6, %ecx            # imm = 0x2B6
jg 0x55b42
movslq %ecx, %rdx
movl (%rax,%rdx,4), %ecx
movl $0x0, (%rax,%rdx,4)
cmpl $0x2b7, %ecx            # imm = 0x2B7
jl 0x55b2d
movl $0x2b7, (%rax)          # imm = 0x2B7
xorl %eax, %eax
movq 0x18(%rsp), %rcx
movl %eax, 0x98728(%rbx,%rcx,4)
movl %eax, 0x98730(%rbx,%rcx,4)
leal (%r15,%r15), %ecx
leal (%rcx,%rcx,2), %ecx
movslq %ecx, %rcx
movl %eax, 0x97372(%rbx,%rcx)
movw $0x0, 0x97376(%rbx,%rcx)
movq 0x43d0(%rbx), %rcx
movups 0x43d0(%rbx), %xmm0
movups %xmm0, 0x43d8(%rbx)
movl 0x43b0(%rbx), %eax
movl 0x43c8(%rbx), %edx
testl %edx, %edx
setne %sil
leal -0x1(%rax), %edi
cmpl %edi, %edx
sete %dl
andb %sil, %dl
cmpb $0x1, %dl
jne 0x55bcd
movslq 0x43c4(%rbx), %rdx
shlq $0x5, %rdx
leaq 0x1c6d815f(%rip), %rsi  # 0x1c72dd20
xorq 0x18(%rsi,%rdx), %rcx
movq %rcx, 0x43d0(%rbx)
cmpl $0x43a, %eax            # imm = 0x43A
jg 0x55c23
movslq %eax, %rdx
imulq $0x6920, %rdx, %rsi     # imm = 0x6920
shlq $0x4, %rdx
movl %r14d, (%rbx,%rdx)
movl %r15d, 0x4(%rbx,%rdx)
movslq %r15d, %r8
movslq %r14d, %r9
movq %r8, %rdi
shlq $0x5, %rdi
addq 0x20381(%rip), %rsi     # 0x75f80
addq %rdi, %rsi
movq (%rsi,%r9,8), %rsi
xorq %rsi, 0x43f0(%rbx)
testl %r15d, %r15d
jne 0x55c32
movq 0x43e8(%rbx), %rsi
movq %rsi, 0x8(%rbx,%rdx)
jmp 0x55da8
testl %r15d, %r15d
je 0x55da8
movslq %r15d, %r8
movslq %r14d, %r9
leaq 0x9620(%rbx), %r13
movb %r14b, 0x43f8(%rbx,%r8)
movb $0x0, 0x96ce0(%rbx,%r8)
movq %r8, 0x48(%rsp)
movq %r8, %rax
shlq $0x5, %rax
addq 0x202f3(%rip), %rax     # 0x75f50
movq (%rax,%r9,8), %rcx
xorq %rcx, 0x43d0(%rbx)
movq %r9, 0x50(%rsp)
movq (%rax,%r9,8), %rax
xorq %rax, 0x43e8(%rbx)
leaq 0x4748(%rbx), %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0x4e989
movl 0x20c2c(%rip), %eax     # 0x768bc
movl %r15d, %ecx
subl %eax, %ecx
movl %ecx, 0x20(%rsp)
leal -0x1(%r15), %ecx
movl %ecx, 0x24(%rsp)
leal 0x1(%r15), %ecx
movl %ecx, 0x28(%rsp)
movq %r15, 0x8(%rsp)
addl %r15d, %eax
movl %eax, 0x2c(%rsp)
xorl %r12d, %r12d
xorl %ebp, %ebp
movl $0x0, 0x14(%rsp)
movslq 0x20(%rsp,%r12,4), %r15
movsbl 0x43f8(%rbx,%r15), %eax
cmpl %r14d, %eax
jne 0x55d07
movslq 0x95720(%rbx,%r15,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
addq %r13, %rsi
movq %rbx, %rdi
movq 0x8(%rsp), %rdx
callq 0x536c2
movl 0x95720(%rbx,%r15,4), %eax
movslq %ebp, %rcx
incl %ebp
movl %eax, 0x30(%rsp,%rcx,4)
jmp 0x55d63
xorl %r14d, %eax
cmpl $0x3, %eax
jne 0x55d63
movslq 0x95720(%rbx,%r15,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
addq %r13, %rsi
movq %rbx, %rdi
movq 0x8(%rsp), %rdx
callq 0x536c2
movslq 0x95720(%rbx,%r15,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
cmpl $0x0, 0x4(%r13,%rsi)
jne 0x55d63
addq %r13, %rsi
movq %rbx, %rdi
movl %r14d, %edx
callq 0x5378d
addl %eax, 0x14(%rsp)
movq 0x18(%rsp), %rax
incl 0x98730(%rbx,%rax,4)
incq %r12
cmpq $0x4, %r12
jne 0x55cc2
movq 0x50(%rsp), %rax
movl 0x14(%rsp), %r15d
addl %r15d, 0x43b4(%rbx,%rax,4)
testl %ebp, %ebp
je 0x55ded
cmpl $0x1, %ebp
jne 0x55e5f
movl 0x30(%rsp), %ecx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl %r14d, %edx
callq 0x53238
jmp 0x55e76
movslq 0x4744(%rbx), %rdx
leal 0x1(%rdx), %esi
movl %esi, 0x4744(%rbx)
movq %rdx, %rsi
shlq $0x5, %rsi
leaq 0x1c6d7f5a(%rip), %rdi  # 0x1c72dd20
xorq (%rdi,%rsi), %rcx
movq %rcx, 0x43d0(%rbx)
cmpq $0x348, %rdx            # imm = 0x348
jl 0x55e98
movl $0x0, 0x4744(%rbx)
jmp 0x55e98
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl %r14d, %edx
callq 0x53040
cmpl $0x1, %r15d
movq 0x48(%rsp), %rcx
jne 0x55e76
movslq 0x95720(%rbx,%rcx,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r13,%rax)
jne 0x55e76
movl 0x43b0(%rbx), %eax
movl %eax, 0x43c8(%rbx)
movslq 0x95720(%rbx,%rcx,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
movswq 0x8(%r13,%rax), %rax
movl %eax, 0x43c4(%rbx)
shlq $0x5, %rax
leaq 0x1c6d7ecf(%rip), %rcx  # 0x1c72dd20
movq 0x18(%rcx,%rax), %rax
xorq %rax, 0x43d0(%rbx)
jmp 0x55e76
leaq 0x30(%rsp), %r8
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl %r14d, %edx
movl %ebp, %ecx
callq 0x533df
movslq 0x43b0(%rbx), %rax
cmpq $0x43a, %rax            # imm = 0x43A
jg 0x55e98
movq 0x43e8(%rbx), %rcx
movq %rax, %rdx
shlq $0x4, %rdx
movq %rcx, 0x8(%rbx,%rdx)
incl %eax
movl %eax, 0x43b0(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq