// File: /workspace/github_cpp_makefile_star2_O3/kobanium[P]Ray/src/feature/Semeai.cpp
// Function: IsSelfAtari(game_info_t const*, int, int)
bool
IsSelfAtari( const 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 already[4] = { 0 };
  int already_num = 0;
  int lib, count = 0, libs = 0;
  int lib_candidate[10];
  int i;
  int id;
  bool 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 false;

  // 上を調べる
  if (board[NORTH(pos)] == color) {
    id = string_id[NORTH(pos)];
    if (string[id].libs > 2) return false;
    lib = string[id].lib[0];
    count = 0;
    while (lib != LIBERTY_END) {
      if (lib != pos) {
        checked = false;
        for (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;
    already[already_num++] = id;
    if (libs >= 2) return false;
  } else if (board[NORTH(pos)] == other &&
             string[string_id[NORTH(pos)]].libs == 1) {
    return false;
  }

  // 左を調べる
  if (board[WEST(pos)] == color) {
    id = string_id[WEST(pos)];
    if (already[0] != id) {
      if (string[id].libs > 2) return false;
      lib = string[id].lib[0];
      count = 0;
      while (lib != LIBERTY_END) {
        if (lib != pos) {
          checked = false;
          for (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;
      already[already_num++] = id;
      if (libs >= 2) return false;
    }
  } else if (board[WEST(pos)] == other &&
             string[string_id[WEST(pos)]].libs == 1) {
    return false;
  }

  // 右を調べる
  if (board[EAST(pos)] == color) {
    id = string_id[EAST(pos)];
    if (already[0] != id && already[1] != id) {
      if (string[id].libs > 2) return false;
      lib = string[id].lib[0];
      count = 0;
      while (lib != LIBERTY_END) {
        if (lib != pos) {
          checked = false;
          for (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;
      already[already_num++] = id;
      if (libs >= 2) return false;
    }
  } else if (board[EAST(pos)] == other &&
             string[string_id[EAST(pos)]].libs == 1) {
    return false;
  }


  // 下を調べる
  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 false;
      lib = string[id].lib[0];
      count = 0;
      while (lib != LIBERTY_END) {
        if (lib != pos) {
          checked = false;
          for (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;
      already[already_num++] = id;
      if (libs >= 2) return false;
    }
  } else if (board[SOUTH(pos)] == other &&
             string[string_id[SOUTH(pos)]].libs == 1) {
    return false;
  }

  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0x58(%rsp)
leaq 0x1a9bc(%rip), %rax     # 0x768bc
movl (%rax), %r8d
movl %edx, %r9d
subl %r8d, %r9d
movslq %r9d, %rax
movsbl 0x43f8(%rdi,%rax), %r14d
xorl %ecx, %ecx
testl %r14d, %r14d
jne 0x5bf26
movl %r9d, -0x28(%rsp)
movl $0x1, %ecx
movslq %edx, %rbx
movsbl 0x43f7(%rdi,%rbx), %r15d
testl %r15d, %r15d
jne 0x5bf45
leal -0x1(%rdx), %r9d
movl %ecx, %r10d
incl %ecx
movl %r9d, -0x28(%rsp,%r10,4)
movsbl 0x43f9(%rdi,%rbx), %ebp
testl %ebp, %ebp
jne 0x5bf5f
leal 0x1(%rdx), %r9d
movl %ecx, %r10d
incl %ecx
movl %r9d, -0x28(%rsp,%r10,4)
addl %edx, %r8d
movslq %r8d, %r11
movsbl 0x43f8(%rdi,%r11), %r10d
testl %r10d, %r10d
jne 0x5bf7d
movl %ecx, %r9d
incl %ecx
movl %r8d, -0x28(%rsp,%r9,4)
cmpl $0x1, %ecx
jbe 0x5bf8f
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r11, -0x30(%rsp)
leaq 0x9620(%rdi), %r13
movl %esi, %r11d
xorl $0x3, %r11d
cmpl %esi, %r14d
jne 0x5c03e
movslq 0x95720(%rdi,%rax,4), %r12
imulq $0x7c8, %r12, %r14      # imm = 0x7C8
xorl %eax, %eax
cmpl $0x2, 0x4(%r13,%r14)
jg 0x5bf84
movq %r13, -0x40(%rsp)
addq %r13, %r14
movzwl 0x8(%r14), %r13d
cmpl $0x2b7, %r13d           # imm = 0x2B7
je 0x5c01f
xorl %eax, %eax
movswl %r13w, %r13d
cmpl %edx, %r13d
je 0x5c00d
xorl %r9d, %r9d
movl -0x28(%rsp), %r8d
cmpl %ecx, %r9d
jae 0x5bfff
movl $0x1, %r9d
cmpl %r13d, %r8d
jne 0x5bfed
jmp 0x5c00d
leal (%rax,%rcx), %r8d
movslq %r8d, %r8
movl %r13d, -0x28(%rsp,%r8,4)
incl %eax
movslq %r13d, %r8
movzwl 0x8(%r14,%r8,2), %r13d
cmpl $0x2b7, %r13d           # imm = 0x2B7
jne 0x5bfdc
addl %ecx, %eax
movl %r12d, -0x58(%rsp)
movl $0x1, %r9d
movl %eax, %ecx
cmpl $0x1, %eax
movq -0x40(%rsp), %r13
jg 0x5bf82
jmp 0x5c074
xorl %r12d, %r12d
cmpl %r14d, %r11d
jne 0x5c071
movslq 0x95720(%rdi,%rax,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
xorl %r12d, %r12d
cmpl $0x1, 0x4(%r13,%rax)
movl $0x0, %r9d
movl $0x0, %eax
je 0x5bf84
jmp 0x5c074
xorl %r9d, %r9d
cmpl %esi, %r15d
jne 0x5c12a
movl 0x9571c(%rdi,%rbx,4), %r8d
cmpl %r8d, %r12d
je 0x5c14a
movslq %r8d, %rax
imulq $0x7c8, %rax, %r12      # imm = 0x7C8
xorl %eax, %eax
cmpl $0x2, 0x4(%r13,%r12)
jg 0x5bf84
movl %r8d, -0x34(%rsp)
movq %r13, -0x40(%rsp)
addq %r13, %r12
movzwl 0x8(%r12), %r14d
cmpl $0x2b7, %r14d           # imm = 0x2B7
je 0x5c106
movl %ecx, %r13d
xorl %eax, %eax
movswl %r14w, %r14d
cmpl %edx, %r14d
je 0x5c0f4
testl %ecx, %ecx
jle 0x5c0e6
xorl %r15d, %r15d
cmpl %r14d, -0x28(%rsp,%r15,4)
je 0x5c0f4
incq %r15
cmpq %r15, %r13
jne 0x5c0d7
leal (%rax,%rcx), %r8d
movslq %r8d, %r8
movl %r14d, -0x28(%rsp,%r8,4)
incl %eax
movslq %r14d, %r8
movzwl 0x8(%r12,%r8,2), %r14d
cmpl $0x2b7, %r14d           # imm = 0x2B7
jne 0x5c0c7
addl %ecx, %eax
movl %r9d, %ecx
movl -0x34(%rsp), %r8d
movl %r8d, -0x58(%rsp,%rcx,4)
cmpl $0x1, %eax
jg 0x5bf82
incl %r9d
movl %eax, %ecx
movq -0x40(%rsp), %r13
jmp 0x5c14a
cmpl %r15d, %r11d
jne 0x5c14a
movslq 0x9571c(%rdi,%rbx,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r13,%rax)
je 0x5bf82
cmpl %esi, %ebp
jne 0x5c1ec
movl 0x95724(%rdi,%rbx,4), %ebx
cmpl %ebx, -0x58(%rsp)
je 0x5c20c
cmpl %ebx, -0x54(%rsp)
je 0x5c20c
movslq %ebx, %rax
imulq $0x7c8, %rax, %r15      # imm = 0x7C8
xorl %eax, %eax
cmpl $0x2, 0x4(%r13,%r15)
jg 0x5bf84
addq %r13, %r15
movzwl 0x8(%r15), %ebp
cmpl $0x2b7, %ebp            # imm = 0x2B7
je 0x5c1d6
movl %ecx, %r12d
xorl %eax, %eax
movswl %bp, %ebp
cmpl %edx, %ebp
je 0x5c1c5
testl %ecx, %ecx
jle 0x5c1b7
xorl %r14d, %r14d
cmpl %ebp, -0x28(%rsp,%r14,4)
je 0x5c1c5
incq %r14
cmpq %r14, %r12
jne 0x5c1a8
leal (%rax,%rcx), %r8d
movslq %r8d, %r8
movl %ebp, -0x28(%rsp,%r8,4)
incl %eax
movslq %ebp, %r8
movzwl 0x8(%r15,%r8,2), %ebp
cmpl $0x2b7, %ebp            # imm = 0x2B7
jne 0x5c19a
addl %ecx, %eax
movl %r9d, %ecx
movl %ebx, -0x58(%rsp,%rcx,4)
movl %eax, %ecx
cmpl $0x1, %eax
jg 0x5bf82
jmp 0x5c20c
cmpl %ebp, %r11d
jne 0x5c20c
movslq 0x95724(%rdi,%rbx,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r13,%rax)
je 0x5bf82
cmpl %esi, %r10d
jne 0x5c2b3
movq -0x30(%rsp), %rax
movl 0x95720(%rdi,%rax,4), %eax
cmpl %eax, -0x58(%rsp)
je 0x5c2d8
cmpl %eax, -0x54(%rsp)
je 0x5c2d8
cmpl %eax, -0x50(%rsp)
je 0x5c2d8
cltq
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
xorl %eax, %eax
cmpl $0x2, 0x4(%r13,%rsi)
jg 0x5bf84
addq %rsi, %r13
movzwl 0x8(%r13), %edi
cmpl $0x2b7, %edi            # imm = 0x2B7
je 0x5c2a6
movl %ecx, %esi
xorl %eax, %eax
movswl %di, %edi
cmpl %edx, %edi
je 0x5c295
testl %ecx, %ecx
jle 0x5c287
xorl %r9d, %r9d
cmpl %edi, -0x28(%rsp,%r9,4)
je 0x5c295
incq %r9
cmpq %r9, %rsi
jne 0x5c278
leal (%rax,%rcx), %r8d
movslq %r8d, %r8
movl %edi, -0x28(%rsp,%r8,4)
incl %eax
movslq %edi, %rdi
movzwl 0x8(%r13,%rdi,2), %edi
cmpl $0x2b7, %edi            # imm = 0x2B7
jne 0x5c26a
addl %ecx, %eax
cmpl $0x1, %eax
jg 0x5bf82
jmp 0x5c2d8
cmpl %r10d, %r11d
jne 0x5c2d8
movq -0x30(%rsp), %rax
movslq 0x95720(%rdi,%rax,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r13,%rax)
je 0x5bf82
movb $0x1, %al
jmp 0x5bf84