// File: /workspace/github_cpp_makefile_star2_O3/kobanium[P]Ray/src/feature/Ladder.cpp
// Function: LadderExtension(game_info_t*, int, bool*)
void
LadderExtension( game_info_t *game, int color, bool *ladder_pos )
{
  const string_t *string = game->string;
  std::unique_ptr<search_game_info_t> search_game;
  bool checked[BOARD_MAX] = { false };

  for (int i = 0; i < MAX_STRING; i++) {
    if (!string[i].flag ||
        string[i].color != color) {
      continue;
    }
    // アタリから逃げる着手箇所
    int ladder = string[i].lib[0];

    bool flag = false;

    // アタリを逃げる手で未探索のものを確認
    if (!checked[ladder] && string[i].libs == 1) {
      if (!search_game)
        search_game.reset(new search_game_info_t(game));
      search_game_info_t *ladder_game = search_game.get();
      // 隣接する敵連を取って助かるかを確認
      int neighbor = string[i].neighbor[0];
      while (neighbor != NEIGHBOR_END && !flag) {
        if (string[neighbor].libs == 1) {
          if (IsLegal(game, string[neighbor].lib[0], color)) {
            PutStoneForSearch(ladder_game, string[neighbor].lib[0], color);
            if (IsLadderCaptured(0, ladder_game, string[i].origin, GetOppositeColor(color)) == DEAD) {
              if (string[i].size >= 2) {
                ladder_pos[string[neighbor].lib[0]] = true;
              }
            } else {
              flag = true;
            }
            Undo(ladder_game);
          }
        }
        neighbor = string[i].neighbor[neighbor];
      }

      // 取って助からない時は逃げてみる
      if (!flag) {
        if (IsLegal(game, ladder, color)) {
          PutStoneForSearch(ladder_game, ladder, color);
          if (string[i].size >= 2 &&
              IsLadderCaptured(0, ladder_game, ladder, GetOppositeColor(color)) == DEAD) {
            ladder_pos[ladder] = true;
          }
          Undo(ladder_game);
        }
      }
      checked[ladder] = true;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x388, %rsp            # imm = 0x388
movq %rdx, 0x20(%rsp)
movl %esi, %ebx
movq %rdi, 0x18(%rsp)
leaq 0x9620(%rdi), %r12
leaq 0x30(%rsp), %rdi
xorl %r15d, %r15d
movl $0x349, %edx            # imm = 0x349
xorl %esi, %esi
callq 0x6200
movl %ebx, 0x4(%rsp)
xorl $0x3, %ebx
movl %ebx, 0x14(%rsp)
xorl %ebx, %ebx
imulq $0x7c8, %r15, %r13      # imm = 0x7C8
cmpb $0x1, 0x7c4(%r12,%r13)
jne 0x5ca7b
addq %r12, %r13
movsbl (%r13), %eax
cmpl 0x4(%rsp), %eax
jne 0x5ca7b
movswq 0x8(%r13), %r14
cmpb $0x0, 0x30(%rsp,%r14)
jne 0x5ca7b
cmpl $0x1, 0x4(%r13)
jne 0x5ca7b
testq %rbx, %rbx
jne 0x5c958
movl $0x69ae40, %edi         # imm = 0x69AE40
callq 0x6400
movq %rax, %rbx
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x51d58
movzwl 0x57c(%r13), %eax
cmpl $0x11f, %eax            # imm = 0x11F
je 0x5ca20
movq %r14, 0x28(%rsp)
movq %rbx, 0x8(%rsp)
movswq %ax, %rbx
imulq $0x7c8, %rbx, %rbp      # imm = 0x7C8
cmpl $0x1, 0x4(%r12,%rbp)
jne 0x5c9f5
addq %r12, %rbp
movswl 0x8(%rbp), %esi
movq 0x18(%rsp), %rdi
movl 0x4(%rsp), %edx
callq 0x55478
testb %al, %al
je 0x5c9f5
movswl 0x8(%rbp), %esi
movq 0x8(%rsp), %rdi
movl 0x4(%rsp), %edx
callq 0x51f85
movl 0x7bc(%r13), %edx
xorl %edi, %edi
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %ecx
callq 0x5caf3
movl %eax, %r14d
testb %al, %al
jne 0x5c9e9
cmpl $0x2, 0x7c0(%r13)
jl 0x5c9e9
movswq 0x8(%rbp), %rax
movq 0x20(%rsp), %rcx
movb $0x1, (%rcx,%rax)
movq 0x8(%rsp), %rdi
callq 0x52beb
jmp 0x5c9f8
xorl %r14d, %r14d
movzwl 0x57c(%r13,%rbx,2), %eax
cmpl $0x11f, %eax            # imm = 0x11F
je 0x5ca11
testb %r14b, %r14b
je 0x5c975
testb %r14b, %r14b
movq 0x8(%rsp), %rbx
movq 0x28(%rsp), %r14
jne 0x5ca75
movq 0x18(%rsp), %rdi
movl %r14d, %esi
movl 0x4(%rsp), %edx
callq 0x55478
testb %al, %al
je 0x5ca75
movq %rbx, %rdi
movl %r14d, %esi
movl 0x4(%rsp), %edx
callq 0x51f85
cmpl $0x2, 0x7c0(%r13)
jl 0x5ca6d
xorl %edi, %edi
movq %rbx, %rsi
movl %r14d, %edx
movl 0x14(%rsp), %ecx
callq 0x5caf3
testb %al, %al
jne 0x5ca6d
movq 0x20(%rsp), %rax
movb $0x1, (%rax,%r14)
movq %rbx, %rdi
callq 0x52beb
movb $0x1, 0x30(%rsp,%r14)
incq %r15
cmpq $0x120, %r15            # imm = 0x120
jne 0x5c8f5
testq %rbx, %rbx
je 0x5caae
movl $0x69ae40, %esi         # imm = 0x69AE40
movq %rbx, %rdi
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6410
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x69ae40, %esi         # imm = 0x69AE40
movq %rbx, %rdi
jmp 0x5cae6
movq %rax, %r14
jmp 0x5caeb
movq %rbx, 0x8(%rsp)
jmp 0x5cad9
movq %rax, %r14
movl $0x69ae40, %esi         # imm = 0x69AE40
movq 0x8(%rsp), %rdi
callq 0x6410
movq %r14, %rdi
callq 0x6760