// File: /workspace/github_cpp_makefile_star2_O3/kobanium[P]Ray/src/board/GoBoard.cpp
// Function: IsLegal(game_info_t const*, int, int)
bool
IsLegal( const game_info_t *game, const int pos, const int color )
{
  // 既に石がある
  if (game->board[pos] != S_EMPTY) {
    return false;
  }

  // 自殺手である
  if (nb4_empty[Pat3(game->pat, pos)] == 0 &&
      IsSuicide(game, game->string, color, pos)) {
    return false;
  }

  // 劫である
  if (game->ko_pos == pos &&
      game->ko_move == (game->moves - 1)) {
    return false;
  }

  // 超劫である
  if (check_superko &&
      pos != PASS) {
    const int other = GetOppositeColor(color);
    const string_t *string = game->string;
    const int *string_id = game->string_id;
    const int *string_next = game->string_next;
    unsigned long long hash = game->positional_hash;
    int neighbor4[4], check[4], checked = 0, id, str_pos;
    bool flag;

    GetNeighbor4(neighbor4, pos);

    // この1手で石を取れる時の処理
    for (int i = 0; i < 4; i++) {
      if (game->board[neighbor4[i]] == other) {
        id = string_id[neighbor4[i]];
        if (string[id].libs == 1) {
          flag = false;
          for (int j = 0; j < checked; j++) {
            if (check[j] == id) {
              flag = true;
            }
          }
          if (flag) {
            continue;
          }
          str_pos = string[id].origin;
          do {
            hash ^= hash_bit[str_pos][other];
            str_pos = string_next[str_pos];
          } while (str_pos != STRING_END);
        }
        check[checked++] = id;
      }
    }

    // posにcolorを置いたと仮定
    hash ^= hash_bit[pos][color];
    
    for (int i = 0; i < game->moves; i++) {
      if (game->record[i].hash == hash) {
        return false;
      }
    }
  }
  
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movslq %esi, %r15
cmpb $0x0, 0x43f8(%rdi,%r15)
je 0x554a5
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edx, %ebp
movl %esi, %r14d
movq %rdi, %rbx
addq $0x4748, %rdi           # imm = 0x4748
callq 0x4fc2a
movl %eax, %eax
leaq 0x1aaea21e(%rip), %rcx  # 0x1ab3f6e0
cmpb $0x0, (%rax,%rcx)
jne 0x554e0
leaq 0x9620(%rbx), %rsi
movq %rbx, %rdi
movl %ebp, %edx
movl %r14d, %ecx
callq 0x55637
testb %al, %al
jne 0x55494
cmpl %r14d, 0x43c4(%rbx)
jne 0x554f9
movl 0x43b0(%rbx), %eax
decl %eax
cmpl %eax, 0x43c8(%rbx)
je 0x55494
movb $0x1, %al
testl %r14d, %r14d
je 0x55496
cmpb $0x0, 0x1ab0cd59(%rip)  # 0x1ab62260
je 0x55496
movq %r15, 0x28(%rsp)
movl %ebp, 0xc(%rsp)
movl %ebp, %edx
xorl $0x3, %edx
leaq 0x9620(%rbx), %rsi
movq 0x43e8(%rbx), %rcx
movl 0x21391(%rip), %edi     # 0x768bc
movl %r14d, %r8d
subl %edi, %r8d
movl %r8d, 0x10(%rsp)
leal -0x1(%r14), %r8d
movl %r8d, 0x14(%rsp)
leal 0x1(%r14), %r8d
movl %r8d, 0x18(%rsp)
addl %r14d, %edi
movl %edi, 0x1c(%rsp)
movslq %edx, %rdi
xorl %r8d, %r8d
leaq 0x1c6d87c4(%rip), %r9   # 0x1c72dd20
xorl %r10d, %r10d
movslq 0x10(%rsp,%r8,4), %r11
movsbl 0x43f8(%rbx,%r11), %ebp
cmpl %ebp, %edx
jne 0x555e2
movslq 0x95720(%rbx,%r11,4), %r11
imulq $0x7c8, %r11, %r14      # imm = 0x7C8
cmpl $0x1, 0x4(%rsi,%r14)
jne 0x555d7
testl %r10d, %r10d
jle 0x555af
movl %r10d, %r12d
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpl %r11d, 0x30(%rsp,%r13,4)
sete %r15b
orb %r15b, %bpl
incq %r13
cmpq %r13, %r12
jne 0x55595
testb $0x1, %bpl
jne 0x555e2
addq %rsi, %r14
movl 0x7bc(%r14), %ebp
movslq %ebp, %r14
movl 0x96200(%rbx,%r14,4), %ebp
shlq $0x5, %r14
addq %r9, %r14
xorq (%r14,%rdi,8), %rcx
cmpl $0x2b7, %ebp            # imm = 0x2B7
jne 0x555b9
movslq %r10d, %r14
incl %r10d
movl %r11d, 0x30(%rsp,%r14,4)
incq %r8
cmpq $0x4, %r8
jne 0x5555f
movslq 0x43b0(%rbx), %rdx
testq %rdx, %rdx
jle 0x55496
movslq 0xc(%rsp), %rsi
movq 0x28(%rsp), %rdi
shlq $0x5, %rdi
addq 0x2093c(%rip), %rdi     # 0x75f50
xorq (%rdi,%rsi,8), %rcx
shlq $0x4, %rdx
xorl %esi, %esi
cmpq %rcx, 0x8(%rbx,%rsi)
je 0x55494
addq $0x10, %rsi
cmpq %rsi, %rdx
jne 0x5561e
jmp 0x55496