// File: /workspace/github_c_makefile_star2_O3/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, %r14d
movq %rdx, %r12
movl %esi, %r15d
movl %edi, %r13d
movq 0xf3f1(%rip), %rdi      # 0x22610
testq %rdi, %rdi
je 0x13229
callq 0x2040
movq 0xf3e8(%rip), %rdi      # 0x22618
testq %rdi, %rdi
je 0x1323a
callq 0x2040
movl %r15d, %ebx
negl %ebx
cmovsl %r15d, %ebx
cmpl $0x9, %ebx
je 0x1325b
cmpl $0xa, %ebx
jne 0x135a8
movl %r13d, %edi
callq 0x11b27
jmp 0x13263
movl %r13d, %edi
callq 0x12541
movl %eax, %esi
xorl %eax, %eax
movl %esi, 0xf3bb(%rip)      # 0x22628
movl %r13d, 0xf3b8(%rip)     # 0x2262c
movl %ebx, 0xf3b6(%rip)      # 0x22630
movl %esi, (%r12)
testl %r15d, %r15d
jns 0x13596
negl 0xf3a3(%rip)            # 0x22630
testl %r14d, %r14d
jne 0x132bd
movl $0x0, 0xf398(%rip)      # 0x22634
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0x2, %eax
leal 0x2(%r14), %ecx
movl %eax, %edx
imull %ecx, %edx
imull %ecx, %edx
incl %r14d
addl $0x2, %eax
cmpl %esi, %edx
jl 0x132a7
movl %r14d, 0xf398(%rip)     # 0x2265c
movl %r14d, %eax
imull %eax, %eax
imull %r14d, %eax
movq %rax, 0x40(%rsp)
cvtsi2sd %r14d, %xmm0
movsd 0x75d0(%rip), %xmm1     # 0x1a8b0
divsd %xmm0, %xmm1
movsd %xmm1, 0xf374(%rip)     # 0x22660
movslq %esi, %r14
movl %esi, %ebp
leaq 0x7651(%rip), %rdi      # 0x1a949
movl $0x4, %ecx
movl $0x1d2, %esi            # imm = 0x1D2
movq %r14, %rdx
callq 0x11571
movq %rax, %r15
movq 0xf2fc(%rip), %rbx      # 0x22610
movl %ebp, %eax
movq %rax, 0x28(%rsp)
movl %ebp, 0xc(%rsp)
testl %ebp, %ebp
jle 0x13445
movsd 0xf331(%rip), %xmm0     # 0x22660
movl 0xf327(%rip), %edx      # 0x2265c
leal -0x1(%rdx), %eax
movl %eax, 0x8(%rsp)
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(%rbx), %r13
xorl %r12d, %r12d
movq %rdx, 0x48(%rsp)
movupd -0x10(%r13), %xmm0
addpd 0x74ae(%rip), %xmm0     # 0x1a820
divpd 0x60(%rsp), %xmm0
movapd %xmm0, 0x30(%rsp)
callq 0x21e0
movapd %xmm0, 0x10(%rsp)
movapd 0x30(%rsp), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
callq 0x21e0
movapd 0x10(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
cvttpd2dq %xmm1, %xmm1
movapd %xmm1, %xmm0
pcmpgtd 0x6cce(%rip), %xmm0     # 0x1a080
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 0x133d7
movl 0x8(%rsp), %ecx
movl %ecx, 0x10(%rsp)
jmp 0x133dd
movd %xmm0, 0x10(%rsp)
testb $0x2, %al
jne 0x133e7
movl 0x8(%rsp), %ebp
jmp 0x133f0
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %ebp
movsd (%r13), %xmm0
addsd 0x646a(%rip), %xmm0     # 0x19868
divsd 0x70(%rsp), %xmm0
callq 0x21e0
cvttsd2si %xmm0, %eax
testl %eax, %eax
jg 0x13413
xorl %eax, %eax
movq 0x48(%rsp), %rdx
cmpl %edx, %eax
movq 0x28(%rsp), %rcx
jl 0x13425
movl 0x8(%rsp), %eax
imull %edx, %eax
addl %ebp, %eax
imull %edx, %eax
addl 0x10(%rsp), %eax
movl %eax, (%r15,%r12,4)
incq %r12
addq $0x18, %r13
cmpq %r12, %rcx
jne 0x13364
movq 0x40(%rsp), %r12
leal 0x1(,%r12,2), %eax
movslq %eax, %rdx
leaq 0x74ed(%rip), %rdi      # 0x1a949
movl $0x4, %ecx
movl $0x1df, %esi            # imm = 0x1DF
callq 0x11571
movq %rax, 0xf1a6(%rip)      # 0x22618
movslq %r12d, %rcx
leaq (%rax,%rcx,4), %rcx
movq %rcx, 0x10(%rsp)
movq %rcx, 0xf19b(%rip)      # 0x22620
cmpl $0x0, 0xc(%rsp)
movq 0x28(%rsp), %rcx
jle 0x134a2
xorl %edx, %edx
movslq (%r15,%rdx,4), %rsi
incl (%rax,%rsi,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x13493
movslq 0xf1b3(%rip), %rdx      # 0x2265c
testq %rdx, %rdx
jle 0x1358c
leaq 0x10(%rbx), %rcx
movq %rcx, 0x30(%rsp)
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %r10d, %r10d
movl %edx, %r11d
imull %r10d, %r11d
addl %r9d, %r11d
imull %edx, %r11d
addl %edi, %r11d
movslq %r11d, %r12
movq 0x10(%rsp), %rcx
movl %r8d, (%rcx,%r12,4)
xorl %r13d, %r13d
cmpl 0xc(%rsp), %r8d
jge 0x13566
movslq %r8d, %rbp
leaq (,%rbp,2), %rsi
addq %rbp, %rsi
movq 0x30(%rsp), %rcx
leaq (%rcx,%rsi,8), %rsi
xorl %r13d, %r13d
cmpl (%r15,%rbp,4), %r11d
jne 0x1355a
movsd (%rsi), %xmm0
leal (%r8,%r8,2), %ecx
movslq %ecx, %rcx
movsd (%rbx,%rcx,8), %xmm1
movups -0x10(%rsi), %xmm2
movsd %xmm1, -0x10(%rsi)
movsd 0x8(%rbx,%rcx,8), %xmm1
movsd %xmm1, -0x8(%rsi)
movsd 0x10(%rbx,%rcx,8), %xmm1
movsd %xmm1, (%rsi)
movups %xmm2, (%rbx,%rcx,8)
movsd %xmm0, 0x10(%rbx,%rcx,8)
movslq %r8d, %r8
movl (%r15,%r8,4), %ecx
movl %ecx, (%r15,%rbp,4)
movl %r11d, (%r15,%r8,4)
incl %r8d
incl %r13d
incq %rbp
addq $0x18, %rsi
cmpq %rbp, %r14
jne 0x13507
movl %r13d, (%rax,%r12,4)
incq %r10
cmpq %rdx, %r10
jne 0x134c6
incl %r9d
cmpl %edx, %r9d
jne 0x134c3
incl %edi
cmpl %edx, %edi
jne 0x134c0
movq %r15, %rdi
callq 0x2040
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x739a(%rip), %rax      # 0x1a949
movq %rax, 0xf08a(%rip)      # 0x22640
movl $0x1bc, 0xf078(%rip)    # imm = 0x1BC
shrl $0x1f, %r15d
leal 0x2b(,%r15,2), %esi
leaq 0x7515(%rip), %rdi      # 0x1aae8
movl %ebx, %edx
xorl %eax, %eax
callq 0x258d
movl $0x1, %eax
jmp 0x13596