// File: /workspace/github_cpp_makefile_star2_O1/Luthaf[P]zeopp/rmsd.cc
// Function: jacobi3(double (*) [3], double*, double (*) [3], int*)
int jacobi3(double a[3][3], double d[3], double v[3][3], int* n_rot)
{
  int count, k, i, j;
  double tresh, theta, tau, t, sum, s, h, g, c, b[3], z[3];

  /*Initialize v to the identity matrix.*/
  for (i=0; i<3; i++) 
  { 
    for (j=0; j<3; j++) 
      v[i][j] = 0.0;
    v[i][i] = 1.0;
  }

  /* Initialize b and d to the diagonal of a */
  for (i=0; i<3; i++) 
    b[i] = d[i] = a[i][i];

  /* z will accumulate terms */
  for (i=0; i<3; i++) 
    z[i] = 0.0; 
  
  *n_rot = 0;

  /* 50 tries */
  for (count=0; count<50; count++)     
  {

    /* sum off-diagonal elements */
    sum = 0.0;
    for (i=0; i<2; i++) 
    {
      for (j=i+1; j<3; j++)
         sum += fabs(a[i][j]);
    }

    /* if converged to machine underflow */
    if (sum == 0.0) 
      return(1);

    /* on 1st three sweeps... */
    if (count < 3) 
      tresh = sum * 0.2 / 9.0;    
    else       
      tresh = 0.0;      

    for (i=0; i<2; i++) 
    {
      for (j=i+1; j<3; j++) 
      {
        g = 100.0 * fabs(a[i][j]);

        /*  after four sweeps, skip the rotation if
         *   the off-diagonal element is small 
         */
        if ( count > 3  &&  fabs(d[i])+g == fabs(d[i])
              &&  fabs(d[j])+g == fabs(d[j]) ) 
        {
          a[i][j] = 0.0;
        } 
        else if (fabs(a[i][j]) > tresh) 
        {
          h = d[j] - d[i];
          
          if (fabs(h)+g == fabs(h))
          {
            t = a[i][j] / h;
          }
          else 
          {
            theta = 0.5 * h / (a[i][j]);
            t = 1.0 / ( fabs(theta) +
                        (double)sqrt(1.0 + theta*theta) );
            if (theta < 0.0) 
              t = -t;
          }
          
          c = 1.0 / (double) sqrt(1 + t*t);
          s = t * c;
          tau = s / (1.0 + c);
          h = t * a[i][j];

          z[i] -= h;
          z[j] += h;
          d[i] -= h;
          d[j] += h;

          a[i][j] = 0.0;

          for (k=0; k<=i-1; k++) 
            ROTATE(a, k, i, k, j)

          for (k=i+1; k<=j-1; k++) 
            ROTATE(a, i, k, k, j)

          for (k=j+1; k<3; k++) 
            ROTATE(a, i, k, j, k)

          for (k=0; k<3; k++) 
            ROTATE(v, k, i, k, j)

          ++(*n_rot);
        }
      }
    }

    for (i=0; i<3; i++) 
    {
      b[i] += z[i];
      d[i] = b[i];
      z[i] = 0.0;
    }
  }

  printf("Too many iterations in jacobi3\n");
  return (0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rcx, 0x28(%rsp)
movq %rdx, %r13
movq %rdi, %r12
xorl %eax, %eax
xorpd %xmm0, %xmm0
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rdx, %rdi
movupd %xmm0, (%r13,%rax)
movq $0x0, 0x10(%r13,%rax)
movq %rcx, (%rdi)
addq $0x20, %rdi
addq $0x18, %rax
cmpq $0x48, %rax
jne 0xa3526
xorl %eax, %eax
movsd (%r12,%rax,4), %xmm0
movsd %xmm0, (%rsi,%rax)
movsd %xmm0, 0x80(%rsp,%rax)
addq $0x8, %rax
cmpq $0x18, %rax
jne 0xa3549
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x28(%rsp), %rax
movl $0x0, (%rax)
leaq 0x8(%r13), %rax
movq %rax, 0x60(%rsp)
xorl %r10d, %r10d
movapd 0xabaf7(%rip), %xmm6    # 0x14f090
xorpd %xmm7, %xmm7
movsd 0xaba9a(%rip), %xmm8    # 0x14f040
movsd 0xabaa9(%rip), %xmm9    # 0x14f058
movapd 0xafff8(%rip), %xmm10   # 0x1535b0
movq %rsi, 0x8(%rsp)
movq %r13, 0x68(%rsp)
movq %r12, %rax
xorl %ecx, %ecx
xorpd %xmm11, %xmm11
movq %rcx, %r8
incq %rcx
movl $0x1, %r9d
movsd (%rax,%r9,8), %xmm0
andpd %xmm6, %xmm0
addsd %xmm0, %xmm11
leaq (%r8,%r9), %rdi
incq %rdi
incq %r9
cmpq $0x3, %rdi
jne 0xa35d8
addq $0x20, %rax
testq %r8, %r8
je 0xa35cc
ucomisd %xmm7, %xmm11
jne 0xa360d
jnp 0xa3a27
mulsd 0xb50ba(%rip), %xmm11   # 0x1586d0
divsd 0xab9f1(%rip), %xmm11   # 0x14f010
cmpl $0x3, %r10d
jb 0xa362a
xorpd %xmm11, %xmm11
movl $0x1, %r15d
movq 0x60(%rsp), %rbp
movq $0x0, (%rsp)
movq %r10, 0x18(%rsp)
movsd %xmm11, 0x10(%rsp)
movq (%rsp), %rcx
leaq 0x1(%rcx), %rax
movq %rax, (%rsp)
leaq (%rcx,%rcx,2), %rax
leaq (%r12,%rax,8), %rbx
movq %rbp, 0x70(%rsp)
movq %r15, 0x78(%rsp)
movq %rcx, 0x20(%rsp)
leaq (%rbx,%r15,8), %r14
movsd (%r14), %xmm4
movapd %xmm4, %xmm1
andpd %xmm6, %xmm1
movapd %xmm1, %xmm0
mulsd %xmm8, %xmm0
cmpl $0x4, %r10d
jb 0xa36cb
movsd (%rsi,%rcx,8), %xmm2
andpd %xmm6, %xmm2
movapd %xmm0, %xmm3
addsd %xmm2, %xmm3
ucomisd %xmm2, %xmm3
jne 0xa36cb
jp 0xa36cb
movsd (%rsi,%r15,8), %xmm2
andpd %xmm6, %xmm2
movapd %xmm0, %xmm3
addsd %xmm2, %xmm3
ucomisd %xmm2, %xmm3
jne 0xa36cb
jp 0xa36cb
movq $0x0, (%r14)
jmp 0xa39a5
ucomisd %xmm11, %xmm1
jbe 0xa39a5
movsd (%rsi,%r15,8), %xmm2
subsd (%rsi,%rcx,8), %xmm2
movapd %xmm2, %xmm1
andpd %xmm6, %xmm1
addsd %xmm1, %xmm0
ucomisd %xmm1, %xmm0
jne 0xa36fe
jp 0xa36fe
divsd %xmm2, %xmm4
jmp 0xa378d
mulsd 0xab932(%rip), %xmm2    # 0x14f038
divsd %xmm4, %xmm2
movapd %xmm2, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm9, %xmm0
ucomisd %xmm7, %xmm0
jb 0xa3723
sqrtsd %xmm0, %xmm0
jmp 0xa3771
movapd %xmm2, 0x50(%rsp)
callq 0x4350
movapd 0x50(%rsp), %xmm2
movq 0x20(%rsp), %rcx
movsd 0x10(%rsp), %xmm11
movapd 0xafe67(%rip), %xmm10   # 0x1535b0
movsd 0xab906(%rip), %xmm9    # 0x14f058
movsd 0xab8e5(%rip), %xmm8    # 0x14f040
xorpd %xmm7, %xmm7
movapd 0xab929(%rip), %xmm6    # 0x14f090
movq 0x18(%rsp), %r10
movq 0x8(%rsp), %rsi
ucomisd %xmm2, %xmm7
andpd %xmm6, %xmm2
addsd %xmm0, %xmm2
movapd %xmm9, %xmm4
divsd %xmm2, %xmm4
jbe 0xa378d
xorpd %xmm10, %xmm4
movapd %xmm4, %xmm0
mulsd %xmm4, %xmm0
addsd %xmm9, %xmm0
ucomisd %xmm7, %xmm0
jb 0xa37a6
sqrtsd %xmm0, %xmm0
jmp 0xa37f4
movapd %xmm4, 0x50(%rsp)
callq 0x4350
movapd 0x50(%rsp), %xmm4
movq 0x20(%rsp), %rcx
movsd 0x10(%rsp), %xmm11
movapd 0xafde4(%rip), %xmm10   # 0x1535b0
movsd 0xab883(%rip), %xmm9    # 0x14f058
movsd 0xab862(%rip), %xmm8    # 0x14f040
xorpd %xmm7, %xmm7
movapd 0xab8a6(%rip), %xmm6    # 0x14f090
movq 0x18(%rsp), %r10
movq 0x8(%rsp), %rsi
movapd %xmm9, %xmm2
divsd %xmm0, %xmm2
movapd %xmm4, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm9, %xmm2
mulsd (%r14), %xmm4
movsd 0x30(%rsp,%rcx,8), %xmm1
subsd %xmm4, %xmm1
movsd %xmm1, 0x30(%rsp,%rcx,8)
movsd 0x30(%rsp,%r15,8), %xmm1
addsd %xmm4, %xmm1
movsd %xmm1, 0x30(%rsp,%r15,8)
movsd (%rsi,%rcx,8), %xmm1
subsd %xmm4, %xmm1
movsd %xmm1, (%rsi,%rcx,8)
movapd %xmm0, %xmm1
addsd (%rsi,%r15,8), %xmm4
divsd %xmm2, %xmm1
movsd %xmm4, (%rsi,%r15,8)
movq $0x0, (%r14)
cmpq $0x1, %rcx
jne 0xa38a4
movsd (%r12,%rcx,8), %xmm2
movsd (%r12,%r15,8), %xmm3
movapd %xmm2, %xmm4
mulsd %xmm1, %xmm4
addsd %xmm3, %xmm4
mulsd %xmm0, %xmm4
movapd %xmm2, %xmm5
subsd %xmm4, %xmm5
movsd %xmm5, (%r12,%rcx,8)
movapd %xmm3, %xmm4
mulsd %xmm1, %xmm4
subsd %xmm4, %xmm2
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
movsd %xmm2, (%r12,%r15,8)
cmpq %r15, (%rsp)
jae 0xa38f9
movapd %xmm0, %xmm2
xorpd %xmm10, %xmm2
movsd 0x8(%rbx), %xmm3
movsd 0x18(%r12,%r15,8), %xmm4
movapd %xmm3, %xmm5
mulsd %xmm1, %xmm5
addsd %xmm4, %xmm5
mulsd %xmm2, %xmm5
addsd %xmm3, %xmm5
movsd %xmm5, 0x8(%rbx)
movapd %xmm4, %xmm5
mulsd %xmm1, %xmm5
subsd %xmm5, %xmm3
mulsd %xmm0, %xmm3
addsd %xmm4, %xmm3
movsd %xmm3, 0x18(%r12,%r15,8)
cmpq $0x2, %r15
ja 0xa38b3
cmpq $0x2, %r15
je 0xa3947
movsd 0x10(%rbx), %xmm2
leaq (%r15,%r15,2), %rax
movsd 0x10(%r12,%rax,8), %xmm3
movapd %xmm2, %xmm4
mulsd %xmm1, %xmm4
addsd %xmm3, %xmm4
mulsd %xmm0, %xmm4
movapd %xmm2, %xmm5
subsd %xmm4, %xmm5
movsd %xmm5, 0x10(%rbx)
movapd %xmm3, %xmm4
mulsd %xmm1, %xmm4
subsd %xmm4, %xmm2
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
movsd %xmm2, 0x10(%r12,%rax,8)
movapd %xmm0, %xmm2
xorpd %xmm10, %xmm2
xorl %eax, %eax
movsd (%r13,%rax), %xmm3
movsd (%rbp,%rax), %xmm4
movapd %xmm3, %xmm5
mulsd %xmm1, %xmm5
addsd %xmm4, %xmm5
mulsd %xmm2, %xmm5
addsd %xmm3, %xmm5
movsd %xmm5, (%r13,%rax)
movapd %xmm4, %xmm5
mulsd %xmm1, %xmm5
subsd %xmm5, %xmm3
mulsd %xmm0, %xmm3
addsd %xmm4, %xmm3
movsd %xmm3, (%rbp,%rax)
addq $0x18, %rax
cmpq $0x48, %rax
jne 0xa3952
movq 0x28(%rsp), %rax
incl (%rax)
incq %r15
addq $0x8, %rbp
cmpq $0x3, %r15
jne 0xa366c
movq 0x78(%rsp), %r15
incq %r15
addq $0x8, %r13
movq 0x70(%rsp), %rbp
addq $0x8, %rbp
testq %rcx, %rcx
je 0xa3649
xorl %eax, %eax
movsd 0x30(%rsp,%rax,8), %xmm0
addsd 0x80(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x80(%rsp,%rax,8)
movsd %xmm0, (%rsi,%rax,8)
movq $0x0, 0x30(%rsp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0xa39d6
incl %r10d
cmpl $0x32, %r10d
movq 0x68(%rsp), %r13
jne 0xa35c2
leaq 0xb4cf2(%rip), %rdi     # 0x158710
callq 0x4660
xorl %eax, %eax
jmp 0xa3a2c
movl $0x1, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq