// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::SplitEdge(gaps::R3MeshEdge*, gaps::R3Point const&, gaps::R3MeshEdge**, gaps::R3MeshEdge**)
R3MeshVertex *R3Mesh::
SplitEdge(R3MeshEdge *edge, const R3Point& point, R3MeshEdge **e0, R3MeshEdge **e1)
{
  // Create vertex at split point
  R3MeshVertex *vertex = CreateVertex(R3zero_point);

  // 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];

  // Get other edges and vertex on f[0]
  R3MeshEdge *ef[2][2] = { { NULL, NULL }, { NULL, NULL } };
  R3MeshVertex *vf[2] = { NULL, NULL };
  int m[2] = { 0, 0 };
  int s[2] = { 0, 0 };
  int c[2] = { 0, 0 };
  if (f[0]) {
    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]));
    m[0] = FaceMaterial(f[0]);
    s[0] = FaceSegment(f[0]);
    c[0] = FaceCategory(f[0]);
  }
  if (f[1]) {
    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]));
    m[1] = FaceMaterial(f[1]);
    s[1] = FaceSegment(f[1]);
    c[1] = FaceCategory(f[1]);
  }

  // Delete the edge and the two adjacent faces
  if (f[0]) DeleteFace(f[0]);
  if (f[1]) DeleteFace(f[1]);
  DeleteEdge(edge);

  // Create two new edges
  R3MeshEdge *e[2];
  e[0] = CreateEdge(v[0], vertex);
  e[1] = CreateEdge(vertex, v[1]);

  // Create two new faces on one side
  if (f[0]) {
    // Create edge splitting f[0]
    R3MeshEdge *es = CreateEdge(vertex, vf[0]);

    // Create new face on v[0] side of split
    assert((vf[0] != v[0]) && (vf[0] != vertex) && (v[0] != vertex));
    assert((ef[0][0] != e[0]) && (ef[0][0] != es) && (e[0] != es));
    R3MeshFace *f00 = CreateFace(vf[0], v[0], vertex, ef[0][0], e[0], es);
    SetFaceMaterial(f00, m[0]);
    SetFaceSegment(f00, s[0]);
    SetFaceCategory(f00, c[0]);

    // Create new face on v[1] side of split
    assert((es != e[1]) && (es != ef[0][1]) && (e[1] != ef[0][1]));
    R3MeshFace *f01 = CreateFace(vf[0], vertex, v[1], es, e[1], ef[0][1]);
    SetFaceMaterial(f01, m[0]);
    SetFaceSegment(f01, s[0]);
    SetFaceCategory(f01, c[0]);
  }

  // Create two new faces on other side
  if (f[1]) {
    // Create edge splitting f[1]
    R3MeshEdge *es = CreateEdge(vertex, vf[1]);
    
    // Create new face on v[1] side of split
    assert((vf[1] != v[1]) && (vf[1] != vertex) && (v[1] != vertex));
    assert((ef[1][1] != e[1]) && (ef[1][1] != es) && (e[1] != es));
    R3MeshFace *f10 = CreateFace(vf[1], v[1], vertex, ef[1][1], e[1], es);
    SetFaceMaterial(f10, m[1]);
    SetFaceSegment(f10, s[1]);
    SetFaceCategory(f10, c[1]);

    // Create new face on v[0] side of split
    assert((es != e[0]) && (es != ef[1][0]) && (e[0] != ef[1][0]));
    R3MeshFace *f11 = CreateFace(vf[1], vertex, v[0], es, e[0], ef[1][0]);
    SetFaceMaterial(f11, m[1]);
    SetFaceSegment(f11, s[1]);
    SetFaceCategory(f11, c[1]);
  }

  // Interpolate vertex properties
  RNScalar d0 = R3Distance(point, VertexPosition(v[0]));
  RNScalar d1 = R3Distance(point, VertexPosition(v[1]));
  RNScalar dsum = d0 + d1;
  if (RNIsPositive(dsum)) {
    d0 /= dsum;  d1 /= dsum;
    SetVertexTextureCoords(vertex, d1*VertexTextureCoords(v[0]) + d0*VertexTextureCoords(v[1])); 
    SetVertexColor(vertex, d1*VertexColor(v[0]) + d0*VertexColor(v[1])); 
  }

  // Set vertex position (also marks things in need of update)
  SetVertexPosition(vertex, point);

  // Return edges
  if (e0) *e0 = e[0];
  if (e1) *e1 = e[1];

  // Return vertex
  return vertex;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r8, 0x88(%rsp)
movq %rcx, 0x80(%rsp)
movq %rdx, 0x68(%rsp)
movq %rsi, %r14
movq %rdi, %r13
movq (%rdi), %rax
leaq 0x4c6c34(%rip), %rsi    # 0x4dba70
movq $0x0, 0x8(%rsp)
xorl %edx, %edx
callq *0x20(%rax)
movq %rax, %r15
movq 0x8(%r14), %rax
movq %rax, 0x20(%rsp)
movq 0x10(%r14), %rbp
movq 0x18(%r14), %rbx
movq 0x20(%r14), %r12
movl $0x0, %ecx
movl $0x0, %edx
movl $0x0, %esi
movl $0x0, %edi
movl $0x0, %eax
movl $0x0, %r8d
movq %r8, (%rsp)
testq %rbx, %rbx
je 0x14ecb
movq 0x20(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x12dd6
movq %rax, (%rsp)
movq %rbp, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x12dd6
movq 0x8(%rax), %rdi
cmpq %rbp, %rdi
jne 0x14eb9
movq 0x10(%rax), %rdi
movl 0x98(%rbx), %esi
movl 0x9c(%rbx), %edx
movl 0xa0(%rbx), %ecx
movq %rdi, 0x78(%rsp)
movl %esi, 0x3c(%rsp)
movl %edx, 0x40(%rsp)
movl %ecx, 0x44(%rsp)
movq %rax, 0x28(%rsp)
testq %r12, %r12
je 0x14f47
movq 0x20(%rsp), %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x12dd6
movq %rax, 0x58(%rsp)
movq %rbp, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x12dd6
movq 0x8(%rax), %rcx
cmpq %rbp, %rcx
jne 0x14f16
movq 0x10(%rax), %rcx
movq %rax, 0x48(%rsp)
movq %rcx, 0x50(%rsp)
movl 0x98(%r12), %eax
movl %eax, 0x10(%rsp)
movl 0x9c(%r12), %eax
movq %rax, 0x8(%rsp)
movl 0xa0(%r12), %eax
movl %eax, 0x14(%rsp)
jmp 0x14f72
movl $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movq $0x0, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x58(%rsp)
testq %rbx, %rbx
je 0x14f84
movq (%r13), %rax
movq %r13, %rdi
movq %rbx, %rsi
callq *0x70(%rax)
testq %r12, %r12
je 0x14f96
movq (%r13), %rax
movq %r13, %rdi
movq %r12, %rsi
callq *0x70(%rax)
movq (%r13), %rax
movq %r13, %rdi
movq %r14, %rsi
callq *0x68(%rax)
movq (%r13), %rax
movq %r13, %rdi
movq 0x20(%rsp), %r14
movq %r14, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq *0x40(%rax)
movq %rax, 0x30(%rsp)
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq *0x40(%rax)
testq %rbx, %rbx
movq %rax, 0x18(%rsp)
movq %rbp, 0x60(%rsp)
je 0x15098
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
movq 0x78(%rsp), %rbp
movq %rbp, %rdx
xorl %ecx, %ecx
callq *0x40(%rax)
movq %rax, 0x70(%rsp)
movq (%r13), %r10
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq (%rsp), %r8
movq 0x30(%rsp), %r9
pushq $0x0
pushq %rax
callq *0x58(%r10)
addq $0x10, %rsp
movl 0x3c(%rsp), %ebx
movl %ebx, 0x98(%rax)
movq %r12, (%rsp)
movl 0x40(%rsp), %r12d
movl %r12d, 0x9c(%rax)
movl 0x44(%rsp), %r14d
movl %r14d, 0xa0(%rax)
movq (%r13), %rax
movq %r13, %rdi
movq %rbp, %rsi
movq 0x60(%rsp), %rbp
movq %r15, %rdx
movq %rbp, %rcx
movq 0x70(%rsp), %r8
movq 0x18(%rsp), %r9
pushq $0x0
pushq 0x30(%rsp)
callq *0x58(%rax)
addq $0x10, %rsp
movl %ebx, 0x98(%rax)
movl %r12d, 0x9c(%rax)
movq (%rsp), %r12
movl %r14d, 0xa0(%rax)
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %r14
testq %r12, %r12
movq %rax, %r12
je 0x15159
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
movq %r15, (%rsp)
movq 0x50(%rsp), %r15
movq %r15, %rdx
xorl %ecx, %ecx
callq *0x40(%rax)
movq %rax, 0x28(%rsp)
movq (%r13), %r10
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq (%rsp), %rcx
movq 0x48(%rsp), %r8
movq %r12, %r9
pushq $0x0
pushq %rax
callq *0x58(%r10)
addq $0x10, %rsp
movl 0x10(%rsp), %ebp
movl %ebp, 0x98(%rax)
movq 0x8(%rsp), %r12
movl %r12d, 0x9c(%rax)
movq %r14, %rbx
movl 0x14(%rsp), %r14d
movl %r14d, 0xa0(%rax)
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
movq (%rsp), %r15
movq %r15, %rdx
movq %rbx, %rcx
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %r9
pushq $0x0
pushq 0x60(%rsp)
callq *0x58(%rax)
addq $0x10, %rsp
movl %ebp, 0x98(%rax)
movq 0x60(%rsp), %rbp
movl %r12d, 0x9c(%rax)
movq 0x18(%rsp), %r12
movl %r14d, 0xa0(%rax)
movq %rbx, %r14
leaq 0x18(%r14), %rsi
movq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0x4321a
movsd %xmm0, 0x8(%rsp)
leaq 0x18(%rbp), %rsi
movq %rbx, %rdi
callq 0x4321a
movsd 0x8(%rsp), %xmm2
movapd %xmm2, %xmm1
addsd %xmm0, %xmm2
leaq 0xc5a17(%rip), %rax     # 0xdaba8
ucomisd (%rax), %xmm2
jbe 0x15266
divsd %xmm2, %xmm1
movsd %xmm1, 0x8(%rsp)
divsd %xmm2, %xmm0
movsd %xmm0, (%rsp)
leaq 0x48(%r14), %rsi
leaq 0x90(%rsp), %rdi
callq 0x5f833
leaq 0x48(%rbp), %rsi
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x5f833
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
leaq 0x90(%rsp), %rsi
movq %rbx, %rdx
callq 0x5f7a1
leaq 0x48(%r15), %rdi
movq %r12, %rsi
callq 0x5f6c4
addq $0x58, %r14
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movsd (%rsp), %xmm0
callq 0x223f6
addq $0x58, %rbp
leaq 0xa8(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x223f6
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq 0x18(%rsp), %rbx
callq 0x213f3
movq %r15, %rdi
addq $0x58, %rdi
movq %r12, %rsi
movq %rbx, %r12
callq 0x66212
movq %r13, %rdi
movq %r15, %rsi
movq 0x68(%rsp), %rdx
callq 0x12ccc
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0x1528b
movq 0x30(%rsp), %rcx
movq %rcx, (%rax)
movq 0x88(%rsp), %rax
testq %rax, %rax
je 0x1529b
movq %r12, (%rax)
movq %r15, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq