// File: kobanium[P]Ray/src/board/SearchBoard.cpp
// Function: RestoreChain(search_game_info_t*, int, int const*, int, int)
static void
RestoreChain( search_game_info_t *game, const int id, const int stone[], const int stones, const int color )
{
  string_t *string = game->string;
  string_t *new_string;
  char *board = game->board;
  int *string_id = game->string_id;
  int lib_add = 0;
  const int other = GetOppositeColor(color);
  int neighbor, neighbor4[4];
  int pos;

  // 新しい連のポインタ
  new_string = &game->string[id];

  // 連の初期化
  std::fill_n(new_string->lib, STRING_LIB_MAX, 0);
  std::fill_n(new_string->neighbor, MAX_NEIGHBOR, 0);
  new_string->color = static_cast<char>(color);
  new_string->lib[0] = LIBERTY_END;
  new_string->neighbor[0] = NEIGHBOR_END;
  new_string->libs = 0;
  new_string->origin = stone[0];
  new_string->size = stones;
  new_string->neighbors = 0;

  for (int i = 0; i < stones; i++) {
    pos = stone[i];
    board[pos] = static_cast<char>(color);
    game->string_id[pos] = id;
    UpdateMD2Stone(game->pat, color, pos); 
  }

  for (int i = 0; i < stones - 1; i++) {
    game->string_next[stone[i]] = stone[i + 1];
  }
  game->string_next[stone[stones - 1]] = STRING_END;

  for (int i = 0; i < stones; i++) {
    pos = stone[i];
    // 上下左右の座標の導出
    GetNeighbor4(neighbor4, pos);
    // 新しく作成した連の上下左右の座標を確認
    // 空点ならば, 作成した連に呼吸点を追加する
    // 敵の連ならば, 隣接する連をお互いに追加する
    for (int j = 0; j < 4; j++) {
      if (board[neighbor4[j]] == S_EMPTY) {
        AddLiberty(new_string, neighbor4[j], lib_add);
      } else if (board[neighbor4[j]] == other) {
        neighbor = string_id[neighbor4[j]];
        RemoveLiberty(game, &string[neighbor], pos);
        AddNeighbor(&string[neighbor], id);
        AddNeighbor(&string[id], neighbor);
      }
    }
  }

  // 連の存在フラグをオンにする
  new_string->flag = true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq -0x8(%rbp), %rax
addq $0x95f0, %rax           # imm = 0x95F0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
addq $0x43cc, %rax           # imm = 0x43CC
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
addq $0x956f0, %rax          # imm = 0x956F0
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x20(%rbp), %edi
callq 0x25810
movl %eax, -0x48(%rbp)
movq -0x8(%rbp), %rax
addq $0x95f0, %rax           # imm = 0x95F0
movslq -0xc(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
addq $0x8, %rdi
movl $0x0, -0x68(%rbp)
movl $0x2b8, %esi            # imm = 0x2B8
leaq -0x68(%rbp), %rdx
callq 0x9a090
movq -0x30(%rbp), %rdi
addq $0x57c, %rdi            # imm = 0x57C
movl $0x0, -0x6c(%rbp)
movl $0x120, %esi            # imm = 0x120
leaq -0x6c(%rbp), %rdx
callq 0x9a090
movl -0x20(%rbp), %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, (%rax)
movq -0x30(%rbp), %rax
movw $0x2b7, 0x8(%rax)       # imm = 0x2B7
movq -0x30(%rbp), %rax
movw $0x11f, 0x57c(%rax)     # imm = 0x11F
movq -0x30(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x7bc(%rax)
movl -0x1c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x7c0(%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0x578(%rax)
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x997da
movq -0x18(%rbp), %rax
movslq -0x70(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x64(%rbp)
movl -0x20(%rbp), %eax
movb %al, %dl
movq -0x38(%rbp), %rax
movslq -0x64(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0xc(%rbp), %edx
movq -0x8(%rbp), %rax
movslq -0x64(%rbp), %rcx
movl %edx, 0x956f0(%rax,%rcx,4)
movq -0x8(%rbp), %rdi
addq $0x4718, %rdi           # imm = 0x4718
movl -0x20(%rbp), %esi
movl -0x64(%rbp), %edx
callq 0x90cc0
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x99781
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x99820
movq -0x18(%rbp), %rax
movl -0x74(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq -0x8(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0x74(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, 0x961d0(%rax,%rcx,4)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x997e1
movq -0x8(%rbp), %rax
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
movl $0x2b7, 0x961d0(%rax,%rcx,4) # imm = 0x2B7
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x99947
movq -0x18(%rbp), %rax
movslq -0x78(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x64(%rbp)
leaq -0x60(%rbp), %rdi
movl -0x64(%rbp), %esi
callq 0x9bc90
movl $0x0, -0x7c(%rbp)
cmpl $0x4, -0x7c(%rbp)
jge 0x99937
movq -0x38(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movslq -0x60(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x998aa
movq -0x30(%rbp), %rdi
movslq -0x7c(%rbp), %rax
movl -0x60(%rbp,%rax,4), %esi
movl -0x44(%rbp), %edx
callq 0x99a30
jmp 0x99927
movq -0x38(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movslq -0x60(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl -0x48(%rbp), %eax
jne 0x99925
movq -0x40(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movslq -0x60(%rbp,%rcx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movslq -0x4c(%rbp), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
addq %rax, %rsi
movl -0x64(%rbp), %edx
callq 0x98af0
movq -0x28(%rbp), %rdi
movslq -0x4c(%rbp), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
addq %rax, %rdi
movl -0xc(%rbp), %esi
callq 0x99ad0
movq -0x28(%rbp), %rdi
movslq -0xc(%rbp), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
addq %rax, %rdi
movl -0x4c(%rbp), %esi
callq 0x99ad0
jmp 0x99927
jmp 0x99929
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x99874
jmp 0x99939
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x99847
movq -0x30(%rbp), %rax
movb $0x1, 0x7c4(%rax)
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)