// File: /workspace/github_c_makefile_star2_O1/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 0xf30f(%rip), %rdi      # 0x22610
testq %rdi, %rdi
je 0x1330b
callq 0x2040
movq 0xf306(%rip), %rdi      # 0x22618
testq %rdi, %rdi
je 0x1331c
callq 0x2040
movl %r15d, %ebx
negl %ebx
cmovsl %r15d, %ebx
cmpl $0x9, %ebx
je 0x1333d
cmpl $0xa, %ebx
jne 0x136ae
movl %r13d, %edi
callq 0x119cd
jmp 0x13345
movl %r13d, %edi
callq 0x12461
movl %eax, %esi
xorl %eax, %eax
movl %esi, 0xf2d9(%rip)      # 0x22628
movl %r13d, 0xf2d6(%rip)     # 0x2262c
movl %ebx, 0xf2d4(%rip)      # 0x22630
movl %esi, (%r12)
testl %r15d, %r15d
jns 0x1369c
negl 0xf2c1(%rip)            # 0x22630
testl %r14d, %r14d
jne 0x1339f
movl $0x0, 0xf2b6(%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 0x13389
movl %r14d, 0xf2b6(%rip)     # 0x2265c
movl %r14d, %eax
imull %eax, %eax
imull %r14d, %eax
movq %rax, 0x40(%rsp)
cvtsi2sd %r14d, %xmm0
movsd 0x7506(%rip), %xmm1     # 0x1a8c8
divsd %xmm0, %xmm1
movsd %xmm1, 0xf292(%rip)     # 0x22660
movslq %esi, %r14
leaq 0x7581(%rip), %rdi      # 0x1a959
movl $0x4, %ecx
movl %esi, %ebp
movl $0x1d2, %esi            # imm = 0x1D2
movq %r14, %rdx
callq 0x1139d
movq %rax, %r15
movq 0xf21a(%rip), %rbx      # 0x22610
movl %ebp, %eax
movq %rax, 0x28(%rsp)
movl %ebp, 0xc(%rsp)
testl %ebp, %ebp
jle 0x13527
movsd 0xf24f(%rip), %xmm0     # 0x22660
movl 0xf245(%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 0x73dc(%rip), %xmm0     # 0x1a830
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 0x6bec(%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 0x134b9
movl 0x8(%rsp), %ecx
movl %ecx, 0x10(%rsp)
jmp 0x134bf
movd %xmm0, 0x10(%rsp)
testb $0x2, %al
jne 0x134c9
movl 0x8(%rsp), %ebp
jmp 0x134d2
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %ebp
movsd (%r13), %xmm0
addsd 0x6388(%rip), %xmm0     # 0x19868
divsd 0x70(%rsp), %xmm0
callq 0x21e0
cvttsd2si %xmm0, %eax
testl %eax, %eax
jg 0x134f5
xorl %eax, %eax
movq 0x48(%rsp), %rdx
cmpl %edx, %eax
movq 0x28(%rsp), %rcx
jl 0x13507
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 0x13446
movq 0x40(%rsp), %r12
leal 0x1(,%r12,2), %eax
movslq %eax, %rdx
leaq 0x741b(%rip), %rdi      # 0x1a959
movl $0x4, %ecx
movl $0x1df, %esi            # imm = 0x1DF
callq 0x1139d
movq %rax, 0xf0c4(%rip)      # 0x22618
movslq %r12d, %rcx
leaq (%rax,%rcx,4), %rcx
movq %rcx, 0xf0be(%rip)      # 0x22620
cmpl $0x0, 0xc(%rsp)
movq 0x28(%rsp), %rdi
jle 0x1357f
xorl %edx, %edx
movslq (%r15,%rdx,4), %rsi
incl (%rax,%rsi,4)
incq %rdx
cmpq %rdx, %rdi
jne 0x13570
movl 0xf0d7(%rip), %edx      # 0x2265c
movl %edx, 0x30(%rsp)
testl %edx, %edx
jle 0x13692
movl 0xf0c5(%rip), %esi      # 0x2265c
leaq 0x10(%rbx), %rdx
movq %rdx, 0x10(%rsp)
xorl %r8d, %r8d
xorl %r9d, %r9d
testl %esi, %esi
jle 0x13684
xorl %r10d, %r10d
testl %esi, %esi
jle 0x13678
xorl %r11d, %r11d
movl %esi, %edi
imull %r11d, %edi
addl %r10d, %edi
imull %esi, %edi
addl %r8d, %edi
movslq %edi, %rdx
movl %r9d, (%rcx,%rdx,4)
xorl %r12d, %r12d
cmpl 0xc(%rsp), %r9d
jge 0x13663
movslq %r9d, %r13
leaq (,%r13,2), %rdx
addq %r13, %rdx
movq 0x10(%rsp), %r12
leaq (%r12,%rdx,8), %rdx
xorl %r12d, %r12d
movl (%r15,%r13,4), %ebp
cmpl %ebp, %edi
jne 0x13653
movsd (%rdx), %xmm0
leal (%r9,%r9,2), %edi
movslq %edi, %rdi
movsd (%rbx,%rdi,8), %xmm1
movups -0x10(%rdx), %xmm2
movsd %xmm1, -0x10(%rdx)
movsd 0x8(%rbx,%rdi,8), %xmm1
movsd %xmm1, -0x8(%rdx)
movsd 0x10(%rbx,%rdi,8), %xmm1
movsd %xmm1, (%rdx)
movups %xmm2, (%rbx,%rdi,8)
movsd %xmm0, 0x10(%rbx,%rdi,8)
movslq %r9d, %r9
movl (%r15,%r9,4), %edi
movl %edi, (%r15,%r13,4)
movl %ebp, (%r15,%r9,4)
incl %r9d
incl %r12d
movl %ebp, %edi
jmp 0x13655
movl %edi, %ebp
incq %r13
addq $0x18, %rdx
cmpq %r13, %r14
jne 0x135fa
jmp 0x13665
movl %edi, %ebp
movslq %ebp, %rdx
movl %r12d, (%rax,%rdx,4)
incq %r11
cmpq %rsi, %r11
jne 0x135bc
incl %r10d
cmpl %esi, %r10d
jne 0x135b1
incl %r8d
cmpl 0x30(%rsp), %r8d
jne 0x135a6
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 0x72a4(%rip), %rax      # 0x1a959
movq %rax, 0xef84(%rip)      # 0x22640
movl $0x1bc, 0xef72(%rip)    # imm = 0x1BC
shrl $0x1f, %r15d
leal 0x2b(,%r15,2), %esi
leaq 0x741f(%rip), %rdi      # 0x1aaf8
movl %ebx, %edx
xorl %eax, %eax
callq 0x258d
movl $0x1, %eax
jmp 0x1369c