// File: /workspace/github_cpp_makefile_star2_O2/kobanium[P]Ray/src/board/SearchBoard.cpp
// Function: PutStoneForSearch(search_game_info_t*, int, int)
void
PutStoneForSearch( search_game_info_t *game, const int pos, const int color )
{
  int *string_id = game->string_id;
  char *board = game->board;
  string_t *string = game->string;
  int other = GetOppositeColor(color);
  int connection = 0;
  int connect[4] = { 0 };
  int prisoner = 0;
  int neighbor[4];

  // 着手箇所と色を記録
  if (game->moves < MAX_RECORDS) {
    game->record[game->moves].color = color;
    game->record[game->moves].pos = pos;
    game->undo[game->moves].strings = 0;
  }

  // 着手がパスなら手数を進めて終了
  if (pos == PASS) {
    game->moves++;
    return;
  }

  // 石を置く
  board[pos] = (char)color;

  // 候補手から除外
  game->candidates[pos] = false;

  // パターンの更新(MD5)
  UpdateMD2Stone(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) {
        RecordString(game, string_id[neighbor[i]]);
        prisoner += RemoveString(game, &string[string_id[neighbor[i]]]);
      }
    }
  }

  // 打ち上げた石をアゲハマに追加
  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) {
    RecordString(game, connect[0]);
    AddStone(game, pos, color, connect[0]);
  } else {
    ConnectString(game, pos, color, connection, connect);
  }

  // 手数を1つだけ進める
  game->moves++;
  memset(&game->undo[game->moves], 0, sizeof(undo_record_t));
  game->undo[game->moves].ko_move_record = game->ko_move;
  game->undo[game->moves].ko_pos_record = game->ko_pos;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %esi, %r14d
movq %rdi, %rbx
pxor %xmm0, %xmm0
movdqa %xmm0, 0x20(%rsp)
movslq 0x43b0(%rdi), %rax
cmpq $0x43a, %rax            # imm = 0x43A
jg 0x3952d
movq %rax, %rcx
shlq $0x4, %rcx
movl %edx, (%rbx,%rcx)
movl %r14d, 0x4(%rbx,%rcx)
imulq $0x16c0, %rax, %rcx     # imm = 0x16C0
andl $0x0, 0x986ac(%rbx,%rcx)
testl %r14d, %r14d
je 0x3968e
leaq 0x95f0(%rbx), %rax
movq %rax, (%rsp)
movslq %r14d, %rax
movb %dl, 0x43cc(%rbx,%rax)
movq %rax, 0x10(%rsp)
movb $0x0, 0x96cb0(%rbx,%rax)
leaq 0x4718(%rbx), %rdi
movl %edx, 0x8(%rsp)
movl %edx, %esi
movl %r14d, %edx
callq 0x36625
leaq 0x70(%rsp), %rdi
movl %r14d, %esi
callq 0x3b128
xorl %r15d, %r15d
xorl %ebp, %ebp
movl $0x0, 0x18(%rsp)
movslq %ebp, %r13
cmpq $0x4, %r15
je 0x39648
movslq 0x70(%rsp,%r15,4), %r12
movsbl 0x43cc(%rbx,%r12), %eax
movl 0x8(%rsp), %ecx
cmpl %ecx, %eax
jne 0x395db
movslq 0x956f0(%rbx,%r12,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
addq (%rsp), %rsi
movq %rbx, %rdi
movl %r14d, %edx
callq 0x39b16
movl 0x956f0(%rbx,%r12,4), %eax
leal 0x1(%r13), %ebp
movl %eax, 0x20(%rsp,%r13,4)
jmp 0x39640
xorl %ecx, %eax
cmpl $0x3, %eax
jne 0x39640
movslq 0x956f0(%rbx,%r12,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
movq (%rsp), %r13
addq %r13, %rsi
movq %rbx, %rdi
movl %r14d, %edx
callq 0x39b16
movslq 0x956f0(%rbx,%r12,4), %rsi
imulq $0x7c8, %rsi, %rax      # imm = 0x7C8
cmpl $0x0, 0x4(%r13,%rax)
jne 0x39640
movq %rbx, %rdi
callq 0x39b5f
movslq 0x956f0(%rbx,%r12,4), %rax
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
addq %r13, %rsi
movq %rbx, %rdi
callq 0x39c00
addl %eax, 0x18(%rsp)
incq %r15
jmp 0x39587
movl 0x8(%rsp), %ecx
movslq %ecx, %rax
movl 0x18(%rsp), %edx
addl %edx, 0x43b4(%rbx,%rax,4)
testl %ebp, %ebp
je 0x3969b
cmpl $0x1, %ebp
jne 0x39828
movl 0x20(%rsp), %r15d
movq %rbx, %rdi
movl %r15d, %esi
movl %ecx, %ebp
callq 0x39b5f
movq %rbx, %rdi
movl %r14d, %esi
movl %ebp, %edx
movl %r15d, %ecx
callq 0x39d86
jmp 0x39ab2
incl %eax
movl %eax, 0x43b0(%rbx)
jmp 0x39b04
xorl %r15d, %r15d
movq (%rsp), %r12
incl %r15d
cmpb $0x0, 0xf8c(%r12)
leaq 0x7c8(%r12), %r12
jne 0x396a2
leaq 0x8(%r12), %rcx
leaq 0x57c(%r12), %rax
movl $0x570, %edx            # imm = 0x570
testq %rdx, %rdx
je 0x396dd
andw $0x0, (%rcx)
addq $0x2, %rcx
addq $-0x2, %rdx
jmp 0x396ca
movl $0x240, %ecx            # imm = 0x240
movl 0x8(%rsp), %edx
testq %rcx, %rcx
je 0x396f9
andw $0x0, (%rax)
addq $0x2, %rax
addq $-0x2, %rcx
jmp 0x396e6
movb %dl, (%r12)
movw $0x2b7, 0x8(%r12)       # imm = 0x2B7
movw $0x11f, 0x57c(%r12) # imm = 0x11F
andl $0x0, 0x4(%r12)
movl %r14d, 0x7bc(%r12)
movl $0x1, 0x7c0(%r12)
andl $0x0, 0x578(%r12)
movq 0x10(%rsp), %rax
movl %r15d, 0x956f0(%rbx,%rax,4)
movl $0x2b7, 0x961d0(%rbx,%rax,4) # imm = 0x2B7
leaq 0x50(%rsp), %rdi
movl %r14d, %esi
callq 0x3b128
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpq $0x4, %rbp
je 0x397c1
movl 0x50(%rsp,%rbp,4), %r13d
movslq %r13d, %rax
movsbl 0x43cc(%rbx,%rax), %ecx
testl %ecx, %ecx
je 0x397ab
xorl 0x8(%rsp), %ecx
cmpl $0x3, %ecx
jne 0x397a6
movslq 0x956f0(%rbx,%rax,4), %r13
imulq $0x7c8, %r13, %rdi      # imm = 0x7C8
addq (%rsp), %rdi
movl %r15d, %esi
callq 0x3a1dc
movq %r12, %rdi
movl %r13d, %esi
callq 0x3a1dc
movl %r14d, %r13d
jmp 0x397b9
movq %r12, %rdi
movl %r13d, %esi
movl %r14d, %edx
callq 0x3a1ae
incq %rbp
movl %r13d, %r14d
jmp 0x3975d
movb $0x1, 0x7c4(%r12)
cmpl $0x1, 0x18(%rsp)
movq (%rsp), %rcx
jne 0x39ab2
movq 0x10(%rsp), %rax
movslq 0x956f0(%rbx,%rax,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rcx,%rax)
jne 0x39ab2
movl 0x43b0(%rbx), %eax
movl %eax, 0x43c8(%rbx)
movq 0x10(%rsp), %rax
movslq 0x956f0(%rbx,%rax,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
movswl 0x8(%rcx,%rax), %eax
movl %eax, 0x43c4(%rbx)
jmp 0x39ab2
movl 0x20(%rsp), %eax
xorl %r12d, %r12d
pushq $0x1
popq %rcx
movl %eax, %r15d
movslq %r12d, %rdx
cmpq %r13, %rcx
jge 0x39883
movl 0x20(%rsp,%rcx,4), %esi
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x39857
leaq 0x1(%rdi), %r8
cmpl %esi, 0x20(%rsp,%rdi,4)
movq %r8, %rdi
jne 0x39843
jmp 0x3987e
cmpl %esi, %r15d
movl %esi, %edi
cmovgl %r15d, %edi
cmovgel %esi, %r15d
movslq %edi, %rdi
imulq $0x7c8, %rdi, %rdi      # imm = 0x7C8
addq (%rsp), %rdi
movq %rdi, 0x50(%rsp,%rdx,8)
movl %esi, 0x40(%rsp,%rdx,4)
incl %r12d
incq %rcx
jmp 0x39835
movl %eax, 0x40(%rsp,%rdx,4)
testq %rdx, %rdx
pushq $-0x1
popq %r13
cmovnsq %rdx, %r13
incq %r13
xorl %ebp, %ebp
cmpq %rbp, %r13
je 0x398ad
movl 0x40(%rsp,%rbp,4), %esi
movq %rbx, %rdi
callq 0x39b5f
incq %rbp
jmp 0x39897
movq %rbx, %rdi
movl %r14d, %esi
movl 0x8(%rsp), %edx
movl %r15d, %ecx
callq 0x39d86
testl %r12d, %r12d
jle 0x39ab2
movslq %r15d, %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
movq (%rsp), %rcx
leaq (%rcx,%rax), %r13
movslq 0x7bc(%rcx,%rax), %rax
movslq 0x956f0(%rbx,%rax,4), %r14
imulq $0x7c8, %r14, %r10      # imm = 0x7C8
addq %rcx, %r10
movl %r12d, %eax
movq %rax, 0x10(%rsp)
xorl %eax, %eax
movq %r10, 0x38(%rsp)
cmpq 0x10(%rsp), %rax
je 0x39ab2
movq %rax, 0x18(%rsp)
movq 0x50(%rsp,%rax,8), %rbp
movslq 0x7bc(%rbp), %rax
movslq 0x956f0(%rbx,%rax,4), %rax
movq %rax, 0x8(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
movq (%rsp), %r9
cmpl $0x2b7, %ecx            # imm = 0x2B7
je 0x39974
movslq %ecx, %rdx
cmpw $0x0, 0x8(%r13,%rdx,2)
jne 0x3996d
movl %eax, %esi
movslq %eax, %rdi
movzwl 0x8(%r13,%rdi,2), %r8d
movswl %r8w, %eax
cmpl %eax, %ecx
jg 0x39948
movw %r8w, 0x8(%r13,%rdx,2)
movw %cx, 0x8(%r13,%rdi,2)
incl 0x4(%r13)
movl %esi, %eax
movswl 0x8(%rbp,%rdx,2), %ecx
jmp 0x39934
movl 0x7bc(%r10), %edx
imulq $0x7c8, 0x8(%rsp), %rax # imm = 0x7C8
movl 0x7bc(%r9,%rax), %ecx
cmpl %ecx, %edx
jle 0x399b4
movslq %ecx, %rsi
movl 0x961d0(%rbx,%rsi,4), %edi
movl %edx, 0x961d0(%rbx,%rsi,4)
movl %r14d, 0x956f0(%rbx,%rsi,4)
movl %ecx, 0x7bc(%r10)
movl %ecx, %edx
movl %edi, %ecx
addq %r9, %rax
cmpl $0x2b7, %ecx            # imm = 0x2B7
je 0x399f9
movslq %ecx, %rdi
movl %r14d, 0x956f0(%rbx,%rdi,4)
movl 0x961d0(%rbx,%rdi,4), %esi
movl %edx, %r8d
movslq %edx, %r9
movl 0x961d0(%rbx,%r9,4), %edx
cmpl %ecx, %edx
jl 0x399d1
movl %edx, 0x961d0(%rbx,%rdi,4)
movl %ecx, 0x961d0(%rbx,%r9,4)
movl %r8d, %edx
movl %esi, %ecx
jmp 0x399b7
movl 0x7c0(%rax), %eax
addl %eax, 0x7c0(%r10)
movzwl 0x57c(%rbp), %eax
xorl %ecx, %ecx
xorl %edx, %edx
cmpl $0x11f, %ecx            # imm = 0x11F
je 0x39a93
movslq %ecx, %rsi
cmpw $0x0, 0x57c(%r13,%rsi,2)
jne 0x39a59
movl %edx, %edi
movslq %edx, %r8
movzwl 0x57c(%r13,%r8,2), %r9d
movswl %r9w, %edx
cmpl %edx, %ecx
jg 0x39a28
movw %r9w, 0x57c(%r13,%rsi,2)
movw %cx, 0x57c(%r13,%r8,2)
incl 0x578(%r13)
movl %edi, %edx
movswl 0x57c(%rbp,%rsi,2), %ecx
jmp 0x39a11
movswq %ax, %r15
imulq $0x7c8, %r15, %r12      # imm = 0x7C8
addq (%rsp), %r12
movq %r12, %rdi
movq 0x8(%rsp), %rsi
callq 0x3a219
movq %r12, %rdi
movl %r14d, %esi
callq 0x3a1dc
movzwl 0x57c(%rbp,%r15,2), %eax
cmpw $0x11f, %ax             # imm = 0x11F
jne 0x39a63
movb $0x0, 0x7c4(%rbp)
movq 0x18(%rsp), %rax
incq %rax
movq 0x38(%rsp), %r10
jmp 0x39903
movslq 0x43b0(%rbx), %rax
incq %rax
movl %eax, 0x43b0(%rbx)
imulq $0x16c0, %rax, %rax     # imm = 0x16C0
leaq (%rbx,%rax), %rdi
addq $0x96ffc, %rdi          # imm = 0x96FFC
movl $0x16c0, %edx           # imm = 0x16C0
xorl %esi, %esi
callq 0x61f0
movslq 0x43b0(%rbx), %rax
imulq $0x16c0, %rax, %rax     # imm = 0x16C0
movq 0x43c4(%rbx), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x986b4(%rbx,%rax)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq