// File: /workspace/github_cpp_makefile_star2_O3/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 0x8ee4
cmpl $0x2, %eax
jne 0x83a0
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 0x5416
testl %eax, %eax
jne 0x83a0
leaq 0x7aff(%rip), %r13      # 0xfd80
leaq 0x38(%rsp), %r12
movq %r13, (%r12)
movl %r15d, 0x1c(%r12)
movl %r15d, 0x8(%r12)
movq %r14, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x5388
cmpl %r15d, 0x1c(%r12)
jle 0x83a0
movslq %ebp, %rax
movq %rax, 0x10(%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %r13, 0x18(%rsp)
movl %r15d, 0x34(%rsp)
movl %r15d, 0x20(%rsp)
movl 0x40(%rsp,%r12,4), %esi
movq %r14, %rdi
leaq 0x18(%rsp), %rdx
callq 0x5388
cmpl $0x4, %eax
je 0x8387
movl 0x40(%rsp,%r12,4), %eax
movl %eax, %ecx
subl %ebp, %ecx
movl %ecx, 0x8(%rsp)
movslq 0x34(%rsp), %rdx
testq %rdx, %rdx
jle 0x839d
addl %eax, %ecx
xorl %esi, %esi
movl 0x20(%rsp,%rsi,4), %edi
cmpl %ebp, %edi
je 0x830f
cmpl %ecx, %edi
jne 0x831c
incq %rsi
cmpq %rsi, %rdx
jne 0x8303
jmp 0x839d
subl %eax, %edi
movl %edi, 0xc(%rsp)
je 0x839d
movq 0x10(%rsp), %rax
cmpl $0x2, 0xdf6f8(%r14,%rax,4)
jl 0x837c
xorl %ecx, %ecx
movl %ebp, %edx
movl %ecx, %eax
movl 0x8(%rsp,%rax,4), %esi
addl %esi, %edx
movslq %edx, %rax
movq 0x18(%r14,%rax,8), %rdi
testq %rdi, %rdi
jne 0x8372
addl %edx, %esi
movslq %esi, %rsi
movq 0x18(%r14,%rsi,8), %rsi
testq %rsi, %rsi
jne 0x8378
movl $0x1, %esi
subl %ecx, %esi
cmpl $0x2, 0xdf6f8(%r14,%rax,4)
movl %esi, %ecx
jge 0x8338
jmp 0x837c
cmpb %bl, (%rdi)
je 0x837c
jmp 0x839d
cmpb %bl, (%rsi)
jne 0x839d
cmpl $0x1, 0xdf6f8(%r14,%rax,4)
jle 0x839d
incq %r12
movslq 0x54(%rsp), %rax
cmpq %rax, %r12
jl 0x82ba
xorl %r15d, %r15d
jmp 0x83a0
movb $0x1, %r15b
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq