// File: /workspace/github_cpp_makefile_star2_O3/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, (%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), %r15
leaq (,%r15,8), %rax
testq %r15, %r15
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r14
testl %r15d, %r15d
jle 0x17b91
xorl %r13d, %r13d
leaq 0x20(%rsp), %r15
movq %r14, %r12
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rsi
movq %rbx, %rdi
callq 0x17522
movsd %xmm0, (%r12)
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x17a09
movq %r15, %rdi
movq %r12, %rsi
callq 0x23edc
incq %r13
movslq 0x24(%rbx), %rax
addq $0x8, %r12
cmpq %rax, %r13
jl 0x179dd
jmp 0x17b86
leaq 0x20(%rsp), %rdi
callq 0x23ff0
movsd (%rsp), %xmm0
ucomisd (%rax), %xmm0
ja 0x17b91
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 0x17522
leaq (%r14,%rbp,8), %r13
ucomisd (%r14,%rbp,8), %xmm0
jne 0x17a62
jnp 0x17a85
movsd %xmm0, (%r13)
ucomisd (%rsp), %xmm0
jb 0x17b86
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x23edc
jmp 0x17b86
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
movapd %xmm0, 0x10(%rsp)
callq *0xd0(%rax)
movapd 0x10(%rsp), %xmm0
testl %eax, %eax
je 0x17b86
xorpd 0x8a570(%rip), %xmm0    # 0xa2020
movlpd %xmm0, (%r13)
movb $0x1, %al
xorl %r13d, %r13d
movl %eax, 0xc(%rsp)
movb $0x1, %bpl
movq 0x18(%r12,%r13,8), %rax
testq %rax, %rax
je 0x17b61
movq 0x20(%rax), %rsi
cmpq %r12, %rsi
je 0x17aec
movq 0x28(%rax), %rcx
cmpq %r12, %rcx
je 0x17af8
testb $0x1, %bpl
cmoveq %rcx, %rsi
jmp 0x17b02
testb $0x1, %bpl
je 0x17afe
movq 0x28(%rax), %rsi
jmp 0x17b02
testb $0x1, %bpl
je 0x17b02
movq 0x30(%rax), %rsi
movslq 0x30(%rsi), %r15
movsd (%r14,%r15,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %rbx, %rdi
callq 0x17522
leaq (%r14,%r15,8), %rsi
movsd %xmm0, (%rsi)
movsd (%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
ucomisd %xmm1, %xmm2
jae 0x17b45
ucomisd %xmm1, %xmm0
jb 0x17b61
leaq 0x20(%rsp), %rdi
callq 0x23edc
jmp 0x17b61
ucomisd %xmm1, %xmm0
jae 0x17b57
leaq 0x20(%rsp), %rdi
callq 0x24112
jmp 0x17b61
leaq 0x20(%rsp), %rdi
callq 0x24096
testb $0x1, %bpl
movl $0x0, %ebp
jne 0x17ac2
movl $0x1, %r13d
testb $0x1, 0xc(%rsp)
movl $0x0, %eax
jne 0x17abb
cmpl $0x0, 0x28(%rsp)
jne 0x17a1e
movq %r14, %rdi
callq 0xe5b0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x17ba8
callq 0xe5b0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x17bbf
jmp 0x17bbf
jmp 0x17bbf
jmp 0x17bbf
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x17bd1
callq 0xe5b0
movq %rbx, %rdi
callq 0xe7e0
nop