// File: /workspace/github_cpp_makefile_star2_O1/kobanium[P]Ray/src/feature/Nakade.cpp
// Function: IsNakadeSelfAtari(game_info_t const*, int, int)
bool
IsNakadeSelfAtari( const game_info_t *game, const int pos, const int color )
{
  const char *board = game->board;
  const string_t *string = game->string;
  const int *string_id = game->string_id;
  const int *string_next = game->string_next;
  const int neighbor4[4] = { NORTH(pos), WEST(pos), EAST(pos), SOUTH(pos) };
  unsigned long long hash = 0;
  int stones[10], checked[4] = { 0 };
  int check = 0, n = 0, my_stone, reviser, id;

  // 上下左右を確認し, 自分の連だったら, それぞれの石の座標を記録
  for (int i = 0 ; i < 4; i++) {
    if (board[neighbor4[i]] == color) {
      id = string_id[neighbor4[i]];
      bool check_flag = false;
      for (int j = 0; j < check; j++) {
        if (checked[j] == id) {
          check_flag = true;
          break;
        }
      }
      if (check_flag) continue;
      my_stone = string[id].origin;
      while (my_stone != STRING_END) {
        stones[n++] = my_stone;
        my_stone = string_next[my_stone];
      }
      checked[check++] = id;
    }
  }

  // 確認している座標も加える
  stones[n++] = pos;
  
  // 連の大きさが6以上なら打たないように
  // falseを返す
  if (n > 5) {
    return false;
  }

  // 座標をソート
  std::sort(stones, stones + n);

  // 石の座標の補正項
  reviser = start - stones[0];

  // ハッシュ値を計算
  for (int i = 0; i < n; i++) {
    hash ^= shape_bit[stones[i] + reviser];
  }

  // ハッシュ値が一致しているか確認
  // ナカデのパターンならばtrueを返す
  // そうでなければfalseを返す
  switch (n) {
    case 3:
    case 4:
    case 5:
      for (int i = 0; i < NAKADE_PATTERNS[n - 3]; i++) {
        if (nakade_hash[n - 3][i] == hash) {
          return true;
        }
      }
      break;
  }

  return false;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x50, %rsp
leaq 0x9620(%rdi), %rax
leaq 0x1ccbe(%rip), %rcx     # 0x758bc
movl (%rcx), %ecx
movl %esi, %r8d
subl %ecx, %r8d
movl %r8d, (%rsp)
leal -0x1(%rsi), %r8d
movl %r8d, 0x4(%rsp)
leal 0x1(%rsi), %r8d
movl %r8d, 0x8(%rsp)
addl %esi, %ecx
movl %ecx, 0xc(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
xorl %ecx, %ecx
xorl %r15d, %r15d
xorl %r8d, %r8d
movslq (%rsp,%rcx,4), %r9
movsbl 0x43f8(%rdi,%r9), %r10d
cmpl %edx, %r10d
jne 0x58cb5
movl 0x95720(%rdi,%r9,4), %r9d
testl %r8d, %r8d
jle 0x58c66
movl %r8d, %r10d
xorl %r11d, %r11d
cmpl %r9d, 0x10(%rsp,%r11,4)
je 0x58cb5
incq %r11
cmpq %r11, %r10
jne 0x58c57
movslq %r9d, %r10
imulq $0x7c8, %r10, %r10      # imm = 0x7C8
movl 0x7bc(%rax,%r10), %r10d
cmpl $0x2b7, %r10d           # imm = 0x2B7
je 0x58caa
movslq %r15d, %r11
leaq (%rsp,%r11,4), %r11
addq $0x20, %r11
movl %r10d, (%r11)
movslq %r10d, %r10
movl 0x96200(%rdi,%r10,4), %r10d
addq $0x4, %r11
incl %r15d
cmpl $0x2b7, %r10d           # imm = 0x2B7
jne 0x58c8c
movslq %r8d, %r10
incl %r8d
movl %r9d, 0x10(%rsp,%r10,4)
incq %rcx
cmpq $0x4, %rcx
jne 0x58c32
movslq %r15d, %rax
movl %esi, 0x20(%rsp,%rax,4)
cmpl $0x4, %eax
jg 0x58d9b
leaq (%rsp,%rax,4), %rbx
addq $0x24, %rbx
leaq 0x20(%rsp), %r14
cmpq %rbx, %r14
je 0x58d11
movq %rbx, %rax
subq %r14, %rax
sarq $0x2, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x593b8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x59493
testl %r15d, %r15d
js 0x58d48
movl 0x1cbbc(%rip), %ecx     # 0x758d8
subl 0x20(%rsp), %ecx
leal 0x1(%r15), %eax
xorl %edx, %edx
movslq %ecx, %rsi
leaq 0x1c6da910(%rip), %rdi  # 0x1c733640
xorl %ecx, %ecx
movslq 0x20(%rsp,%rdx,4), %r8
addq %rsi, %r8
xorq (%rdi,%r8,8), %rcx
incq %rdx
cmpq %rdx, %rax
jne 0x58d32
jmp 0x58d4a
xorl %ecx, %ecx
addl $-0x2, %r15d
cmpl $0x2, %r15d
ja 0x58d9b
movl %r15d, %eax
leaq 0x80c2(%rip), %rdx      # 0x60e20
movl (%rdx,%rax,4), %esi
cmpl $0x2, %esi
movl $0x1, %edx
cmovgel %esi, %edx
decq %rdx
shlq $0x3, %rax
leaq (%rax,%rax,8), %rsi
xorl %edi, %edi
leaq 0x1c6dca10(%rip), %r8   # 0x1c735790
leaq (%rsi,%rdi,8), %rax
cmpq %rcx, (%r8,%rax)
sete %al
je 0x58d9d
leaq 0x1(%rdi), %r9
cmpq %rdi, %rdx
movq %r9, %rdi
jne 0x58d80
jmp 0x58d9d
xorl %eax, %eax
addq $0x50, %rsp
popq %rbx
popq %r14
popq %r15
retq