// File: /workspace/github_cpp_makefile_star2_O1/kobanium[P]Ray/src/feature/Ladder.cpp
// Function: IsLadderCaptured(int, search_game_info_t*, int, int)
static bool
IsLadderCaptured( const int depth, search_game_info_t *game, const int ren_xy, const int turn_color )
{
  const char *board = game->board;
  const string_t *string = game->string;
  const int str = game->string_id[ren_xy];
  int escape_color, capture_color, escape_xy, capture_xy, neighbor;
  bool result;
  
  if (depth >= 100 || game->moves >= MAX_RECORDS - 1) {
    return ALIVE;
  }

  if (board[ren_xy] == S_EMPTY) {
    return DEAD;
  } else if (string[str].libs >= 3) {
    return ALIVE;
  }

  escape_color = board[ren_xy];
  capture_color = GetOppositeColor(escape_color);

  if (turn_color == escape_color) {
    // 周囲の敵連が取れるか確認し,
    // 取れるなら取って探索を続ける
    neighbor = string[str].neighbor[0];
    while (neighbor != NEIGHBOR_END) {
      if (string[neighbor].libs == 1) {
        if (IsLegalForSearch(game, string[neighbor].lib[0], escape_color)) {
          PutStoneForSearch(game, string[neighbor].lib[0], escape_color);
          result = IsLadderCaptured(depth + 1, game, ren_xy, GetOppositeColor(turn_color));
          Undo(game);
          if (result == ALIVE) {
            return ALIVE;
          }
        }
      }
      neighbor = string[str].neighbor[neighbor];
    }
    
    // 逃げる手を打ってみて探索を続ける
    escape_xy = string[str].lib[0];
    while (escape_xy != LIBERTY_END) {
      if (IsLegalForSearch(game, escape_xy, escape_color)) {
        PutStoneForSearch(game, escape_xy, escape_color);
        result = IsLadderCaptured(depth + 1, game, ren_xy, GetOppositeColor(turn_color));
        Undo(game);
        if (result == ALIVE) {
          return ALIVE;
        }
      }
      escape_xy = string[str].lib[escape_xy];
    }
    return DEAD;
  } else {
    if (string[str].libs == 1) {
      return DEAD;
    }
    // 追いかける側なのでアタリにする手を打ってみる
    capture_xy = string[str].lib[0];
    while (capture_xy != LIBERTY_END) {
      if (IsLegalForSearch(game, capture_xy, capture_color)) {
        PutStoneForSearch(game, capture_xy, capture_color);
        result = IsLadderCaptured(depth + 1, game, ren_xy, GetOppositeColor(turn_color));
        Undo(game);
        if (result == DEAD) {
          return DEAD;
        }
      }
      capture_xy = string[str].lib[capture_xy];
    }
  }

  return ALIVE;
}

// Assembly:
cmpl $0x63, %edi
jle 0x5c28c
movb $0x1, %al
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
cmpl $0x439, 0x43b0(%rsi)    # imm = 0x439
jg 0x5c2df
movslq %edx, %rax
movsbl 0x43cc(%r14,%rax), %r12d
testl %r12d, %r12d
je 0x5c41a
movl %ecx, %ebp
leaq 0x95f0(%r14), %rbx
movslq 0x956f0(%r14,%rax,4), %rax
imulq $0x7c8, %rax, %rcx      # imm = 0x7C8
movl 0x4(%rbx,%rcx), %eax
cmpl $0x2, %eax
jle 0x5c2e6
movb $0x1, %al
jmp 0x5c41c
movl %edi, %r15d
addq %rbx, %rcx
cmpl %ebp, %r12d
movq %rcx, 0x8(%rsp)
jne 0x5c415
movl %edx, (%rsp)
movl %ebp, 0x4(%rsp)
movzwl 0x57c(%rcx), %eax
cmpl $0x11f, %eax            # imm = 0x11F
je 0x5c3a0
leal 0x1(%r15), %edx
movl %edx, 0x14(%rsp)
movl 0x4(%rsp), %edx
xorl $0x3, %edx
movl %edx, 0x10(%rsp)
movswq %ax, %rbp
imulq $0x7c8, %rbp, %r13      # imm = 0x7C8
cmpl $0x1, 0x4(%rbx,%r13)
jne 0x5c391
addq %rbx, %r13
movswl 0x8(%r13), %esi
movq %r14, %rdi
movl %r12d, %edx
callq 0x51adc
movq 0x8(%rsp), %rcx
testb %al, %al
je 0x5c391
movswl 0x8(%r13), %esi
movq %r14, %rdi
movl %r12d, %edx
callq 0x51bb9
movl 0x14(%rsp), %edi
movq %r14, %rsi
movl (%rsp), %edx
movl 0x10(%rsp), %ecx
callq 0x5c284
movl %eax, %r13d
movq %r14, %rdi
callq 0x5280d
movq 0x8(%rsp), %rcx
testb %r13b, %r13b
jne 0x5c2df
movzwl 0x57c(%rcx,%rbp,2), %eax
cmpl $0x11f, %eax            # imm = 0x11F
jne 0x5c326
movzwl 0x8(%rcx), %ecx
cmpl $0x2b7, %ecx            # imm = 0x2B7
je 0x5c41a
incl %r15d
movl 0x4(%rsp), %ebp
xorl $0x3, %ebp
movswl %cx, %r13d
movq %r14, %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0x51adc
testb %al, %al
je 0x5c3fc
movq %r14, %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0x51bb9
movl %r15d, %edi
movq %r14, %rsi
movl (%rsp), %edx
movl %ebp, %ecx
callq 0x5c284
movl %eax, %ebx
movq %r14, %rdi
callq 0x5280d
testb %bl, %bl
jne 0x5c2df
movslq %r13d, %rax
movq 0x8(%rsp), %rcx
movzwl 0x8(%rcx,%rax,2), %ecx
xorl %eax, %eax
cmpl $0x2b7, %ecx            # imm = 0x2B7
jne 0x5c3b6
jmp 0x5c41c
cmpl $0x1, %eax
jne 0x5c42b
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzwl 0x8(%rcx), %eax
cmpl $0x2b7, %eax            # imm = 0x2B7
je 0x5c2df
movl %edx, (%rsp)
xorl $0x3, %r12d
incl %r15d
xorl $0x3, %ebp
movswl %ax, %r13d
movq %r14, %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0x51adc
testb %al, %al
je 0x5c489
movq %r14, %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0x51bb9
movl %r15d, %edi
movq %r14, %rsi
movl (%rsp), %edx
movl %ebp, %ecx
callq 0x5c284
movl %eax, %ebx
movq %r14, %rdi
callq 0x5280d
testb %bl, %bl
je 0x5c41a
movslq %r13d, %rax
movq 0x8(%rsp), %rcx
movzwl 0x8(%rcx,%rax,2), %eax
cmpl $0x2b7, %eax            # imm = 0x2B7
jne 0x5c447
jmp 0x5c2df