// File: /workspace/github_cpp_makefile_star2_O1/antoniogarro[P]Hara/heuristics.cpp
// Function: Goban::fast_ladder(int, bool) const
bool Goban::fast_ladder(int point, bool color) const
{
  if (total_liberties(point, color, 0) != 2) return false;
  if (neighbour_groups(point, !color, 2, 0)) return false;
  PointList<5> liberties;
  point_liberties(point, liberties);
  for (int i = 0; i < liberties.length(); i++) {
    PointList<5> secondary_libs;
    int delta[2] = {0};
    if (point_liberties(liberties[i], secondary_libs) == 4) continue;
  
    delta[0] = liberties[i] - point;
    for (int j = 0; j < secondary_libs.length(); j++) {
      if (secondary_libs[j] != point && secondary_libs[j] != liberties[i] + delta[0]) {
        delta[1] = secondary_libs[j] - liberties[i];
        break;
      }
    }
    if (delta[1] == 0) return true;
    int p = point, act = 0;
    while (distance_to_edge[p] > 1) {
      p = p + delta[act];
      //look for ladder breakers. TODO: complete.
      if (points[p]) {
        if (points[p]->get_color() == color) break;
        else return true;
      }
      if (points[p + delta[act]]) {
        if (points[p + delta[act]]->get_color() == color) break;
        else return true;
      }
      act = 1-act;
    }
    if (distance_to_edge[p] < 2) return true;
  }
  return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %ebx
movl %esi, %ebp
movq %rdi, %r14
xorl %r15d, %r15d
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x90a8
cmpl $0x2, %eax
jne 0x8348
movl %ebx, %eax
xorb $0x1, %al
movzbl %al, %edx
xorl %r15d, %r15d
movq %r14, %rdi
movl %ebp, %esi
movl $0x2, %ecx
xorl %r8d, %r8d
callq 0x52f0
testl %eax, %eax
je 0x835e
andb $0x1, %r15b
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x78fb(%rip), %rax      # 0xfc60
leaq 0x38(%rsp), %r12
movq %rax, (%r12)
movl %r15d, 0x1c(%r12)
movl %r15d, 0x8(%r12)
movq %r14, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x525c
cmpl %r15d, 0x1c(%r12)
setg %r15b
jle 0x8348
movslq %ebp, %rax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
leaq 0x78bb(%rip), %rax      # 0xfc60
movq %rax, 0x18(%rsp)
movl %r13d, 0x34(%rsp)
movl %r13d, 0x20(%rsp)
movq $0x0, 0x8(%rsp)
movl 0x40(%rsp,%r12,4), %esi
movq %r14, %rdi
leaq 0x18(%rsp), %rdx
callq 0x525c
movl $0x4, %ecx
cmpl $0x4, %eax
je 0x8485
movl 0x40(%rsp,%r12,4), %eax
movl %eax, %ecx
subl %ebp, %ecx
movl %ecx, 0x8(%rsp)
movslq 0x34(%rsp), %rdx
testq %rdx, %rdx
jle 0x8414
addl %eax, %ecx
xorl %esi, %esi
movl 0x20(%rsp,%rsi,4), %edi
cmpl %ebp, %edi
je 0x8404
cmpl %ecx, %edi
jne 0x840e
incq %rsi
cmpq %rsi, %rdx
jne 0x83f8
jmp 0x8414
subl %eax, %edi
movl %edi, 0xc(%rsp)
movl $0x1, %ecx
cmpl $0x0, 0xc(%rsp)
je 0x8485
movq 0x10(%rsp), %rax
cmpl $0x2, 0xdf6f8(%r14,%rax,4)
movl %ebp, %eax
jl 0x8475
xorl %edx, %edx
movl %ebp, %eax
movl %edx, %esi
movl 0x8(%rsp,%rsi,4), %edi
addl %edi, %eax
movslq %eax, %rsi
movq 0x18(%r14,%rsi,8), %r8
testq %r8, %r8
jne 0x8470
addl %eax, %edi
movslq %edi, %rdi
movq 0x18(%r14,%rdi,8), %rdi
testq %rdi, %rdi
jne 0x84a8
movl $0x1, %edi
subl %edx, %edi
cmpl $0x2, 0xdf6f8(%r14,%rsi,4)
movl %edi, %edx
jge 0x8436
jmp 0x8475
cmpb %bl, (%r8)
jne 0x8485
cltq
xorl %ecx, %ecx
cmpl $0x2, 0xdf6f8(%r14,%rax,4)
setl %cl
testb $0x3, %cl
jne 0x8348
incq %r12
movslq 0x54(%rsp), %rax
cmpq %rax, %r12
setl %r15b
jl 0x839e
jmp 0x8348
cmpb %bl, (%rdi)
je 0x8475
jmp 0x8485