// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::FillHoleBoundary(gaps::R3Mesh*, gaps::R3MeshBoundaryVertexData*)
static void
FillHoleBoundary(R3Mesh *mesh, R3MeshBoundaryVertexData *head)
{
  // Just checking
  // AssertBoundaryVertexData(mesh, head);

  // Initialize heap
  R3MeshBoundaryVertexData tmp;
  RNHeap<R3MeshBoundaryVertexData *> heap(&tmp, &(tmp.error), &(tmp.heappointer));
  R3MeshBoundaryVertexData *vdata = head;
  if (vdata) do {
    heap.Push(vdata);
    vdata = vdata->next;
  } while (vdata != head);

  // Iteratively select boundary vertex to form apex of new triangle
  while (heap.NEntries() > 3) {
    R3MeshBoundaryVertexData *vdata = heap.Pop();
    R3MeshBoundaryVertexData *prev_vdata = vdata->prev;
    R3MeshBoundaryVertexData *next_vdata = vdata->next;
    // AssertBoundaryVertexData(mesh, vdata);

    // Create face
    R3MeshVertex *vertex = vdata->vertex;
    R3MeshVertex *prev_vertex = vdata->prev->vertex;
    R3MeshVertex *next_vertex = vdata->next->vertex;
    R3MeshFace *face = mesh->CreateFace(prev_vertex, vertex, next_vertex);
    if (!face) { /* ERROR */ return; }

    // Update edge 
    vdata->prev->edge = mesh->EdgeAcrossFace(face, vertex);

    // Remove vdata from linked list
    vdata->prev->next = vdata->next;
    vdata->next->prev = vdata->prev;
    delete vdata;

    // Update error of prev_vdata and next_vdata
    for (int i = 0; i < 2; i++) {
      R3MeshBoundaryVertexData *vdata = (i == 0) ? prev_vdata : next_vdata;
      if (!vdata) continue;
      R3MeshVertex *prev_vertex = vdata->prev->vertex;
      R3MeshVertex *next_vertex = vdata->next->vertex;
      const R3Point& position = mesh->VertexPosition(vertex);
      const R3Point& prev_position = mesh->VertexPosition(prev_vertex);
      const R3Point& next_position = mesh->VertexPosition(next_vertex);
      const R3Vector& prev_normal = mesh->VertexNormal(prev_vertex);
      const R3Vector& next_normal = mesh->VertexNormal(next_vertex);
      R3Vector prev_vector = prev_position - position;
      R3Vector next_vector = next_position - position;
      R3Vector cross = prev_vector % next_vector;
      RNAngle interior_angle = R3InteriorAngle(prev_vector, next_vector);
      if (cross.Dot(mesh->VertexNormal(vdata->vertex)) > 0) interior_angle = RN_TWO_PI - interior_angle;
      RNAngle normal_angle = R3InteriorAngle(prev_normal, next_normal);
      vdata->error = interior_angle + normal_angle;
      heap.Update(vdata);
    }
  }

  // Create last triangle
  if (heap.NEntries() == 3) {
    R3MeshBoundaryVertexData *vdata = heap.Pop();
    R3MeshVertex *vertex = vdata->vertex;
    R3MeshVertex *prev_vertex = vdata->prev->vertex;
    R3MeshVertex *next_vertex = vdata->next->vertex;
    mesh->CreateFace(prev_vertex, vertex, next_vertex);
    delete vdata;
    vdata = heap.Pop(); delete vdata;
    vdata = heap.Pop(); delete vdata;
  }
}

// Assembly:
subq $0x228, %rsp            # imm = 0x228
movq %rdi, 0x220(%rsp)
movq %rsi, 0x218(%rsp)
leaq 0x1e8(%rsp), %rdx
addq $0x28, %rdx
leaq 0x1e8(%rsp), %rcx
addq $0x20, %rcx
leaq 0x1b0(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
movl $0x1, %r8d
callq 0x3b460
movq 0x218(%rsp), %rax
movq %rax, 0x1a8(%rsp)
cmpq $0x0, 0x1a8(%rsp)
je 0x20f8c
jmp 0x20f27
movq 0x1a8(%rsp), %rsi
leaq 0x1b0(%rsp), %rdi
callq 0x3b500
jmp 0x20f3e
movq 0x1a8(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x1a8(%rsp)
movq 0x1a8(%rsp), %rax
cmpq 0x218(%rsp), %rax
jne 0x20f27
jmp 0x20f8c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x3b940
jmp 0x2155c
jmp 0x20f8e
leaq 0x1b0(%rsp), %rdi
callq 0x3b720
movl %eax, 0x9c(%rsp)
jmp 0x20fa4
movl 0x9c(%rsp), %eax
cmpl $0x3, %eax
jle 0x213e2
leaq 0x1b0(%rsp), %rdi
callq 0x3b730
movq %rax, 0x90(%rsp)
jmp 0x20fcb
movq 0x90(%rsp), %rax
movq %rax, 0x190(%rsp)
movq 0x190(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x188(%rsp)
movq 0x190(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x180(%rsp)
movq 0x190(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x178(%rsp)
movq 0x190(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq %rax, 0x170(%rsp)
movq 0x190(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, 0x168(%rsp)
movq 0x220(%rsp), %rdi
movq 0x170(%rsp), %rsi
movq 0x178(%rsp), %rdx
movq 0x168(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x88(%rsp)
jmp 0x2107a
movq 0x88(%rsp), %rax
movq %rax, 0x160(%rsp)
cmpq $0x0, 0x160(%rsp)
jne 0x210a5
movl $0x1, 0x15c(%rsp)
jmp 0x21547
movq 0x220(%rsp), %rdi
movq 0x160(%rsp), %rsi
movq 0x178(%rsp), %rdx
callq 0x39c40
movq %rax, 0x80(%rsp)
jmp 0x210cc
movq 0x80(%rsp), %rcx
movq 0x190(%rsp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x8(%rax)
movq 0x190(%rsp), %rax
movq 0x18(%rax), %rcx
movq 0x190(%rsp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x18(%rax)
movq 0x190(%rsp), %rax
movq 0x10(%rax), %rcx
movq 0x190(%rsp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x10(%rax)
movq 0x190(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, %rax
je 0x2113e
movq 0x78(%rsp), %rdi
movl $0x30, %esi
callq 0xf4d0
movl $0x0, 0x158(%rsp)
cmpl $0x2, 0x158(%rsp)
jge 0x213dd
cmpl $0x0, 0x158(%rsp)
jne 0x21170
movq 0x188(%rsp), %rax
movq %rax, 0x70(%rsp)
jmp 0x2117d
movq 0x180(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x150(%rsp)
cmpq $0x0, 0x150(%rsp)
jne 0x2119a
jmp 0x213c7
movq 0x150(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq %rax, 0x148(%rsp)
movq 0x150(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, 0x140(%rsp)
movq 0x220(%rsp), %rdi
movq 0x178(%rsp), %rsi
callq 0x13660
movq %rax, 0x138(%rsp)
movq 0x220(%rsp), %rdi
movq 0x148(%rsp), %rsi
callq 0x13660
movq %rax, 0x130(%rsp)
movq 0x220(%rsp), %rdi
movq 0x140(%rsp), %rsi
callq 0x13660
movq %rax, 0x128(%rsp)
movq 0x220(%rsp), %rdi
movq 0x148(%rsp), %rsi
callq 0x36860
movq %rax, 0x68(%rsp)
jmp 0x2123b
movq 0x68(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x220(%rsp), %rdi
movq 0x140(%rsp), %rsi
callq 0x36860
movq %rax, 0x60(%rsp)
jmp 0x21264
movq 0x60(%rsp), %rax
movq %rax, 0x118(%rsp)
movq 0x130(%rsp), %rsi
movq 0x138(%rsp), %rdx
leaq 0x100(%rsp), %rdi
callq 0x6eeb0
jmp 0x21290
movq 0x128(%rsp), %rsi
movq 0x138(%rsp), %rdx
leaq 0xe8(%rsp), %rdi
callq 0x6eeb0
jmp 0x212af
leaq 0xd0(%rsp), %rdi
leaq 0x100(%rsp), %rsi
leaq 0xe8(%rsp), %rdx
callq 0x70af0
jmp 0x212ce
leaq 0x100(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x8c230
movsd %xmm0, 0x58(%rsp)
jmp 0x212eb
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movq 0x220(%rsp), %rdi
movq 0x150(%rsp), %rax
movq (%rax), %rsi
callq 0x36860
movq %rax, 0x50(%rsp)
jmp 0x21319
movq 0x50(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x702c0
movsd %xmm0, 0x48(%rsp)
jmp 0x21333
movsd 0x48(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2135f
leaq 0x1155af(%rip), %rax    # 0x1368f8
movsd (%rax), %xmm0
subsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movq 0x120(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x8c230
movsd %xmm0, 0x40(%rsp)
jmp 0x2137c
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0xc8(%rsp), %xmm0
movsd 0xc0(%rsp), %xmm1
addsd %xmm1, %xmm0
movq 0x150(%rsp), %rax
movsd %xmm0, 0x28(%rax)
movq 0x150(%rsp), %rsi
leaq 0x1b0(%rsp), %rdi
callq 0x3b840
jmp 0x213c5
jmp 0x213c7
movl 0x158(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x158(%rsp)
jmp 0x21149
jmp 0x20f8e
leaq 0x1b0(%rsp), %rdi
callq 0x3b720
movl %eax, 0x3c(%rsp)
jmp 0x213f5
movl 0x3c(%rsp), %eax
cmpl $0x3, %eax
jne 0x2153c
leaq 0x1b0(%rsp), %rdi
callq 0x3b730
movq %rax, 0x30(%rsp)
jmp 0x21416
movq 0x30(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
movq 0xb8(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq %rax, 0xa8(%rsp)
movq 0xb8(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, 0xa0(%rsp)
movq 0x220(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0xb0(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
jmp 0x21492
movq 0xb8(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x214b4
movq 0x28(%rsp), %rdi
movl $0x30, %esi
callq 0xf4d0
leaq 0x1b0(%rsp), %rdi
callq 0x3b730
movq %rax, 0x20(%rsp)
jmp 0x214c8
movq 0x20(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x214f7
movq 0x18(%rsp), %rdi
movl $0x30, %esi
callq 0xf4d0
leaq 0x1b0(%rsp), %rdi
callq 0x3b730
movq %rax, 0x10(%rsp)
jmp 0x2150b
movq 0x10(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x2153a
movq 0x8(%rsp), %rdi
movl $0x30, %esi
callq 0xf4d0
jmp 0x2153c
movl $0x0, 0x15c(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x3b940
addq $0x228, %rsp            # imm = 0x228
retq
movq 0x1a0(%rsp), %rdi
callq 0xf890
nopl (%rax)