// File: /workspace/github_cpp_makefile_star2_O1/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
movl %esi, %r14d
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movslq %edx, %rdi
imulq $0xae0, %rdi, %rax      # imm = 0xAE0
movl $0x2b7, 0x97c58(%rax,%rbx) # imm = 0x2B7
xorl %eax, %eax
movl %eax, 0x98724(%rbx,%rdi,4)
movl %eax, 0x9872c(%rbx,%rdi,4)
movslq 0x43b0(%rbx), %rax
cmpq $0x43a, %rax            # imm = 0x43A
jg 0x55550
shlq $0x4, %rax
movl %ebp, (%rbx,%rax)
movl %r14d, 0x4(%rbx,%rax)
testl %r14d, %r14d
je 0x5581b
movslq %r14d, %rsi
movb %bpl, 0x43f8(%rbx,%rsi)
movb $0x0, 0x96ce0(%rbx,%rsi)
movq 0x9a848(%rbx,%rsi,8), %rax
subq %rax, 0x9dea8(%rbx)
imulq $0x5a4, %rdi, %rax      # imm = 0x5A4
movq 0x9a848(%rbx,%rsi,8), %rcx
leaq 0x1aab841f(%rip), %rdx  # 0x1ab0d9b0
movslq (%rdx,%rsi,4), %rdx
subq %rcx, 0x9dcd8(%rbx,%rdx,8)
leaq 0x9620(%rbx), %rcx
movq %rcx, 0x8(%rsp)
movq %rdi, 0x40(%rsp)
leaq -0x1(%rdi), %rcx
movq %rcx, 0x50(%rsp)
xorl %r12d, %r12d
movq %r12, 0x9a848(%rbx,%rsi,8)
movq 0x9c290(%rbx,%rsi,8), %rcx
subq %rcx, 0x9deb0(%rbx)
movq 0x9c290(%rbx,%rsi,8), %rcx
subq %rcx, 0x9ddc0(%rbx,%rdx,8)
addq %rbx, %rax
addq $0x9975c, %rax          # imm = 0x9975C
movq %rax, 0x48(%rsp)
movq %rsi, 0x18(%rsp)
movq %r12, 0x9c290(%rbx,%rsi,8)
leal (%r14,%r14), %eax
leal (%rax,%rax,2), %eax
cltq
movw $0x0, 0x97376(%rbx,%rax)
movl $0x0, 0x97372(%rbx,%rax)
leaq 0x4748(%rbx), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x4e058
movl 0x2028b(%rip), %eax     # 0x758bc
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, (%rsp)
addl %r14d, %eax
movl %eax, 0x2c(%rsp)
xorl %r15d, %r15d
movl $0x0, 0x14(%rsp)
movq 0x8(%rsp), %r13
movslq 0x20(%rsp,%r12,4), %r14
movsbl 0x43f8(%rbx,%r14), %eax
cmpl %ebp, %eax
jne 0x556ad
movslq 0x95720(%rbx,%r14,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
addq 0x8(%rsp), %rsi
movq %rbx, %rdi
movq (%rsp), %rdx
movl %ebp, %ecx
callq 0x532fb
movl 0x95720(%rbx,%r14,4), %eax
movslq %r15d, %rcx
incl %r15d
movl %eax, 0x30(%rsp,%rcx,4)
jmp 0x556fc
xorl %ebp, %eax
cmpl $0x3, %eax
jne 0x556fc
movslq 0x95720(%rbx,%r14,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
addq %r13, %rsi
movq %rbx, %rdi
movq (%rsp), %rdx
movl %ebp, %ecx
callq 0x532fb
movslq 0x95720(%rbx,%r14,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
cmpl $0x0, 0x4(%r13,%rsi)
jne 0x556fc
addq %r13, %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x53635
addl %eax, 0x14(%rsp)
incq %r12
cmpq $0x4, %r12
jne 0x55665
movq 0x40(%rsp), %rax
movl 0x14(%rsp), %r14d
addl %r14d, 0x43b4(%rbx,%rax,4)
testl %r15d, %r15d
je 0x5573a
cmpl $0x1, %r15d
jne 0x55798
movl 0x30(%rsp), %ecx
movq %rbx, %rdi
movq (%rsp), %rsi
movl %ebp, %edx
callq 0x52e3c
jmp 0x557ae
movq %rbx, %rdi
movq (%rsp), %rsi
movl %ebp, %edx
callq 0x52c44
cmpl $0x1, %r14d
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %r9
jne 0x557b8
movslq 0x95720(%rbx,%rsi,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r9,%rax)
jne 0x557b8
movl 0x43b0(%rbx), %eax
movl %eax, 0x43c8(%rbx)
movslq 0x95720(%rbx,%rsi,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
movswl 0x8(%r9,%rax), %eax
movl %eax, 0x43c4(%rbx)
jmp 0x557b8
leaq 0x30(%rsp), %r8
movq %rbx, %rdi
movq (%rsp), %rsi
movl %ebp, %edx
movl %r15d, %ecx
callq 0x52fe3
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %r9
movslq 0x95720(%rbx,%rsi,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x2, 0x4(%r9,%rax)
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %r8
jg 0x5581b
addq %r9, %rax
movzwl 0x8(%rax), %eax
cmpl $0x2b7, %eax            # imm = 0x2B7
je 0x5581b
cwtl
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
movswq %ax, %rax
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %r9, %rcx
movzwl 0x8(%rcx,%rax,2), %eax
jmp 0x557e0
incl 0x43b0(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq