// File: /workspace/github_cpp_makefile_star2_O2/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 0x10(%rsp), %rax
andq $0x0, (%rax)
leaq 0x40(%rsp), %rdi
xorl %r13d, %r13d
movl $0x349, %edx            # imm = 0x349
xorl %esi, %esi
callq 0x61f0
movl %ebx, 0x4(%rsp)
xorl $0x3, %ebx
movl %ebx, 0x1c(%rsp)
cmpq $0x120, %r13            # imm = 0x120
je 0x42e5d
imulq $0x7c8, %r13, %r14      # imm = 0x7C8
cmpb $0x1, 0x7c4(%r12,%r14)
jne 0x42e55
addq %r12, %r14
movsbl (%r14), %eax
cmpl 0x4(%rsp), %eax
jne 0x42e55
movswq 0x8(%r14), %rbx
cmpb $0x0, 0x40(%rsp,%rbx)
jne 0x42e55
cmpl $0x1, 0x4(%r14)
jne 0x42e55
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x42d2b
movl $0x69ae40, %edi         # imm = 0x69AE40
callq 0x63e0
movq %rax, %r15
movq %rax, %rdi
movq 0x20(%rsp), %rsi
callq 0x392bc
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x43186
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rbx, 0x30(%rsp)
movq %r13, 0x38(%rsp)
leaq 0x57c(%r14), %rbp
xorl %ebx, %ebx
movq %rbp, %rax
movswq (%rax), %r15
cmpq $0x11f, %r15            # imm = 0x11F
je 0x42dea
testb $0x1, %bl
jne 0x42dea
imulq $0x7c8, %r15, %r13      # imm = 0x7C8
cmpl $0x1, 0x4(%r12,%r13)
jne 0x42dd8
addq %r12, %r13
movswl 0x8(%r13), %esi
movq 0x20(%rsp), %rdi
movl 0x4(%rsp), %edx
callq 0x3bc4c
testb %al, %al
je 0x42dd8
movswl 0x8(%r13), %esi
movq 0x8(%rsp), %rdi
movl 0x4(%rsp), %edx
callq 0x394df
movl 0x7bc(%r14), %edx
xorl %edi, %edi
movq 0x8(%rsp), %rsi
movl 0x1c(%rsp), %ecx
callq 0x42ea4
movl %eax, %ebx
testb %al, %al
jne 0x42dcc
cmpl $0x2, 0x7c0(%r14)
jl 0x42dcc
movswq 0x8(%r13), %rax
movq 0x28(%rsp), %rcx
movb $0x1, (%rcx,%rax)
movq 0x8(%rsp), %rdi
callq 0x39e87
jmp 0x42dda
xorl %ebx, %ebx
leaq (,%r15,2), %rax
addq %rbp, %rax
jmp 0x42d41
testb $0x1, %bl
movq 0x38(%rsp), %r13
movq 0x30(%rsp), %rbx
jne 0x42e50
movq 0x20(%rsp), %rdi
movl %ebx, %esi
movl 0x4(%rsp), %edx
callq 0x3bc4c
testb %al, %al
je 0x42e50
movq 0x8(%rsp), %rdi
movl %ebx, %esi
movl 0x4(%rsp), %edx
callq 0x394df
cmpl $0x2, 0x7c0(%r14)
jl 0x42e46
xorl %edi, %edi
movq 0x8(%rsp), %rsi
movl %ebx, %edx
movl 0x1c(%rsp), %ecx
callq 0x42ea4
testb %al, %al
jne 0x42e46
movq 0x28(%rsp), %rax
movb $0x1, (%rax,%rbx)
movq 0x8(%rsp), %rdi
callq 0x39e87
movb $0x1, 0x40(%rsp,%rbx)
incq %r13
jmp 0x42c9c
leaq 0x10(%rsp), %rdi
callq 0x42c32
addq $0x398, %rsp            # imm = 0x398
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x69ae40, %esi         # imm = 0x69AE40
movq %r15, %rdi
callq 0x63f0
jmp 0x42e92
jmp 0x42e8f
jmp 0x42e8f
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x42c32
movq %rbx, %rdi
callq 0x6740