// File: /workspace/github_cpp_makefile_star2_O3/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 0x1d3d0(%rip), %rcx     # 0x768bc
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 0x595a3
movl 0x95720(%rdi,%r9,4), %r9d
testl %r8d, %r8d
jle 0x59554
movl %r8d, %r10d
xorl %r11d, %r11d
cmpl %r9d, 0x10(%rsp,%r11,4)
je 0x595a3
incq %r11
cmpq %r11, %r10
jne 0x59545
movslq %r9d, %r10
imulq $0x7c8, %r10, %r10      # imm = 0x7C8
movl 0x7bc(%rax,%r10), %r10d
cmpl $0x2b7, %r10d           # imm = 0x2B7
je 0x59598
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 0x5957a
movslq %r8d, %r10
incl %r8d
movl %r9d, 0x10(%rsp,%r10,4)
incq %rcx
cmpq $0x4, %rcx
jne 0x59520
movslq %r15d, %rax
movl %esi, 0x20(%rsp,%rax,4)
cmpl $0x4, %eax
jg 0x5968d
leaq (%rsp,%rax,4), %rbx
addq $0x20, %rbx
addq $0x4, %rbx
leaq 0x20(%rsp), %r14
cmpq %rbx, %r14
je 0x59603
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 0x59c99
movq %r14, %rdi
movq %rbx, %rsi
callq 0x59d74
testl %r15d, %r15d
js 0x5968d
movl 0x1d2c6(%rip), %ecx     # 0x768d8
subl 0x20(%rsp), %ecx
leal 0x1(%r15), %eax
xorl %edx, %edx
movslq %ecx, %rsi
leaq 0x1c6db01a(%rip), %rdi  # 0x1c734640
xorl %ecx, %ecx
movslq 0x20(%rsp,%rdx,4), %r8
addq %rsi, %r8
xorq (%rdi,%r8,8), %rcx
incq %rdx
cmpq %rdx, %rax
jne 0x59628
addl $-0x2, %r15d
cmpl $0x2, %r15d
ja 0x5968d
movl %r15d, %eax
leaq 0x7a70(%rip), %rdx      # 0x610c0
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 0x1c6dd11e(%rip), %r8   # 0x1c736790
leaq (%rsi,%rdi,8), %rax
cmpq %rcx, (%r8,%rax)
sete %al
je 0x5968f
leaq 0x1(%rdi), %r9
cmpq %rdi, %rdx
movq %r9, %rdi
jne 0x59672
jmp 0x5968f
xorl %eax, %eax
addq $0x50, %rsp
popq %rbx
popq %r14
popq %r15
retq