// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x1f161(%rip), %rax     # 0x618bc
movl (%rax), %ecx
movl %edx, %eax
subl %ecx, %eax
movslq %eax, %r9
movsbl 0x43f8(%rdi,%r9), %r15d
xorl %r14d, %r14d
testl %r15d, %r15d
jne 0x4277d
movl %eax, 0x40(%rsp)
pushq $0x1
popq %r14
movslq %edx, %r10
movsbl 0x43f7(%rbx,%r10), %r12d
testl %r12d, %r12d
jne 0x4279b
leal -0x1(%rdx), %eax
movl %r14d, %edi
incl %r14d
movl %eax, 0x40(%rsp,%rdi,4)
movsbl 0x43f9(%rbx,%r10), %r11d
testl %r11d, %r11d
jne 0x427b6
leal 0x1(%rdx), %eax
movl %r14d, %edi
incl %r14d
movl %eax, 0x40(%rsp,%rdi,4)
addl %edx, %ecx
movslq %ecx, %rdi
movsbl 0x43f8(%rbx,%rdi), %r8d
testl %r8d, %r8d
jne 0x427d3
movl %r14d, %eax
incl %r14d
movl %ecx, 0x40(%rsp,%rax,4)
movb $0x1, %bpl
cmpl $0x1, %r14d
jbe 0x427ed
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, 0x38(%rsp)
leaq 0x9620(%rbx), %rax
movl %esi, %ecx
xorl $0x3, %ecx
movl %esi, 0x8(%rsp)
cmpl %esi, %r15d
movl %ecx, 0xc(%rsp)
jne 0x42876
movslq 0x95720(%rbx,%r9,4), %r13
imulq $0x7c8, %r13, %rsi      # imm = 0x7C8
cmpl $0x2, 0x4(%rax,%rsi)
jg 0x427dc
pushq $0x1
popq %r9
movq %rax, 0x10(%rsp)
addq %rax, %rsi
leaq 0x8(%rsi), %rbp
xorl %r15d, %r15d
movq %rbp, %rax
movswl (%rax), %eax
cmpl $0x2b7, %eax            # imm = 0x2B7
je 0x428aa
cmpl %edx, %eax
je 0x42867
xorl %ecx, %ecx
movl 0x40(%rsp), %edi
cmpl %r14d, %ecx
jae 0x42859
movl %r9d, %ecx
cmpl %eax, %edi
jne 0x4284b
jmp 0x42867
leal (%r15,%r14), %ecx
movslq %ecx, %rcx
movl %eax, 0x40(%rsp,%rcx,4)
incl %r15d
cltq
leaq (,%rax,2), %rax
addq %rbp, %rax
jmp 0x42837
xorl %r13d, %r13d
cmpl %r15d, %ecx
jne 0x428c6
movq %rax, %rcx
movslq 0x95720(%rbx,%r9,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rcx,%rax)
movq %rcx, %rax
movl $0x0, %esi
movl $0x0, %r15d
jne 0x428de
jmp 0x427dc
addl %r14d, %r15d
movl 0x7c0(%rsi), %esi
movl %r13d, 0x20(%rsp)
cmpl $0x1, %r15d
jle 0x428cd
movb $0x1, %bpl
jmp 0x427dc
xorl %esi, %esi
xorl %r15d, %r15d
jmp 0x428de
pushq $0x1
popq %rax
movl %r15d, %r14d
movl %eax, %r15d
movb $0x1, %bpl
movq 0x10(%rsp), %rax
cmpl 0x8(%rsp), %r12d
jne 0x42900
movl 0x9571c(%rbx,%r10,4), %r12d
cmpl %r12d, %r13d
jne 0x42937
movl 0x8(%rsp), %r13d
movl 0xc(%rsp), %edi
jmp 0x429b8
movl 0xc(%rsp), %edi
cmpl %r12d, %edi
jne 0x429b3
movq %rax, %rcx
movslq 0x9571c(%rbx,%r10,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rcx,%rax)
movq %rcx, %rax
movl 0x8(%rsp), %r13d
jne 0x429b8
jmp 0x427dc
movl %r15d, 0x1c(%rsp)
movq %rax, 0x10(%rsp)
movslq %r12d, %rax
imulq $0x7c8, %rax, %r15      # imm = 0x7C8
movq 0x10(%rsp), %rax
cmpl $0x2, 0x4(%rax,%r15)
jg 0x427dc
addq %rax, %r15
leaq 0x8(%r15), %rcx
xorl %r13d, %r13d
testl %r14d, %r14d
movl $0x0, %ebp
cmovgl %r14d, %ebp
movq %rcx, %rax
movswl (%rax), %eax
cmpl $0x2b7, %eax            # imm = 0x2B7
je 0x42a86
cmpl %edx, %eax
je 0x429ab
xorl %edi, %edi
cmpq %rdi, %rbp
je 0x4299d
leaq 0x1(%rdi), %r9
cmpl %eax, 0x40(%rsp,%rdi,4)
movq %r9, %rdi
jne 0x42989
jmp 0x429ab
leal (%r14,%r13), %edi
movslq %edi, %rdi
movl %eax, 0x40(%rsp,%rdi,4)
incl %r13d
cltq
leaq (%rcx,%rax,2), %rax
jmp 0x42975
movl 0x8(%rsp), %r13d
cmpl %r13d, %r11d
jne 0x42a5c
movl 0x95724(%rbx,%r10,4), %r10d
cmpl %r10d, 0x20(%rsp)
je 0x42aef
cmpl %r10d, 0x24(%rsp)
je 0x42aef
movl %r15d, 0x1c(%rsp)
movq %rax, 0x10(%rsp)
movslq %r10d, %rax
imulq $0x7c8, %rax, %r15      # imm = 0x7C8
movq 0x10(%rsp), %rax
cmpl $0x2, 0x4(%rax,%r15)
jg 0x427dc
addq %rax, %r15
leaq 0x8(%r15), %rcx
xorl %r11d, %r11d
testl %r14d, %r14d
movl $0x0, %r12d
cmovgl %r14d, %r12d
movq %rcx, %rax
movswl (%rax), %eax
cmpl $0x2b7, %eax            # imm = 0x2B7
je 0x42abf
cmpl %edx, %eax
je 0x42a54
xorl %edi, %edi
cmpq %rdi, %r12
je 0x42a46
leaq 0x1(%rdi), %r9
cmpl %eax, 0x40(%rsp,%rdi,4)
movq %r9, %rdi
jne 0x42a32
jmp 0x42a54
leal (%r11,%r14), %edi
movslq %edi, %rdi
movl %eax, 0x40(%rsp,%rdi,4)
incl %r11d
cltq
leaq (%rcx,%rax,2), %rax
jmp 0x42a1e
cmpl %r11d, %edi
jne 0x42aef
movq %rax, %rcx
movslq 0x95724(%rbx,%r10,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rcx,%rax)
movq %rcx, %rax
jne 0x42aef
jmp 0x427dc
addl 0x7c0(%r15), %esi
addl %r14d, %r13d
movl 0x1c(%rsp), %r15d
movl %r15d, %eax
movl %r12d, 0x20(%rsp,%rax,4)
cmpl $0x1, %r13d
jg 0x428be
incl %r15d
movl %r13d, %r14d
movb $0x1, %bpl
movl 0x8(%rsp), %r13d
movq 0x10(%rsp), %rax
jmp 0x428f7
addl 0x7c0(%r15), %esi
addl %r14d, %r11d
movl 0x1c(%rsp), %r15d
movl %r15d, %eax
movl %r10d, 0x20(%rsp,%rax,4)
cmpl $0x1, %r11d
jg 0x427dc
incl %r15d
movl %r11d, %r14d
movq 0x10(%rsp), %rax
movl 0xc(%rsp), %edi
cmpl %r13d, %r8d
jne 0x42b91
movq %rax, %rcx
movq 0x38(%rsp), %rax
movl 0x95720(%rbx,%rax,4), %eax
cmpl %eax, 0x20(%rsp)
je 0x42bd2
cmpl %eax, 0x24(%rsp)
je 0x42bd2
cmpl %eax, 0x28(%rsp)
je 0x42bd2
cltq
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x2, 0x4(%rcx,%rax)
jg 0x427dc
addq %rax, %rcx
movq %rcx, %r11
addq $0x8, %rcx
xorl %edi, %edi
testl %r14d, %r14d
movl $0x0, %r8d
cmovgl %r14d, %r8d
movq %rcx, %rax
movswl (%rax), %eax
cmpl $0x2b7, %eax            # imm = 0x2B7
je 0x42bb9
cmpl %edx, %eax
je 0x42b89
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x42b7b
leaq 0x1(%r9), %r10
cmpl %eax, 0x40(%rsp,%r9,4)
movq %r10, %r9
jne 0x42b66
jmp 0x42b89
leal (%rdi,%r14), %r9d
movslq %r9d, %r9
movl %eax, 0x40(%rsp,%r9,4)
incl %edi
cltq
leaq (%rcx,%rax,2), %rax
jmp 0x42b55
cmpl %r8d, %edi
jne 0x42bd2
movq %rax, %rcx
movq 0x38(%rsp), %rax
movslq 0x95720(%rbx,%rax,4), %rax
imulq $0x7c8, %rax, %rax      # imm = 0x7C8
cmpl $0x1, 0x4(%rcx,%rax)
jne 0x42bd2
jmp 0x427dc
addl %r14d, %edi
cmpl $0x1, %edi
jg 0x427dc
addl 0x7c0(%r11), %esi
incl %r15d
movl %edi, %r14d
cmpl $0x2, %esi
jl 0x427dc
cmpl $0x5, %esi
ja 0x42c2b
movq %rbx, %rdi
movl %edx, %esi
movl %r13d, %edx
callq 0x3fa04
testb %al, %al
je 0x42c2b
cmpl $0x1, %r15d
jne 0x427dc
cmpl $0x1, %r14d
jne 0x427dc
addq $0x4748, %rbx           # imm = 0x4748
movl 0x40(%rsp), %esi
movq %rbx, %rdi
callq 0x37bc6
movl %eax, %eax
leaq 0x1aaf7abf(%rip), %rcx  # 0x1ab3a6e0
cmpb $0x5, (%rcx,%rax)
jne 0x427dc
xorl %ebp, %ebp
jmp 0x427dc