// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %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), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, %r14
cmpl $0x0, 0x24(%rbx)
jle 0x17c53
xorl %r13d, %r13d
leaq 0x20(%rsp), %r15
movq %r14, %r12
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rsi
movq %rbx, %rdi
callq 0x175e6
movsd %xmm0, (%r12)
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x17acc
movq %r15, %rdi
movq %r12, %rsi
callq 0x2412e
incq %r13
movslq 0x24(%rbx), %rax
addq $0x8, %r12
cmpq %rax, %r13
jl 0x17a9f
jmp 0x17c53
leaq 0x20(%rsp), %rdi
callq 0x2422a
movq %rax, %r13
movsd (%rax), %xmm1
movsd 0x8(%rsp), %xmm0
movsd %xmm1, 0x60(%rsp)
ucomisd %xmm1, %xmm0
ja 0x17c45
subq %r14, %r13
movq 0x18(%rbx), %rax
shrq $0x3, %r13
movslq %r13d, %rcx
movq (%rax,%rcx,8), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x175e6
movslq %r13d, %rax
leaq (%r14,%rax,8), %r13
ucomisd (%r14,%rax,8), %xmm0
jne 0x17b36
jnp 0x17b5a
movsd %xmm0, (%r13)
ucomisd 0x8(%rsp), %xmm0
jb 0x17c45
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x2412e
jmp 0x17c45
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
movapd %xmm0, 0x10(%rsp)
callq *0xd0(%rax)
movapd 0x10(%rsp), %xmm0
testl %eax, %eax
je 0x17c45
xorpd 0x8a49b(%rip), %xmm0    # 0xa2020
movlpd %xmm0, (%r13)
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x18(%r12,%r13,8), %rax
testq %rax, %rax
je 0x17c2a
movq 0x20(%rax), %rsi
cmpq %r12, %rsi
je 0x17bb8
movq 0x28(%rax), %rcx
cmpq %r12, %rcx
je 0x17bc2
testl %ebp, %ebp
cmovneq %rcx, %rsi
jmp 0x17bca
testl %ebp, %ebp
jne 0x17bc6
movq 0x28(%rax), %rsi
jmp 0x17bca
testl %ebp, %ebp
jne 0x17bca
movq 0x30(%rax), %rsi
movslq 0x30(%rsi), %r15
movsd (%r14,%r15,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %rbx, %rdi
callq 0x175e6
leaq (%r14,%r15,8), %rsi
movsd %xmm0, (%rsi)
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
ucomisd %xmm1, %xmm2
jae 0x17c0e
ucomisd %xmm1, %xmm0
jb 0x17c2a
leaq 0x20(%rsp), %rdi
callq 0x2412e
jmp 0x17c2a
ucomisd %xmm1, %xmm0
jae 0x17c20
leaq 0x20(%rsp), %rdi
callq 0x24334
jmp 0x17c2a
leaq 0x20(%rsp), %rdi
callq 0x242c4
incl %ebp
cmpl $0x1, %ebp
je 0x17b90
leaq 0x1(%r13), %rax
testq %r13, %r13
movq %rax, %r13
je 0x17b8e
movsd 0x8(%rsp), %xmm0
ucomisd 0x60(%rsp), %xmm0
ja 0x17c5e
cmpl $0x0, 0x28(%rsp)
jne 0x17ae1
movq %r14, %rdi
callq 0xe5b0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x17c75
callq 0xe5b0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x17c8c
jmp 0x17c8c
jmp 0x17c8c
jmp 0x17c8c
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x17c9e
callq 0xe5b0
movq %rbx, %rdi
callq 0xe7e0