// File: /workspace/github_cpp_makefile_star2_O1/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 0x5518e
movslq %ecx, %rdx
movl (%rax,%rdx,4), %ecx
movl $0x0, (%rax,%rdx,4)
cmpl $0x2b7, %ecx            # imm = 0x2B7
jl 0x55179
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 0x43d8(%rbx), %rax
movq %rax, 0x43e0(%rbx)
movq 0x43d0(%rbx), %rax
movq %rax, 0x43d8(%rbx)
movl 0x43c8(%rbx), %ecx
testl %ecx, %ecx
je 0x55214
movl 0x43b0(%rbx), %edx
decl %edx
cmpl %edx, %ecx
jne 0x55214
movslq 0x43c4(%rbx), %rcx
shlq $0x5, %rcx
leaq 0x1c6d7b18(%rip), %rdx  # 0x1c72cd20
xorq 0x18(%rdx,%rcx), %rax
movq %rax, 0x43d0(%rbx)
movslq 0x43b0(%rbx), %rax
movslq %r15d, %rsi
movslq %r14d, %rdi
cmpq $0x43a, %rax            # imm = 0x43A
jg 0x5525c
movq %rax, %rcx
shlq $0x4, %rcx
movl %r14d, (%rbx,%rcx)
movl %r15d, 0x4(%rbx,%rcx)
movq %rsi, %rcx
shlq $0x5, %rcx
imulq $0x6920, %rax, %rdx     # imm = 0x6920
addq 0x1fd32(%rip), %rdx     # 0x74f80
addq %rcx, %rdx
movq (%rdx,%rdi,8), %rcx
xorq %rcx, 0x43f0(%rbx)
testl %r15d, %r15d
je 0x553da
leaq 0x9620(%rbx), %r13
movb %r14b, 0x43f8(%rbx,%rsi)
movb $0x0, 0x96ce0(%rbx,%rsi)
movq %rsi, 0x48(%rsp)
movq %rsi, %rax
shlq $0x5, %rax
addq 0x1fcc1(%rip), %rax     # 0x74f50
movq (%rax,%rdi,8), %rcx
xorq %rcx, 0x43d0(%rbx)
movq %rdi, 0x50(%rsp)
movq (%rax,%rdi,8), %rax
xorq %rax, 0x43e8(%rbx)
leaq 0x4748(%rbx), %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0x4e655
movl 0x205fa(%rip), %eax     # 0x758bc
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 0x55339
movslq 0x95720(%rbx,%r15,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
addq %r13, %rsi
movq %rbx, %rdi
movq 0x8(%rsp), %rdx
callq 0x532b1
movl 0x95720(%rbx,%r15,4), %eax
movslq %ebp, %rcx
incl %ebp
movl %eax, 0x30(%rsp,%rcx,4)
jmp 0x55395
xorl %r14d, %eax
cmpl $0x3, %eax
jne 0x55395
movslq 0x95720(%rbx,%r15,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
addq %r13, %rsi
movq %rbx, %rdi
movq 0x8(%rsp), %rdx
callq 0x532b1
movslq 0x95720(%rbx,%r15,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
cmpl $0x0, 0x4(%r13,%rsi)
jne 0x55395
addq %r13, %rsi
movq %rbx, %rdi
movl %r14d, %edx
callq 0x5337c
addl %eax, 0x14(%rsp)
movq 0x18(%rsp), %rax
incl 0x98730(%rbx,%rax,4)
incq %r12
cmpq $0x4, %r12
jne 0x552f4
movq 0x50(%rsp), %rax
movl 0x14(%rsp), %r15d
addl %r15d, 0x43b4(%rbx,%rax,4)
testl %ebp, %ebp
je 0x55435
cmpl $0x1, %ebp
jne 0x554a7
movl 0x30(%rsp), %ecx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl %r14d, %edx
callq 0x52e3c
jmp 0x554be
cmpl $0x43a, %eax            # imm = 0x43A
jg 0x553f1
movq 0x43e8(%rbx), %rcx
shlq $0x4, %rax
movq %rcx, 0x8(%rbx,%rax)
movslq 0x4744(%rbx), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x4744(%rbx)
movq %rax, %rcx
shlq $0x5, %rcx
leaq 0x1c6d7911(%rip), %rdx  # 0x1c72cd20
movq (%rdx,%rcx), %rcx
xorq %rcx, 0x43d0(%rbx)
cmpq $0x348, %rax            # imm = 0x348
jl 0x554dd
movl $0x0, 0x4744(%rbx)
jmp 0x554dd
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl %r14d, %edx
callq 0x52c44
cmpl $0x1, %r15d
movq 0x48(%rsp), %rcx
jne 0x554be
movslq 0x95720(%rbx,%rcx,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r13,%rax)
jne 0x554be
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 0x1c6d7887(%rip), %rcx  # 0x1c72cd20
movq 0x18(%rcx,%rax), %rax
xorq %rax, 0x43d0(%rbx)
jmp 0x554be
leaq 0x30(%rsp), %r8
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl %r14d, %edx
movl %ebp, %ecx
callq 0x52fe3
movslq 0x43b0(%rbx), %rax
cmpq $0x43a, %rax            # imm = 0x43A
jg 0x554dd
movq 0x43e8(%rbx), %rcx
shlq $0x4, %rax
movq %rcx, 0x8(%rbx,%rax)
incl 0x43b0(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq