// File: /workspace/github_cpp_makefile_star2_O2/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, %r8d
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 0x3c349
movslq %ecx, %rdx
movl (%rax,%rdx,4), %ecx
andl $0x0, (%rax,%rdx,4)
jmp 0x3c335
movl $0x2b7, (%rax)          # imm = 0x2B7
movq 0x18(%rsp), %rax
andl $0x0, 0x98728(%rbx,%rax,4)
andl $0x0, 0x98730(%rbx,%rax,4)
imull $0x6, %r8d, %eax
cltq
andw $0x0, 0x97376(%rbx,%rax)
andl $0x0, 0x97372(%rbx,%rax)
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 0x3c3d0
movslq 0x43c4(%rbx), %rdx
shlq $0x5, %rdx
leaq 0x1c6dc95c(%rip), %rsi  # 0x1c718d20
xorq 0x18(%rsi,%rdx), %rcx
movq %rcx, 0x43d0(%rbx)
cmpl $0x43a, %eax            # imm = 0x43A
jg 0x3c426
movslq %eax, %rdx
imulq $0x6920, %rdx, %rsi     # imm = 0x6920
shlq $0x4, %rdx
movl %r14d, (%rbx,%rdx)
movl %r8d, 0x4(%rbx,%rdx)
movslq %r8d, %r9
movslq %r14d, %r10
movq %r9, %rdi
shlq $0x5, %rdi
addq 0x24b96(%rip), %rsi     # 0x60f98
addq %rdi, %rsi
movq (%rsi,%r10,8), %rsi
xorq %rsi, 0x43f0(%rbx)
testl %r8d, %r8d
jne 0x3c435
movq 0x43e8(%rbx), %rsi
movq %rsi, 0x8(%rbx,%rdx)
jmp 0x3c5ae
testl %r8d, %r8d
je 0x3c5ae
movslq %r8d, %r9
movslq %r14d, %r10
leaq 0x9620(%rbx), %r13
movb %r14b, 0x43f8(%rbx,%r9)
movb $0x0, 0x96ce0(%rbx,%r9)
movq %r9, 0x48(%rsp)
movq %r9, %rax
shlq $0x5, %rax
addq 0x24b10(%rip), %rax     # 0x60f70
movq (%rax,%r10,8), %rcx
xorq %rcx, 0x43d0(%rbx)
movq %r10, 0x50(%rsp)
movq (%rax,%r10,8), %rax
xorq %rax, 0x43e8(%rbx)
leaq 0x4748(%rbx), %rdi
movl %r14d, %esi
movl %r8d, %edx
movq %r8, 0x10(%rsp)
callq 0x36c0c
movq 0x10(%rsp), %rdx
movl 0x2541f(%rip), %eax     # 0x618bc
movl %edx, %ecx
subl %eax, %ecx
movl %ecx, 0x20(%rsp)
leal -0x1(%rdx), %ecx
movl %ecx, 0x24(%rsp)
leal 0x1(%rdx), %ecx
movl %ecx, 0x28(%rsp)
addl %edx, %eax
movl %eax, 0x2c(%rsp)
xorl %r12d, %r12d
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
cmpq $0x4, %r12
je 0x3c579
movslq 0x20(%rsp,%r12,4), %r15
movsbl 0x43f8(%rbx,%r15), %eax
cmpl %r14d, %eax
jne 0x3c515
movslq 0x95720(%rbx,%r15,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
addq %r13, %rsi
movq %rbx, %rdi
callq 0x3a71a
movq 0x10(%rsp), %rdx
movl 0x95720(%rbx,%r15,4), %eax
movslq %ebp, %rcx
incl %ebp
movl %eax, 0x30(%rsp,%rcx,4)
jmp 0x3c571
xorl %r14d, %eax
cmpl $0x3, %eax
jne 0x3c571
movslq 0x95720(%rbx,%r15,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
addq %r13, %rsi
movq %rbx, %rdi
callq 0x3a71a
movslq 0x95720(%rbx,%r15,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
cmpl $0x0, 0x4(%r13,%rsi)
jne 0x3c56c
addq %r13, %rsi
movq %rbx, %rdi
movl %r14d, %edx
callq 0x3a7e3
addl %eax, 0xc(%rsp)
movq 0x18(%rsp), %rax
incl 0x98730(%rbx,%rax,4)
movq 0x10(%rsp), %rdx
incq %r12
jmp 0x3c4c6
movq 0x50(%rsp), %rax
movl 0xc(%rsp), %r15d
addl %r15d, 0x43b4(%rbx,%rax,4)
testl %ebp, %ebp
je 0x3c5f0
cmpl $0x1, %ebp
jne 0x3c65f
movl 0x30(%rsp), %ecx
movq %rbx, %rdi
movl %edx, %esi
movl %r14d, %edx
callq 0x3a413
jmp 0x3c673
movslq 0x4744(%rbx), %rdx
leal 0x1(%rdx), %esi
movl %esi, 0x4744(%rbx)
movq %rdx, %rsi
shlq $0x5, %rsi
leaq 0x1c6dc754(%rip), %rdi  # 0x1c718d20
xorq (%rdi,%rsi), %rcx
movq %rcx, 0x43d0(%rbx)
cmpq $0x348, %rdx            # imm = 0x348
jl 0x3c695
andl $0x0, 0x4744(%rbx)
jmp 0x3c695
movq %rbx, %rdi
movl %edx, %esi
movl %r14d, %edx
callq 0x3a25c
cmpl $0x1, %r15d
movq 0x48(%rsp), %rcx
jne 0x3c673
movslq 0x95720(%rbx,%rcx,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r13,%rax)
jne 0x3c673
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 0x1c6dc6cf(%rip), %rcx  # 0x1c718d20
movq 0x18(%rcx,%rax), %rax
xorq %rax, 0x43d0(%rbx)
jmp 0x3c673
leaq 0x30(%rsp), %r8
movq %rbx, %rdi
movl %edx, %esi
movl %r14d, %edx
movl %ebp, %ecx
callq 0x3a532
movslq 0x43b0(%rbx), %rax
cmpq $0x43a, %rax            # imm = 0x43A
jg 0x3c695
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