// File: /workspace/github_cpp_makefile_star2_O3/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 $0x5d8, %rsp            # imm = 0x5D8
movq %rcx, 0x8(%rsp)
movl %edx, %r14d
movl %esi, %r15d
movq %rdi, %r12
leaq 0x9620(%rdi), %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
movl %edx, %esi
callq 0x54386
xorl %ecx, %ecx
xorl %eax, %eax
movslq 0x20(%rsp,%rcx,4), %rdx
cmpb $0x0, 0x43f8(%r12,%rdx)
jne 0x5b1bc
movslq %eax, %rsi
incl %eax
movl %edx, 0x30(%rsp,%rsi,4)
incq %rcx
cmpq $0x4, %rcx
jne 0x5b1a3
movb $0x1, %bpl
cmpl $0x1, %eax
jle 0x5b1e1
movl %ebp, %eax
addq $0x5d8, %rsp            # imm = 0x5D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edx, %edx
xorl %ecx, %ecx
xorl %esi, %esi
movslq 0x20(%rsp,%rdx,4), %rdi
movsbl 0x43f8(%r12,%rdi), %r8d
cmpl %r15d, %r8d
jne 0x5b2bd
movl 0x95720(%r12,%rdi,4), %edi
testl %esi, %esi
jle 0x5b231
movl %esi, %r8d
xorl %r9d, %r9d
xorl %r10d, %r10d
cmpl %edi, 0x10(%rsp,%r9,4)
sete %r11b
orb %r11b, %r10b
incq %r9
cmpq %r9, %r8
jne 0x5b213
testb $0x1, %r10b
jne 0x5b2e1
movslq %edi, %r8
imulq $0x7c8, %r8, %r9        # imm = 0x7C8
cmpl $0x2, 0x4(%r13,%r9)
jg 0x5b1cd
addq %r13, %r9
movzwl 0x8(%r9), %r10d
xorl %r8d, %r8d
cmpl $0x2b7, %r10d           # imm = 0x2B7
je 0x5b29b
xorl %r8d, %r8d
movswl %r10w, %r10d
cmpl %r14d, %r10d
je 0x5b289
xorl %ebx, %ebx
movl 0x30(%rsp), %r11d
cmpl %eax, %ebx
jge 0x5b27a
movl $0x1, %ebx
cmpl %r10d, %r11d
jne 0x5b26a
jmp 0x5b289
leal (%r8,%rax), %r11d
movslq %r11d, %r11
movl %r10d, 0x30(%rsp,%r11,4)
incl %r8d
movslq %r10d, %r10
movzwl 0x8(%r9,%r10,2), %r10d
cmpl $0x2b7, %r10d           # imm = 0x2B7
jne 0x5b25a
addl 0x7c0(%r9), %ecx
addl %eax, %r8d
movslq %esi, %rax
movl %edi, 0x10(%rsp,%rax,4)
cmpl $0x1, %r8d
jg 0x5b1cd
incl %esi
movl %r8d, %eax
jmp 0x5b2e1
xorl %r15d, %r8d
cmpl $0x3, %r8d
jne 0x5b2e1
movslq 0x95720(%r12,%rdi,4), %rdi
imulq $0x7c8, %rdi, %rdi      # imm = 0x7C8
cmpl $0x1, 0x4(%r13,%rdi)
je 0x5b1cd
incq %rdx
cmpq $0x4, %rdx
jne 0x5b1e7
cmpl $0x1, %ecx
jg 0x5b31b
leal (,%r14,8), %eax
subl %r14d, %eax
cltq
movq 0x8(%rsp), %rcx
cmpl $0x0, (%rcx,%rax,4)
jne 0x5b1cd
movl $0x1, (%rcx,%rax,4)
jmp 0x5b1cd
cmpl $0x5, %ecx
ja 0x5b35d
movq %r12, %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0x59699
leal (,%r14,8), %ecx
subl %r14d, %ecx
movslq %ecx, %rcx
movq 0x8(%rsp), %rsi
movl (%rsi,%rcx,4), %edx
testb %al, %al
je 0x5b37e
cmpl $0x1, %edx
ja 0x5b1cd
movl $0x2, (%rsi,%rcx,4)
jmp 0x5b1cd
leal (,%r14,8), %eax
subl %r14d, %eax
cltq
movq 0x8(%rsp), %rcx
cmpl $0x2, (%rcx,%rax,4)
ja 0x5b38a
movl $0x3, (%rcx,%rax,4)
jmp 0x5b38a
cmpl $0x2, %edx
ja 0x5b38a
movl $0x3, (%rsi,%rcx,4)
xorl %ebp, %ebp
jmp 0x5b1cd