// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %ebp
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movslq %edx, %rdx
imulq $0xae0, %rdx, %rax      # imm = 0xAE0
movl $0x2b7, 0x97c58(%rax,%rdi) # imm = 0x2B7
andl $0x0, 0x98724(%rdi,%rdx,4)
andl $0x0, 0x9872c(%rdi,%rdx,4)
movl %esi, %r14d
movslq 0x43b0(%rdi), %rax
cmpq $0x43a, %rax            # imm = 0x43A
jg 0x3c70a
shlq $0x4, %rax
movl %ebp, (%rbx,%rax)
movl %r14d, 0x4(%rbx,%rax)
testl %r14d, %r14d
je 0x3c9c8
imulq $0x5a4, %rdx, %rax      # imm = 0x5A4
movslq %r14d, %rsi
movb %bpl, 0x43f8(%rbx,%rsi)
movb $0x0, 0x96ce0(%rbx,%rsi)
movq 0x9a848(%rbx,%rsi,8), %rcx
subq %rcx, 0x9dea8(%rbx)
leaq 0x9620(%rbx), %r13
movq %rdx, 0x40(%rsp)
leaq -0x1(%rdx), %rcx
movq %rcx, 0x50(%rsp)
movq 0x9a848(%rbx,%rsi,8), %rcx
leaq 0x1aabd250(%rip), %rdx  # 0x1aaf99b0
movslq (%rdx,%rsi,4), %rdx
subq %rcx, 0x9dcd8(%rbx,%rdx,8)
addq %rbx, %rax
addq $0x9975c, %rax          # imm = 0x9975C
movq %rax, 0x48(%rsp)
andq $0x0, 0x9a848(%rbx,%rsi,8)
movq 0x9c290(%rbx,%rsi,8), %rax
subq %rax, 0x9deb0(%rbx)
movq 0x9c290(%rbx,%rsi,8), %rax
subq %rax, 0x9ddc0(%rbx,%rdx,8)
movq %rsi, 0x18(%rsp)
andq $0x0, 0x9c290(%rbx,%rsi,8)
imull $0x6, %r14d, %eax
cltq
andw $0x0, 0x97376(%rbx,%rax)
andl $0x0, 0x97372(%rbx,%rax)
leaq 0x4748(%rbx), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x36625
movl 0x250de(%rip), %eax     # 0x618bc
movl %r14d, %ecx
subl %eax, %ecx
movl %ecx, 0x20(%rsp)
leal -0x1(%r14), %ecx
movl %ecx, 0x24(%rsp)
leal 0x1(%r14), %ecx
movl %ecx, 0x28(%rsp)
movq %r14, 0x8(%rsp)
addl %r14d, %eax
movl %eax, 0x2c(%rsp)
xorl %r12d, %r12d
movl $0x0, 0x14(%rsp)
xorl %r15d, %r15d
cmpq $0x4, %r12
je 0x3c8ba
movslq 0x20(%rsp,%r12,4), %r14
movsbl 0x43f8(%rbx,%r14), %eax
cmpl %ebp, %eax
jne 0x3c862
movslq 0x95720(%rbx,%r14,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
addq %r13, %rsi
movq %rbx, %rdi
movq 0x8(%rsp), %rdx
movl %ebp, %ecx
callq 0x3a763
movl 0x95720(%rbx,%r14,4), %eax
movslq %r15d, %rcx
incl %r15d
movl %eax, 0x30(%rsp,%rcx,4)
jmp 0x3c8b2
xorl %ebp, %eax
cmpl $0x3, %eax
jne 0x3c8b2
movslq 0x95720(%rbx,%r14,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
addq %r13, %rsi
movq %rbx, %rdi
movq 0x8(%rsp), %rdx
movl %ebp, %ecx
callq 0x3a763
movslq 0x95720(%rbx,%r14,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
cmpl $0x0, 0x4(%r13,%rsi)
jne 0x3c8b2
addq %r13, %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x3aa42
addl %eax, 0x14(%rsp)
incq %r12
jmp 0x3c811
movq 0x40(%rsp), %rax
movl 0x14(%rsp), %r14d
addl %r14d, 0x43b4(%rbx,%rax,4)
testl %r15d, %r15d
je 0x3c8ec
cmpl $0x1, %r15d
jne 0x3c946
movl 0x30(%rsp), %ecx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl %ebp, %edx
callq 0x3a413
jmp 0x3c95d
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl %ebp, %edx
callq 0x3a25c
cmpl $0x1, %r14d
movq 0x18(%rsp), %rsi
jne 0x3c962
movslq 0x95720(%rbx,%rsi,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r13,%rax)
jne 0x3c962
movl 0x43b0(%rbx), %eax
movl %eax, 0x43c8(%rbx)
movslq 0x95720(%rbx,%rsi,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
movswl 0x8(%r13,%rax), %eax
movl %eax, 0x43c4(%rbx)
jmp 0x3c962
leaq 0x30(%rsp), %r8
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl %ebp, %edx
movl %r15d, %ecx
callq 0x3a532
movq 0x18(%rsp), %rsi
movslq 0x95720(%rbx,%rsi,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x2, 0x4(%r13,%rax)
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %r8
jg 0x3c9c8
addq %r13, %rax
addq $0x8, %rax
movswl (%rax), %eax
cmpl $0x2b7, %eax            # imm = 0x2B7
je 0x3c9c8
movslq 0x99cf8(%rbx,%rdi,4), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x99cf8(%rbx,%rdi,4)
movl %eax, (%r8,%rcx,4)
movslq 0x95720(%rbx,%rsi,4), %rcx
cltq
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %r13, %rcx
leaq (%rcx,%rax,2), %rax
addq $0x8, %rax
jmp 0x3c98a
incl 0x43b0(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq