// File: /workspace/github_cpp_makefile_star2_O2/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, -0x48(%rsp)
leaq 0x1f684(%rip), %rax     # 0x618bc
movl (%rax), %r8d
movl %edx, %r9d
subl %r8d, %r9d
movslq %r9d, %rax
movsbl 0x43f8(%rdi,%rax), %r14d
xorl %ecx, %ecx
testl %r14d, %r14d
jne 0x4225e
movl %r9d, -0x28(%rsp)
movl $0x1, %ecx
movslq %edx, %rbx
movsbl 0x43f7(%rdi,%rbx), %r15d
testl %r15d, %r15d
jne 0x4227d
leal -0x1(%rdx), %r9d
movl %ecx, %r10d
incl %ecx
movl %r9d, -0x28(%rsp,%r10,4)
movsbl 0x43f9(%rdi,%rbx), %ebp
testl %ebp, %ebp
jne 0x42297
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 0x422b5
movl %ecx, %r9d
incl %ecx
movl %r8d, -0x28(%rsp,%r9,4)
cmpl $0x1, %ecx
jbe 0x422c7
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r11, -0x50(%rsp)
leaq 0x9620(%rdi), %r8
movl %esi, %r9d
xorl $0x3, %r9d
cmpl %esi, %r14d
movl %r9d, -0x54(%rsp)
jne 0x42352
movslq 0x95720(%rdi,%rax,4), %r12
imulq $0x7c8, %r12, %r14      # imm = 0x7C8
xorl %eax, %eax
cmpl $0x2, 0x4(%r8,%r14)
jg 0x422bc
movl $0x1, %r11d
movq %r8, -0x30(%rsp)
addq %r8, %r14
addq $0x8, %r14
movq %r14, %r8
movswl (%r8), %r8d
cmpl $0x2b7, %r8d            # imm = 0x2B7
je 0x42385
cmpl %edx, %r8d
je 0x42349
xorl %r13d, %r13d
movl -0x28(%rsp), %r9d
cmpl %ecx, %r13d
jae 0x4233b
movl %r11d, %r13d
cmpl %r8d, %r9d
jne 0x4232c
jmp 0x42349
leal (%rax,%rcx), %r9d
movslq %r9d, %r9
movl %r8d, -0x28(%rsp,%r9,4)
incl %eax
movslq %r8d, %r8
leaq (%r14,%r8,2), %r8
jmp 0x42312
xorl %r12d, %r12d
cmpl %r14d, %r9d
jne 0x423a4
movslq 0x95720(%rdi,%rax,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
xorl %r12d, %r12d
cmpl $0x1, 0x4(%r8,%rax)
movl $0x0, %r14d
movl $0x0, %eax
je 0x422bc
jmp 0x423a7
addl %ecx, %eax
movl %r12d, -0x48(%rsp)
cmpl $0x1, %eax
jg 0x422ba
movl $0x1, %r14d
movl %eax, %ecx
movq -0x30(%rsp), %r8
jmp 0x423a7
xorl %r14d, %r14d
cmpl %esi, %r15d
jne 0x423c8
movl 0x9571c(%rdi,%rbx,4), %r15d
cmpl %r15d, %r12d
jne 0x423f8
movq -0x50(%rsp), %r9
movl -0x54(%rsp), %r11d
jmp 0x42475
movl -0x54(%rsp), %r11d
cmpl %r15d, %r11d
jne 0x42470
movslq 0x9571c(%rdi,%rbx,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r8,%rax)
movq -0x50(%rsp), %r9
je 0x422ba
jmp 0x42475
movslq %r15d, %rax
imulq $0x7c8, %rax, %r12      # imm = 0x7C8
xorl %eax, %eax
cmpl $0x2, 0x4(%r8,%r12)
jg 0x422bc
movq %r8, -0x30(%rsp)
addq %r8, %r12
addq $0x8, %r12
testl %ecx, %ecx
movl $0x0, %r13d
cmovgl %ecx, %r13d
movq %r12, %r8
movswl (%r8), %r8d
cmpl $0x2b7, %r8d            # imm = 0x2B7
je 0x4252f
cmpl %edx, %r8d
je 0x42467
xorl %r9d, %r9d
cmpq %r9, %r13
je 0x42459
leaq 0x1(%r9), %r11
cmpl %r8d, -0x28(%rsp,%r9,4)
movq %r11, %r9
jne 0x42444
jmp 0x42467
leal (%rax,%rcx), %r9d
movslq %r9d, %r9
movl %r8d, -0x28(%rsp,%r9,4)
incl %eax
movslq %r8d, %r8
leaq (%r12,%r8,2), %r8
jmp 0x4242b
movq -0x50(%rsp), %r9
cmpl %esi, %ebp
jne 0x4250d
movl 0x95724(%rdi,%rbx,4), %ebx
cmpl %ebx, -0x48(%rsp)
je 0x42570
cmpl %ebx, -0x44(%rsp)
je 0x42570
movslq %ebx, %rax
imulq $0x7c8, %rax, %r15      # imm = 0x7C8
xorl %eax, %eax
cmpl $0x2, 0x4(%r8,%r15)
jg 0x422bc
movl %r11d, %ebp
movq %r8, %r13
addq %r8, %r15
addq $0x8, %r15
testl %ecx, %ecx
movl $0x0, %r12d
cmovgl %ecx, %r12d
movq %r15, %r8
movswl (%r8), %r8d
cmpl $0x2b7, %r8d            # imm = 0x2B7
je 0x42551
cmpl %edx, %r8d
je 0x42504
xorl %r9d, %r9d
cmpq %r9, %r12
je 0x424f6
leaq 0x1(%r9), %r11
cmpl %r8d, -0x28(%rsp,%r9,4)
movq %r11, %r9
jne 0x424e1
jmp 0x42504
leal (%rax,%rcx), %r9d
movslq %r9d, %r9
movl %r8d, -0x28(%rsp,%r9,4)
incl %eax
movslq %r8d, %r8
leaq (%r15,%r8,2), %r8
jmp 0x424cc
cmpl %ebp, %r11d
jne 0x42570
movslq 0x95724(%rdi,%rbx,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r8,%rax)
je 0x422ba
jmp 0x42570
addl %ecx, %eax
movl %r14d, %ecx
movl %r15d, -0x48(%rsp,%rcx,4)
cmpl $0x1, %eax
jg 0x422ba
incl %r14d
movl %eax, %ecx
movq -0x30(%rsp), %r8
jmp 0x423b9
addl %ecx, %eax
movl %r14d, %ecx
movl %ebx, -0x48(%rsp,%rcx,4)
movl %eax, %ecx
cmpl $0x1, %eax
movq %r13, %r8
movq -0x50(%rsp), %r9
movl %ebp, %r11d
jg 0x422ba
cmpl %esi, %r10d
jne 0x4260e
movl 0x95720(%rdi,%r9,4), %eax
cmpl %eax, -0x48(%rsp)
je 0x4263b
cmpl %eax, -0x44(%rsp)
je 0x4263b
cmpl %eax, -0x40(%rsp)
je 0x4263b
cltq
imulq $0x7c8, %rax, %rsi      # imm = 0x7C8
xorl %eax, %eax
cmpl $0x2, 0x4(%r8,%rsi)
jg 0x422bc
addq %rsi, %r8
addq $0x8, %r8
testl %ecx, %ecx
movl $0x0, %esi
cmovgl %ecx, %esi
movq %r8, %rdi
movswl (%rdi), %edi
cmpl $0x2b7, %edi            # imm = 0x2B7
je 0x42630
cmpl %edx, %edi
je 0x42605
movq %r8, %r10
xorl %r8d, %r8d
cmpq %r8, %rsi
je 0x425f4
leaq 0x1(%r8), %r9
cmpl %edi, -0x28(%rsp,%r8,4)
movq %r9, %r8
jne 0x425df
jmp 0x42602
leal (%rax,%rcx), %r8d
movslq %r8d, %r8
movl %edi, -0x28(%rsp,%r8,4)
incl %eax
movq %r10, %r8
movslq %edi, %rdi
leaq (%r8,%rdi,2), %rdi
jmp 0x425ca
cmpl %r10d, %r11d
jne 0x4263b
movslq 0x95720(%rdi,%r9,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r8,%rax)
je 0x422ba
jmp 0x4263b
addl %ecx, %eax
cmpl $0x1, %eax
jg 0x422ba
movb $0x1, %al
jmp 0x422bc