// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x22c3a(%rip), %rax     # 0x618bc
movl (%rax), %r10d
movl %edx, %eax
subl %r10d, %eax
movslq %eax, %rdi
movsbl 0x43f8(%rbx,%rdi), %r15d
xorl %ecx, %ecx
testl %r15d, %r15d
jne 0x3eca4
movl %eax, 0x40(%rsp)
pushq $0x1
popq %rcx
movslq %edx, %r14
movsbl 0x43f7(%rbx,%r14), %r12d
testl %r12d, %r12d
jne 0x3ecc2
leal -0x1(%rdx), %eax
movl %ecx, %r8d
incl %ecx
movl %eax, 0x40(%rsp,%r8,4)
movsbl 0x43f9(%rbx,%r14), %r11d
testl %r11d, %r11d
jne 0x3ecdd
leal 0x1(%rdx), %eax
movl %ecx, %r8d
incl %ecx
movl %eax, 0x40(%rsp,%r8,4)
addl %edx, %r10d
movslq %r10d, %r8
movsbl 0x43f8(%rbx,%r8), %r9d
testl %r9d, %r9d
jne 0x3ecfa
movl %ecx, %eax
incl %ecx
movl %r10d, 0x40(%rsp,%rax,4)
movb $0x1, %bpl
cmpl $0x1, %ecx
jbe 0x3ed13
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r8, 0x38(%rsp)
leaq 0x9620(%rbx), %rax
movl %esi, %r8d
xorl $0x3, %r8d
movl %esi, 0xc(%rsp)
cmpl %esi, %r15d
jne 0x3eda0
movl %r8d, 0x8(%rsp)
movslq 0x95720(%rbx,%rdi,4), %r13
imulq $0x7c8, %r13, %rdi      # imm = 0x7C8
cmpl $0x2, 0x4(%rax,%rdi)
jg 0x3ed02
pushq $0x1
popq %r10
movq %rax, 0x10(%rsp)
addq %rax, %rdi
leaq 0x8(%rdi), %rbp
xorl %r15d, %r15d
movq %rbp, %rax
movswl (%rax), %eax
cmpl $0x2b7, %eax            # imm = 0x2B7
je 0x3edd4
cmpl %edx, %eax
je 0x3ed91
xorl %esi, %esi
movl 0x40(%rsp), %r8d
cmpl %ecx, %esi
jae 0x3ed83
movl %r10d, %esi
cmpl %eax, %r8d
jne 0x3ed75
jmp 0x3ed91
leal (%r15,%rcx), %esi
movslq %esi, %rsi
movl %eax, 0x40(%rsp,%rsi,4)
incl %r15d
cltq
leaq (,%rax,2), %rax
addq %rbp, %rax
jmp 0x3ed60
xorl %r13d, %r13d
cmpl %r15d, %r8d
jne 0x3edf0
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 0x3ed02
jmp 0x3ee09
addl %ecx, %r15d
movl 0x7c0(%rdi), %edi
movl %r13d, 0x20(%rsp)
cmpl $0x1, %r15d
jle 0x3edf6
movb $0x1, %bpl
jmp 0x3ed02
xorl %edi, %edi
xorl %esi, %esi
jmp 0x3ee09
pushq $0x1
popq %rsi
movl %r15d, %ecx
movb $0x1, %bpl
movq 0x10(%rsp), %rax
movl 0x8(%rsp), %r8d
cmpl 0xc(%rsp), %r12d
jne 0x3eeab
movl 0x9571c(%rbx,%r14,4), %r12d
cmpl %r12d, %r13d
je 0x3eedd
movl %edi, 0x1c(%rsp)
movq %rax, 0x10(%rsp)
movslq %r12d, %rax
imulq $0x7c8, %rax, %rdi      # imm = 0x7C8
movq 0x10(%rsp), %rax
cmpl $0x2, 0x4(%rax,%rdi)
jg 0x3ed02
movl %esi, 0x18(%rsp)
movl %r8d, 0x8(%rsp)
addq %rax, %rdi
leaq 0x8(%rdi), %rsi
xorl %r13d, %r13d
testl %ecx, %ecx
movl $0x0, %r15d
cmovgl %ecx, %r15d
movq %rsi, %rax
movswl (%rax), %eax
cmpl $0x2b7, %eax            # imm = 0x2B7
je 0x3efba
cmpl %edx, %eax
je 0x3eea3
xorl %r8d, %r8d
cmpq %r8, %r15
je 0x3ee94
leaq 0x1(%r8), %r10
cmpl %eax, 0x40(%rsp,%r8,4)
movq %r10, %r8
jne 0x3ee7f
jmp 0x3eea3
leal (%rcx,%r13), %r8d
movslq %r8d, %r8
movl %eax, 0x40(%rsp,%r8,4)
incl %r13d
cltq
leaq (%rsi,%rax,2), %rax
jmp 0x3ee6a
cmpl %r12d, %r8d
jne 0x3eedd
movl %esi, %r10d
movq %rax, %rsi
movslq 0x9571c(%rbx,%r14,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rsi,%rax)
movq %rsi, %rax
movl %r10d, %esi
movl 0xc(%rsp), %r13d
je 0x3ed02
jmp 0x3eee2
movl 0xc(%rsp), %r13d
cmpl %r13d, %r11d
jne 0x3ef8f
movl 0x95724(%rbx,%r14,4), %r11d
cmpl %r11d, 0x20(%rsp)
je 0x3f026
cmpl %r11d, 0x24(%rsp)
je 0x3f026
movl %edi, 0x1c(%rsp)
movq %rax, 0x10(%rsp)
movslq %r11d, %rax
imulq $0x7c8, %rax, %rdi      # imm = 0x7C8
movq 0x10(%rsp), %rax
cmpl $0x2, 0x4(%rax,%rdi)
jg 0x3ed02
movl %esi, 0x18(%rsp)
movl %r8d, 0x8(%rsp)
addq %rax, %rdi
leaq 0x8(%rdi), %rsi
xorl %r12d, %r12d
testl %ecx, %ecx
movl $0x0, %r15d
cmovgl %ecx, %r15d
movq %rsi, %rax
movswl (%rax), %eax
cmpl $0x2b7, %eax            # imm = 0x2B7
je 0x3eff7
cmpl %edx, %eax
je 0x3ef87
xorl %r8d, %r8d
cmpq %r8, %r15
je 0x3ef78
leaq 0x1(%r8), %r10
cmpl %eax, 0x40(%rsp,%r8,4)
movq %r10, %r8
jne 0x3ef63
jmp 0x3ef87
leal (%r12,%rcx), %r8d
movslq %r8d, %r8
movl %eax, 0x40(%rsp,%r8,4)
incl %r12d
cltq
leaq (%rsi,%rax,2), %rax
jmp 0x3ef4e
cmpl %r11d, %r8d
jne 0x3f026
movq %rax, %rsi
movslq 0x95724(%rbx,%r14,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rsi,%rax)
movq %rsi, %rax
je 0x3ed02
jmp 0x3f026
movl 0x1c(%rsp), %eax
addl 0x7c0(%rdi), %eax
movl %eax, %edi
addl %ecx, %r13d
movl 0x18(%rsp), %esi
movl %esi, %eax
movl %r12d, 0x20(%rsp,%rax,4)
cmpl $0x1, %r13d
jg 0x3ed02
incl %esi
movl %r13d, %ecx
movl 0xc(%rsp), %r13d
movq 0x10(%rsp), %rax
movl 0x8(%rsp), %r8d
jmp 0x3eee2
addl %ecx, %r12d
movl 0x1c(%rsp), %eax
addl 0x7c0(%rdi), %eax
movl %eax, %edi
movl 0x18(%rsp), %eax
movl %r11d, 0x20(%rsp,%rax,4)
movl %r12d, %ecx
cmpl $0x1, %r12d
movq 0x10(%rsp), %rax
movl 0x8(%rsp), %r8d
jg 0x3ed02
cmpl %r13d, %r9d
jne 0x3f0ca
movq %rax, %rsi
movq 0x38(%rsp), %rax
movl 0x95720(%rbx,%rax,4), %eax
cmpl %eax, 0x20(%rsp)
je 0x3f108
cmpl %eax, 0x24(%rsp)
je 0x3f108
cmpl %eax, 0x28(%rsp)
je 0x3f108
cltq
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x2, 0x4(%rsi,%rax)
jg 0x3ed02
movl %edi, %r15d
addq %rax, %rsi
movq %rsi, %r11
addq $0x8, %rsi
xorl %edi, %edi
testl %ecx, %ecx
movl $0x0, %r8d
cmovgl %ecx, %r8d
movq %rsi, %rax
movswl (%rax), %eax
cmpl $0x2b7, %eax            # imm = 0x2B7
je 0x3f0f3
cmpl %edx, %eax
je 0x3f0c2
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x3f0b4
leaq 0x1(%r9), %r10
cmpl %eax, 0x40(%rsp,%r9,4)
movq %r10, %r9
jne 0x3f09f
jmp 0x3f0c2
leal (%rdi,%rcx), %r9d
movslq %r9d, %r9
movl %eax, 0x40(%rsp,%r9,4)
incl %edi
cltq
leaq (%rsi,%rax,2), %rax
jmp 0x3f08e
cmpl %r9d, %r8d
jne 0x3f108
movq %rax, %rcx
movq 0x38(%rsp), %rax
movslq 0x95720(%rbx,%rax,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rcx,%rax)
je 0x3ed02
jmp 0x3f108
addl %ecx, %edi
cmpl $0x1, %edi
jg 0x3ed02
movl %r15d, %edi
addl 0x7c0(%r11), %edi
cmpl $0x1, %edi
jg 0x3f12c
imulq $0x6, %r14, %rax
cmpb $0x0, 0x97372(%rbx,%rax)
jne 0x3ed02
movb $0x1, 0x97372(%rbx,%rax)
jmp 0x3ed02
cmpl $0x4, %edi
ja 0x3f163
movq %rbx, %rdi
movl %edx, %esi
movl %r13d, %edx
callq 0x3fa04
imulq $0x6, %r14, %rcx
movb 0x97372(%rbx,%rcx), %dl
testb %al, %al
je 0x3f17b
cmpb $0x1, %dl
ja 0x3ed02
movb $0x2, 0x97372(%rbx,%rcx)
jmp 0x3ed02
imulq $0x6, %r14, %rax
cmpb $0x2, 0x97372(%rbx,%rax)
ja 0x3f187
movb $0x3, 0x97372(%rbx,%rax)
jmp 0x3f187
testb %dl, %dl
jne 0x3f187
movb $0x1, 0x97372(%rbx,%rcx)
xorl %ebp, %ebp
jmp 0x3ed02