// File: /workspace/github_cpp_makefile_star2_O3/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), %rcx
movl 0x48(%rsi), %eax
testq %rcx, %rcx
je 0xbbec
cmpl %eax, 0x48(%rcx)
jne 0xbbe3
movl 0x50(%rcx), %r9d
testl %r9d, %r9d
jne 0xbc0d
movq 0x80(%rcx), %rcx
jmp 0xbbd0
movl 0x50(%rsi), %ecx
cmpl $0x1, %ecx
adcl $0x0, %ecx
movl %ecx, 0x54(%rsi)
movl $0x0, 0x58(%rsi)
leaq 0xa0(%rdi), %rcx
xorl %edx, %edx
jmp 0xbcd7
movl 0x50(%rsi), %edx
testl %edx, %edx
jne 0xbc26
cmpl $0x1, 0x68(%rdi)
je 0xbc26
movl $0x1, 0x54(%rsi)
jmp 0xbccd
xorl %r8d, %r8d
testl %eax, %eax
sete %r8b
cmpl $0x0, 0xb4(%rdi,%r8,4)
je 0xbc5c
movl 0x54(%rcx), %r8d
movl %r8d, %r10d
imull %r9d, %r10d
testl %r10d, %r10d
js 0xbc96
testl %edx, %edx
jne 0xbca6
movl %r8d, %edx
sarl $0x1f, %edx
orl $0x1, %edx
addl %edx, %r8d
jmp 0xbcc2
testl %edx, %edx
jne 0xbcca
movq 0x80(%rcx), %r8
testq %r8, %r8
je 0xbcc8
movb $0x1, %dl
cmpl %eax, 0x48(%r8)
jne 0xbc80
cmpl $0x0, 0x50(%r8)
setne %r9b
xorb %r9b, %dl
movq 0x80(%r8), %r8
testq %r8, %r8
jne 0xbc6e
notb %dl
andb $0x1, %dl
movzbl %dl, %edx
jmp 0xbcca
movl %r8d, %r10d
negl %r10d
cmovsl %r8d, %r10d
cmpl $0x2, %r10d
jb 0xbcb9
imull %edx, %r9d
xorl %r10d, %r10d
testl %r9d, %r9d
cmovnsl %edx, %r10d
addl %r10d, %r8d
jmp 0xbcc2
cmpl $0x1, %edx
adcl $0x0, %edx
movl %edx, %r8d
movl %r8d, 0x54(%rsi)
jmp 0xbccd
xorl %edx, %edx
movl %edx, 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 0xbd19
cmpq %rsi, %rcx
je 0xbd1e
addl 0x50(%rcx), %edx
movq 0x78(%rcx), %rcx
cmpq %rsi, %rcx
jne 0xbcf3
movl %edx, 0x58(%rsi)
retq
cmpl $0x0, 0x50(%rcx)
je 0xbd15
xorl %eax, %eax
testl %edx, %edx
sete %al
movl %eax, 0x58(%rsi)
movl %eax, %edx
movq 0x78(%rcx), %rcx
cmpq %rsi, %rcx
jne 0xbd03
retq
nop