// File: /workspace/github_cpp_makefile_star2_O2/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:
movl 0x48(%rsi), %eax
movq %rsi, %rcx
movq 0x80(%rcx), %rcx
testq %rcx, %rcx
je 0xaa49
cmpl %eax, 0x48(%rcx)
jne 0xaa16
movl 0x50(%rcx), %r9d
testl %r9d, %r9d
je 0xaa16
movl 0x50(%rsi), %edx
testl %edx, %edx
jne 0xaa67
cmpl $0x1, 0x68(%rdi)
je 0xaa67
movl $0x1, 0x54(%rsi)
jmp 0xab03
movl 0x50(%rsi), %ecx
cmpl $0x1, %ecx
adcl $0x0, %ecx
movl %ecx, 0x54(%rsi)
andl $0x0, 0x58(%rsi)
leaq 0xa0(%rdi), %rcx
xorl %edx, %edx
jmp 0xab0d
xorl %r8d, %r8d
testl %eax, %eax
sete %r8b
cmpl $0x0, 0xb4(%rdi,%r8,4)
je 0xaa9d
movl 0x54(%rcx), %r8d
movl %r8d, %r10d
imull %r9d, %r10d
testl %r10d, %r10d
js 0xaac6
testl %edx, %edx
jne 0xaad6
movl %r8d, %edx
sarl $0x1f, %edx
orl $0x1, %edx
addl %edx, %r8d
jmp 0xaaff
testl %edx, %edx
jne 0xaaf1
movb $0x1, %dl
movq %rcx, %r8
movq 0x80(%r8), %r8
testq %r8, %r8
je 0xaae9
cmpl %eax, 0x48(%r8)
jne 0xaaa6
cmpl $0x0, 0x50(%r8)
setne %r9b
xorb %r9b, %dl
jmp 0xaaa6
movl %r8d, %r10d
negl %r10d
cmovsl %r8d, %r10d
cmpl $0x2, %r10d
jb 0xaaf6
imull %edx, %r9d
xorl %r10d, %r10d
testl %r9d, %r9d
cmovnsl %edx, %r10d
addl %r10d, %r8d
jmp 0xaaff
notb %dl
andb $0x1, %dl
movzbl %dl, %edx
movl %edx, 0x54(%rsi)
jmp 0xab03
cmpl $0x1, %edx
adcl $0x0, %edx
movl %edx, %r8d
movl %r8d, 0x54(%rsi)
movl 0x58(%rcx), %edx
movl %edx, 0x58(%rsi)
addq $0x78, %rcx
movq (%rcx), %rcx
xorl %r8d, %r8d
testl %eax, %eax
setne %r8b
cmpl $0x0, 0xb4(%rdi,%r8,4)
je 0xab4b
cmpq %rsi, %rcx
je 0xab50
addl 0x50(%rcx), %edx
movl %edx, 0x58(%rsi)
movq 0x78(%rcx), %rcx
jmp 0xab24
cmpl $0x0, 0x50(%rcx)
je 0xab47
xorl %eax, %eax
testl %edx, %edx
sete %al
movl %eax, 0x58(%rsi)
movl %eax, %edx
movq 0x78(%rcx), %rcx
cmpq %rsi, %rcx
jne 0xab35
retq
nop