// File: /workspace/github_cpp_makefile_star2_O3/kobanium[P]Ray/src/feature/SimulationFeature.cpp
// Function: CheckSelfAtariForSimulation(game_info_t*, int, int)
bool
CheckSelfAtariForSimulation( game_info_t *game, const int color, const int pos )
{
  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, id, count = 0, libs = 0, size = 0, already_num = 0;
  int already[4] = { 0 }, lib_candidate[10];
  bool flag, checked;

  // 上下左右が空点なら呼吸点の候補に入れる
  if (board[NORTH(pos)] == S_EMPTY) lib_candidate[libs++] = NORTH(pos);
  if (board[ WEST(pos)] == S_EMPTY) lib_candidate[libs++] =  WEST(pos);
  if (board[ EAST(pos)] == S_EMPTY) lib_candidate[libs++] =  EAST(pos);
  if (board[SOUTH(pos)] == S_EMPTY) lib_candidate[libs++] = SOUTH(pos);

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

  // 上を調べる
  if (board[NORTH(pos)] == color) {
    id = string_id[NORTH(pos)];
    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 i = 0; i < libs; i++) {
          if (lib_candidate[i] == 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[NORTH(pos)] == other &&
             string[string_id[NORTH(pos)]].libs == 1) {
    return true;
  }

  // 左を調べる
  if (board[WEST(pos)] == color) {
    id = string_id[WEST(pos)];
    if (already[0] != id) {
      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 i = 0; i < libs; i++) {
            if (lib_candidate[i] == 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[WEST(pos)] == other &&
             string[string_id[WEST(pos)]].libs == 1) {
    return true;
  }

  // 右を調べる
  if (board[EAST(pos)] == color) {
    id = string_id[EAST(pos)];
    if (already[0] != id && already[1] != id) {
      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 i = 0; i < libs; i++) {
            if (lib_candidate[i] == 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[EAST(pos)] == other &&
             string[string_id[EAST(pos)]].libs == 1) {
    return true;
  }


  // 下を調べる
  if (board[SOUTH(pos)] == color) {
    id = string_id[SOUTH(pos)];
    if (already[0] != id && already[1] != id && already[2] != id) {
      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 i = 0; i < libs; i++) {
            if (lib_candidate[i] == 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[SOUTH(pos)] == other &&
             string[string_id[SOUTH(pos)]].libs == 1) {
    return true;
  }

  // 自己アタリになる連の大きさが2以下,
  // または大きさが5以下でナカデの形になる場合は
  // 打っても良いものとする
  if (size < 2) {
    CompareSwapFeature(game->tactical_features, pos, CAPTURE, SIM_SELF_ATARI_SMALL);
    flag = true;
  } else if (size < 5) {
    if (IsNakadeSelfAtari(game, pos, color)) {
      CompareSwapFeature(game->tactical_features, pos, CAPTURE, SIM_SELF_ATARI_NAKADE);
      flag = true;
    } else {
      CompareSwapFeature(game->tactical_features, pos, CAPTURE, SIM_SELF_ATARI_SMALL);
      flag = false;
    }
  } else {
    CompareSwapFeature(game->tactical_features, pos, CAPTURE, SIM_SELF_ATARI_LARGE);
    flag = false;
  }

  return flag;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x1e181(%rip), %rax     # 0x768bc
movl (%rax), %r10d
movl %edx, %r8d
subl %r10d, %r8d
movslq %r8d, %rdi
movsbl 0x43f8(%rbx,%rdi), %r15d
xorl %ecx, %ecx
testl %r15d, %r15d
jne 0x58761
movl %r8d, 0x30(%rsp)
movl $0x1, %ecx
movslq %edx, %r14
movsbl 0x43f7(%rbx,%r14), %r12d
testl %r12d, %r12d
jne 0x5877f
leal -0x1(%rdx), %eax
movl %ecx, %r8d
incl %ecx
movl %eax, 0x30(%rsp,%r8,4)
movsbl 0x43f9(%rbx,%r14), %r11d
testl %r11d, %r11d
jne 0x5879a
leal 0x1(%rdx), %eax
movl %ecx, %r8d
incl %ecx
movl %eax, 0x30(%rsp,%r8,4)
addl %edx, %r10d
movslq %r10d, %r8
movsbl 0x43f8(%rbx,%r8), %r9d
testl %r9d, %r9d
jne 0x587b7
movl %ecx, %eax
incl %ecx
movl %r10d, 0x30(%rsp,%rax,4)
movb $0x1, %bpl
cmpl $0x1, %ecx
jbe 0x587d0
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9620(%rbx), %rax
movl %esi, %r10d
xorl $0x3, %r10d
movl %esi, (%rsp)
cmpl %esi, %r15d
jne 0x5887d
movslq 0x95720(%rbx,%rdi,4), %r13
imulq $0x7c8, %r13, %rdi      # imm = 0x7C8
cmpl $0x2, 0x4(%rax,%rdi)
jg 0x587bf
movq %rax, 0x8(%rsp)
addq %rax, %rdi
movzwl 0x8(%rdi), %esi
xorl %r15d, %r15d
cmpl $0x2b7, %esi            # imm = 0x2B7
je 0x58853
xorl %r15d, %r15d
movswl %si, %esi
cmpl %edx, %esi
je 0x58843
xorl %eax, %eax
movl 0x30(%rsp), %ebp
cmpl %ecx, %eax
jae 0x58836
movl $0x1, %eax
cmpl %esi, %ebp
jne 0x58827
jmp 0x58843
leal (%r15,%rcx), %eax
cltq
movl %esi, 0x30(%rsp,%rax,4)
incl %r15d
movslq %esi, %rax
movzwl 0x8(%rdi,%rax,2), %esi
cmpl $0x2b7, %esi            # imm = 0x2B7
jne 0x5881a
addl %ecx, %r15d
movl 0x7c0(%rdi), %edi
movl %r13d, 0x10(%rsp)
movl $0x1, %esi
movl %r15d, %ecx
cmpl $0x1, %r15d
movb $0x1, %bpl
movq 0x8(%rsp), %rax
jg 0x587bf
jmp 0x588b5
xorl %r13d, %r13d
cmpl %r15d, %r10d
jne 0x588b1
movq %rax, %rsi
movslq 0x95720(%rbx,%rdi,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rsi,%rax)
movq %rsi, %rax
movl $0x0, %edi
movl $0x0, %esi
je 0x587bf
jmp 0x588b5
xorl %edi, %edi
xorl %esi, %esi
cmpl (%rsp), %r12d
jne 0x58989
movl 0x9571c(%rbx,%r14,4), %r15d
cmpl %r15d, %r13d
je 0x589ba
movq %rax, 0x8(%rsp)
movslq %r15d, %rax
imulq $0x7c8, %rax, %r13      # imm = 0x7C8
movq 0x8(%rsp), %rax
cmpl $0x2, 0x4(%rax,%r13)
jg 0x587bf
movl %r15d, 0x2c(%rsp)
movl %esi, %ebp
movl %edi, 0x4(%rsp)
addq %rax, %r13
movzwl 0x8(%r13), %esi
xorl %edi, %edi
cmpl $0x2b7, %esi            # imm = 0x2B7
je 0x5894d
movl %ecx, %r15d
xorl %edi, %edi
movswl %si, %r12d
cmpl %edx, %r12d
je 0x5893c
testl %ecx, %ecx
jle 0x58930
xorl %esi, %esi
cmpl %r12d, 0x30(%rsp,%rsi,4)
je 0x5893c
incq %rsi
cmpq %rsi, %r15
jne 0x58921
leal (%rdi,%rcx), %eax
cltq
movl %r12d, 0x30(%rsp,%rax,4)
incl %edi
movslq %r12d, %rax
movzwl 0x8(%r13,%rax,2), %esi
cmpl $0x2b7, %esi            # imm = 0x2B7
jne 0x58912
movl 0x4(%rsp), %eax
addl 0x7c0(%r13), %eax
movl %eax, 0x4(%rsp)
addl %ecx, %edi
movl %ebp, %esi
movl %ebp, %eax
movl 0x2c(%rsp), %ecx
movl %ecx, 0x10(%rsp,%rax,4)
cmpl $0x1, %edi
movb $0x1, %bpl
jg 0x587bf
incl %esi
movl %edi, %ecx
movl (%rsp), %r15d
movq 0x8(%rsp), %rax
movl 0x4(%rsp), %edi
jmp 0x589be
cmpl %r12d, %r10d
jne 0x589ba
movl %esi, %r15d
movq %rax, %rsi
movslq 0x9571c(%rbx,%r14,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rsi,%rax)
movq %rsi, %rax
movl %r15d, %esi
movl (%rsp), %r15d
je 0x587bf
jmp 0x589be
movl (%rsp), %r15d
cmpl %r15d, %r11d
jne 0x58a8c
movl 0x95724(%rbx,%r14,4), %r11d
cmpl %r11d, 0x10(%rsp)
je 0x58ab1
cmpl %r11d, 0x14(%rsp)
je 0x58ab1
movq %rax, 0x8(%rsp)
movslq %r11d, %rax
imulq $0x7c8, %rax, %r12      # imm = 0x7C8
movq 0x8(%rsp), %rax
cmpl $0x2, 0x4(%rax,%r12)
jg 0x587bf
movl %esi, 0x4(%rsp)
movl %edi, %ebp
addq %rax, %r12
movzwl 0x8(%r12), %esi
xorl %edi, %edi
cmpl $0x2b7, %esi            # imm = 0x2B7
je 0x58a5e
movl %ecx, %r15d
xorl %edi, %edi
movswl %si, %r13d
cmpl %edx, %r13d
je 0x58a4d
testl %ecx, %ecx
jle 0x58a41
xorl %esi, %esi
cmpl %r13d, 0x30(%rsp,%rsi,4)
je 0x58a4d
incq %rsi
cmpq %rsi, %r15
jne 0x58a32
leal (%rdi,%rcx), %eax
cltq
movl %r13d, 0x30(%rsp,%rax,4)
incl %edi
movslq %r13d, %rax
movzwl 0x8(%r12,%rax,2), %esi
cmpl $0x2b7, %esi            # imm = 0x2B7
jne 0x58a23
addl %ecx, %edi
addl 0x7c0(%r12), %ebp
movl 0x4(%rsp), %eax
movl %r11d, 0x10(%rsp,%rax,4)
movl %edi, %ecx
cmpl $0x1, %edi
movl %ebp, %edi
movb $0x1, %bpl
movl (%rsp), %r15d
movq 0x8(%rsp), %rax
jg 0x587bf
jmp 0x58ab1
cmpl %r11d, %r10d
jne 0x58ab1
movq %rax, %rsi
movslq 0x95724(%rbx,%r14,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rsi,%rax)
movq %rsi, %rax
je 0x587bf
cmpl %r15d, %r9d
jne 0x58b70
movl 0x95720(%rbx,%r8,4), %esi
cmpl %esi, 0x10(%rsp)
je 0x58b94
cmpl %esi, 0x14(%rsp)
je 0x58b94
cmpl %esi, 0x18(%rsp)
je 0x58b94
movq %rax, %r8
movslq %esi, %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x2, 0x4(%r8,%rax)
jg 0x58bef
movl %edi, %r11d
addq %rax, %r8
movzwl 0x8(%r8), %esi
xorl %edi, %edi
cmpl $0x2b7, %esi            # imm = 0x2B7
movq %r8, %rax
je 0x58b57
movl %ecx, %r8d
xorl %edi, %edi
movq %rax, %r10
movswl %si, %r9d
cmpl %edx, %r9d
je 0x58b43
testl %ecx, %ecx
jle 0x58b37
xorl %esi, %esi
cmpl %r9d, 0x30(%rsp,%rsi,4)
je 0x58b43
incq %rsi
cmpq %rsi, %r8
jne 0x58b28
leal (%rdi,%rcx), %eax
cltq
movl %r9d, 0x30(%rsp,%rax,4)
incl %edi
movslq %r9d, %rax
movzwl 0x8(%r10,%rax,2), %esi
movq %r10, %rax
cmpl $0x2b7, %esi            # imm = 0x2B7
jne 0x58b19
addl %ecx, %edi
cmpl $0x1, %edi
movb $0x1, %bpl
jg 0x587bf
movl %r11d, %edi
addl 0x7c0(%rax), %edi
jmp 0x58b97
cmpl %r9d, %r10d
jne 0x58b97
movq %rax, %rcx
movslq 0x95720(%rbx,%r8,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rcx,%rax)
je 0x587bf
jmp 0x58b97
movb $0x1, %bpl
cmpl $0x1, %edi
jg 0x58bbe
addq %r14, %r14
leaq (%r14,%r14,2), %rax
cmpb $0x0, 0x97372(%rbx,%rax)
jne 0x587bf
movb $0x1, 0x97372(%rbx,%rax)
jmp 0x587bf
cmpl $0x4, %edi
ja 0x58bf7
movq %rbx, %rdi
movl %edx, %esi
movl %r15d, %edx
callq 0x594d5
addq %r14, %r14
leaq (%r14,%r14,2), %rcx
movb 0x97372(%rbx,%rcx), %dl
testb %al, %al
je 0x58c12
cmpb $0x1, %dl
ja 0x58bef
movb $0x2, 0x97372(%rbx,%rcx)
movb $0x1, %bpl
jmp 0x587bf
addq %r14, %r14
leaq (%r14,%r14,2), %rax
cmpb $0x2, 0x97372(%rbx,%rax)
ja 0x58c1e
movb $0x3, 0x97372(%rbx,%rax)
jmp 0x58c1e
testb %dl, %dl
jne 0x58c1e
movb $0x1, 0x97372(%rbx,%rcx)
xorl %ebp, %ebp
jmp 0x587bf