// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movslq %esi, %r15
cmpb $0x0, 0x43f8(%rdi,%r15)
je 0x54abf
xorl %eax, %eax
jmp 0x54c63
movl %edx, %ebp
movl %esi, %r14d
movq %rdi, %rbx
addq $0x4748, %rdi           # imm = 0x4748
callq 0x4f84b
movl %eax, %eax
leaq 0x1aae9c04(%rip), %rcx  # 0x1ab3e6e0
cmpb $0x0, (%rax,%rcx)
jne 0x54afa
leaq 0x9620(%rbx), %rsi
movq %rbx, %rdi
movl %ebp, %edx
movl %r14d, %ecx
callq 0x54c72
testb %al, %al
jne 0x54ab8
cmpl %r14d, 0x43c4(%rbx)
jne 0x54b13
movl 0x43b0(%rbx), %eax
decl %eax
cmpl %eax, 0x43c8(%rbx)
je 0x54ab8
testl %r14d, %r14d
je 0x54c61
cmpb $0x0, 0x1ab0c73d(%rip)  # 0x1ab61260
je 0x54c61
movl %ebp, 0xc(%rsp)
movl %ebp, %ecx
xorl $0x3, %ecx
leaq 0x9620(%rbx), %rdx
movq 0x43e8(%rbx), %rax
movl 0x20d76(%rip), %esi     # 0x758bc
movl %r14d, %edi
subl %esi, %edi
movl %edi, 0x10(%rsp)
leal -0x1(%r14), %edi
movl %edi, 0x14(%rsp)
leal 0x1(%r14), %edi
movl %edi, 0x18(%rsp)
addl %r14d, %esi
movl %esi, 0x1c(%rsp)
movslq %ecx, %rsi
xorl %edi, %edi
movl $0x1, %r8d
leaq 0x1c6d81a8(%rip), %r9   # 0x1c72cd20
xorl %r10d, %r10d
movslq 0x10(%rsp,%rdi,4), %r11
movsbl 0x43f8(%rbx,%r11), %ebp
cmpl %ebp, %ecx
jne 0x54bff
movslq 0x95720(%rbx,%r11,4), %r11
imulq $0x7c8, %r11, %r14      # imm = 0x7C8
cmpl $0x1, 0x4(%rdx,%r14)
jne 0x54bf4
testl %r10d, %r10d
jle 0x54bcc
movl %r10d, %r12d
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpl %r11d, 0x20(%rsp,%r13,4)
movzbl %bpl, %ebp
cmovel %r8d, %ebp
incq %r13
cmpq %r13, %r12
jne 0x54bb1
testb $0x1, %bpl
jne 0x54bff
addq %rdx, %r14
movl 0x7bc(%r14), %ebp
movslq %ebp, %r14
movl 0x96200(%rbx,%r14,4), %ebp
shlq $0x5, %r14
addq %r9, %r14
xorq (%r14,%rsi,8), %rax
cmpl $0x2b7, %ebp            # imm = 0x2B7
jne 0x54bd6
movslq %r10d, %r14
incl %r10d
movl %r11d, 0x20(%rsp,%r14,4)
incq %rdi
cmpq $0x4, %rdi
jne 0x54b7b
movslq 0x43b0(%rbx), %rcx
testq %rcx, %rcx
setg %dl
jle 0x54c61
movslq 0xc(%rsp), %rsi
shlq $0x5, %r15
addq 0x20325(%rip), %r15     # 0x74f50
xorq (%r15,%rsi,8), %rax
cmpq %rax, 0x8(%rbx)
je 0x54c59
addq $0x18, %rbx
leaq -0x1(%rcx), %rsi
xorl %edx, %edx
cmpq %rdx, %rsi
je 0x54c61
leaq 0x10(%rbx), %rdi
incq %rdx
cmpq %rax, (%rbx)
movq %rdi, %rbx
jne 0x54c3f
cmpq %rcx, %rdx
setb %dl
testb %dl, %dl
jne 0x54ab8
movb $0x1, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq