// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::SwapEdges(double)
void R3Mesh::
SwapEdges(RNAngle min_angle_improvement)
{
  // Initialize edge values
  RNHeap<RNScalar *> heap(0, -1, FALSE);
  RNScalar *edge_values = new RNScalar [ NEdges() ];
  for (int i = 0; i < NEdges(); i++) {
    R3MeshEdge *edge = Edge(i);
    edge_values[i] = EdgeSwapAngleImprovement(edge);
    if (edge_values[i] < min_angle_improvement) continue;
    heap.Push(&edge_values[i]);
  }

  // Swap edges
  while (!heap.IsEmpty()) {
    // Pop edge
    RNScalar *edge_value = heap.Pop();
    int edge_index = edge_value - edge_values;
    R3MeshEdge *edge = Edge(edge_index);
    if (*edge_value < min_angle_improvement) break;

    // Check current edge value
    RNScalar current_value = EdgeSwapAngleImprovement(edge);
    if (current_value != edge_values[edge_index]) {
      edge_values[edge_index] = current_value;
      if (current_value >= min_angle_improvement) heap.Push(&edge_values[edge_index]);
      continue;
    }

    // Swap the edge
    if (!SwapEdge(edge)) continue;

    // Update edge value
    edge_values[edge_index] = -current_value; // EdgeSwapAngleImprovement(edge);

    // Update neighbors
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 2; j++) {
        R3MeshFace *face = FaceOnEdge(edge, i);
        if (!face) continue;
        R3MeshEdge *neighbor_edge = EdgeOnFace(face, edge, j);
        int neighbor_index = EdgeID(neighbor_edge);
        RNScalar old_neighbor_value = edge_values[neighbor_index];
        edge_values[neighbor_index] = EdgeSwapAngleImprovement(neighbor_edge);
        if (old_neighbor_value >= min_angle_improvement) {
          if (edge_values[neighbor_index] >= min_angle_improvement) heap.Update(&edge_values[neighbor_index]);
          else heap.Remove(&edge_values[neighbor_index]);
        }
        else {
          if (edge_values[neighbor_index] >= min_angle_improvement) heap.Push(&edge_values[neighbor_index]);
        }
      }
    }
  }

  // Delete edge values
  delete [] edge_values;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movsd %xmm0, 0x8(%rsp)
movq %rdi, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x30(%rsp)
movupd %xmm0, 0x38(%rsp)
movupd %xmm0, 0x44(%rsp)
movslq 0x24(%rdi), %rbp
leaq (,%rbp,8), %rax
testq %rbp, %rbp
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r14
xorl %r13d, %r13d
leaq 0x20(%rsp), %r15
movq %rax, %r12
movslq %ebp, %rax
cmpq %rax, %r13
jge 0x166ad
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rsi
movq %rbx, %rdi
callq 0x1612c
movsd %xmm0, (%r12)
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x16566
movq %r15, %rdi
movq %r12, %rsi
callq 0x218b6
incq %r13
movl 0x24(%rbx), %ebp
addq $0x8, %r12
jmp 0x1652d
leaq 0x20(%rsp), %rdi
callq 0x219be
movsd 0x8(%rsp), %xmm0
ucomisd (%rax), %xmm0
ja 0x166b8
subq %r14, %rax
movq 0x18(%rbx), %rcx
shrq $0x3, %rax
movslq %eax, %rbp
movq (%rcx,%rbp,8), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1612c
leaq (%r14,%rbp,8), %r13
ucomisd (%r14,%rbp,8), %xmm0
jne 0x165b7
jnp 0x165db
movsd %xmm0, (%r13)
ucomisd 0x8(%rsp), %xmm0
jb 0x166ad
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x218b6
jmp 0x166ad
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
movapd %xmm0, 0x10(%rsp)
callq *0xd0(%rax)
movapd 0x10(%rsp), %xmm0
testl %eax, %eax
je 0x166ad
xorpd 0x7ca2a(%rip), %xmm0    # 0x93030
movlpd %xmm0, (%r13)
xorl %r13d, %r13d
cmpq $0x2, %r13
je 0x166ad
xorl %ebp, %ebp
cmpl $0x2, %ebp
je 0x166a5
movq 0x18(%r12,%r13,8), %rsi
testq %rsi, %rsi
je 0x1669e
movq %rbx, %rdi
movq %r12, %rdx
movl %ebp, %ecx
callq 0x21a5e
movslq 0x30(%rax), %r15
movsd (%r14,%r15,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1612c
leaq (%r14,%r15,8), %rsi
movsd %xmm0, (%rsi)
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
ucomisd %xmm1, %xmm2
jae 0x16682
ucomisd %xmm1, %xmm0
jb 0x1669e
leaq 0x20(%rsp), %rdi
callq 0x218b6
jmp 0x1669e
ucomisd %xmm1, %xmm0
jae 0x16694
leaq 0x20(%rsp), %rdi
callq 0x21b0e
jmp 0x1669e
leaq 0x20(%rsp), %rdi
callq 0x21a94
incl %ebp
jmp 0x1661b
incq %r13
jmp 0x1660f
cmpl $0x0, 0x28(%rsp)
jne 0x16572
movq %r14, %rdi
callq 0xe5d0
leaq 0x20(%rsp), %rdi
callq 0x21c10
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x166e1
jmp 0x166e1
jmp 0x166e1
jmp 0x166e1
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x21c10
movq %rbx, %rdi
callq 0xe810