// File: /workspace/github_cpp_makefile_star2_O2/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:
movslq %esi, %rax
cmpb $0x0, 0x43f8(%rdi,%rax)
je 0x3bc5c
xorl %eax, %eax
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, %r14d
movq %rdi, %rbx
movl %edx, 0xc(%rsp)
movq %rax, 0x28(%rsp)
addq $0x4748, %rdi           # imm = 0x4748
callq 0x37bc6
movl %eax, %eax
leaq 0x1aaeea52(%rip), %rcx  # 0x1ab2a6e0
cmpb $0x0, (%rax,%rcx)
jne 0x3bcb2
leaq 0x9620(%rbx), %rsi
movq %rbx, %rdi
movl 0xc(%rsp), %edx
movl %r14d, %ecx
callq 0x3be24
testb %al, %al
jne 0x3be13
cmpl %r14d, 0x43c4(%rbx)
jne 0x3bccf
movl 0x43b0(%rbx), %eax
decl %eax
cmpl %eax, 0x43c8(%rbx)
je 0x3be13
movb $0x1, %al
testl %r14d, %r14d
je 0x3be15
cmpb $0x0, 0x1ab1157f(%rip)  # 0x1ab4d260
je 0x3be15
movl 0xc(%rsp), %edx
xorl $0x3, %edx
leaq 0x9620(%rbx), %rsi
movq 0x43e8(%rbx), %rcx
movl 0x25bba(%rip), %edi     # 0x618bc
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 0x1c6dcfed(%rip), %r9   # 0x1c718d20
xorl %r10d, %r10d
cmpq $0x4, %r8
je 0x3bdd2
movslq 0x10(%rsp,%r8,4), %r11
movsbl 0x43f8(%rbx,%r11), %ebp
cmpl %ebp, %edx
jne 0x3bdca
movslq 0x95720(%rbx,%r11,4), %r11
imulq $0x7c8, %r11, %r14      # imm = 0x7C8
cmpl $0x1, 0x4(%rsi,%r14)
jne 0x3bdbf
addq %rsi, %r14
xorl %r12d, %r12d
testl %r10d, %r10d
movl $0x0, %r13d
cmovgl %r10d, %r13d
xorl %ebp, %ebp
cmpq %r12, %r13
je 0x3bd94
cmpl %r11d, 0x30(%rsp,%r12,4)
sete %r15b
orb %r15b, %bpl
incq %r12
jmp 0x3bd7e
testb $0x1, %bpl
jne 0x3bdca
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 0x3bda1
movslq %r10d, %r14
incl %r10d
movl %r11d, 0x30(%rsp,%r14,4)
incq %r8
jmp 0x3bd36
movslq 0xc(%rsp), %rdx
movq 0x28(%rsp), %rsi
shlq $0x5, %rsi
addq 0x25189(%rip), %rsi     # 0x60f70
xorq (%rsi,%rdx,8), %rcx
movl 0x43b0(%rbx), %edx
xorl %esi, %esi
testl %edx, %edx
cmovlel %esi, %edx
shlq $0x4, %rdx
addq $0x10, %rdx
leaq 0x10(%rsi), %rdi
cmpq %rdi, %rdx
je 0x3be15
cmpq %rcx, 0x8(%rbx,%rsi)
movq %rdi, %rsi
jne 0x3be00
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq