// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movsbl 0x43f8(%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x9d0e3
movb $0x0, -0x1(%rbp)
jmp 0x9d34f
movq -0x10(%rbp), %rdi
addq $0x4748, %rdi           # imm = 0x4748
movl -0x14(%rbp), %esi
callq 0x93d30
movl %eax, %eax
movl %eax, %ecx
leaq 0x1ab1375f(%rip), %rax  # 0x1abb0860
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x9d133
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x9620, %rsi           # imm = 0x9620
movl -0x18(%rbp), %edx
movl -0x14(%rbp), %ecx
callq 0x9d360
testb $0x1, %al
jne 0x9d12a
jmp 0x9d133
movb $0x0, -0x1(%rbp)
jmp 0x9d34f
movq -0x10(%rbp), %rax
movl 0x43c4(%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x9d166
movq -0x10(%rbp), %rax
movl 0x43c8(%rax), %eax
movq -0x10(%rbp), %rcx
movl 0x43b0(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x9d166
movb $0x0, -0x1(%rbp)
jmp 0x9d34f
testb $0x1, 0x1ab36273(%rip)  # 0x1abd33e0
je 0x9d34b
cmpl $0x0, -0x14(%rbp)
je 0x9d34b
movl -0x18(%rbp), %edi
callq 0x25810
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
addq $0x9620, %rax           # imm = 0x9620
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
addq $0x95720, %rax          # imm = 0x95720
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
addq $0x96200, %rax          # imm = 0x96200
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x43e8(%rax), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x64(%rbp)
leaq -0x50(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x9bc90
movl $0x0, -0x74(%rbp)
cmpl $0x4, -0x74(%rbp)
jge 0x9d2e5
movq -0x10(%rbp), %rax
movslq -0x74(%rbp), %rcx
movslq -0x50(%rbp,%rcx,4), %rcx
movsbl 0x43f8(%rax,%rcx), %eax
cmpl -0x1c(%rbp), %eax
jne 0x9d2d5
movq -0x30(%rbp), %rax
movslq -0x74(%rbp), %rcx
movslq -0x50(%rbp,%rcx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x68(%rbp)
movq -0x28(%rbp), %rax
movslq -0x68(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
cmpl $0x1, 0x4(%rax)
jne 0x9d2c1
movb $0x0, -0x6d(%rbp)
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x64(%rbp), %eax
jge 0x9d263
movslq -0x78(%rbp), %rax
movl -0x60(%rbp,%rax,4), %eax
cmpl -0x68(%rbp), %eax
jne 0x9d256
movb $0x1, -0x6d(%rbp)
jmp 0x9d258
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x9d23d
testb $0x1, -0x6d(%rbp)
je 0x9d26b
jmp 0x9d2d7
movq -0x28(%rbp), %rax
movslq -0x68(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
movl 0x7bc(%rax), %eax
movl %eax, -0x6c(%rbp)
movslq -0x6c(%rbp), %rcx
leaq 0x1c701c0f(%rip), %rax  # 0x1c79eea0
shlq $0x5, %rcx
addq %rcx, %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
xorq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x6c(%rbp)
cmpl $0x2b7, -0x6c(%rbp)     # imm = 0x2B7
jne 0x9d286
jmp 0x9d2c1
movl -0x68(%rbp), %ecx
movl -0x64(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x64(%rbp)
cltq
movl %ecx, -0x60(%rbp,%rax,4)
jmp 0x9d2d7
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x9d1db
movslq -0x14(%rbp), %rcx
leaq 0x1c701bb0(%rip), %rax  # 0x1c79eea0
shlq $0x5, %rcx
addq %rcx, %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rax
xorq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x43b0(%rcx), %eax
jge 0x9d349
movq -0x10(%rbp), %rax
movslq -0x7c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
cmpq -0x40(%rbp), %rax
jne 0x9d33c
movb $0x0, -0x1(%rbp)
jmp 0x9d34f
jmp 0x9d33e
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x9d30e
jmp 0x9d34b
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)