// File: /workspace/github_cpp_makefile_star2_O2/kobanium[P]Ray/src/feature/UctFeature.cpp
// Function: CheckSelfAtariForTree(game_info_t const*, int, int, unsigned int*)
bool
CheckSelfAtariForTree( const game_info_t *game, const int color, const int pos, unsigned int *tactical_features )
{
  const char *board = game->board;
  const string_t *string = game->string;
  const int *string_id = game->string_id;
  const int other = GetOppositeColor(color);
  int id, lib, already_num = 0, size = 0, libs = 0, count;
  int lib_candidate[PURE_BOARD_MAX], neighbor4[4], already[4] = { 0 };
  bool checked, flag, already_checked;

  GetNeighbor4(neighbor4, pos);

  for (int i = 0; i < 4; i++) {
    if (board[neighbor4[i]] == S_EMPTY) {
      lib_candidate[libs++] = neighbor4[i];
    }
  }

  //  空点
  if (libs >= 2) return true;

  //  上下左右の確認
  for (int i = 0; i < 4; i++) {
    if (board[neighbor4[i]] == color) {
      id = string_id[neighbor4[i]];
      already_checked = false;
      for (int j = 0; j < already_num; j++) {
        if (already[j] == id) {
          already_checked = true;
        }
      }
      if (already_checked) continue;

      if (string[id].libs > 2) return true;
      lib = string[id].lib[0];
      count = 0;
      while (lib != LIBERTY_END) {
        if (lib != pos) {
          checked = false;
          for (int j = 0; j < libs; j++) {
            if (lib_candidate[j] == lib) {
              checked = true;
              break;
            }
          }
          if (!checked) {
            lib_candidate[libs + count] = lib;
            count++;
          }
        }
        lib = string[id].lib[lib];
      }
      libs += count;
      size += string[id].size;
      already[already_num++] = id;
      if (libs >= 2) return true;
    } else if (board[neighbor4[i]] == other &&
               string[string_id[neighbor4[i]]].libs == 1) {
      return true;
    }
  }

  // 自己アタリの分類
  // 1.大きさが2以下の自己アタリ
  // 2.大きさが6以下でナカデの形になる自己アタリ
  // 3.それ以外の自己アタリ
  if (size < 2) {
    CompareSwapFeature(tactical_features, pos, UCT_CAPTURE_INDEX, UCT_SELF_ATARI_SMALL);
    flag = true;
  } else if (size < 6) {
    if (IsUctNakadeSelfAtari(game, pos, color)) {
      CompareSwapFeature(tactical_features, pos, UCT_CAPTURE_INDEX, UCT_SELF_ATARI_NAKADE);
      flag = true;
    } else {
      CompareSwapFeature(tactical_features, pos, UCT_CAPTURE_INDEX, UCT_SELF_ATARI_LARGE);
      flag = false;
    }
  } else {
    CompareSwapFeature(tactical_features, pos, UCT_CAPTURE_INDEX, UCT_SELF_ATARI_LARGE);
    flag = false;
  }
  return flag;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5e8, %rsp            # imm = 0x5E8
movq %rcx, %rbx
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, %r12
leaq 0x9620(%rdi), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x30(%rsp), %rdi
movl %edx, %esi
callq 0x3b128
xorl %ecx, %ecx
xorl %r9d, %r9d
cmpq $0x4, %rcx
je 0x41500
movslq 0x30(%rsp,%rcx,4), %rdx
cmpb $0x0, 0x43f8(%r12,%rdx)
jne 0x414fb
movslq %r9d, %rsi
incl %r9d
movl %edx, 0x40(%rsp,%rsi,4)
incq %rcx
jmp 0x414db
movb $0x1, %al
cmpl $0x1, %r9d
jg 0x416a6
movq %rbx, 0x10(%rsp)
xorl %edx, %edx
pushq $0x1
popq %r13
xorl %edi, %edi
xorl %ecx, %ecx
movl %ebp, 0xc(%rsp)
movq %r15, 0x18(%rsp)
cmpq $0x4, %rdx
je 0x4162f
movslq 0x30(%rsp,%rdx,4), %r8
movsbl 0x43f8(%r12,%r8), %esi
cmpl %ebp, %esi
jne 0x41572
movl 0x95720(%r12,%r8,4), %r8d
movslq %r8d, %rax
xorl %r10d, %r10d
testl %edi, %edi
movl $0x0, %r11d
cmovgl %edi, %r11d
xorl %ebx, %ebx
cmpq %r10, %r11
je 0x4159d
cmpl %r8d, 0x20(%rsp,%r10,4)
sete %sil
orb %sil, %bl
incq %r10
jmp 0x4155c
xorl %ebp, %esi
cmpl $0x3, %esi
jne 0x41627
movslq 0x95720(%r12,%r8,4), %rsi
imulq $0x7c8, %rsi, %rsi      # imm = 0x7C8
cmpl $0x1, 0x4(%r15,%rsi)
jne 0x41627
jmp 0x4167f
testb $0x1, %bl
jne 0x41627
imulq $0x7c8, %rax, %r10      # imm = 0x7C8
cmpl $0x2, 0x4(%r15,%r10)
jg 0x4167f
addq %r15, %r10
leaq 0x8(%r10), %r11
xorl %r15d, %r15d
movq %r11, %rbx
movswl (%rbx), %ebx
cmpl $0x2b7, %ebx            # imm = 0x2B7
je 0x41601
cmpl %r14d, %ebx
je 0x415f8
xorl %esi, %esi
movl 0x40(%rsp), %ebp
cmpl %r9d, %esi
jge 0x415ea
movl %r13d, %esi
cmpl %ebx, %ebp
jne 0x415dc
jmp 0x415f8
leal (%r15,%r9), %esi
movslq %esi, %rsi
movl %ebx, 0x40(%rsp,%rsi,4)
incl %r15d
movslq %ebx, %rsi
leaq (%r11,%rsi,2), %rbx
jmp 0x415c6
addl 0x7c0(%r10), %ecx
addl %r9d, %r15d
movslq %edi, %rax
movl %r8d, 0x20(%rsp,%rax,4)
cmpl $0x1, %r15d
jg 0x4167f
incl %edi
movl %r15d, %r9d
movl 0xc(%rsp), %ebp
movq 0x18(%rsp), %r15
incq %rdx
jmp 0x41524
cmpl $0x1, %ecx
jg 0x4164e
imull $0x7, %r14d, %eax
cltq
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rcx,%rax,4)
jne 0x4167f
movl $0x1, (%rcx,%rax,4)
jmp 0x4167f
cmpl $0x5, %ecx
movq 0x10(%rsp), %rbx
ja 0x41683
movq %r12, %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0x3fb94
imull $0x7, %r14d, %ecx
movslq %ecx, %rcx
movl (%rbx,%rcx,4), %edx
testb %al, %al
je 0x41698
cmpl $0x1, %edx
ja 0x4167f
movl $0x2, (%rbx,%rcx,4)
movb $0x1, %al
jmp 0x416a6
imull $0x7, %r14d, %eax
cltq
cmpl $0x2, (%rbx,%rax,4)
ja 0x416a4
movl $0x3, (%rbx,%rax,4)
jmp 0x416a4
cmpl $0x2, %edx
ja 0x416a4
movl $0x3, (%rbx,%rcx,4)
xorl %eax, %eax
addq $0x5e8, %rsp            # imm = 0x5E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq