// File: /workspace/github_cpp_makefile_star2_O3/kobanium[P]Ray/src/feature/Semeai.cpp
// Function: IsCapturableAtariForSimulation(game_info_t const*, int, int, int)
bool
IsCapturableAtariForSimulation( const game_info_t *game, const int pos, const int color, const int id )
{
  const char *board = game->board;
  const string_t *string = game->string;
  const int *string_id = game->string_id;
  const int other = GetOppositeColor(color);
  int lib;
  bool neighbor = false;
  int index_distance;
  int pat3;
  int empty;
  int connect_libs = 0;
  int tmp_id;

  lib = string[id].lib[0];

  if (lib == pos) {
    lib = string[id].lib[lib];
  }

  index_distance = lib - pos;

  // 詰める方とは逆のダメの周囲の空点数を調べる
  pat3 = Pat3(game->pat, lib);
  empty = nb4_empty[pat3];

  // 逆のダメの周囲の空点数が3なら取れないのでfalse
  if (empty == 3) {
    return false;
  }

  if (index_distance ==           1) neighbor = true;
  if (index_distance ==          -1) neighbor = true;
  if (index_distance ==  board_size) neighbor = true;
  if (index_distance == -board_size) neighbor = true;

  // ダメが隣り合っている時と
  // ダメが離れている時の分岐
  if (( neighbor && empty >= 3) ||
      (!neighbor && empty >= 2)) {
    return false;
  }

  // 隣接する連がlib以外に持つ呼吸点の合計数が
  // 2以上なら無条件で1手で取れるアタリではないのでfalse

  // 上の確認
  if (board[NORTH(lib)] == other && 
      string_id[NORTH(lib)] != id) {
    tmp_id = string_id[NORTH(lib)];
    if (string[tmp_id].libs > 2) {
      return false;
    } else {
      connect_libs += string[tmp_id].libs - 1;
    }
  } 

  // 左の確認
  if (board[WEST(lib)] == other && 
      string_id[WEST(lib)] != id) {
    tmp_id = string_id[WEST(lib)];
    if (string[tmp_id].libs > 2) {
      return false;
    } else {
      connect_libs += string[tmp_id].libs - 1;
    }
  }

  // 右の確認
  if (board[EAST(lib)] == other && 
      string_id[EAST(lib)] != id) {
    tmp_id = string_id[EAST(lib)];
    if (string[tmp_id].libs > 2) {
      return false;
    } else {
      connect_libs += string[tmp_id].libs - 1;
    }
  }

  // 下の確認
  if (board[SOUTH(lib)] == other && 
      string_id[SOUTH(lib)] != id) {
    tmp_id = string_id[SOUTH(lib)];
    if (string[tmp_id].libs > 2) {
      return false;
    } else {
      connect_libs += string[tmp_id].libs - 1;
    }
  }

  // ダメに打っても増える呼吸点数が1以下なら
  // 1手で取れるアタリ
  if (( neighbor && connect_libs < 2) ||
      (!neighbor && connect_libs < 1)) {
    return true;
  } else {
    return false;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, %ebx
movl %edx, %ebp
movl %esi, %r12d
movq %rdi, %r14
leaq 0x9620(%rdi), %r13
movslq %ecx, %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
movswl 0x9628(%rdi,%rax), %r15d
cmpl %esi, %r15d
jne 0x5bbff
movslq %r15d, %rcx
addq %r13, %rax
movswl 0x8(%rax,%rcx,2), %r15d
leaq 0x4748(%r14), %rdi
movl %r15d, %esi
callq 0x4fc2a
cltq
leaq 0x1aae3ac9(%rip), %rcx  # 0x1ab3f6e0
movb (%rcx,%rax), %sil
cmpb $0x3, %sil
jne 0x5bc28
xorl %eax, %eax
jmp 0x5bd41
movl %r15d, %eax
subl %r12d, %eax
leal 0x1(%rax), %ecx
testl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
sete %dil
leaq 0x1ac7a(%rip), %rcx     # 0x768bc
movl (%rcx), %edx
cmpl %edx, %eax
sete %r8b
addl %edx, %eax
sete %cl
orb %r8b, %cl
orb %dil, %cl
leal 0x2(%rcx), %edi
xorl %eax, %eax
cmpb %dil, %sil
jae 0x5bd41
xorl $0x3, %ebp
movl %r15d, %esi
subl %edx, %esi
movslq %esi, %rsi
movsbl 0x43f8(%r14,%rsi), %edi
cmpl %edi, %ebp
jne 0x5bc9a
movslq 0x95720(%r14,%rsi,4), %rsi
cmpl %ebx, %esi
je 0x5bc9a
imulq $0x7c8, %rsi, %rax      # imm = 0x7C8
movl 0x4(%r13,%rax), %eax
cmpl $0x2, %eax
jg 0x5bc21
decl %eax
movslq %r15d, %rsi
movsbl 0x43f7(%r14,%rsi), %edi
cmpl %edi, %ebp
jne 0x5bccf
movslq 0x9571c(%r14,%rsi,4), %rdi
cmpl %ebx, %edi
je 0x5bccf
imulq $0x7c8, %rdi, %rdi      # imm = 0x7C8
movl 0x4(%r13,%rdi), %edi
cmpl $0x2, %edi
jg 0x5bc21
addl %edi, %eax
decl %eax
movsbl 0x43f9(%r14,%rsi), %edi
cmpl %edi, %ebp
jne 0x5bd01
movslq 0x95724(%r14,%rsi,4), %rsi
cmpl %ebx, %esi
je 0x5bd01
imulq $0x7c8, %rsi, %rsi      # imm = 0x7C8
movl 0x4(%r13,%rsi), %esi
cmpl $0x2, %esi
jg 0x5bc21
addl %esi, %eax
decl %eax
addl %r15d, %edx
movslq %edx, %rdx
movsbl 0x43f8(%r14,%rdx), %esi
cmpl %esi, %ebp
jne 0x5bd39
movslq 0x95720(%r14,%rdx,4), %rdx
cmpl %ebx, %edx
je 0x5bd39
imulq $0x7c8, %rdx, %rdx      # imm = 0x7C8
movl 0x4(%r13,%rdx), %edx
cmpl $0x2, %edx
jg 0x5bc21
addl %edx, %eax
decl %eax
movzbl %cl, %ecx
cmpl %ecx, %eax
setle %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq