// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %ebx
movl %esi, 0xc(%rsp)
movq %rdi, %r14
leaq 0x9620(%rdi), %r15
xorl %r12d, %r12d
movq %rdi, %r13
incq %r12
cmpb $0x0, 0xa5ac(%r13)
leaq 0x7c8(%r13), %r13
jne 0x52c68
leaq 0x9628(%r13), %rdi
xorl %ebp, %ebp
movl $0x570, %edx            # imm = 0x570
xorl %esi, %esi
callq 0x6210
leaq 0x9b9c(%r13), %rdi
movl $0x240, %edx            # imm = 0x240
xorl %esi, %esi
callq 0x6210
movw $0x2b7, 0x9628(%r13)    # imm = 0x2B7
movw $0x11f, 0x9b9c(%r13)    # imm = 0x11F
leaq 0x9624(%r13), %rax
movq %rax, 0x10(%rsp)
movl %ebp, 0x9624(%r13)
movb %bl, 0x9620(%r13)
movl 0xc(%rsp), %ecx
movl %ecx, 0x9ddc(%r13)
movl $0x1, 0x9de0(%r13)
leaq 0x9b98(%r13), %rax
movq %rax, 0x18(%rsp)
movl %ebp, 0x9b98(%r13)
movslq %ecx, %rsi
movl %r12d, 0x95720(%r14,%rsi,4)
movl $0x2b7, 0x96200(%r14,%rsi,4) # imm = 0x2B7
leaq 0x20(%rsp), %rdi
callq 0x53e40
movq 0x10(%rsp), %r11
movl %r12d, %eax
xorl %ecx, %ecx
movl 0x20(%rsp,%rbp,4), %edx
movslq %edx, %rsi
movsbl 0x43f8(%r14,%rsi), %edi
testl %edi, %edi
je 0x52dd6
xorl %ebx, %edi
cmpl $0x3, %edi
jne 0x52e18
movl 0x95720(%r14,%rsi,4), %edx
movslq %edx, %rsi
imulq $0x7c8, %rsi, %rdi      # imm = 0x7C8
addq %r15, %rdi
cmpw $0x0, 0x57c(%rdi,%rax,2)
jne 0x52d99
xorl %r9d, %r9d
movslq %r9d, %r8
movzwl 0x57c(%rdi,%r8,2), %r10d
movswl %r10w, %r9d
cmpl %r9d, %r12d
jg 0x52d6c
movw %r10w, 0x57c(%rdi,%rax,2)
movw %r12w, 0x57c(%rdi,%r8,2)
incl 0x578(%rdi)
cmpw $0x0, 0x9b9c(%r13,%rsi,2)
jne 0x52e18
xorl %edi, %edi
movslq %edi, %r9
movzwl 0x9b9c(%r13,%r9,2), %r8d
movswl %r8w, %edi
cmpl %edi, %edx
jg 0x52da7
leaq 0x9b9c(,%r9,2), %rdi
addq %r13, %rdi
movw %r8w, 0x9b9c(%r13,%rsi,2)
movq 0x18(%rsp), %rsi
jmp 0x52e13
cmpw $0x0, 0x9628(%r13,%rsi,2)
je 0x52de6
movl %edx, %ecx
jmp 0x52e18
movslq %ecx, %rdi
movzwl 0x9628(%r13,%rdi,2), %r8d
movswl %r8w, %ecx
cmpl %ecx, %edx
jg 0x52de6
leaq 0x9628(,%rdi,2), %rdi
addq %r13, %rdi
movw %r8w, 0x9628(%r13,%rsi,2)
movq %r11, %rsi
movl %edx, %ecx
movw %dx, (%rdi)
incl (%rsi)
incq %rbp
cmpq $0x4, %rbp
jne 0x52d26
movb $0x1, 0x9de4(%r13)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq