// File: 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
movq %rsp, %rbp
subq $0x400, %rsp            # imm = 0x400
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
addq $0x43f8, %rax           # imm = 0x43F8
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
addq $0x95720, %rax          # imm = 0x95720
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
addq $0x9620, %rax           # imm = 0x9620
movq %rax, -0x28(%rbp)
leaq -0x3d0(%rbp), %rdi
xorl %esi, %esi
movl $0x349, %edx            # imm = 0x349
callq 0x72b0
movl $0x0, -0x3d8(%rbp)
movl -0x3d8(%rbp), %eax
leaq 0x478e9(%rip), %rcx     # 0xe7a40
cmpl (%rcx), %eax
jge 0xa01c8
movslq -0x3d8(%rbp), %rcx
leaq 0x1ab32707(%rip), %rax  # 0x1abd2870
movl (%rax,%rcx,4), %eax
movl %eax, -0x3dc(%rbp)
movq -0x8(%rbp), %rdi
movl -0x3dc(%rbp), %edx
movl $0x1, %esi
callq 0xa8e70
testb $0x1, %al
jne 0xa018c
jmp 0xa01b5
movq -0x8(%rbp), %rdi
movl -0x3dc(%rbp), %edx
movl $0x2, %esi
callq 0xa8e70
testb $0x1, %al
jne 0xa01a6
jmp 0xa01b5
movslq -0x3dc(%rbp), %rax
movb $0x1, -0x3d0(%rbp,%rax)
jmp 0xa01b7
movl -0x3d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3d8(%rbp)
jmp 0xa014a
movl $0x0, -0x3e0(%rbp)
cmpl $0x120, -0x3e0(%rbp)    # imm = 0x120
jge 0xa07b2
movq -0x28(%rbp), %rax
movslq -0x3e0(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
testb $0x1, 0x7c4(%rax)
je 0xa021b
movq -0x28(%rbp), %rax
movslq -0x3e0(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
cmpl $0x2, 0x4(%rax)
je 0xa0220
jmp 0xa079e
movq -0x28(%rbp), %rax
movslq -0x3e0(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
cmpl $0x6, 0x7c0(%rax)
jl 0xa0243
jmp 0xa079e
movq -0x28(%rbp), %rax
movslq -0x3e0(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
movswl 0x8(%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movslq -0x3e0(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
movslq -0x30(%rbp), %rcx
movswl 0x8(%rax,%rcx,2), %eax
movl %eax, -0x34(%rbp)
movslq -0x30(%rbp), %rax
testb $0x1, -0x3d0(%rbp,%rax)
je 0xa079c
movslq -0x34(%rbp), %rax
testb $0x1, -0x3d0(%rbp,%rax)
je 0xa079c
leaq -0x80(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x9bc90
movl $0x0, -0x64(%rbp)
movl $0x0, -0x3e4(%rbp)
cmpl $0x4, -0x3e4(%rbp)
jge 0xa03a4
movq -0x18(%rbp), %rax
movslq -0x3e4(%rbp), %rcx
movslq -0x80(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x1, %eax
je 0xa0304
movq -0x18(%rbp), %rax
movslq -0x3e4(%rbp), %rcx
movslq -0x80(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2, %eax
jne 0xa038e
movq -0x20(%rbp), %rax
movslq -0x3e4(%rbp), %rcx
movslq -0x80(%rbp,%rcx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x3e0(%rbp), %eax
je 0xa038c
movb $0x0, -0x3d1(%rbp)
movl $0x0, -0x3e8(%rbp)
movl -0x3e8(%rbp), %eax
cmpl -0x64(%rbp), %eax
jge 0xa036d
movslq -0x3e8(%rbp), %rax
movl -0x50(%rbp,%rax,4), %eax
cmpl -0x2c(%rbp), %eax
jne 0xa035a
movb $0x1, -0x3d1(%rbp)
jmp 0xa036d
jmp 0xa035c
movl -0x3e8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3e8(%rbp)
jmp 0xa0336
testb $0x1, -0x3d1(%rbp)
jne 0xa038a
movl -0x2c(%rbp), %ecx
movl -0x64(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x64(%rbp)
cltq
movl %ecx, -0x50(%rbp,%rax,4)
jmp 0xa038c
jmp 0xa038e
jmp 0xa0390
movl -0x3e4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3e4(%rbp)
jmp 0xa02c1
leaq -0x80(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x9bc90
movl $0x0, -0x68(%rbp)
movl $0x0, -0x3ec(%rbp)
cmpl $0x4, -0x3ec(%rbp)
jge 0xa04a4
movq -0x18(%rbp), %rax
movslq -0x3ec(%rbp), %rcx
movslq -0x80(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x1, %eax
je 0xa0404
movq -0x18(%rbp), %rax
movslq -0x3ec(%rbp), %rcx
movslq -0x80(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2, %eax
jne 0xa048e
movq -0x20(%rbp), %rax
movslq -0x3ec(%rbp), %rcx
movslq -0x80(%rbp,%rcx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x3e0(%rbp), %eax
je 0xa048c
movb $0x0, -0x3d1(%rbp)
movl $0x0, -0x3f0(%rbp)
movl -0x3f0(%rbp), %eax
cmpl -0x68(%rbp), %eax
jge 0xa046d
movslq -0x3f0(%rbp), %rax
movl -0x60(%rbp,%rax,4), %eax
cmpl -0x2c(%rbp), %eax
jne 0xa045a
movb $0x1, -0x3d1(%rbp)
jmp 0xa046d
jmp 0xa045c
movl -0x3f0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3f0(%rbp)
jmp 0xa0436
testb $0x1, -0x3d1(%rbp)
jne 0xa048a
movl -0x2c(%rbp), %ecx
movl -0x68(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x68(%rbp)
cltq
movl %ecx, -0x60(%rbp,%rax,4)
jmp 0xa048c
jmp 0xa048e
jmp 0xa0490
movl -0x3ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3ec(%rbp)
jmp 0xa03c1
cmpl $0x1, -0x64(%rbp)
jne 0xa079a
cmpl $0x1, -0x68(%rbp)
jne 0xa079a
movq -0x28(%rbp), %rax
movslq -0x50(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
movswl 0x8(%rax), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x30(%rbp), %eax
je 0xa04e1
movl -0x6c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jne 0xa04ff
movq -0x28(%rbp), %rax
movslq -0x50(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
movslq -0x6c(%rbp), %rcx
movswl 0x8(%rax,%rcx,2), %eax
movl %eax, -0x6c(%rbp)
movq -0x28(%rbp), %rax
movslq -0x60(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
movswl 0x8(%rax), %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x30(%rbp), %eax
je 0xa0528
movl -0x70(%rbp), %eax
cmpl -0x34(%rbp), %eax
jne 0xa0546
movq -0x28(%rbp), %rax
movslq -0x60(%rbp), %rcx
imulq $0x7c8, %rcx, %rcx      # imm = 0x7C8
addq %rcx, %rax
movslq -0x70(%rbp), %rcx
movswl 0x8(%rax,%rcx,2), %eax
movl %eax, -0x70(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x70(%rbp), %eax
jne 0xa059e
movq -0x8(%rbp), %rdi
addq $0x4748, %rdi           # imm = 0x4748
movl -0x6c(%rbp), %esi
callq 0x93d30
movl %eax, %eax
movl %eax, %ecx
leaq 0x1ab202f4(%rip), %rax  # 0x1abc0860
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0xa0599
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movq -0x10(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movb $0x1, (%rax,%rcx)
jmp 0xa0798
movq -0x8(%rbp), %rdi
addq $0x4748, %rdi           # imm = 0x4748
movl -0x6c(%rbp), %esi
callq 0x93d30
movl %eax, %eax
movl %eax, %ecx
leaq 0x1ab202a4(%rip), %rax  # 0x1abc0860
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0xa0796
movq -0x8(%rbp), %rdi
addq $0x4748, %rdi           # imm = 0x4748
movl -0x70(%rbp), %esi
callq 0x93d30
movl %eax, %eax
movl %eax, %ecx
leaq 0x1ab20279(%rip), %rax  # 0x1abc0860
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0xa0796
movl $0x0, -0x3f4(%rbp)
movl $0x0, -0x3f8(%rbp)
leaq -0x80(%rbp), %rdi
movl -0x6c(%rbp), %esi
callq 0x9bc90
movl $0x0, -0x3fc(%rbp)
cmpl $0x4, -0x3fc(%rbp)
jge 0xa06af
movq -0x18(%rbp), %rax
movslq -0x3fc(%rbp), %rcx
movslq -0x80(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x1, %eax
je 0xa065d
movq -0x18(%rbp), %rax
movslq -0x3fc(%rbp), %rcx
movslq -0x80(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2, %eax
jne 0xa0699
movq -0x20(%rbp), %rax
movslq -0x3fc(%rbp), %rcx
movslq -0x80(%rbp,%rcx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x50(%rbp), %eax
je 0xa0697
movl -0x2c(%rbp), %eax
cmpl -0x60(%rbp), %eax
je 0xa0697
movl -0x2c(%rbp), %eax
cmpl -0x3f4(%rbp), %eax
je 0xa0697
movl -0x2c(%rbp), %eax
movl %eax, -0x3f4(%rbp)
jmp 0xa0699
jmp 0xa069b
movl -0x3fc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3fc(%rbp)
jmp 0xa061e
leaq -0x80(%rbp), %rdi
movl -0x70(%rbp), %esi
callq 0x9bc90
movl $0x0, -0x400(%rbp)
cmpl $0x4, -0x400(%rbp)
jge 0xa0756
movq -0x18(%rbp), %rax
movslq -0x400(%rbp), %rcx
movslq -0x80(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x1, %eax
je 0xa0704
movq -0x18(%rbp), %rax
movslq -0x400(%rbp), %rcx
movslq -0x80(%rbp,%rcx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2, %eax
jne 0xa0740
movq -0x20(%rbp), %rax
movslq -0x400(%rbp), %rcx
movslq -0x80(%rbp,%rcx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x50(%rbp), %eax
je 0xa073e
movl -0x2c(%rbp), %eax
cmpl -0x60(%rbp), %eax
je 0xa073e
movl -0x2c(%rbp), %eax
cmpl -0x3f8(%rbp), %eax
je 0xa073e
movl -0x2c(%rbp), %eax
movl %eax, -0x3f8(%rbp)
jmp 0xa0740
jmp 0xa0742
movl -0x400(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x400(%rbp)
jmp 0xa06c5
movl -0x3f4(%rbp), %eax
cmpl -0x3f8(%rbp), %eax
jne 0xa0794
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movq -0x10(%rbp), %rax
movslq -0x70(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movq -0x10(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movb $0x1, (%rax,%rcx)
jmp 0xa0796
jmp 0xa0798
jmp 0xa079a
jmp 0xa079c
jmp 0xa079e
movl -0x3e0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3e0(%rbp)
jmp 0xa01d2
addq $0x400, %rsp            # imm = 0x400
popq %rbp
retq
nopl (%rax,%rax)