// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movl %edx, %r14d
movl %esi, 0xc(%rsp)
movq %rdi, %r12
leaq 0x9620(%rdi), %rax
movq %rax, 0x18(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x30(%rsp), %rdi
movl %edx, %esi
callq 0x53e40
xorl %ecx, %ecx
xorl %r15d, %r15d
movslq 0x30(%rsp,%rcx,4), %rdx
cmpb $0x0, 0x43f8(%r12,%rdx)
jne 0x5a902
movslq %r15d, %rsi
incl %r15d
movl %edx, 0x40(%rsp,%rsi,4)
incq %rcx
cmpq $0x4, %rcx
jne 0x5a8e8
movb $0x1, %al
cmpl $0x1, %r15d
jg 0x5aab4
xorl %edi, %edi
movl $0x1, %esi
xorl %r8d, %r8d
xorl %ecx, %ecx
xorl %ebx, %ebx
movl 0xc(%rsp), %edx
movq 0x18(%rsp), %rax
movslq 0x30(%rsp,%rdi,4), %r9
movsbl 0x43f8(%r12,%r9), %r10d
cmpl %edx, %r10d
jne 0x5aa11
movl 0x95720(%r12,%r9,4), %r9d
testl %ebx, %ebx
jle 0x5a979
movl %ebx, %ebp
movl %ebx, %r10d
xorl %r11d, %r11d
xorl %ebx, %ebx
cmpl %r9d, 0x20(%rsp,%r11,4)
movzbl %bl, %ebx
cmovel %esi, %ebx
incq %r11
cmpq %r11, %r10
jne 0x5a95b
testb $0x1, %bl
movl %ebp, %ebx
jne 0x5aa31
movslq %r9d, %r10
imulq $0x7c8, %r10, %r11      # imm = 0x7C8
cmpl $0x2, 0x4(%rax,%r11)
jg 0x5aa50
movl %ebx, %edx
addq %rax, %r11
movzwl 0x8(%r11), %ebx
xorl %r10d, %r10d
cmpl $0x2b7, %ebx            # imm = 0x2B7
je 0x5a9e7
xorl %r10d, %r10d
movswl %bx, %ebx
cmpl %r14d, %ebx
je 0x5a9d6
xorl %r13d, %r13d
movl 0x40(%rsp), %ebp
cmpl %r15d, %r13d
jge 0x5a9c7
movl $0x1, %r13d
cmpl %ebx, %ebp
jne 0x5a9b6
jmp 0x5a9d6
leal (%r10,%r15), %ebp
movslq %ebp, %r13
movl %ebx, 0x40(%rsp,%r13,4)
incl %r10d
movslq %ebx, %rbx
movzwl 0x8(%r11,%rbx,2), %ebx
cmpl $0x2b7, %ebx            # imm = 0x2B7
jne 0x5a9a7
addl 0x7c0(%r11), %ecx
addl %r15d, %r10d
movslq %edx, %rax
movl %r9d, 0x20(%rsp,%rax,4)
cmpl $0x1, %r10d
jg 0x5aa4c
movl %edx, %ebx
incl %ebx
movl %r10d, %r15d
movl 0xc(%rsp), %edx
movq 0x18(%rsp), %rax
jmp 0x5aa31
xorl %edx, %r10d
cmpl $0x3, %r10d
jne 0x5aa31
movslq 0x95720(%r12,%r9,4), %r9
imulq $0x7c8, %r9, %r9        # imm = 0x7C8
cmpl $0x1, 0x4(%rax,%r9)
je 0x5aa50
cmpq $0x3, %rdi
leaq 0x1(%rdi), %r9
setae %r8b
movq %r9, %rdi
cmpq $0x4, %r9
jne 0x5a92e
jmp 0x5aa50
movl 0xc(%rsp), %edx
testb $0x1, %r8b
je 0x5aab2
cmpl $0x1, %ecx
jg 0x5aa7c
leal (,%r14,8), %eax
subl %r14d, %eax
cltq
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rcx,%rax,4)
jne 0x5aab2
movl $0x1, (%rcx,%rax,4)
jmp 0x5aab2
cmpl $0x5, %ecx
jg 0x5aac6
movq %r12, %rdi
movl %r14d, %esi
callq 0x58da7
leal (,%r14,8), %ecx
subl %r14d, %ecx
movslq %ecx, %rcx
movq 0x10(%rsp), %rsi
movl (%rsi,%rcx,4), %edx
testb %al, %al
je 0x5aae7
cmpl $0x1, %edx
ja 0x5aab2
movl $0x2, (%rsi,%rcx,4)
movb $0x1, %al
addq $0x5e8, %rsp            # imm = 0x5E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leal (,%r14,8), %eax
subl %r14d, %eax
cltq
movq 0x10(%rsp), %rcx
cmpl $0x2, (%rcx,%rax,4)
ja 0x5aaf3
movl $0x3, (%rcx,%rax,4)
jmp 0x5aaf3
cmpl $0x2, %edx
ja 0x5aaf3
movl $0x3, (%rsi,%rcx,4)
xorl %eax, %eax
jmp 0x5aab4