// File: hzeller[P]gcode-multi-shell-extrude/third_party/clipper.cpp
// Function: ClipperLib::Clipper::AppendPolygon(ClipperLib::TEdge*, ClipperLib::TEdge*)
void Clipper::AppendPolygon(TEdge *e1, TEdge *e2)
{
  //get the start and ends of both output polygons ...
  OutRec *outRec1 = m_PolyOuts[e1->OutIdx];
  OutRec *outRec2 = m_PolyOuts[e2->OutIdx];

  OutRec *holeStateRec;
  if (Param1RightOfParam2(outRec1, outRec2)) 
    holeStateRec = outRec2;
  else if (Param1RightOfParam2(outRec2, outRec1)) 
    holeStateRec = outRec1;
  else 
    holeStateRec = GetLowermostRec(outRec1, outRec2);

  //get the start and ends of both output polygons and
  //join e2 poly onto e1 poly and delete pointers to e2 ...

  OutPt* p1_lft = outRec1->Pts;
  OutPt* p1_rt = p1_lft->Prev;
  OutPt* p2_lft = outRec2->Pts;
  OutPt* p2_rt = p2_lft->Prev;

  EdgeSide Side;
  //join e2 poly onto e1 poly and delete pointers to e2 ...
  if(  e1->Side == esLeft )
  {
    if(  e2->Side == esLeft )
    {
      //z y x a b c
      ReversePolyPtLinks(p2_lft);
      p2_lft->Next = p1_lft;
      p1_lft->Prev = p2_lft;
      p1_rt->Next = p2_rt;
      p2_rt->Prev = p1_rt;
      outRec1->Pts = p2_rt;
    } else
    {
      //x y z a b c
      p2_rt->Next = p1_lft;
      p1_lft->Prev = p2_rt;
      p2_lft->Prev = p1_rt;
      p1_rt->Next = p2_lft;
      outRec1->Pts = p2_lft;
    }
    Side = esLeft;
  } else
  {
    if(  e2->Side == esRight )
    {
      //a b c z y x
      ReversePolyPtLinks(p2_lft);
      p1_rt->Next = p2_rt;
      p2_rt->Prev = p1_rt;
      p2_lft->Next = p1_lft;
      p1_lft->Prev = p2_lft;
    } else
    {
      //a b c x y z
      p1_rt->Next = p2_lft;
      p2_lft->Prev = p1_rt;
      p1_lft->Prev = p2_rt;
      p2_rt->Next = p1_lft;
    }
    Side = esRight;
  }

  outRec1->BottomPt = 0;
  if (holeStateRec == outRec2)
  {
    if (outRec2->FirstLeft != outRec1)
      outRec1->FirstLeft = outRec2->FirstLeft;
    outRec1->IsHole = outRec2->IsHole;
  }
  outRec2->Pts = 0;
  outRec2->BottomPt = 0;
  outRec2->FirstLeft = outRec1;

  int OKIdx = e1->OutIdx;
  int ObsoleteIdx = e2->OutIdx;

  e1->OutIdx = Unassigned; //nb: safe because we only get here via AddLocalMaxPoly
  e2->OutIdx = Unassigned;

  TEdge* e = m_ActiveEdges;
  while( e )
  {
    if( e->OutIdx == ObsoleteIdx )
    {
      e->OutIdx = OKIdx;
      e->Side = Side;
      break;
    }
    e = e->NextInAEL;
  }

  outRec2->Idx = outRec1->Idx;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x70(%rbp)
addq $0x8, %rdi
movq -0x10(%rbp), %rax
movslq 0x5c(%rax), %rsi
callq 0x20d90
movq -0x70(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x20(%rbp)
addq $0x8, %rdi
movq -0x18(%rbp), %rax
movslq 0x5c(%rax), %rsi
callq 0x20d90
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x189f0
testb $0x1, %al
jne 0x17a13
jmp 0x17a1d
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x17a4d
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x189f0
testb $0x1, %al
jne 0x17a30
jmp 0x17a3a
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x17a4b
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x188a0
movq %rax, -0x30(%rbp)
jmp 0x17a4d
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x1, 0x4c(%rax)
jne 0x17b21
movq -0x18(%rbp), %rax
cmpl $0x1, 0x4c(%rax)
jne 0x17adc
movq -0x48(%rbp), %rdi
callq 0x11ec0
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x50(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x40(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x50(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x17b18
movq -0x38(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x50(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x48(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x1, -0x54(%rbp)
jmp 0x17b9d
movq -0x18(%rbp), %rax
cmpl $0x2, 0x4c(%rax)
jne 0x17b66
movq -0x48(%rbp), %rdi
callq 0x11ec0
movq -0x50(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x40(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
jmp 0x17b96
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x50(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x38(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x2, -0x54(%rbp)
movq -0x20(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0x17be2
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
cmpq -0x20(%rbp), %rax
je 0x17bd1
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movb 0x4(%rax), %cl
movq -0x20(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x4(%rax)
movq -0x70(%rbp), %rax
movq -0x28(%rbp), %rcx
movq $0x0, 0x18(%rcx)
movq -0x28(%rbp), %rcx
movq $0x0, 0x20(%rcx)
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq %rdx, 0x8(%rcx)
movq -0x10(%rbp), %rcx
movl 0x5c(%rcx), %ecx
movl %ecx, -0x58(%rbp)
movq -0x18(%rbp), %rcx
movl 0x5c(%rcx), %ecx
movl %ecx, -0x5c(%rbp)
movq -0x10(%rbp), %rcx
movl $0xffffffff, 0x5c(%rcx) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rcx
movl $0xffffffff, 0x5c(%rcx) # imm = 0xFFFFFFFF
movq 0xa0(%rax), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x17c76
movq -0x68(%rbp), %rax
movl 0x5c(%rax), %eax
cmpl -0x5c(%rbp), %eax
jne 0x17c68
movl -0x58(%rbp), %ecx
movq -0x68(%rbp), %rax
movl %ecx, 0x5c(%rax)
movl -0x54(%rbp), %ecx
movq -0x68(%rbp), %rax
movl %ecx, 0x4c(%rax)
jmp 0x17c76
movq -0x68(%rbp), %rax
movq 0x78(%rax), %rax
movq %rax, -0x68(%rbp)
jmp 0x17c3f
movq -0x20(%rbp), %rax
movl (%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)