// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x1a4b8(%rip), %rax     # 0x768bc
movl (%rax), %ecx
movl %edx, %edi
subl %ecx, %edi
movslq %edi, %r9
movsbl 0x43f8(%rbx,%r9), %r15d
xorl %r14d, %r14d
testl %r15d, %r15d
jne 0x5c428
movl %edi, 0x30(%rsp)
movl $0x1, %r14d
movslq %edx, %r10
movsbl 0x43f7(%rbx,%r10), %r12d
testl %r12d, %r12d
jne 0x5c446
leal -0x1(%rdx), %eax
movl %r14d, %edi
incl %r14d
movl %eax, 0x30(%rsp,%rdi,4)
movsbl 0x43f9(%rbx,%r10), %r11d
testl %r11d, %r11d
jne 0x5c461
leal 0x1(%rdx), %eax
movl %r14d, %edi
incl %r14d
movl %eax, 0x30(%rsp,%rdi,4)
addl %edx, %ecx
movslq %ecx, %rdi
movsbl 0x43f8(%rbx,%rdi), %r8d
testl %r8d, %r8d
jne 0x5c47e
movl %r14d, %eax
incl %r14d
movl %ecx, 0x30(%rsp,%rax,4)
movb $0x1, %bpl
cmpl $0x1, %r14d
jbe 0x5c498
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, 0x28(%rsp)
leaq 0x9620(%rbx), %rcx
movl %esi, %edi
xorl $0x3, %edi
movl %esi, 0x8(%rsp)
cmpl %esi, %r15d
jne 0x5c546
movslq 0x95720(%rbx,%r9,4), %r13
imulq $0x7c8, %r13, %rsi      # imm = 0x7C8
cmpl $0x2, 0x4(%rcx,%rsi)
jg 0x5c487
movq %rcx, %r9
addq %rcx, %rsi
movzwl 0x8(%rsi), %ebp
xorl %r15d, %r15d
cmpl $0x2b7, %ebp            # imm = 0x2B7
je 0x5c51e
xorl %r15d, %r15d
movswl %bp, %ebp
cmpl %edx, %ebp
je 0x5c50e
xorl %eax, %eax
movl 0x30(%rsp), %ecx
cmpl %r14d, %eax
jae 0x5c501
movl $0x1, %eax
cmpl %ebp, %ecx
jne 0x5c4f1
jmp 0x5c50e
leal (%r15,%r14), %eax
cltq
movl %ebp, 0x30(%rsp,%rax,4)
incl %r15d
movslq %ebp, %rax
movzwl 0x8(%rsi,%rax,2), %ebp
cmpl $0x2b7, %ebp            # imm = 0x2B7
jne 0x5c4e4
addl %r14d, %r15d
movl 0x7c0(%rsi), %eax
movl %r13d, 0x10(%rsp)
movl $0x1, %esi
movl %r15d, %r14d
cmpl $0x1, %r15d
movl %eax, %r15d
movb $0x1, %bpl
jg 0x5c487
jmp 0x5c580
xorl %r13d, %r13d
cmpl %r15d, %edi
jne 0x5c578
movslq 0x95720(%rbx,%r9,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
movq %rcx, %r9
cmpl $0x1, 0x4(%rcx,%rax)
movl $0x0, %r15d
movl $0x0, %esi
je 0x5c487
jmp 0x5c580
xorl %r15d, %r15d
xorl %esi, %esi
movq %rcx, %r9
cmpl 0x8(%rsp), %r12d
jne 0x5c644
movl 0x9571c(%rbx,%r10,4), %r12d
cmpl %r12d, %r13d
je 0x5c66b
movl %r15d, %ecx
movslq %r12d, %rax
imulq $0x7c8, %rax, %r15      # imm = 0x7C8
cmpl $0x2, 0x4(%r9,%r15)
jg 0x5c487
movl %r12d, 0x24(%rsp)
movl %esi, 0xc(%rsp)
addq %r9, %r15
movzwl 0x8(%r15), %ebp
xorl %r13d, %r13d
cmpl $0x2b7, %ebp            # imm = 0x2B7
je 0x5c615
movl %r14d, %esi
xorl %r13d, %r13d
movswl %bp, %r12d
cmpl %edx, %r12d
je 0x5c604
testl %r14d, %r14d
jle 0x5c5f6
xorl %ebp, %ebp
cmpl %r12d, 0x30(%rsp,%rbp,4)
je 0x5c604
incq %rbp
cmpq %rbp, %rsi
jne 0x5c5e7
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 0x5c5d7
addl 0x7c0(%r15), %ecx
movl %ecx, %r15d
addl %r14d, %r13d
movl 0xc(%rsp), %esi
movl %esi, %eax
movl 0x24(%rsp), %ecx
movl %ecx, 0x10(%rsp,%rax,4)
cmpl $0x1, %r13d
movb $0x1, %bpl
jg 0x5c487
incl %esi
movl %r13d, %r14d
jmp 0x5c66b
cmpl %r12d, %edi
jne 0x5c66b
movslq 0x9571c(%rbx,%r10,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r9,%rax)
movl 0x8(%rsp), %r13d
je 0x5c487
jmp 0x5c670
movl 0x8(%rsp), %r13d
cmpl %r13d, %r11d
jne 0x5c73d
movl 0x95724(%rbx,%r10,4), %r10d
cmpl %r10d, 0x10(%rsp)
je 0x5c75d
cmpl %r10d, 0x14(%rsp)
je 0x5c75d
movl %r15d, %ecx
movslq %r10d, %rax
imulq $0x7c8, %rax, %r15      # imm = 0x7C8
cmpl $0x2, 0x4(%r9,%r15)
jg 0x5c487
movl %esi, 0xc(%rsp)
addq %r9, %r15
movzwl 0x8(%r15), %ebp
xorl %r11d, %r11d
cmpl $0x2b7, %ebp            # imm = 0x2B7
je 0x5c711
movl %r14d, %esi
xorl %r11d, %r11d
movswl %bp, %r12d
cmpl %edx, %r12d
je 0x5c700
testl %r14d, %r14d
jle 0x5c6ed
xorl %r13d, %r13d
cmpl %r12d, 0x30(%rsp,%r13,4)
je 0x5c6fb
incq %r13
cmpq %r13, %rsi
jne 0x5c6de
leal (%r11,%r14), %eax
cltq
movl %r12d, 0x30(%rsp,%rax,4)
incl %r11d
movl 0x8(%rsp), %r13d
movslq %r12d, %rax
movzwl 0x8(%r15,%rax,2), %ebp
cmpl $0x2b7, %ebp            # imm = 0x2B7
jne 0x5c6cd
addl 0x7c0(%r15), %ecx
addl %r14d, %r11d
movl 0xc(%rsp), %esi
movl %esi, %eax
movl %r10d, 0x10(%rsp,%rax,4)
cmpl $0x1, %r11d
movb $0x1, %bpl
jg 0x5c487
movl %ecx, %r15d
incl %esi
movl %r11d, %r14d
jmp 0x5c75d
cmpl %r11d, %edi
jne 0x5c75d
movslq 0x95724(%rbx,%r10,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r9,%rax)
je 0x5c487
cmpl %r13d, %r8d
jne 0x5c823
movq 0x28(%rsp), %rax
movl 0x95720(%rbx,%rax,4), %ecx
cmpl %ecx, 0x10(%rsp)
je 0x5c848
cmpl %ecx, 0x14(%rsp)
je 0x5c848
cmpl %ecx, 0x18(%rsp)
je 0x5c848
movslq %ecx, %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x2, 0x4(%r9,%rax)
jg 0x5c487
movl %esi, %r10d
addq %rax, %r9
movzwl 0x8(%r9), %r8d
xorl %edi, %edi
cmpl $0x2b7, %r8d            # imm = 0x2B7
je 0x5c806
movl %r14d, %esi
xorl %edi, %edi
movswl %r8w, %r8d
cmpl %edx, %r8d
je 0x5c7f4
movq %r9, %rcx
testl %r14d, %r14d
jle 0x5c7e4
xorl %r9d, %r9d
cmpl %r8d, 0x30(%rsp,%r9,4)
je 0x5c7f1
incq %r9
cmpq %r9, %rsi
jne 0x5c7d5
leal (%rdi,%r14), %eax
cltq
movl %r8d, 0x30(%rsp,%rax,4)
incl %edi
movq %rcx, %r9
movslq %r8d, %rax
movzwl 0x8(%r9,%rax,2), %r8d
cmpl $0x2b7, %r8d            # imm = 0x2B7
jne 0x5c7c1
addl %r14d, %edi
cmpl $0x1, %edi
jg 0x5c487
addl 0x7c0(%r9), %r15d
movl %r10d, %esi
incl %esi
movl %edi, %r14d
jmp 0x5c848
cmpl %r8d, %edi
jne 0x5c848
movq 0x28(%rsp), %rax
movslq 0x95720(%rbx,%rax,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%r9,%rax)
je 0x5c487
cmpl $0x2, %r15d
jl 0x5c487
cmpl $0x5, %r15d
ja 0x5c8a6
movl %esi, %r15d
movq %rbx, %rdi
movl %edx, %esi
movl %r13d, %edx
callq 0x594d5
testb %al, %al
je 0x5c8a6
cmpl $0x1, %r15d
jne 0x5c487
cmpl $0x1, %r14d
jne 0x5c487
addq $0x4748, %rbx           # imm = 0x4748
movl 0x30(%rsp), %esi
movq %rbx, %rdi
callq 0x4fc2a
movl %eax, %eax
leaq 0x1aaf2e44(%rip), %rcx  # 0x1ab4f6e0
cmpb $0x5, (%rcx,%rax)
jne 0x5c487
xorl %ebp, %ebp
jmp 0x5c487
nopl (%rax)