// File: /workspace/github_cpp_makefile_star2_O1/kobanium[P]Ray/src/feature/Semeai.cpp
// Function: IsMeaningfulSelfAtari(game_info_t const*, int, int)
bool
IsMeaningfulSelfAtari( 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 size = 0;
  int already[4] = { 0 };
  int already_num = 0;
  int lib, count = 0, libs = 0;
  int lib_candidate[10];
  int id;
  bool flag;
  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 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) {
    flag = true;
  } else if (size < 6) {
    if (IsNakadeSelfAtari(game, pos, color)) {
      if (already_num == 1 && libs == 1 &&
          eye_condition[Pat3(game->pat, lib_candidate[0])] == E_COMPLETE_ONE_EYE) {
        flag = false;
      } else {
        flag = true;
      }
    } else {
      flag = false;
    }
  } else {
    flag = false;
  }

  return flag;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, 0x4(%rsp)
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x19d3b(%rip), %rax     # 0x758bc
movl (%rax), %ecx
movl %edx, %esi
subl %ecx, %esi
movslq %esi, %r15
movsbl 0x43f8(%rdi,%r15), %r13d
xorl %r14d, %r14d
testl %r13d, %r13d
jne 0x5bba5
movl %esi, 0x30(%rsp)
movl $0x1, %r14d
movslq %edx, %r10
movsbl 0x43f7(%rbx,%r10), %r12d
testl %r12d, %r12d
jne 0x5bbc3
leal -0x1(%rdx), %eax
movl %r14d, %esi
incl %r14d
movl %eax, 0x30(%rsp,%rsi,4)
movsbl 0x43f9(%rbx,%r10), %r11d
testl %r11d, %r11d
jne 0x5bbde
leal 0x1(%rdx), %eax
movl %r14d, %esi
incl %r14d
movl %eax, 0x30(%rsp,%rsi,4)
addl %edx, %ecx
movslq %ecx, %rdi
movsbl 0x43f8(%rbx,%rdi), %r8d
testl %r8d, %r8d
jne 0x5bbfb
movl %r14d, %eax
incl %r14d
movl %ecx, 0x30(%rsp,%rax,4)
movb $0x1, %bpl
cmpl $0x1, %r14d
jle 0x5bc15
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9620(%rbx), %rcx
movl 0x4(%rsp), %eax
movl %eax, %r9d
xorl $0x3, %r9d
cmpl %eax, %r13d
jne 0x5bcc5
movslq 0x95720(%rbx,%r15,4), %r15
imulq $0x7c8, %r15, %rsi      # imm = 0x7C8
cmpl $0x2, 0x4(%rcx,%rsi)
jg 0x5bc04
movq %rcx, 0x20(%rsp)
addq %rcx, %rsi
movzwl 0x8(%rsi), %ebp
xorl %r13d, %r13d
cmpl $0x2b7, %ebp            # imm = 0x2B7
je 0x5bc9a
xorl %r13d, %r13d
movswl %bp, %ebp
cmpl %edx, %ebp
je 0x5bc8a
xorl %eax, %eax
movl 0x30(%rsp), %ecx
cmpl %r14d, %eax
jae 0x5bc7d
movl $0x1, %eax
cmpl %ebp, %ecx
jne 0x5bc6d
jmp 0x5bc8a
leal (%r14,%r13), %eax
cltq
movl %ebp, 0x30(%rsp,%rax,4)
incl %r13d
movslq %ebp, %rax
movzwl 0x8(%rsi,%rax,2), %ebp
cmpl $0x2b7, %ebp            # imm = 0x2B7
jne 0x5bc60
addl %r14d, %r13d
movl 0x7c0(%rsi), %esi
movl %r15d, 0x10(%rsp)
movl $0x1, %r15d
movl %r13d, %r14d
cmpl $0x1, %r13d
movb $0x1, %bpl
movq 0x20(%rsp), %rcx
jg 0x5bc04
jmp 0x5bcf1
xorl %esi, %esi
cmpl %r13d, %r9d
jne 0x5bcee
movslq 0x95720(%rbx,%r15,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rcx,%rax)
movl $0x0, %r15d
je 0x5bc04
jmp 0x5bcf1
xorl %r15d, %r15d
cmpl 0x4(%rsp), %r12d
jne 0x5bdc7
movl 0x9571c(%rbx,%r10,4), %r12d
cmpl %r12d, 0x10(%rsp)
je 0x5bde6
movl %r15d, 0xc(%rsp)
movslq %r12d, %rax
imulq $0x7c8, %rax, %r15      # imm = 0x7C8
cmpl $0x2, 0x4(%rcx,%r15)
jg 0x5bc04
movl %r12d, 0x2c(%rsp)
movl %esi, 0x8(%rsp)
movq %rcx, 0x20(%rsp)
addq %rcx, %r15
movzwl 0x8(%r15), %ebp
xorl %r13d, %r13d
cmpl $0x2b7, %ebp            # imm = 0x2B7
je 0x5bd8f
movl %r14d, %esi
xorl %r13d, %r13d
movswl %bp, %r12d
cmpl %edx, %r12d
je 0x5bd7e
testl %r14d, %r14d
jle 0x5bd70
xorl %ebp, %ebp
cmpl %r12d, 0x30(%rsp,%rbp,4)
je 0x5bd7e
incq %rbp
cmpq %rbp, %rsi
jne 0x5bd61
leal (%r14,%r13), %eax
cltq
movl %r12d, 0x30(%rsp,%rax,4)
incl %r13d
movslq %r12d, %rax
movzwl 0x8(%r15,%rax,2), %ebp
cmpl $0x2b7, %ebp            # imm = 0x2B7
jne 0x5bd51
movl 0x8(%rsp), %esi
addl 0x7c0(%r15), %esi
addl %r14d, %r13d
movl 0xc(%rsp), %r15d
movl %r15d, %eax
movl 0x2c(%rsp), %ecx
movl %ecx, 0x10(%rsp,%rax,4)
cmpl $0x1, %r13d
movb $0x1, %bpl
jg 0x5bc04
incl %r15d
movl %r13d, %r14d
movq 0x20(%rsp), %rcx
jmp 0x5bde6
cmpl %r12d, %r9d
jne 0x5bde6
movslq 0x9571c(%rbx,%r10,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rcx,%rax)
je 0x5bc04
cmpl 0x4(%rsp), %r11d
jne 0x5beb6
movl 0x95724(%rbx,%r10,4), %r10d
cmpl %r10d, 0x10(%rsp)
je 0x5bed5
cmpl %r10d, 0x14(%rsp)
je 0x5bed5
movl %r15d, 0xc(%rsp)
movslq %r10d, %rax
imulq $0x7c8, %rax, %r15      # imm = 0x7C8
cmpl $0x2, 0x4(%rcx,%r15)
jg 0x5bc04
movl %esi, 0x8(%rsp)
addq %rcx, %r15
movzwl 0x8(%r15), %ebp
xorl %r11d, %r11d
cmpl $0x2b7, %ebp            # imm = 0x2B7
je 0x5be86
movl %r14d, %esi
xorl %r11d, %r11d
movswl %bp, %r12d
cmpl %edx, %r12d
je 0x5be75
testl %r14d, %r14d
jle 0x5be67
xorl %r13d, %r13d
cmpl %r12d, 0x30(%rsp,%r13,4)
je 0x5be75
incq %r13
cmpq %r13, %rsi
jne 0x5be58
leal (%r11,%r14), %eax
cltq
movl %r12d, 0x30(%rsp,%rax,4)
incl %r11d
movslq %r12d, %rax
movzwl 0x8(%r15,%rax,2), %ebp
cmpl $0x2b7, %ebp            # imm = 0x2B7
jne 0x5be47
movl 0x8(%rsp), %esi
addl 0x7c0(%r15), %esi
addl %r14d, %r11d
movl 0xc(%rsp), %r15d
movl %r15d, %eax
movl %r10d, 0x10(%rsp,%rax,4)
cmpl $0x1, %r11d
movb $0x1, %bpl
jg 0x5bc04
incl %r15d
movl %r11d, %r14d
jmp 0x5bed5
cmpl %r11d, %r9d
jne 0x5bed5
movslq 0x95724(%rbx,%r10,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rcx,%rax)
je 0x5bc04
cmpl 0x4(%rsp), %r8d
jne 0x5bf9c
movl 0x95720(%rbx,%rdi,4), %edi
cmpl %edi, 0x10(%rsp)
je 0x5bfbb
cmpl %edi, 0x14(%rsp)
je 0x5bfbb
cmpl %edi, 0x18(%rsp)
je 0x5bfbb
movslq %edi, %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x2, 0x4(%rcx,%rax)
jg 0x5bc04
movl %esi, %r11d
addq %rax, %rcx
movzwl 0x8(%rcx), %r9d
xorl %r8d, %r8d
cmpl $0x2b7, %r9d            # imm = 0x2B7
je 0x5bf77
movl %r14d, %esi
xorl %r8d, %r8d
movswl %r9w, %r9d
cmpl %edx, %r9d
je 0x5bf65
testl %r14d, %r14d
jle 0x5bf57
xorl %r10d, %r10d
cmpl %r9d, 0x30(%rsp,%r10,4)
je 0x5bf65
incq %r10
cmpq %r10, %rsi
jne 0x5bf48
leal (%r8,%r14), %eax
cltq
movl %r9d, 0x30(%rsp,%rax,4)
incl %r8d
movslq %r9d, %rax
movzwl 0x8(%rcx,%rax,2), %r9d
cmpl $0x2b7, %r9d            # imm = 0x2B7
jne 0x5bf37
movl %r11d, %esi
addl 0x7c0(%rcx), %esi
addl %r14d, %r8d
movl %r15d, %eax
movl %edi, 0x10(%rsp,%rax,4)
cmpl $0x1, %r8d
jg 0x5bc04
incl %r15d
movl %r8d, %r14d
jmp 0x5bfbb
cmpl %r8d, %r9d
jne 0x5bfbb
movslq 0x95720(%rbx,%rdi,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rcx,%rax)
je 0x5bc04
cmpl $0x2, %esi
jl 0x5bc04
cmpl $0x5, %esi
jg 0x5c015
movq %rbx, %rdi
movl %edx, %esi
movl 0x4(%rsp), %edx
callq 0x58be7
testb %al, %al
je 0x5c015
cmpl $0x1, %r15d
jne 0x5bc04
cmpl $0x1, %r14d
jne 0x5bc04
addq $0x4748, %rbx           # imm = 0x4748
movl 0x30(%rsp), %esi
movq %rbx, %rdi
callq 0x4f84b
movl %eax, %eax
leaq 0x1aaf26d5(%rip), %rcx  # 0x1ab4e6e0
cmpb $0x5, (%rcx,%rax)
jne 0x5bc04
xorl %ebp, %ebp
jmp 0x5bc04