// File: /workspace/github_cpp_makefile_star2_O1/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
movl %esi, 0xc(%rsp)
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x1da7d(%rip), %rax     # 0x758bc
movl (%rax), %esi
movl %edx, %edi
subl %esi, %edi
movslq %edi, %r11
movsbl 0x43f8(%rbx,%r11), %r13d
xorl %ecx, %ecx
testl %r13d, %r13d
jne 0x57e61
movl %edi, 0x30(%rsp)
movl $0x1, %ecx
movslq %edx, %r14
movsbl 0x43f7(%rbx,%r14), %r12d
testl %r12d, %r12d
jne 0x57e7d
leal -0x1(%rdx), %eax
movl %ecx, %edi
incl %ecx
movl %eax, 0x30(%rsp,%rdi,4)
movsbl 0x43f9(%rbx,%r14), %r15d
testl %r15d, %r15d
jne 0x57e96
leal 0x1(%rdx), %eax
movl %ecx, %edi
incl %ecx
movl %eax, 0x30(%rsp,%rdi,4)
addl %edx, %esi
movslq %esi, %r8
movsbl 0x43f8(%rbx,%r8), %r9d
testl %r9d, %r9d
jne 0x57eb1
movl %ecx, %eax
incl %ecx
movl %esi, 0x30(%rsp,%rax,4)
movb $0x1, %bpl
cmpl $0x1, %ecx
jle 0x57eca
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9620(%rbx), %rsi
movl 0xc(%rsp), %eax
movl %eax, %r10d
xorl $0x3, %r10d
cmpl %eax, %r13d
jne 0x57f79
movslq 0x95720(%rbx,%r11,4), %r11
imulq $0x7c8, %r11, %rdi      # imm = 0x7C8
cmpl $0x2, 0x4(%rsi,%rdi)
jg 0x57eb9
movq %rsi, 0x18(%rsp)
addq %rsi, %rdi
movzwl 0x8(%rdi), %ebp
xorl %r13d, %r13d
cmpl $0x2b7, %ebp            # imm = 0x2B7
je 0x57f4e
xorl %r13d, %r13d
movswl %bp, %ebp
cmpl %edx, %ebp
je 0x57f3e
xorl %eax, %eax
movl 0x30(%rsp), %esi
cmpl %ecx, %eax
jae 0x57f31
movl $0x1, %eax
cmpl %ebp, %esi
jne 0x57f22
jmp 0x57f3e
leal (%rcx,%r13), %eax
cltq
movl %ebp, 0x30(%rsp,%rax,4)
incl %r13d
movslq %ebp, %rax
movzwl 0x8(%rdi,%rax,2), %ebp
cmpl $0x2b7, %ebp            # imm = 0x2B7
jne 0x57f15
addl %ecx, %r13d
movl 0x7c0(%rdi), %edi
movl %r11d, 0x20(%rsp)
movl %r13d, %ecx
cmpl $0x1, %r13d
movl $0x1, %r13d
movb $0x1, %bpl
movq 0x18(%rsp), %rsi
jg 0x57eb9
jmp 0x57fa5
xorl %edi, %edi
cmpl %r13d, %r10d
jne 0x57fa2
movslq 0x95720(%rbx,%r11,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rsi,%rax)
movl $0x0, %r13d
je 0x57eb9
jmp 0x57fa5
xorl %r13d, %r13d
cmpl 0xc(%rsp), %r12d
jne 0x58076
movl 0x9571c(%rbx,%r14,4), %r11d
cmpl %r11d, 0x20(%rsp)
je 0x58095
movl %r13d, 0x10(%rsp)
movl %edi, 0x14(%rsp)
movslq %r11d, %rax
imulq $0x7c8, %rax, %r13      # imm = 0x7C8
cmpl $0x2, 0x4(%rsi,%r13)
jg 0x57eb9
movq %rsi, %rdi
movl %r11d, %esi
movq %rdi, 0x18(%rsp)
addq %rdi, %r13
movzwl 0x8(%r13), %ebp
xorl %r11d, %r11d
cmpl $0x2b7, %ebp            # imm = 0x2B7
je 0x58042
movl %ecx, %edi
xorl %r11d, %r11d
movswl %bp, %r12d
cmpl %edx, %r12d
je 0x58031
testl %ecx, %ecx
jle 0x58023
xorl %ebp, %ebp
cmpl %r12d, 0x30(%rsp,%rbp,4)
je 0x58031
incq %rbp
cmpq %rbp, %rdi
jne 0x58014
leal (%r11,%rcx), %eax
cltq
movl %r12d, 0x30(%rsp,%rax,4)
incl %r11d
movslq %r12d, %rax
movzwl 0x8(%r13,%rax,2), %ebp
cmpl $0x2b7, %ebp            # imm = 0x2B7
jne 0x58005
movl 0x14(%rsp), %edi
addl 0x7c0(%r13), %edi
addl %ecx, %r11d
movl 0x10(%rsp), %r13d
movl %r13d, %eax
movl %esi, 0x20(%rsp,%rax,4)
cmpl $0x1, %r11d
movb $0x1, %bpl
jg 0x57eb9
incl %r13d
movl %r11d, %ecx
movq 0x18(%rsp), %rsi
jmp 0x58095
cmpl %r12d, %r10d
jne 0x58095
movslq 0x9571c(%rbx,%r14,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rsi,%rax)
je 0x57eb9
cmpl 0xc(%rsp), %r15d
jne 0x5816e
movl 0x95724(%rbx,%r14,4), %r15d
cmpl %r15d, 0x20(%rsp)
je 0x5818d
cmpl %r15d, 0x24(%rsp)
je 0x5818d
movslq %r15d, %rax
imulq $0x7c8, %rax, %r12      # imm = 0x7C8
cmpl $0x2, 0x4(%rsi,%r12)
jg 0x57eb9
movl %r13d, 0x10(%rsp)
movl %edi, 0x14(%rsp)
movq %rsi, 0x18(%rsp)
addq %rsi, %r12
movzwl 0x8(%r12), %ebp
xorl %r11d, %r11d
cmpl $0x2b7, %ebp            # imm = 0x2B7
je 0x58138
movl %ecx, %edi
xorl %r11d, %r11d
movswl %bp, %r13d
cmpl %edx, %r13d
je 0x58127
testl %ecx, %ecx
jle 0x58119
xorl %ebp, %ebp
cmpl %r13d, 0x30(%rsp,%rbp,4)
je 0x58127
incq %rbp
cmpq %rbp, %rdi
jne 0x5810a
leal (%r11,%rcx), %eax
cltq
movl %r13d, 0x30(%rsp,%rax,4)
incl %r11d
movslq %r13d, %rax
movzwl 0x8(%r12,%rax,2), %ebp
cmpl $0x2b7, %ebp            # imm = 0x2B7
jne 0x580fb
movl 0x14(%rsp), %edi
addl 0x7c0(%r12), %edi
addl %ecx, %r11d
movl 0x10(%rsp), %r13d
movl %r13d, %eax
movl %r15d, 0x20(%rsp,%rax,4)
cmpl $0x1, %r11d
movb $0x1, %bpl
jg 0x57eb9
incl %r13d
movl %r11d, %ecx
movq 0x18(%rsp), %rsi
jmp 0x5818d
cmpl %r15d, %r10d
jne 0x5818d
movslq 0x95724(%rbx,%r14,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rsi,%rax)
je 0x57eb9
cmpl 0xc(%rsp), %r9d
jne 0x58251
movl 0x95720(%rbx,%r8,4), %r8d
cmpl %r8d, 0x20(%rsp)
je 0x58270
cmpl %r8d, 0x24(%rsp)
je 0x58270
cmpl %r8d, 0x28(%rsp)
je 0x58270
movslq %r8d, %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x2, 0x4(%rsi,%rax)
jg 0x57eb9
movl %edi, %r15d
addq %rax, %rsi
movzwl 0x8(%rsi), %r10d
xorl %r9d, %r9d
cmpl $0x2b7, %r10d           # imm = 0x2B7
je 0x58231
movl %ecx, %edi
xorl %r9d, %r9d
movswl %r10w, %r10d
cmpl %edx, %r10d
je 0x5821f
testl %ecx, %ecx
jle 0x58211
xorl %r11d, %r11d
cmpl %r10d, 0x30(%rsp,%r11,4)
je 0x5821f
incq %r11
cmpq %r11, %rdi
jne 0x58202
leal (%r9,%rcx), %eax
cltq
movl %r10d, 0x30(%rsp,%rax,4)
incl %r9d
movslq %r10d, %rax
movzwl 0x8(%rsi,%rax,2), %r10d
cmpl $0x2b7, %r10d           # imm = 0x2B7
jne 0x581f2
movl %r15d, %edi
addl 0x7c0(%rsi), %edi
addl %ecx, %r9d
movl %r13d, %eax
movl %r8d, 0x20(%rsp,%rax,4)
cmpl $0x1, %r9d
jg 0x57eb9
jmp 0x58270
cmpl %r9d, %r10d
jne 0x58270
movslq 0x95720(%rbx,%r8,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rsi,%rax)
je 0x57eb9
cmpl $0x1, %edi
jg 0x58297
addq %r14, %r14
leaq (%r14,%r14,2), %rax
cmpb $0x0, 0x97372(%rbx,%rax)
jne 0x57eb9
movb $0x1, 0x97372(%rbx,%rax)
jmp 0x57eb9
cmpl $0x4, %edi
jg 0x582d2
movq %rbx, %rdi
movl %edx, %esi
movl 0xc(%rsp), %edx
callq 0x58be7
addq %r14, %r14
leaq (%r14,%r14,2), %rcx
movb 0x97372(%rbx,%rcx), %dl
testb %al, %al
je 0x582ed
cmpb $0x1, %dl
ja 0x57eb9
movb $0x2, 0x97372(%rbx,%rcx)
jmp 0x57eb9
addq %r14, %r14
leaq (%r14,%r14,2), %rax
cmpb $0x2, 0x97372(%rbx,%rax)
ja 0x582f9
movb $0x3, 0x97372(%rbx,%rax)
jmp 0x582f9
testb %dl, %dl
jne 0x582f9
movb $0x1, 0x97372(%rbx,%rcx)
xorl %ebp, %ebp
jmp 0x57eb9