// File: /workspace/github_cpp_makefile_star2_O1/hzeller[P]gcode-multi-shell-extrude/third_party/clipper.cpp
// Function: ClipperLib::Clipper::SetWindingCount(ClipperLib::TEdge&)
void Clipper::SetWindingCount(TEdge &edge)
{
  TEdge *e = edge.PrevInAEL;
  //find the edge of the same polytype that immediately preceeds 'edge' in AEL
  while (e  && ((e->PolyTyp != edge.PolyTyp) || (e->WindDelta == 0))) e = e->PrevInAEL;
  if (!e)
  {
    edge.WindCnt = (edge.WindDelta == 0 ? 1 : edge.WindDelta);
    edge.WindCnt2 = 0;
    e = m_ActiveEdges; //ie get ready to calc WindCnt2
  }   
  else if (edge.WindDelta == 0 && m_ClipType != ctUnion)
  {
    edge.WindCnt = 1;
    edge.WindCnt2 = e->WindCnt2;
    e = e->NextInAEL; //ie get ready to calc WindCnt2
  }
  else if (IsEvenOddFillType(edge))
  {
    //EvenOdd filling ...
    if (edge.WindDelta == 0)
    {
      //are we inside a subj polygon ...
      bool Inside = true;
      TEdge *e2 = e->PrevInAEL;
      while (e2)
      {
        if (e2->PolyTyp == e->PolyTyp && e2->WindDelta != 0) 
          Inside = !Inside;
        e2 = e2->PrevInAEL;
      }
      edge.WindCnt = (Inside ? 0 : 1);
    }
    else
    {
      edge.WindCnt = edge.WindDelta;
    }
    edge.WindCnt2 = e->WindCnt2;
    e = e->NextInAEL; //ie get ready to calc WindCnt2
  } 
  else
  {
    //nonZero, Positive or Negative filling ...
    if (e->WindCnt * e->WindDelta < 0)
    {
      //prev edge is 'decreasing' WindCount (WC) toward zero
      //so we're outside the previous polygon ...
      if (Abs(e->WindCnt) > 1)
      {
        //outside prev poly but still inside another.
        //when reversing direction of prev poly use the same WC 
        if (e->WindDelta * edge.WindDelta < 0) edge.WindCnt = e->WindCnt;
        //otherwise continue to 'decrease' WC ...
        else edge.WindCnt = e->WindCnt + edge.WindDelta;
      } 
      else
        //now outside all polys of same polytype so set own WC ...
        edge.WindCnt = (edge.WindDelta == 0 ? 1 : edge.WindDelta);
    } else
    {
      //prev edge is 'increasing' WindCount (WC) away from zero
      //so we're inside the previous polygon ...
      if (edge.WindDelta == 0) 
        edge.WindCnt = (e->WindCnt < 0 ? e->WindCnt - 1 : e->WindCnt + 1);
      //if wind direction is reversing prev then use same WC
      else if (e->WindDelta * edge.WindDelta < 0) edge.WindCnt = e->WindCnt;
      //otherwise add to WC ...
      else edge.WindCnt = e->WindCnt + edge.WindDelta;
    }
    edge.WindCnt2 = e->WindCnt2;
    e = e->NextInAEL; //ie get ready to calc WindCnt2
  }

  //update WindCnt2 ...
  if (IsEvenOddAltFillType(edge))
  {
    //EvenOdd filling ...
    while (e != &edge)
    {
      if (e->WindDelta != 0)
        edge.WindCnt2 = (edge.WindCnt2 == 0 ? 1 : 0);
      e = e->NextInAEL;
    }
  } else
  {
    //nonZero, Positive or Negative filling ...
    while ( e != &edge )
    {
      edge.WindCnt2 += e->WindDelta;
      e = e->NextInAEL;
    }
  }
}

// Assembly:
movq 0x80(%rsi), %rax
testq %rax, %rax
je 0xbf73
movl 0x48(%rsi), %r9d
movl 0x48(%rax), %edx
cmpl %r9d, %edx
jne 0xbf67
movl 0x50(%rax), %r8d
testl %r8d, %r8d
jne 0xbf92
movq 0x80(%rax), %rax
testq %rax, %rax
jne 0xbf56
movl 0x50(%rsi), %eax
cmpl $0x1, %eax
adcl $0x0, %eax
movl %eax, 0x54(%rsi)
movl $0x0, 0x58(%rsi)
leaq 0xa0(%rdi), %rax
jmp 0xc058
movl 0x50(%rsi), %ecx
testl %ecx, %ecx
jne 0xbfab
cmpl $0x1, 0x68(%rdi)
je 0xbfab
movl $0x1, 0x54(%rsi)
jmp 0xc04e
xorl %r10d, %r10d
testl %r9d, %r9d
sete %r10b
cmpl $0x0, 0xb4(%rdi,%r10,4)
je 0xbfdf
movl 0x54(%rax), %edx
movl %edx, %r9d
imull %r8d, %r9d
testl %r9d, %r9d
js 0xc019
testl %ecx, %ecx
jne 0xc029
movl %edx, %ecx
sarl $0x1f, %ecx
orl $0x1, %ecx
addl %ecx, %edx
jmp 0xc044
testl %ecx, %ecx
jne 0xc04b
movq 0x80(%rax), %r8
testq %r8, %r8
je 0xc049
movb $0x1, %cl
cmpl %edx, 0x48(%r8)
jne 0xc003
cmpl $0x0, 0x50(%r8)
setne %r9b
xorb %r9b, %cl
movq 0x80(%r8), %r8
testq %r8, %r8
jne 0xbff1
notb %cl
andb $0x1, %cl
movzbl %cl, %ecx
jmp 0xc04b
movl %edx, %r9d
negl %r9d
cmovsl %edx, %r9d
cmpl $0x2, %r9d
jb 0xc03c
imull %ecx, %r8d
xorl %r9d, %r9d
testl %r8d, %r8d
cmovnsl %ecx, %r9d
addl %r9d, %edx
jmp 0xc044
cmpl $0x1, %ecx
adcl $0x0, %ecx
movl %ecx, %edx
movl %edx, 0x54(%rsi)
jmp 0xc04e
xorl %ecx, %ecx
movl %ecx, 0x54(%rsi)
movl 0x58(%rax), %ecx
movl %ecx, 0x58(%rsi)
addq $0x78, %rax
xorl %ecx, %ecx
cmpl $0x0, 0x48(%rsi)
movq (%rax), %rax
setne %cl
cmpl $0x0, 0xb4(%rdi,%rcx,4)
je 0xc086
cmpq %rsi, %rax
je 0xc0a9
movl 0x58(%rsi), %ecx
addl 0x50(%rax), %ecx
movq 0x78(%rax), %rax
cmpq %rsi, %rax
jne 0xc076
movl %ecx, 0x58(%rsi)
retq
cmpq %rsi, %rax
je 0xc0a9
movl 0x58(%rsi), %ecx
cmpl $0x0, 0x50(%rax)
je 0xc0a0
xorl %edx, %edx
testl %ecx, %ecx
sete %dl
movl %edx, 0x58(%rsi)
movl %edx, %ecx
movq 0x78(%rax), %rax
cmpq %rsi, %rax
jne 0xc08e
retq