// File: /workspace/github_c_makefile_star2_O2/josch[P]stride/nsc.c
// Function: make_unsp
int make_unsp(int densit, int mode, int * num_dot, int cubus) {
  int ndot, ico_cube_cb, i, j, k, l, ijk, tn, tl, tl2;
  point_double xus;
  point_int    work;
  double x, y, z;

  if (xpunsp) free(xpunsp); if (ico_wk) free(ico_wk);

  k=1; if (mode < 0) { k=0; mode = -mode; }
  if (mode == UNSP_ICO_ARC)      { ndot = ico_dot_arc(densit); }
  else if (mode == UNSP_ICO_DOD)      { ndot = ico_dot_dod(densit); }
  else {
    WARNING("make_unsp: mode %c%d not allowed", (k) ? '+':'-',mode);
    return 1;
    }

  last_n_dot = ndot; last_densit = densit; last_unsp = mode;
  *num_dot=ndot; if (k) return 0;

/* in the following the dots of the unit sphere may be resorted */
  last_unsp = -last_unsp;

/* determine distribution of points in elementary cubes */
  if (cubus) {
    ico_cube = cubus;
    }
  else {
    last_cubus = 0;
    i=1;
    while (i*i*i*2 < ndot) i++;
    ico_cube = MAXIMUM(i-1, 0);
    }
  ico_cube_cb = ico_cube*ico_cube*ico_cube;
  del_cube=2./((double)ico_cube);
  work = (int *) CALLOC(ndot, sizeof(int));
  xus = xpunsp;
  for (l=0; l<ndot; l++) {
    i = MAXIMUM((int) floor((1.+xus[3*l])/del_cube), 0);
    if (i>=ico_cube) i = ico_cube-1;
    j = MAXIMUM((int) floor((1.+xus[1+3*l])/del_cube), 0);
    if (j>=ico_cube) j = ico_cube-1;
    k = MAXIMUM((int) floor((1.+xus[2+3*l])/del_cube), 0);
    if (k>=ico_cube) k = ico_cube-1;
    ijk = i+j*ico_cube+k*ico_cube*ico_cube;
    work[l] = ijk;
    }

  ico_wk = (int *) CALLOC(2*ico_cube_cb+1, sizeof(int));
  ico_pt = ico_wk+ico_cube_cb;
  for (l=0; l<ndot; l++) {
    ico_wk[work[l]]++;   /* dots per elementary cube */
    }

/* reordering of the coordinate array in accordance with box number */
  tn=0;
  for (i=0; i<ico_cube; i++) {
    for (j=0; j<ico_cube; j++) {
      for (k=0; k<ico_cube; k++) {
        tl=0;
        tl2 = tn;
        ijk = i+ico_cube*j+ico_cube*ico_cube*k;
        *(ico_pt+ijk) = tn;
        for (l=tl2; l<ndot; l++) {
          if (ijk == work[l]) {
            x = xus[3*l]; y = xus[1+3*l]; z = xus[2+3*l];
            xus[3*l] = xus[3*tn];
            xus[1+3*l] = xus[1+3*tn]; xus[2+3*l] = xus[2+3*tn];
            xus[3*tn] = x; xus[1+3*tn] = y; xus[2+3*tn] = z;
            ijk = work[l]; work[l]=work[tn]; work[tn]=ijk;
            tn++; tl++;
            }
          }
        *(ico_wk+ijk) = tl;
        }		/* cycle k */
      }			/* cycle j */
    }			/* cycle i */
  free(work); return 0;
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %ecx, %r15d
movq %rdx, %r14
movl %esi, %ebx
movl %edi, %r13d
movq 0xe5af(%rip), %rdi      # 0x21620
callq 0x2040
movq 0xe5ab(%rip), %rdi      # 0x21628
callq 0x2040
movl %ebx, %ebp
negl %ebp
cmovsl %ebx, %ebp
cmpl $0x9, %ebp
je 0x130a1
cmpl $0xa, %ebp
jne 0x1328a
movl %r13d, %edi
callq 0x119ab
jmp 0x130a9
movl %r13d, %edi
callq 0x123b6
movl %eax, %esi
xorl %eax, %eax
movl %esi, 0xe585(%rip)      # 0x21638
movl %r13d, 0xe582(%rip)     # 0x2163c
movl %ebp, 0xe580(%rip)      # 0x21640
movl %esi, (%r14)
testl %ebx, %ebx
jns 0x1341b
negl 0xe56f(%rip)            # 0x21640
testl %r15d, %r15d
jne 0x130fa
andl $0x0, 0xe567(%rip)      # 0x21644
pushq $-0x1
popq %r15
pushq $0x2
popq %rax
leal 0x2(%r15), %ecx
movl %eax, %edx
imull %ecx, %edx
imull %ecx, %edx
incl %r15d
addl $0x2, %eax
cmpl %esi, %edx
jl 0x130e4
movl %r15d, 0xe56b(%rip)     # 0x2166c
movl %r15d, %eax
imull %eax, %eax
movq %rax, 0x38(%rsp)
movq %r15, 0x40(%rsp)
cvtsi2sd %r15d, %xmm0
movsd 0x6742(%rip), %xmm1     # 0x19860
divsd %xmm0, %xmm1
movsd %xmm1, 0xe546(%rip)     # 0x21670
movslq %esi, %rbx
leaq 0x67c5(%rip), %rdi      # 0x198f9
pushq $0x4
popq %rcx
movq %rsi, %r14
movl $0x1d2, %esi            # imm = 0x1D2
movq %rbx, %rdx
callq 0x1146e
movq %r14, %rcx
movq %rax, %r14
movq 0xe4cc(%rip), %r13      # 0x21620
movsd 0xe514(%rip), %xmm0     # 0x21670
movl 0xe50a(%rip), %edx      # 0x2166c
leal -0x1(%rdx), %eax
movl %eax, 0x4(%rsp)
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
movaps %xmm0, 0x70(%rsp)
movlhps %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movaps %xmm0, 0x60(%rsp)
movd %edx, %xmm0
pshufd $0x50, %xmm0, %xmm0     # xmm0 = xmm0[0,0,1,1]
movdqa %xmm0, 0x50(%rsp)
leaq 0x10(%r13), %r12
movq %rdx, 0x48(%rsp)
movq %rcx, %r15
cmpq %rax, %rcx
je 0x132c4
movq %rax, 0x8(%rsp)
movupd -0x10(%r12), %xmm0
addpd 0x663b(%rip), %xmm0     # 0x197f0
divpd 0x60(%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
callq 0x21f0
movapd %xmm0, 0x10(%rsp)
movapd 0x20(%rsp), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
callq 0x21f0
movapd 0x10(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
cvttpd2dq %xmm1, %xmm1
movapd %xmm1, %xmm0
pcmpgtd 0x5e5b(%rip), %xmm0     # 0x19050
pand %xmm1, %xmm0
movdqa 0x50(%rsp), %xmm1
pcmpgtd %xmm0, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
movmskpd %xmm1, %eax
testb $0x1, %al
jne 0x1321a
movl 0x4(%rsp), %ecx
movl %ecx, 0x10(%rsp)
jmp 0x13220
movd %xmm0, 0x10(%rsp)
testb $0x2, %al
jne 0x1322a
movl 0x4(%rsp), %ebp
jmp 0x13233
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %ebp
movsd (%r12), %xmm0
addsd 0x5627(%rip), %xmm0     # 0x18868
divsd 0x70(%rsp), %xmm0
callq 0x21f0
cvttsd2si %xmm0, %eax
testl %eax, %eax
jg 0x13256
xorl %eax, %eax
movq 0x48(%rsp), %rdx
cmpl %edx, %eax
movq %r15, %rcx
jl 0x13266
movl 0x4(%rsp), %eax
imull %edx, %eax
addl %ebp, %eax
imull %edx, %eax
addl 0x10(%rsp), %eax
movq 0x8(%rsp), %rsi
movl %eax, (%r14,%rsi,4)
movq %rsi, %rax
incq %rax
addq $0x18, %r12
jmp 0x13195
leaq 0x6668(%rip), %rax      # 0x198f9
movq %rax, 0xe3b8(%rip)      # 0x21650
movl $0x1bc, 0xe3a6(%rip)    # imm = 0x1BC
shrl $0x1f, %ebx
leal 0x2b(,%rbx,2), %esi
leaq 0x67e5(%rip), %rdi      # 0x19a98
movl %ebp, %edx
xorl %eax, %eax
callq 0x11315
pushq $0x1
popq %rax
jmp 0x1341b
movq 0x38(%rsp), %r12
imull 0x40(%rsp), %r12d
leal 0x1(,%r12,2), %eax
movslq %eax, %rdx
leaq 0x6618(%rip), %rdi      # 0x198f9
pushq $0x4
popq %rcx
movl $0x1df, %esi            # imm = 0x1DF
callq 0x1146e
movq %rax, 0xe333(%rip)      # 0x21628
movslq %r12d, %rcx
movq %rax, 0x8(%rsp)
leaq (%rax,%rcx,4), %rax
movq %rax, 0x10(%rsp)
movq %rax, 0xe323(%rip)      # 0x21630
xorl %ecx, %ecx
movq 0x8(%rsp), %rax
cmpq %rcx, %r15
je 0x13325
movslq (%r14,%rcx,4), %rdx
incl (%rax,%rdx,4)
incq %rcx
jmp 0x13314
movl 0xe341(%rip), %edx      # 0x2166c
xorl %esi, %esi
testl %edx, %edx
movl $0x0, %eax
cmovgl %edx, %eax
movl %eax, 0x20(%rsp)
leaq 0x10(%r13), %r8
xorl %r9d, %r9d
cmpl 0x20(%rsp), %r9d
je 0x13411
xorl %r10d, %r10d
cmpl %edx, %r10d
je 0x13409
xorl %r11d, %r11d
cmpq %rdx, %r11
je 0x13401
movl %edx, %ebp
imull %r11d, %ebp
addl %r10d, %ebp
imull %edx, %ebp
addl %r9d, %ebp
movslq %ebp, %r15
movq 0x10(%rsp), %rax
movl %esi, (%rax,%r15,4)
movslq %esi, %r12
imulq $0x18, %r12, %rdi
addq %r8, %rdi
xorl %ecx, %ecx
cmpq %rbx, %r12
jge 0x133f0
cmpl (%r14,%r12,4), %ebp
jne 0x133e7
movq (%rdi), %xmm0
leal (%rsi,%rsi,2), %eax
cltq
movsd (%r13,%rax,8), %xmm1
movups -0x10(%rdi), %xmm2
movsd %xmm1, -0x10(%rdi)
movsd 0x8(%r13,%rax,8), %xmm1
movsd %xmm1, -0x8(%rdi)
movsd 0x10(%r13,%rax,8), %xmm1
movsd %xmm1, (%rdi)
movups %xmm2, (%r13,%rax,8)
movq %xmm0, 0x10(%r13,%rax,8)
movslq %esi, %rsi
movl (%r14,%rsi,4), %eax
movl %eax, (%r14,%r12,4)
movl %ebp, (%r14,%rsi,4)
incl %esi
incl %ecx
incq %r12
addq $0x18, %rdi
jmp 0x1338c
movq 0x8(%rsp), %rax
movl %ecx, (%rax,%r15,4)
incq %r11
jmp 0x1335c
incl %r10d
jmp 0x13350
incl %r9d
jmp 0x13342
movq %r14, %rdi
callq 0x2040
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq