// File: /workspace/github_cpp_makefile_star2_O2/hzeller[P]gcode-multi-shell-extrude/third_party/clipper.cpp
// Function: ClipperLib::ClipperBase::ProcessBound(ClipperLib::TEdge*, bool)
TEdge* ClipperBase::ProcessBound(TEdge* E, bool IsClockwise)
{
  TEdge *EStart = E, *Result = E;
  TEdge *Horz = 0;
  cInt StartX;
  if (IsHorizontal(*E))
  {
    //it's possible for adjacent overlapping horz edges to start heading left
    //before finishing right, so ...
    if (IsClockwise) StartX = E->Prev->Bot.X;
    else StartX = E->Next->Bot.X;
    if (E->Bot.X != StartX) ReverseHorizontal(*E);
  }
  
  if (Result->OutIdx != Skip)
  {
    if (IsClockwise)
    {
      while (Result->Top.Y == Result->Next->Bot.Y && Result->Next->OutIdx != Skip)
        Result = Result->Next;
      if (IsHorizontal(*Result) && Result->Next->OutIdx != Skip)
      {
        //nb: at the top of a bound, horizontals are added to the bound
        //only when the preceding edge attaches to the horizontal's left vertex
        //unless a Skip edge is encountered when that becomes the top divide
        Horz = Result;
        while (IsHorizontal(*Horz->Prev)) Horz = Horz->Prev;
        if (Horz->Prev->Top.X == Result->Next->Top.X) 
        {
          if (!IsClockwise) Result = Horz->Prev;
        }
        else if (Horz->Prev->Top.X > Result->Next->Top.X) Result = Horz->Prev;
      }
      while (E != Result) 
      {
        E->NextInLML = E->Next;
        if (IsHorizontal(*E) && E != EStart &&
          E->Bot.X != E->Prev->Top.X) ReverseHorizontal(*E);
        E = E->Next;
      }
      if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Prev->Top.X) 
        ReverseHorizontal(*E);
      Result = Result->Next; //move to the edge just beyond current bound
    } else
    {
      while (Result->Top.Y == Result->Prev->Bot.Y && Result->Prev->OutIdx != Skip) 
        Result = Result->Prev;
      if (IsHorizontal(*Result) && Result->Prev->OutIdx != Skip)
      {
        Horz = Result;
        while (IsHorizontal(*Horz->Next)) Horz = Horz->Next;
        if (Horz->Next->Top.X == Result->Prev->Top.X) 
        {
          if (!IsClockwise) Result = Horz->Next;
        }
        else if (Horz->Next->Top.X > Result->Prev->Top.X) Result = Horz->Next;
      }

      while (E != Result)
      {
        E->NextInLML = E->Prev;
        if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Next->Top.X) 
          ReverseHorizontal(*E);
        E = E->Prev;
      }
      if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Next->Top.X) 
        ReverseHorizontal(*E);
      Result = Result->Prev; //move to the edge just beyond current bound
    }
  }

  if (Result->OutIdx == Skip) 
  {
    //if edges still remain in the current bound beyond the skip edge then
    //create another LocMin and call ProcessBound once more
    E = Result;
    if (IsClockwise)
    {
      while (E->Top.Y == E->Next->Bot.Y) E = E->Next;
      //don't include top horizontals when parsing a bound a second time,
      //they will be contained in the opposite bound ...
      while (E != Result && IsHorizontal(*E)) E = E->Prev;
    } else
    {
      while (E->Top.Y == E->Prev->Bot.Y) E = E->Prev;
      while (E != Result && IsHorizontal(*E)) E = E->Next;
    }
    if (E == Result)
    {
      if (IsClockwise) Result = E->Next;
      else Result = E->Prev;
    } else
    {
      //there are more edges in the bound beyond result starting with E
      if (IsClockwise)
        E = Result->Next; 
      else
        E = Result->Prev;
      LocalMinima* locMin = new LocalMinima;
      locMin->Next = 0;
      locMin->Y = E->Bot.Y;
      locMin->LeftBound = 0;
      locMin->RightBound = E;
      locMin->RightBound->WindDelta = 0;
      Result = ProcessBound(locMin->RightBound, IsClockwise);
      InsertLocalMinima(locMin);
    }
  }
  return Result;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
cmpq $0x0, 0x38(%rsi)
movzbl %dl, %r14d
jne 0x8a6e
movq 0x60(%rsi,%r14,8), %rcx
movq (%rsi), %rax
cmpq (%rcx), %rax
je 0x8a6e
movq 0x20(%rsi), %rcx
movq %rax, 0x20(%rsi)
movq %rcx, (%rsi)
cmpl $-0x2, 0x5c(%rsi)
je 0x8bcf
movq %rsi, %rcx
testb %dl, %dl
je 0x8afa
movq %rcx, %rax
movq 0x28(%rcx), %rdi
movq 0x60(%rcx), %rcx
cmpq 0x8(%rcx), %rdi
jne 0x8a96
cmpl $-0x2, 0x5c(%rcx)
jne 0x8a7f
cmpq $0x0, 0x38(%rax)
jne 0x8abf
cmpl $-0x2, 0x5c(%rcx)
je 0x8abf
movq %rax, %rdi
movq 0x68(%rdi), %rdi
cmpq $0x0, 0x38(%rdi)
je 0x8aa6
movq 0x20(%rcx), %rcx
cmpq %rcx, 0x20(%rdi)
je 0x8abf
cmovgq %rdi, %rax
movq %rsi, %rdi
movq %rdi, %rcx
cmpq %rax, %rdi
je 0x8b3b
movq 0x38(%rcx), %r8
movq 0x60(%rcx), %rdi
movq %rdi, 0x70(%rcx)
cmpq %rsi, %rcx
je 0x8ac2
testq %r8, %r8
jne 0x8ac2
movq (%rcx), %r8
movq 0x68(%rcx), %r9
cmpq 0x20(%r9), %r8
je 0x8ac2
movq 0x20(%rcx), %r9
movq %r8, 0x20(%rcx)
movq %r9, (%rcx)
jmp 0x8ac2
movq %rcx, %rax
movq 0x28(%rcx), %rdi
movq 0x68(%rcx), %rcx
cmpq 0x8(%rcx), %rdi
jne 0x8b11
cmpl $-0x2, 0x5c(%rcx)
jne 0x8afa
cmpq $0x0, 0x38(%rax)
jne 0x8b69
cmpl $-0x2, 0x5c(%rcx)
je 0x8b69
movq %rax, %rdi
movq 0x60(%rdi), %rdi
cmpq $0x0, 0x38(%rdi)
je 0x8b21
movq 0x20(%rcx), %rcx
cmpq %rcx, 0x20(%rdi)
jne 0x8b65
movq %rdi, %rax
jmp 0x8b69
cmpq %rsi, %rax
je 0x8b5f
cmpq $0x0, 0x38(%rcx)
jne 0x8b5f
movq (%rcx), %rsi
movq 0x68(%rcx), %rdi
cmpq 0x20(%rdi), %rsi
je 0x8b5f
movq 0x20(%rcx), %rdi
movq %rsi, 0x20(%rcx)
movq %rdi, (%rcx)
addq $0x60, %rax
jmp 0x8bcc
cmovgq %rdi, %rax
movq %rsi, %rdi
movq %rdi, %rcx
cmpq %rax, %rdi
je 0x8ba4
movq 0x38(%rcx), %r8
movq 0x68(%rcx), %rdi
movq %rdi, 0x70(%rcx)
cmpq %rsi, %rcx
je 0x8b6c
testq %r8, %r8
jne 0x8b6c
movq (%rcx), %r8
movq 0x60(%rcx), %r9
cmpq 0x20(%r9), %r8
je 0x8b6c
movq 0x20(%rcx), %r9
movq %r8, 0x20(%rcx)
movq %r9, (%rcx)
jmp 0x8b6c
cmpq %rsi, %rax
je 0x8bc8
cmpq $0x0, 0x38(%rcx)
jne 0x8bc8
movq (%rcx), %rsi
movq 0x60(%rcx), %rdi
cmpq 0x20(%rdi), %rsi
je 0x8bc8
movq 0x20(%rcx), %rdi
movq %rsi, 0x20(%rcx)
movq %rdi, (%rcx)
addq $0x68, %rax
movq (%rax), %rsi
cmpl $-0x2, 0x5c(%rsi)
jne 0x8c8f
movq %rsi, %rcx
testb %dl, %dl
je 0x8bf3
movq %rcx, %rax
movq 0x28(%rcx), %rdi
movq 0x60(%rcx), %rcx
cmpq 0x8(%rcx), %rdi
je 0x8be0
jmp 0x8c11
movq %rcx, %rax
movq 0x28(%rcx), %rdi
movq 0x68(%rcx), %rcx
cmpq 0x8(%rcx), %rdi
je 0x8bf3
jmp 0x8c23
cmpq $0x0, 0x38(%rax)
jne 0x8c28
movq 0x68(%rax), %rax
cmpq %rsi, %rax
jne 0x8c06
jmp 0x8c28
cmpq $0x0, 0x38(%rax)
jne 0x8c28
movq 0x60(%rax), %rax
cmpq %rsi, %rax
jne 0x8c18
cmpq %rsi, %rax
je 0x8c81
xorb $0x1, %dl
movzbl %dl, %eax
movq 0x60(%rsi,%rax,8), %r15
pushq $0x20
popq %rdi
callq 0x3230
movq %rax, %r12
andq $0x0, 0x18(%rax)
movq 0x8(%r15), %rax
movq %rax, (%r12)
andq $0x0, 0x8(%r12)
movq %r15, 0x10(%r12)
andl $0x0, 0x50(%r15)
movq %rbx, %rdi
movq %r15, %rsi
movl %r14d, %edx
callq 0x8a40
movq %rax, %r14
movq %rbx, %rdi
movq %r12, %rsi
callq 0x8c9e
movq %r14, %rax
jmp 0x8c92
testb %dl, %dl
je 0x8c8b
movq 0x60(%rax), %rsi
jmp 0x8c8f
movq 0x68(%rax), %rsi
movq %rsi, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq