// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::SwapEdge(gaps::R3MeshEdge*)
int R3Mesh::
SwapEdge(R3MeshEdge *edge)
{
  // Get vertices on edge
  R3MeshVertex *v[2];
  v[0] = edge->vertex[0];
  v[1] = edge->vertex[1];

  // Get faces on edge
  R3MeshFace *f[2];
  f[0] = edge->face[0];
  f[1] = edge->face[1];
  if (!f[0] || !f[1]) return 0;

  // Get edges and vertices across faces
  R3MeshEdge *ef[2][2] = { { NULL, NULL }, { NULL, NULL } };
  R3MeshVertex *vf[2] = { NULL, NULL };
  ef[0][0] = EdgeAcrossVertex(v[0], edge, f[0]);
  ef[0][1] = EdgeAcrossVertex(v[1], edge, f[0]);
  vf[0] = VertexAcrossEdge(ef[0][1], v[1]);
  assert(vf[0] == VertexAcrossEdge(ef[0][0], v[0]));
  ef[1][0] = EdgeAcrossVertex(v[0], edge, f[1]);
  ef[1][1] = EdgeAcrossVertex(v[1], edge, f[1]);
  vf[1] = VertexAcrossEdge(ef[1][1], v[1]);
  assert(vf[1] == VertexAcrossEdge(ef[1][0], v[0]));
  if (EdgeBetweenVertices(vf[0], vf[1])) return 0;

  // Update vertices
  v[0]->edges.Remove(edge);
  v[1]->edges.Remove(edge);
  vf[0]->edges.Insert(edge);
  vf[1]->edges.Insert(edge);

  // Update edge
  edge->vertex[0] = vf[0];
  edge->vertex[1] = vf[1];
  edge->length = 0;
  edge->flags = 0;

  // Update faces
  UpdateFaceRefs(f[0], vf[0], vf[1], v[1], edge, ef[1][1], ef[0][1]);
  UpdateFacePlane(f[0]);
  UpdateFaceBBox(f[0]);
  UpdateFaceRefs(f[1], vf[1], vf[0], v[0], edge, ef[0][0], ef[1][0]);
  UpdateFacePlane(f[1]);
  UpdateFaceBBox(f[1]);

  // Invalidate GL buffer objects
  InvalidateGLBufferObjects();

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq 0x18(%rsi), %r14
movq 0x20(%rsi), %r12
testq %r14, %r14
setne %al
testq %r12, %r12
setne %cl
andb %al, %cl
xorl %eax, %eax
cmpb $0x1, %cl
jne 0x16dc6
movq %rsi, %r15
movq %rdi, %rbx
movq 0x8(%rsi), %rbp
movq 0x10(%rsi), %r13
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x13962
movq %rax, 0x38(%rsp)
movq %r13, %rsi
movq %r15, %rdx
movq %r14, 0x40(%rsp)
movq %r14, %rcx
callq 0x13962
movq %rax, %rcx
movq 0x8(%rax), %rax
movq %rax, (%rsp)
cmpq %r13, %rax
jne 0x16c1b
movq 0x10(%rcx), %rax
movq %rax, (%rsp)
movq %rcx, 0x28(%rsp)
movq %rbp, 0x8(%rsp)
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x13962
movq %rax, 0x30(%rsp)
movq %r13, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x13962
movq %rax, %r9
movq 0x8(%rax), %r14
cmpq %r13, %r14
jne 0x16c56
movq 0x10(%r9), %r14
movq (%rsp), %rbp
cmpq %r14, %rbp
movq 0x8(%rsp), %r8
je 0x16c95
movslq 0x14(%rbp), %rcx
testq %rcx, %rcx
jle 0x16c95
movq 0x8(%rbp), %rdx
xorl %eax, %eax
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
cmpq %r14, 0x8(%rdi)
je 0x16dc6
cmpq %r14, 0x10(%rdi)
je 0x16dc6
incq %rsi
cmpq %rsi, %rcx
jne 0x16c75
movq %r12, 0x20(%rsp)
leaq 0x8(%r8), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r13, 0x10(%rsp)
movq %r8, %r13
movq %r9, 0x18(%rsp)
callq 0x6f884
subq 0x8(%r13), %rax
shrq $0x3, %rax
movq %r12, %rdi
movl %eax, %esi
callq 0x6fa2c
movq 0x10(%rsp), %r13
leaq 0x8(%r13), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x6f884
subq 0x8(%r13), %rax
shrq $0x3, %rax
movq %r12, %rdi
movl %eax, %esi
callq 0x6fa2c
leaq 0x8(%rbp), %rdi
movl 0x14(%rbp), %edx
movq %r15, %rsi
callq 0x6f8a2
leaq 0x8(%r14), %rdi
movl 0x14(%r14), %edx
movq %r15, %rsi
callq 0x6f8a2
movq %rbp, 0x8(%r15)
movq %r14, 0x10(%r15)
xorl %eax, %eax
movq %rax, 0x28(%r15)
movq %rax, 0x38(%r15)
movq (%rbx), %rax
movq %rbx, %rdi
movq 0x40(%rsp), %r12
movq %r12, %rsi
movq %rbp, %rdx
movq %r14, %rcx
movq %r13, %r8
movq %r15, %r9
pushq 0x28(%rsp)
pushq 0x20(%rsp)
callq *0x2d8(%rax)
addq $0x10, %rsp
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x2c8(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x2d0(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
movq 0x20(%rsp), %r12
movq %r12, %rsi
movq %r14, %rdx
movq %rbp, %rcx
movq 0x8(%rsp), %r8
movq %r15, %r9
pushq 0x30(%rsp)
pushq 0x40(%rsp)
callq *0x2d8(%rax)
addq $0x10, %rsp
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x2c8(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x2d0(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x290(%rax)
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop