// File: /workspace/github_cpp_makefile_star2_O1/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 0x1a265(%rip), %rax     # 0x758bc
movl (%rax), %r8d
movl %edx, %r9d
subl %r8d, %r9d
movslq %r9d, %rax
movsbl 0x43f8(%rdi,%rax), %r12d
xorl %ecx, %ecx
testl %r12d, %r12d
jne 0x5b67d
movl %r9d, -0x28(%rsp)
movl $0x1, %ecx
movslq %edx, %r14
movsbl 0x43f7(%rdi,%r14), %r15d
testl %r15d, %r15d
jne 0x5b69c
leal -0x1(%rdx), %r9d
movl %ecx, %r10d
incl %ecx
movl %r9d, -0x28(%rsp,%r10,4)
movsbl 0x43f9(%rdi,%r14), %ebp
testl %ebp, %ebp
jne 0x5b6b7
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 0x5b6d5
movl %ecx, %r9d
incl %ecx
movl %r8d, -0x28(%rsp,%r9,4)
cmpl $0x1, %ecx
jle 0x5b6e7
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, %ebx
xorl $0x3, %ebx
cmpl %esi, %r12d
jne 0x5b795
movslq 0x95720(%rdi,%rax,4), %r11
imulq $0x7c8, %r11, %r12      # imm = 0x7C8
xorl %eax, %eax
cmpl $0x2, 0x4(%r13,%r12)
jg 0x5b6dc
movq %r13, -0x40(%rsp)
addq %r13, %r12
movzwl 0x8(%r12), %r13d
cmpl $0x2b7, %r13d           # imm = 0x2B7
je 0x5b776
xorl %eax, %eax
movswl %r13w, %r13d
cmpl %edx, %r13d
je 0x5b764
xorl %r9d, %r9d
movl -0x28(%rsp), %r8d
cmpl %ecx, %r9d
jae 0x5b756
movl $0x1, %r9d
cmpl %r13d, %r8d
jne 0x5b744
jmp 0x5b764
leal (%rax,%rcx), %r8d
movslq %r8d, %r8
movl %r13d, -0x28(%rsp,%r8,4)
incl %eax
movslq %r13d, %r8
movzwl 0x8(%r12,%r8,2), %r13d
cmpl $0x2b7, %r13d           # imm = 0x2B7
jne 0x5b733
addl %ecx, %eax
movl %r11d, -0x58(%rsp)
movl $0x1, %r11d
movl %eax, %ecx
cmpl $0x1, %eax
movq -0x40(%rsp), %r13
jg 0x5b6da
jmp 0x5b7c0
xorl %r11d, %r11d
cmpl %r12d, %ebx
jne 0x5b7c0
movslq 0x95720(%rdi,%rax,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
xorl %r11d, %r11d
cmpl $0x1, 0x4(%r13,%rax)
movl $0x0, %eax
je 0x5b6dc
cmpl %esi, %r15d
jne 0x5b87e
movl 0x9571c(%rdi,%r14,4), %r8d
cmpl %r8d, -0x58(%rsp)
je 0x5b89e
movslq %r8d, %rax
imulq $0x7c8, %rax, %r12      # imm = 0x7C8
xorl %eax, %eax
cmpl $0x2, 0x4(%r13,%r12)
jg 0x5b6dc
movl %r8d, -0x34(%rsp)
movl %r11d, %r9d
movq %r13, -0x40(%rsp)
addq %r13, %r12
movzwl 0x8(%r12), %r11d
cmpl $0x2b7, %r11d           # imm = 0x2B7
je 0x5b857
movl %ecx, %r13d
xorl %eax, %eax
movswl %r11w, %r11d
cmpl %edx, %r11d
je 0x5b845
testl %ecx, %ecx
jle 0x5b837
xorl %r15d, %r15d
cmpl %r11d, -0x28(%rsp,%r15,4)
je 0x5b845
incq %r15
cmpq %r15, %r13
jne 0x5b828
leal (%rax,%rcx), %r8d
movslq %r8d, %r8
movl %r11d, -0x28(%rsp,%r8,4)
incl %eax
movslq %r11d, %r8
movzwl 0x8(%r12,%r8,2), %r11d
cmpl $0x2b7, %r11d           # imm = 0x2B7
jne 0x5b818
addl %ecx, %eax
movl %r9d, %ecx
movl -0x34(%rsp), %r8d
movl %r8d, -0x58(%rsp,%rcx,4)
cmpl $0x1, %eax
jg 0x5b6da
movl %r9d, %r11d
incl %r11d
movl %eax, %ecx
movq -0x40(%rsp), %r13
jmp 0x5b89e
cmpl %r15d, %ebx
jne 0x5b89e
movslq 0x9571c(%rdi,%r14,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r13,%rax)
je 0x5b6da
cmpl %esi, %ebp
jne 0x5b94e
movl 0x95724(%rdi,%r14,4), %ebp
cmpl %ebp, -0x58(%rsp)
je 0x5b96d
cmpl %ebp, -0x54(%rsp)
je 0x5b96d
movslq %ebp, %rax
imulq $0x7c8, %rax, %r14      # imm = 0x7C8
xorl %eax, %eax
cmpl $0x2, 0x4(%r13,%r14)
jg 0x5b6dc
movl %r11d, %r9d
addq %r13, %r14
movzwl 0x8(%r14), %r11d
cmpl $0x2b7, %r11d           # imm = 0x2B7
je 0x5b932
movl %ecx, %r15d
xorl %eax, %eax
movswl %r11w, %r11d
cmpl %edx, %r11d
je 0x5b920
testl %ecx, %ecx
jle 0x5b912
xorl %r12d, %r12d
cmpl %r11d, -0x28(%rsp,%r12,4)
je 0x5b920
incq %r12
cmpq %r12, %r15
jne 0x5b903
leal (%rax,%rcx), %r8d
movslq %r8d, %r8
movl %r11d, -0x28(%rsp,%r8,4)
incl %eax
movslq %r11d, %r8
movzwl 0x8(%r14,%r8,2), %r11d
cmpl $0x2b7, %r11d           # imm = 0x2B7
jne 0x5b8f3
addl %ecx, %eax
movl %r9d, %ecx
movl %ebp, -0x58(%rsp,%rcx,4)
cmpl $0x1, %eax
jg 0x5b6da
movl %r9d, %r11d
incl %r11d
movl %eax, %ecx
jmp 0x5b96d
cmpl %ebp, %ebx
jne 0x5b96d
movslq 0x95724(%rdi,%r14,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r13,%rax)
je 0x5b6da
cmpl %esi, %r10d
jne 0x5ba20
movq -0x30(%rsp), %rax
movl 0x95720(%rdi,%rax,4), %esi
cmpl %esi, -0x58(%rsp)
je 0x5ba45
cmpl %esi, -0x54(%rsp)
je 0x5ba45
cmpl %esi, -0x50(%rsp)
je 0x5ba45
movslq %esi, %rax
imulq $0x7c8, %rax, %rdi      # imm = 0x7C8
xorl %eax, %eax
cmpl $0x2, 0x4(%r13,%rdi)
jg 0x5b6dc
addq %rdi, %r13
movzwl 0x8(%r13), %r9d
cmpl $0x2b7, %r9d            # imm = 0x2B7
je 0x5ba0c
movl %ecx, %edi
xorl %eax, %eax
movswl %r9w, %r9d
cmpl %edx, %r9d
je 0x5b9fa
testl %ecx, %ecx
jle 0x5b9ec
xorl %r10d, %r10d
cmpl %r9d, -0x28(%rsp,%r10,4)
je 0x5b9fa
incq %r10
cmpq %r10, %rdi
jne 0x5b9dd
leal (%rax,%rcx), %r8d
movslq %r8d, %r8
movl %r9d, -0x28(%rsp,%r8,4)
incl %eax
movslq %r9d, %r8
movzwl 0x8(%r13,%r8,2), %r9d
cmpl $0x2b7, %r9d            # imm = 0x2B7
jne 0x5b9cd
addl %ecx, %eax
movl %r11d, %ecx
movl %esi, -0x58(%rsp,%rcx,4)
cmpl $0x1, %eax
jg 0x5b6da
jmp 0x5ba45
cmpl %r10d, %ebx
jne 0x5ba45
movq -0x30(%rsp), %rax
movslq 0x95720(%rdi,%rax,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r13,%rax)
je 0x5b6da
movb $0x1, %al
jmp 0x5b6dc