// File: /workspace/github_c_makefile_star2_O2/markpeerdeman[P]mbemm/src/emath/contactc.c
// Function: makelocalsystem
void makelocalsystem(matrix tramat, point p)
{ double min; 
  double max;
  double tmp;
  int smallest;
  int largest;
  int middle;
  int sum;
  double len;
  /* STEP 1: copy the contents of the normal vector to the first matrix column: */
             M(tramat,1,1)=p->n1;
             M(tramat,2,1)=p->n2;
             M(tramat,3,1)=p->n3;
  /* STEP 2: construct a vector perpendicular to the normal vector: */
  /*      a: find minimum and maximum components: */
             min=fabs(p->n1); max=min; smallest=1; largest=1; 
             tmp=fabs(p->n2); if (tmp<min) { min=tmp; smallest=2; }
                              if (tmp>max) { max=tmp; largest=2; }
             tmp=fabs(p->n3); if (tmp<min) { min=tmp; smallest=3; }
                              if (tmp>max) { max=tmp; largest=3; }
             sum=smallest+largest;
             if (sum==3) middle=3; else
             if (sum==4) middle=2; else
             if (sum==5) middle=1; 
  /*      b: Put the results in the second matrix column and swap something: */
             M(tramat,smallest,2)=  0.0;
             M(tramat,middle,2)  = -M(tramat,largest,1);
             M(tramat,largest,2) =  M(tramat,middle,1);
             len=sqrt(M(tramat,middle,2)*M(tramat,middle,2)+M(tramat,largest,2)*M(tramat,largest,2));
             M(tramat,middle,2)=M(tramat,middle,2)/len;
             M(tramat,largest,2)=M(tramat,largest,2)/len;
             /* ^ moet genormeerd worden! */
  /* STEP 3: The third base vector is calculated as the vectorproduct of the
             two other vectors: */
             M(tramat,1,3) = p->n2*M(tramat,2,3)-p->n3*M(tramat,2,2);
             M(tramat,2,3) = p->n3*M(tramat,2,1)-p->n1*M(tramat,2,3);
             M(tramat,3,3) = p->n1*M(tramat,2,2)-p->n2*M(tramat,2,1);
}

// Assembly:
pushq %rbx
movsd 0x68(%rsi), %xmm0
movq 0x8(%rdi), %rdi
movq (%rdi), %rdx
movsd %xmm0, (%rdx)
movsd 0x70(%rsi), %xmm0
movq 0x8(%rdi), %rax
movsd %xmm0, (%rax)
movq 0x78(%rsi), %xmm0
movq 0x10(%rdi), %rcx
movq %xmm0, (%rcx)
movdqu 0x68(%rsi), %xmm1
movdqa 0x67ca(%rip), %xmm2     # 0xd010
pand %xmm2, %xmm1
pshufd $0xee, %xmm1, %xmm3     # xmm3 = xmm1[2,3,2,3]
xorl %r8d, %r8d
ucomisd %xmm3, %xmm1
seta %r8b
incl %r8d
movapd %xmm3, %xmm4
minsd %xmm1, %xmm4
xorl %r9d, %r9d
ucomisd %xmm1, %xmm3
seta %r9b
incl %r9d
pand %xmm2, %xmm0
ucomisd %xmm0, %xmm4
movl $0x3, %r10d
cmoval %r10d, %r8d
maxsd %xmm1, %xmm3
ucomisd %xmm3, %xmm0
cmoval %r10d, %r9d
leal (%r8,%r9), %r10d
addl $-0x3, %r10d
movabsq $0x2, %r11
subq %r10, %r11
xorl %ebx, %ebx
cmpl $0x3, %r10d
cmovbq %r11, %rbx
movq -0x8(%rdi,%r8,8), %r8
andq $0x0, 0x8(%r8)
movq -0x8(%rdi,%r9,8), %r8
movsd (%r8), %xmm0
xorps 0x6755(%rip), %xmm0     # 0xd020
movq (%rdi,%rbx,8), %rdi
movlps %xmm0, 0x8(%rdi)
movsd (%rdi), %xmm0
movsd %xmm0, 0x8(%r8)
movsd 0x8(%rdi), %xmm1
mulsd %xmm0, %xmm0
movapd %xmm1, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm0, %xmm2
sqrtsd %xmm2, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rdi)
movsd 0x8(%r8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%r8)
movsd 0x8(%rax), %xmm0
mulsd 0x78(%rsi), %xmm0
movsd 0x70(%rsi), %xmm1
mulsd 0x10(%rax), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rdx)
movsd 0x10(%rax), %xmm0
mulsd 0x68(%rsi), %xmm0
movsd (%rax), %xmm1
movsd 0x78(%rsi), %xmm2
mulsd %xmm1, %xmm2
subsd %xmm0, %xmm2
movsd %xmm2, 0x10(%rax)
movsd 0x68(%rsi), %xmm0
mulsd 0x70(%rsi), %xmm1
mulsd 0x8(%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rcx)
popq %rbx
retq