// File: /workspace/github_cpp_makefile_star2_O2/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
xorl %ebx, %ebx
movl $0x349, %edx            # imm = 0x349
xorl %esi, %esi
callq 0x61f0
leaq 0x24515(%rip), %r13     # 0x618b0
leaq 0x1ab0f34e(%rip), %r15  # 0x1ab4c6f0
pushq $0x1
popq %rbp
movslq (%r13), %rax
cmpq %rax, %rbx
jge 0x3d3e2
movl (%r15,%rbx,4), %r12d
movq %r14, %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0x4221f
testb %al, %al
je 0x3d3dd
movq %r14, %rdi
pushq $0x2
popq %rsi
movl %r12d, %edx
callq 0x4221f
testb %al, %al
je 0x3d3dd
movslq %r12d, %rax
movb $0x1, 0x70(%rsp,%rax)
incq %rbx
jmp 0x3d3a5
leaq 0x9620(%r14), %rbx
leaq 0x4748(%r14), %rax
movq %rax, 0x40(%rsp)
xorl %r12d, %r12d
cmpq $0x120, %r12            # imm = 0x120
je 0x3d6df
imulq $0x7c8, %r12, %rax      # imm = 0x7C8
cmpb $0x1, 0x7c4(%rbx,%rax)
jne 0x3d6d7
addq %rbx, %rax
cmpl $0x2, 0x4(%rax)
jne 0x3d6d7
cmpl $0x5, 0x7c0(%rax)
jg 0x3d6d7
movswq 0x8(%rax), %r13
cmpb $0x1, 0x70(%rsp,%r13)
jne 0x3d6d7
movswl 0x8(%rax,%r13,2), %ebp
movslq %ebp, %rax
movq %rax, 0x20(%rsp)
cmpb $0x1, 0x70(%rsp,%rax)
jne 0x3d6d7
leaq 0x10(%rsp), %rdi
movl %r13d, %esi
callq 0x3b128
xorl %eax, %eax
xorl %r15d, %r15d
cmpq $0x4, %rax
je 0x3d4c8
movslq 0x10(%rsp,%rax,4), %rcx
movb 0x43f8(%r14,%rcx), %dl
decb %dl
cmpb $0x1, %dl
ja 0x3d4c3
movl 0x95720(%r14,%rcx,4), %ecx
cmpq %rcx, %r12
je 0x3d4c3
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r15d, %edx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x3d4b9
leaq 0x1(%rsi), %rdi
cmpl %ecx, 0x60(%rsp,%rsi,4)
movq %rdi, %rsi
jne 0x3d4a5
jmp 0x3d4c3
movslq %r15d, %rdx
incl %r15d
movl %ecx, 0x60(%rsp,%rdx,4)
incq %rax
jmp 0x3d470
leaq 0x10(%rsp), %rdi
movl %ebp, %esi
callq 0x3b128
xorl %ecx, %ecx
xorl %eax, %eax
cmpq $0x4, %rcx
je 0x3d52f
movslq 0x10(%rsp,%rcx,4), %rdx
movb 0x43f8(%r14,%rdx), %sil
decb %sil
cmpb $0x1, %sil
ja 0x3d52a
movl 0x95720(%r14,%rdx,4), %edx
cmpq %rdx, %r12
je 0x3d52a
testl %eax, %eax
movl $0x0, %esi
cmovgl %eax, %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x3d521
leaq 0x1(%rdi), %r8
cmpl %edx, 0x50(%rsp,%rdi,4)
movq %r8, %rdi
jne 0x3d50d
jmp 0x3d52a
movslq %eax, %rsi
incl %eax
movl %edx, 0x50(%rsp,%rsi,4)
incq %rcx
jmp 0x3d4d8
xorl $0x1, %r15d
xorl $0x1, %eax
orl %r15d, %eax
jne 0x3d6d7
movslq 0x60(%rsp), %rdx
imulq $0x7c8, %rdx, %rcx      # imm = 0x7C8
movzwl 0x8(%rbx,%rcx), %eax
cmpw %r13w, %ax
movq %rdx, 0x48(%rsp)
je 0x3d562
cmpw 0x20(%rsp), %ax
jne 0x3d56e
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 0x3d591
cmpw %bp, %r15w
jne 0x3d59e
movswq %r15w, %rdx
addq %rbx, %rcx
movzwl 0x8(%rcx,%rdx,2), %r15d
movswl %ax, %ebp
movq 0x40(%rsp), %rdi
movl %ebp, %esi
callq 0x37bc6
movl %eax, %eax
leaq 0x1aafd12a(%rip), %rcx  # 0x1ab3a6e0
movb (%rcx,%rax), %al
cmpw %r15w, %bp
jne 0x3d5e6
testb %al, %al
je 0x3d6d7
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 0x3d6d7
cmpb $0x1, %al
jne 0x3d6d7
movswl %r15w, %esi
movq 0x40(%rsp), %rdi
movl %esi, 0xc(%rsp)
callq 0x37bc6
movl %eax, %eax
leaq 0x1aafd0d7(%rip), %rcx  # 0x1ab3a6e0
cmpb $0x1, (%rcx,%rax)
jne 0x3d6d7
leaq 0x10(%rsp), %rdi
movl %ebp, 0x2c(%rsp)
movl %ebp, %esi
callq 0x3b128
xorl %eax, %eax
xorl %ebp, %ebp
movq 0x48(%rsp), %r15
movq 0x30(%rsp), %rsi
cmpq $0x4, %rax
je 0x3d665
movslq 0x10(%rsp,%rax,4), %rcx
movb 0x43f8(%r14,%rcx), %dl
decb %dl
cmpb $0x1, %dl
ja 0x3d660
movl 0x95720(%r14,%rcx,4), %ecx
cmpl %esi, %ecx
movl %ecx, %edx
cmovel %ebp, %edx
cmpl %r15d, %ecx
cmovnel %edx, %ebp
incq %rax
jmp 0x3d631
leaq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x3b128
movq 0x30(%rsp), %rdi
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x4, %rax
je 0x3d6b2
movslq 0x10(%rsp,%rax,4), %rdx
movb 0x43f8(%r14,%rdx), %sil
decb %sil
cmpb $0x1, %sil
ja 0x3d6ad
movl 0x95720(%r14,%rdx,4), %edx
cmpl %edi, %edx
movl %edx, %esi
cmovel %ecx, %esi
cmpl %r15d, %edx
cmovnel %esi, %ecx
incq %rax
jmp 0x3d67c
cmpl %ecx, %ebp
movl 0x2c(%rsp), %ebp
jne 0x3d6d7
movq 0x38(%rsp), %rcx
movb $0x1, %al
movq 0x20(%rsp), %rdx
movb %al, (%rcx,%rdx)
movb %al, (%rcx,%r13)
movslq 0xc(%rsp), %r13
jmp 0x3d5d5
incq %r12
jmp 0x3d3f8
addq $0x3c8, %rsp            # imm = 0x3C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)