// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
movq %rsi, 0x18(%rsp)
movslq 0x5c(%rsi), %rax
movq 0x8(%rdi), %rcx
movq (%rcx,%rax,8), %rbx
movq %rdx, 0x20(%rsp)
movslq 0x5c(%rdx), %rax
movq (%rcx,%rax,8), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xb702
movq %r15, %rdx
testb %al, %al
jne 0xafaf
movq %r15, %rdi
movq %rbx, %rsi
callq 0xb702
movq %rbx, %rdx
testb %al, %al
jne 0xafaf
movq %rbx, %rdi
movq %r15, %rsi
callq 0xb681
movq %rax, %rdx
movq 0x18(%rbx), %r12
movq 0x20(%r12), %rcx
movq 0x18(%r15), %rbp
movq 0x20(%rbp), %r13
movq 0x18(%rsp), %rax
cmpl $0x1, 0x4c(%rax)
movq 0x20(%rsp), %rax
movl 0x4c(%rax), %eax
jne 0xafff
cmpl $0x1, %eax
jne 0xb033
movq %rbp, %rdi
movq %rdx, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
callq 0x8299
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rdx
movq %r12, 0x18(%rbp)
movq %rbp, 0x20(%r12)
jmp 0xb03f
cmpl $0x2, %eax
jne 0xb04f
movq %rbp, %rdi
movq %rdx, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
callq 0x8299
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rax
movq %r13, 0x18(%rax)
movq %rax, 0x20(%r13)
movq %r12, 0x18(%rbp)
movq %rbp, 0x20(%r12)
jmp 0xb060
movq %r12, 0x18(%r13)
movq %r13, 0x20(%r12)
movq %rbp, %r13
movq %rcx, 0x20(%r13)
movq %r13, 0x18(%rcx)
movq %r13, 0x18(%rbx)
pushq $0x1
jmp 0xb062
movq %rbp, 0x18(%rcx)
movq %rcx, 0x20(%rbp)
movq %r13, 0x20(%r12)
movq %r12, 0x18(%r13)
pushq $0x2
popq %rax
leaq 0x18(%r15), %rcx
andq $0x0, 0x20(%rbx)
cmpq %r15, %rdx
jne 0xb085
movq 0x8(%r15), %rdx
cmpq %rbx, %rdx
je 0xb07e
movq %rdx, 0x8(%rbx)
movb 0x4(%r15), %dl
movb %dl, 0x4(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rcx)
movq %rbx, 0x8(%r15)
movq 0x18(%rsp), %rdi
movl 0x5c(%rdi), %ecx
movq 0x20(%rsp), %rsi
movl 0x5c(%rsi), %edx
orl $-0x1, 0x5c(%rdi)
orl $-0x1, 0x5c(%rsi)
addq $0xa0, %r14
movq (%r14), %r14
testq %r14, %r14
je 0xb0ca
cmpl %edx, 0x5c(%r14)
je 0xb0c2
addq $0x78, %r14
jmp 0xb0ae
movl %ecx, 0x5c(%r14)
movl %eax, 0x4c(%r14)
movl (%rbx), %eax
movl %eax, (%r15)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq