// File: /workspace/github_cpp_makefile_star2_O3/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r12
movslq 0x5c(%rsi), %rax
movq 0x8(%rdi), %rcx
movq (%rcx,%rax,8), %rbx
movslq 0x5c(%rdx), %rax
movq (%rcx,%rax,8), %r14
movq %rbx, %rcx
movq 0x8(%rcx), %rcx
cmpq %r14, %rcx
je 0xc1d9
testq %rcx, %rcx
jne 0xc1cb
movq %r14, %rax
cmpq %r14, %rcx
je 0xc205
movq %r14, %rcx
movq 0x8(%rcx), %rcx
cmpq %rbx, %rcx
je 0xc1f2
testq %rcx, %rcx
jne 0xc1e4
movq %rbx, %rax
cmpq %rbx, %rcx
je 0xc205
movq %rbx, %rdi
movq %r14, %rsi
callq 0xca8b
movq 0x18(%rbx), %rdx
movq 0x20(%rdx), %rsi
movq 0x18(%r14), %rdi
movq 0x20(%rdi), %rcx
cmpl $0x1, 0x4c(%r13)
movl 0x4c(%r15), %r8d
jne 0xc253
cmpl $0x1, %r8d
jne 0xc286
movq %rdi, %r8
movq 0x18(%r8), %r9
movq 0x20(%r8), %r10
movq %r10, 0x18(%r8)
movq %r9, 0x20(%r8)
movq %r9, %r8
cmpq %rdi, %r9
jne 0xc229
movq %rdx, 0x18(%rdi)
movq %rdi, 0x20(%rdx)
movq %rcx, 0x18(%rsi)
movq %rsi, 0x20(%rcx)
jmp 0xc299
cmpl $0x2, %r8d
jne 0xc2a4
movq %rdi, %r8
movq 0x18(%r8), %r9
movq 0x20(%r8), %r10
movq %r10, 0x18(%r8)
movq %r9, 0x20(%r8)
movq %r9, %r8
cmpq %rdi, %r9
jne 0xc25c
movq %rcx, 0x18(%rsi)
movq %rsi, 0x20(%rcx)
movq %rdx, 0x18(%rdi)
movq %rdi, 0x20(%rdx)
jmp 0xc2b4
movq %rdx, 0x18(%rcx)
movq %rcx, 0x20(%rdx)
movq %rsi, 0x20(%rdi)
movq %rdi, 0x18(%rsi)
movq %rdi, %rcx
movq %rcx, 0x18(%rbx)
movl $0x1, %ecx
jmp 0xc2b9
movq %rdi, 0x18(%rsi)
movq %rsi, 0x20(%rdi)
movq %rcx, 0x20(%rdx)
movq %rdx, 0x18(%rcx)
movl $0x2, %ecx
leaq 0x18(%r14), %rdx
movq $0x0, 0x20(%rbx)
cmpq %r14, %rax
jne 0xc2de
movq 0x8(%r14), %rax
cmpq %rbx, %rax
je 0xc2d7
movq %rax, 0x8(%rbx)
movb 0x4(%r14), %al
movb %al, 0x4(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rdx)
movq %rbx, 0x8(%r14)
movl 0x5c(%r13), %eax
movl 0x5c(%r15), %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %esi, 0x5c(%r13)
movl %esi, 0x5c(%r15)
movq 0xa0(%r12), %rsi
testq %rsi, %rsi
je 0xc31b
cmpl %edx, 0x5c(%rsi)
je 0xc315
movq 0x78(%rsi), %rsi
jmp 0xc305
movl %eax, 0x5c(%rsi)
movl %ecx, 0x4c(%rsi)
movl (%rbx), %eax
movl %eax, (%r14)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq