// File: /workspace/github_cpp_makefile_star2_O3/kobanium[P]Ray/src/feature/Seki.cpp
// Function: CheckSeki(game_info_t const*, bool*)
void
CheckSeki( const game_info_t *game, bool seki[] )
{
  const char *board = game->board;
  const int *string_id = game->string_id;
  const string_t *string = game->string;
  int id, lib1, lib2;
  int lib1_id[4], lib2_id[4], lib1_ids, lib2_ids;
  int neighbor1_lib, neighbor2_lib;
  int neighbor4[4];
  bool seki_candidate[BOARD_MAX] = {false};
  bool already_checked;

  // 双方が自己アタリになっている座標を抽出
  for (int i = 0; i < pure_board_max; i++) {
    const int pos = onboard_pos[i];
    if (IsSelfAtari(game, S_BLACK, pos) &&
        IsSelfAtari(game, S_WHITE, pos)) {
      seki_candidate[pos] = true;
    }
  }

  for (int i = 0; i < MAX_STRING; i++) {
    // 連が存在しない,
    // または連の呼吸点数が2個でなければ次を調べる
    if (!string[i].flag || string[i].libs != 2) continue;

    // 連の大きさが6以上ならシミュレーションで
    // 自己アタリを打たないので次を調べる
    if (string[i].size >= 6) continue;

    lib1 = string[i].lib[0];
    lib2 = string[i].lib[lib1];
    // 連の持つ呼吸点がともにセキの候補
    if (seki_candidate[lib1] &&
        seki_candidate[lib2]) {
      // 呼吸点1の周囲の連のIDを取り出す
      GetNeighbor4(neighbor4, lib1);
      lib1_ids = 0;
      for (int j = 0; j < 4; j++) {
        if (board[neighbor4[j]] == S_BLACK ||
            board[neighbor4[j]] == S_WHITE) {
          id = string_id[neighbor4[j]];
          if (id != i) {
            already_checked = false;
            for (int k = 0; k < lib1_ids; k++) {
              if (lib1_id[k] == id) {
                already_checked = true;
                break;
              }
            }
            if (!already_checked) {
              lib1_id[lib1_ids++] = id;
            }
          }
        }
      }
      // 呼吸点2の周囲の連のIDを取り出す
      GetNeighbor4(neighbor4, lib2);
      lib2_ids = 0;
      for (int j = 0; j < 4; j++) {
        if (board[neighbor4[j]] == S_BLACK ||
            board[neighbor4[j]] == S_WHITE) {
          id = string_id[neighbor4[j]];
          if (id != i) {
            already_checked = false;
            for (int k = 0; k < lib2_ids; k++) {
              if (lib2_id[k] == id) {
          already_checked = true;
          break;
              }
            }
            if (!already_checked) {
              lib2_id[lib2_ids++] = id;
            }
          }
        }
      }

      if (lib1_ids == 1 && lib2_ids == 1) {
        neighbor1_lib = string[lib1_id[0]].lib[0];
        if (neighbor1_lib == lib1 ||
            neighbor1_lib == lib2) {
          neighbor1_lib = string[lib1_id[0]].lib[neighbor1_lib];
        }
        neighbor2_lib = string[lib2_id[0]].lib[0];
        if (neighbor2_lib == lib1 ||
            neighbor2_lib == lib2) {
          neighbor2_lib = string[lib2_id[0]].lib[neighbor2_lib];
        }
        if (neighbor1_lib == neighbor2_lib) {
          if (eye_condition[Pat3(game->pat, neighbor1_lib)] != E_NOT_EYE) {
            seki[lib1] = seki[lib2] = true;
            seki[neighbor1_lib] = true;
          }
        } else if (eye_condition[Pat3(game->pat, neighbor1_lib)] == E_COMPLETE_HALF_EYE &&
                   eye_condition[Pat3(game->pat, neighbor2_lib)] == E_COMPLETE_HALF_EYE) {
          int tmp_id1 = 0, tmp_id2 = 0;
          GetNeighbor4(neighbor4, neighbor1_lib);
          for (int j = 0; j < 4; j++) {
            if (board[neighbor4[j]] == S_BLACK ||
                board[neighbor4[j]] == S_WHITE) {
              id = string_id[neighbor4[j]];
              if (id != lib1_id[0] &&
                  id != lib2_id[0] &&
                  id != tmp_id1) {
              tmp_id1 = id;
              }
            }
          }
          GetNeighbor4(neighbor4, neighbor2_lib);
          for (int j = 0; j < 4; j++) {
            if (board[neighbor4[j]] == S_BLACK ||
                board[neighbor4[j]] == S_WHITE) {
              id = string_id[neighbor4[j]];
              if (id != lib1_id[0] &&
                  id != lib2_id[0] &&
                  id != tmp_id2) {
                tmp_id2 = id;
              }
            }
          }
          if (tmp_id1 == tmp_id2) {
            seki[lib1] = seki[lib2] = true;
            seki[neighbor1_lib] = seki[neighbor2_lib] = true;
          }
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3c8, %rsp            # imm = 0x3C8
movq %rsi, 0x38(%rsp)
movq %rdi, %r14
leaq 0x70(%rsp), %rdi
movl $0x349, %edx            # imm = 0x349
xorl %esi, %esi
callq 0x6200
leaq 0x1faf7(%rip), %rbx     # 0x768b0
cmpl $0x0, (%rbx)
jle 0x56e05
xorl %r15d, %r15d
leaq 0x1ab0a928(%rip), %r12  # 0x1ab616f0
movl (%r12,%r15,4), %ebp
movq %r14, %rdi
movl $0x1, %esi
movl %ebp, %edx
callq 0x5bee7
testb %al, %al
je 0x56dfa
movq %r14, %rdi
movl $0x2, %esi
movl %ebp, %edx
callq 0x5bee7
testb %al, %al
je 0x56dfa
movslq %ebp, %rax
movb $0x1, 0x70(%rsp,%rax)
incq %r15
movslq (%rbx), %rax
cmpq %rax, %r15
jl 0x56dc8
leaq 0x9620(%r14), %rbx
leaq 0x4748(%r14), %rax
movq %rax, 0x40(%rsp)
xorl %r12d, %r12d
imulq $0x7c8, %r12, %rax      # imm = 0x7C8
cmpb $0x1, 0x7c4(%rbx,%rax)
jne 0x570d1
addq %rbx, %rax
cmpl $0x2, 0x4(%rax)
jne 0x570d1
cmpl $0x5, 0x7c0(%rax)
jg 0x570d1
movswq 0x8(%rax), %r13
cmpb $0x1, 0x70(%rsp,%r13)
jne 0x570d1
movswl 0x8(%rax,%r13,2), %ebp
movslq %ebp, %rax
movq %rax, 0x20(%rsp)
cmpb $0x1, 0x70(%rsp,%rax)
jne 0x570d1
leaq 0x10(%rsp), %rdi
movl %r13d, %esi
callq 0x54386
xorl %eax, %eax
xorl %r15d, %r15d
movslq 0x10(%rsp,%rax,4), %rcx
movb 0x43f8(%r14,%rcx), %dl
decb %dl
cmpb $0x1, %dl
ja 0x56ec9
movl 0x95720(%r14,%rcx,4), %ecx
cmpq %rcx, %r12
je 0x56ec9
testl %r15d, %r15d
jle 0x56ebf
movl %r15d, %edx
xorl %esi, %esi
cmpl %ecx, 0x60(%rsp,%rsi,4)
je 0x56ec9
incq %rsi
cmpq %rsi, %rdx
jne 0x56eb1
movslq %r15d, %rdx
incl %r15d
movl %ecx, 0x60(%rsp,%rdx,4)
incq %rax
cmpq $0x4, %rax
jne 0x56e86
leaq 0x10(%rsp), %rdi
movl %ebp, %esi
callq 0x54386
xorl %ecx, %ecx
xorl %eax, %eax
movslq 0x10(%rsp,%rcx,4), %rdx
movb 0x43f8(%r14,%rdx), %sil
decb %sil
cmpb $0x1, %sil
ja 0x56f24
movl 0x95720(%r14,%rdx,4), %edx
cmpq %rdx, %r12
je 0x56f24
testl %eax, %eax
jle 0x56f1b
movl %eax, %esi
xorl %edi, %edi
cmpl %edx, 0x50(%rsp,%rdi,4)
je 0x56f24
incq %rdi
cmpq %rdi, %rsi
jne 0x56f0d
movslq %eax, %rsi
incl %eax
movl %edx, 0x50(%rsp,%rsi,4)
incq %rcx
cmpq $0x4, %rcx
jne 0x56ee2
xorl $0x1, %r15d
xorl $0x1, %eax
orl %r15d, %eax
jne 0x570d1
movslq 0x60(%rsp), %rdx
imulq $0x7c8, %rdx, %rcx      # imm = 0x7C8
movzwl 0x8(%rbx,%rcx), %eax
cmpw %r13w, %ax
movq %rdx, 0x48(%rsp)
je 0x56f60
cmpw 0x20(%rsp), %ax
jne 0x56f6c
movswq %ax, %rax
addq %rbx, %rcx
movzwl 0x8(%rcx,%rax,2), %eax
movslq 0x50(%rsp), %rdx
imulq $0x7c8, %rdx, %rcx      # imm = 0x7C8
movzwl 0x8(%rbx,%rcx), %r15d
cmpw %r13w, %r15w
movq %rdx, 0x30(%rsp)
je 0x56f8f
cmpw %bp, %r15w
jne 0x56f9c
movswq %r15w, %rdx
addq %rbx, %rcx
movzwl 0x8(%rcx,%rdx,2), %r15d
movswl %ax, %ebp
movq 0x40(%rsp), %rdi
movl %ebp, %esi
callq 0x4fc2a
movl %eax, %eax
leaq 0x1aaf872c(%rip), %rcx  # 0x1ab4f6e0
movb (%rcx,%rax), %al
cmpw %r15w, %bp
jne 0x56fe4
testb %al, %al
je 0x570d1
movq 0x38(%rsp), %rcx
movq 0x20(%rsp), %rax
movb $0x1, (%rcx,%rax)
movb $0x1, %dl
movb %dl, (%rcx,%r13)
movslq %ebp, %rax
movb %dl, (%rcx,%rax)
jmp 0x570d1
cmpb $0x1, %al
jne 0x570d1
movswl %r15w, %esi
movq 0x40(%rsp), %rdi
movl %esi, 0xc(%rsp)
callq 0x4fc2a
movl %eax, %eax
leaq 0x1aaf86d9(%rip), %rcx  # 0x1ab4f6e0
cmpb $0x1, (%rcx,%rax)
jne 0x570d1
leaq 0x10(%rsp), %rdi
movl %ebp, 0x2c(%rsp)
movl %ebp, %esi
callq 0x54386
xorl %eax, %eax
xorl %ebp, %ebp
movq 0x48(%rsp), %r15
movq 0x30(%rsp), %rsi
movslq 0x10(%rsp,%rax,4), %rcx
movb 0x43f8(%r14,%rcx), %dl
decb %dl
cmpb $0x1, %dl
ja 0x57058
movl 0x95720(%r14,%rcx,4), %ecx
cmpl %esi, %ecx
movl %ecx, %edx
cmovel %ebp, %edx
cmpl %r15d, %ecx
cmovnel %edx, %ebp
incq %rax
cmpq $0x4, %rax
jne 0x5702f
leaq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x54386
movq 0x30(%rsp), %rdi
xorl %eax, %eax
xorl %ecx, %ecx
movslq 0x10(%rsp,%rax,4), %rdx
movb 0x43f8(%r14,%rdx), %sil
decb %sil
cmpb $0x1, %sil
ja 0x570a3
movl 0x95720(%r14,%rdx,4), %edx
cmpl %edi, %edx
movl %edx, %esi
cmovel %ecx, %esi
cmpl %r15d, %edx
cmovnel %esi, %ecx
incq %rax
cmpq $0x4, %rax
jne 0x57078
cmpl %ecx, %ebp
movl 0x2c(%rsp), %ebp
jne 0x570d1
movq 0x38(%rsp), %rcx
movb $0x1, %al
movq 0x20(%rsp), %rdx
movb %al, (%rcx,%rdx)
movb %al, (%rcx,%r13)
movslq 0xc(%rsp), %r13
jmp 0x56fd3
incq %r12
cmpq $0x120, %r12            # imm = 0x120
jne 0x56e1b
addq $0x3c8, %rsp            # imm = 0x3C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop