// File: /workspace/github_cpp_makefile_star2_O1/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 $0x398, %rsp            # imm = 0x398
movq %rdx, 0x28(%rsp)
movl %esi, %ebx
movq %rdi, 0x20(%rsp)
leaq 0x9620(%rdi), %r12
leaq 0x40(%rsp), %rdi
xorl %r15d, %r15d
movl $0x349, %edx            # imm = 0x349
xorl %esi, %esi
callq 0x6210
movl %ebx, 0xc(%rsp)
xorl $0x3, %ebx
movl %ebx, 0x1c(%rsp)
xorl %ebx, %ebx
imulq $0x7c8, %r15, %r14      # imm = 0x7C8
cmpb $0x1, 0x7c4(%r12,%r14)
jne 0x5c1fd
addq %r12, %r14
movsbl (%r14), %eax
cmpl 0xc(%rsp), %eax
jne 0x5c1fd
movswq 0x8(%r14), %r13
cmpb $0x0, 0x40(%rsp,%r13)
jne 0x5c1fd
cmpl $0x1, 0x4(%r14)
jne 0x5c1fd
testq %rbx, %rbx
jne 0x5c0c6
movl $0x69ae40, %edi         # imm = 0x69AE40
callq 0x6410
movq %rax, %rbp
movq %rax, %rdi
movq 0x20(%rsp), %rsi
callq 0x5197c
movq %rbp, %rbx
movzwl 0x57c(%r14), %eax
cmpl $0x11f, %eax            # imm = 0x11F
je 0x5c1a2
movq %r13, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq %rbx, 0x10(%rsp)
xorl %r13d, %r13d
movswq %ax, %rbx
imulq $0x7c8, %rbx, %rbp      # imm = 0x7C8
movq 0x30(%rsp), %rax
cmpl $0x1, 0x4(%rax,%rbp)
jne 0x5c173
addq %rax, %rbp
movswl 0x8(%rbp), %esi
movq 0x20(%rsp), %rdi
movl 0xc(%rsp), %edx
callq 0x54a9c
testb %al, %al
je 0x5c173
movswl 0x8(%rbp), %esi
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %edx
callq 0x51bb9
movl 0x7bc(%r14), %edx
xorl %edi, %edi
movq 0x10(%rsp), %rsi
movl 0x1c(%rsp), %ecx
callq 0x5c284
movb $0x1, %r12b
testb %al, %al
jne 0x5c166
cmpl $0x2, 0x7c0(%r14)
jl 0x5c163
movswq 0x8(%rbp), %rax
movq 0x28(%rsp), %rcx
movb $0x1, (%rcx,%rax)
movl %r13d, %r12d
movq 0x10(%rsp), %rdi
callq 0x5280d
movl %r12d, %r13d
movzwl 0x57c(%r14,%rbx,2), %eax
cmpl $0x11f, %eax            # imm = 0x11F
je 0x5c18d
testb $0x1, %r13b
je 0x5c0eb
testb $0x1, %r13b
movq 0x10(%rsp), %rbx
movq 0x30(%rsp), %r12
movq 0x38(%rsp), %r13
jne 0x5c1f7
movq 0x20(%rsp), %rdi
movl %r13d, %esi
movl 0xc(%rsp), %edx
callq 0x54a9c
testb %al, %al
je 0x5c1f7
movq %rbx, %rdi
movl %r13d, %esi
movl 0xc(%rsp), %edx
callq 0x51bb9
cmpl $0x2, 0x7c0(%r14)
jl 0x5c1ef
xorl %edi, %edi
movq %rbx, %rsi
movl %r13d, %edx
movl 0x1c(%rsp), %ecx
callq 0x5c284
testb %al, %al
jne 0x5c1ef
movq 0x28(%rsp), %rax
movb $0x1, (%rax,%r13)
movq %rbx, %rdi
callq 0x5280d
movb $0x1, 0x40(%rsp,%r13)
incq %r15
cmpq $0x120, %r15            # imm = 0x120
jne 0x5c061
testq %rbx, %rbx
je 0x5c230
movl $0x69ae40, %esi         # imm = 0x69AE40
movq %rbx, %rdi
addq $0x398, %rsp            # imm = 0x398
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6420
addq $0x398, %rsp            # imm = 0x398
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, 0x10(%rsp)
movq %rax, %r15
movl $0x69ae40, %esi         # imm = 0x69AE40
movq %rbp, %rdi
callq 0x6420
jmp 0x5c265
jmp 0x5c25b
movq %rbx, 0x10(%rsp)
jmp 0x5c262
movq %rax, %r15
cmpq $0x0, 0x10(%rsp)
je 0x5c27c
movl $0x69ae40, %esi         # imm = 0x69AE40
movq 0x10(%rsp), %rdi
callq 0x6420
movq %r15, %rdi
callq 0x6770