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

  // 未使用の連のインデックスを見つける
  while (string[id].flag) { id++; }

  // 新しく連のデータを格納する箇所を保持
  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->lib[0] = LIBERTY_END;
  new_string->neighbor[0] = NEIGHBOR_END;
  new_string->libs = 0;
  new_string->color = static_cast<char>(color);
  new_string->origin = pos;
  new_string->size = 1;
  new_string->neighbors = 0;
  game->string_id[pos] = id;
  game->string_next[pos] = STRING_END;

  // 上下左右の座標の導出
  GetNeighbor4(neighbor4, pos);

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

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq -0x8(%rbp), %rax
addq $0x43f8, %rax           # imm = 0x43F8
movq %rax, -0x18(%rbp)
movl -0x10(%rbp), %edi
callq 0x25810
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rax
addq $0x9620, %rax           # imm = 0x9620
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
addq $0x95720, %rax          # imm = 0x95720
movq %rax, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x28(%rbp), %rax
movslq -0x3c(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
testb $0x1, 0x7c4(%rax)
je 0x9a23b
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x9a215
movq -0x8(%rbp), %rax
addq $0x9620, %rax           # imm = 0x9620
movslq -0x3c(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
addq $0x8, %rdi
movl $0x0, -0x64(%rbp)
movl $0x2b8, %esi            # imm = 0x2B8
leaq -0x64(%rbp), %rdx
callq 0x9a090
movq -0x30(%rbp), %rdi
addq $0x57c, %rdi            # imm = 0x57C
movl $0x0, -0x68(%rbp)
movl $0x120, %esi            # imm = 0x120
leaq -0x68(%rbp), %rdx
callq 0x9a090
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)
movl -0x10(%rbp), %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, (%rax)
movl -0xc(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x7bc(%rax)
movq -0x30(%rbp), %rax
movl $0x1, 0x7c0(%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0x578(%rax)
movl -0x3c(%rbp), %edx
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl %edx, 0x95720(%rax,%rcx,4)
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl $0x2b7, 0x96200(%rax,%rcx,4) # imm = 0x2B7
leaq -0x60(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x9bc90
movl $0x0, -0x6c(%rbp)
cmpl $0x4, -0x6c(%rbp)
jge 0x9a3ce
movq -0x18(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movslq -0x60(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x9a35b
movq -0x30(%rbp), %rdi
movslq -0x6c(%rbp), %rax
movl -0x60(%rbp,%rax,4), %esi
movl -0x40(%rbp), %edx
callq 0x9a3e0
movl %eax, -0x40(%rbp)
jmp 0x9a3be
movq -0x18(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movslq -0x60(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl -0x1c(%rbp), %eax
jne 0x9a3bc
movq -0x38(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movslq -0x60(%rbp,%rcx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x44(%rbp)
movq -0x28(%rbp), %rdi
movslq -0x44(%rbp), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
addq %rax, %rdi
movl -0x3c(%rbp), %esi
xorl %edx, %edx
callq 0x9a480
movq -0x28(%rbp), %rdi
movslq -0x3c(%rbp), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
addq %rax, %rdi
movl -0x44(%rbp), %esi
xorl %edx, %edx
callq 0x9a480
jmp 0x9a3be
jmp 0x9a3c0
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x9a322
movq -0x30(%rbp), %rax
movb $0x1, 0x7c4(%rax)
addq $0x70, %rsp
popq %rbp
retq
nop