// File: tomfunkhouser[P]gaps/apps/msh2prp/msh2prp.cpp
// Function: diagonalize_curv(gaps::R3Vector const&, gaps::R3Vector const&, double, double, double, gaps::R3Vector const&, gaps::R3Vector&, gaps::R3Vector&, double&, double&)
static void diagonalize_curv(const R3Vector &old_u, const R3Vector &old_v,
                             RNScalar ku, RNScalar kuv, RNScalar kv,
                             const R3Vector &new_norm,
                             R3Vector &pdir1, R3Vector &pdir2, RNScalar &k1, RNScalar &k2)
{
  R3Vector r_old_u, r_old_v;
  rot_coord_sys(old_u, old_v, new_norm, r_old_u, r_old_v);

  RNScalar c = 1.0, s = 0.0, tt = 0.0;
  if (kuv != 0.0) {
    // Jacobi rotation to diagonalize
    RNScalar h = 0.5 * (kv - ku) / kuv;
    tt = (h < 0.0) ?
      1.0 / (h - sqrt(1.0 + h*h)) :
      1.0 / (h + sqrt(1.0 + h*h));
    c = 1.0 / sqrt(1.0 + tt*tt);
    s = tt * c;
  }

  k1 = ku - tt * kuv;
  k2 = kv + tt * kuv;

  if (fabs(k1) >= fabs(k2)) {
    pdir1 = c*r_old_u - s*r_old_v;
  } 
  else {
    RNScalar swap = k1;
    k1 = k2;
    k2 = swap;
    pdir1 = s*r_old_u + c*r_old_v;
  }
  pdir2 = new_norm % pdir1;
}

// Assembly:
subq $0x168, %rsp            # imm = 0x168
movq 0x170(%rsp), %rax
movq %rdi, 0x160(%rsp)
movq %rsi, 0x158(%rsp)
movsd %xmm0, 0x150(%rsp)
movsd %xmm1, 0x148(%rsp)
movsd %xmm2, 0x140(%rsp)
movq %rdx, 0x138(%rsp)
movq %rcx, 0x130(%rsp)
movq %r8, 0x128(%rsp)
movq %r9, 0x120(%rsp)
leaq 0x108(%rsp), %rdi
callq 0x89430
leaq 0xf0(%rsp), %rdi
callq 0x89430
movq 0x160(%rsp), %rdi
movq 0x158(%rsp), %rsi
movq 0x138(%rsp), %rdx
leaq 0x108(%rsp), %rcx
leaq 0xf0(%rsp), %r8
callq 0x1c840
movsd 0x157b4f(%rip), %xmm0   # 0x174018
movsd %xmm0, 0xe8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xe0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xd8(%rsp)
movsd 0x148(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1c503
jp 0x1c503
jmp 0x1c650
movsd 0x140(%rsp), %xmm1
subsd 0x150(%rsp), %xmm1
movsd 0x157b03(%rip), %xmm0   # 0x174020
mulsd %xmm1, %xmm0
divsd 0x148(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
xorps %xmm0, %xmm0
ucomisd 0xd0(%rsp), %xmm0
jbe 0x1c595
movsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0xd0(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x157aaa(%rip), %xmm1   # 0x174018
addsd %xmm1, %xmm0
callq 0x114f0
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0x157a8f(%rip), %xmm0   # 0x174018
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0x1c5e7
movsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0xd0(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x157a56(%rip), %xmm1   # 0x174018
addsd %xmm1, %xmm0
callq 0x114f0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x157a3b(%rip), %xmm0   # 0x174018
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
movsd 0xd8(%rsp), %xmm0
movsd 0xd8(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x157a04(%rip), %xmm1   # 0x174018
addsd %xmm1, %xmm0
callq 0x114f0
movaps %xmm0, %xmm1
movsd 0x1579f0(%rip), %xmm0   # 0x174018
divsd %xmm1, %xmm0
movsd %xmm0, 0xe8(%rsp)
movsd 0xd8(%rsp), %xmm0
mulsd 0xe8(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
movsd 0x150(%rsp), %xmm1
movsd 0xd8(%rsp), %xmm0
movsd 0x148(%rsp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x120(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x140(%rsp), %xmm1
movsd 0xd8(%rsp), %xmm0
movsd 0x148(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x170(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x120(%rsp), %rax
movsd (%rax), %xmm0
movaps 0x1579d8(%rip), %xmm1   # 0x1740b0
pand %xmm1, %xmm0
movq 0x170(%rsp), %rax
movsd (%rax), %xmm1
movaps 0x1579c1(%rip), %xmm2   # 0x1740b0
pand %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1c76c
movsd 0xe8(%rsp), %xmm0
leaq 0xa0(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x1f810
movsd 0xe0(%rsp), %xmm0
leaq 0x88(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x1f810
leaq 0xb8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x88(%rsp), %rdx
callq 0x8a830
movq 0x130(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x8a100
jmp 0x1c80a
movq 0x120(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0x170(%rsp), %rax
movsd (%rax), %xmm0
movq 0x120(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x80(%rsp), %xmm0
movq 0x170(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0xe0(%rsp), %xmm0
leaq 0x50(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x1f810
movsd 0xe8(%rsp), %xmm0
leaq 0x38(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x1f810
leaq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x8a7c0
movq 0x130(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x8a100
movq 0x138(%rsp), %rsi
movq 0x130(%rsp), %rdx
leaq 0x20(%rsp), %rdi
callq 0x8a280
movq 0x128(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x8a100
addq $0x168, %rsp            # imm = 0x168
retq
nop