// File: /workspace/github_cpp_makefile_star2_O3/Luthaf[P]zeopp/network.cc
// Function: performVoronoiDecomp(bool, ATOM_NETWORK*, VORONOI_NETWORK*, std::vector<VOR_CELL, std::allocator<VOR_CELL>>&, bool, std::vector<BASIC_VCELL, std::allocator<BASIC_VCELL>>&)
void* performVoronoiDecomp(bool radial, ATOM_NETWORK *cell, VORONOI_NETWORK *vornet, vector<VOR_CELL> &cells, bool saveVorCells,
			   vector<BASIC_VCELL> &bvcells){
  int i,n;
  double bx,bxy,by,bxz,byz,bz;
  vector<int> atomShifts;
  
  // Set the box dimensions and number of particles
  bx  = cell->v_a.x;
  bxy = cell->v_b.x;
  by  = cell->v_b.y;
  bxz = cell->v_c.x;
  byz = cell->v_c.y;
  bz  = cell->v_c.z;
  n   = cell->numAtoms;
  
  // Print the box dimensions
  printf("Box dimensions:\n"
	 "  va=(%f 0 0)\n"
	 "  vb=(%f %f 0)\n"
	 "  vc=(%f %f %f)\n\n",bx,bxy,by,bxz,byz,bz);
  
  // Check that the input parameters make sense
  if(n<1) {
    char* sentence = new char[300];
    sprintf(sentence, "Error: Invalid number of particles provided for Voronoi decomposition (%d particles were read from file, which is <1)\nExiting ...\n", n);
    fputs(sentence, stderr);
    exit(1);
  }
  if(bx<tolerance||by<tolerance||bz<tolerance){
    fputs("Error: Invalid box dimensions calculated for Voronoi decomposition."
	  " Please check unit cell parameters.\nExiting ...\n",stderr);
    exit(1);
  }
  
  // Compute the internal grid size, aiming to make
  // the grid blocks square with around 6 particles
  // in each
  double ls=pow(n/(9*bx*by*bz),1.0/3.0);
  double nxf=bx*ls+1.1;
  double nyf=by*ls+1.1;
  double nzf=bz*ls+1.1;
  
  // Check the grid is not too huge, using floating point numbers to avoid
  // integer wrap-arounds
  if (nxf*nyf*nzf>max_regions) {
    fprintf(stderr,"voro++: Number of computational blocks exceeds the maximum allowed of %d\n"
		   "Either increase the particle length scale, or recompile with an increased\nmaximum.",max_regions);
    exit(1);
  }
  
  // Now that we are confident that the number of regions is reasonable,
  // create integer versions of them
  int nx=int(nxf);
  int ny=int(nyf);
  int nz=int(nzf);
  printf("Total particles = %d\n\nInternal grid size = (%d %d %d)\n\n",n,nx,ny,nz);

  //a temporary fix to the Voronoi volume check issue:
  //if the user allows, when the volume check fails the structure's atom coordinates can be randomly altered by a very small amount, to try and bypass the error
  int numAttemptsPermitted = 1;
  if(cell->allowAdjustCoordsAndCellFlag) numAttemptsPermitted++;
  for(int attempt=0; attempt<numAttemptsPermitted; attempt++) {
    
    if(radial) {
      puts("Using voro++ with radii for particles.");
      // Create a container with the geometry given above
      container_periodic_poly *rad_con;
      rad_con =  new container_periodic_poly (bx,bxy,by,bxz,byz,bz,nx,ny,nz,memory);
      
      // Read in the particles from the provided ATOM_NETWORK
      vector <ATOM> ::iterator iter = cell->atoms.begin();
      i = 0;
      int da, db, dc;
      while(iter != cell->atoms.end()){ 
        rad_con->put(i,iter->x,iter->y,iter->z,iter->radius, da, db, dc);
        atomShifts.push_back(da); atomShifts.push_back(db); atomShifts.push_back(dc);
        iter++;
        i++;
      }

      // Store the Voronoi Network for later use
      bool volume_correct = storeVoronoiNetwork(*rad_con, cell, vornet, bx, by, bz, bvcells, atomShifts, saveVorCells, cells);
      if(volume_correct) {
        vornet->v_a = cell->v_a;
        vornet->v_b = cell->v_b;
        vornet->v_c = cell->v_c;
        return rad_con;
      } else if(attempt==numAttemptsPermitted-1) {
        printf("Exiting...\n");
        exit(1);
      } else {
        cell->randomlyAdjustCoordsAndCell();
      }
    }
    else {
      puts("Using voro++ without radii for particles.");
      // Create a container with the geometry given above
      container_periodic *no_rad_con;
      no_rad_con = new container_periodic(bx,bxy,by,bxz,byz,bz,nx,ny,nz,memory);

      // Read in the particles from the provided ATOM_NETWORK
      vector <ATOM> ::iterator iter = cell->atoms.begin();
      i = 0;
      int da, db, dc;
      while(iter != cell->atoms.end()){
        no_rad_con->put(i,iter->x,iter->y,iter->z, da, db, dc);
        atomShifts.push_back(da); atomShifts.push_back(db); atomShifts.push_back(dc);
        iter++;
        i++;
      }

      // Store the Voronoi Network for later use
      bool volume_correct = storeVoronoiNetwork(*no_rad_con, cell, vornet, bx, by, bz, bvcells, atomShifts, saveVorCells, cells);
      if(volume_correct) {
        vornet->v_a = cell->v_a;
        vornet->v_b = cell->v_b;
        vornet->v_c = cell->v_c;
        return no_rad_con;
      } else if(attempt==numAttemptsPermitted-1) {
        printf("Exiting...\n");
        exit(1);
      } else {
        cell->randomlyAdjustCoordsAndCell();
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r9, 0xb8(%rsp)
movl %r8d, %ebx
movq %rcx, 0xb0(%rsp)
movq %rdx, 0x58(%rsp)
movq %rsi, %r12
movl %edi, 0x7c(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movsd 0x38(%rsi), %xmm0
movsd 0x50(%rsi), %xmm1
movsd 0x58(%rsi), %xmm2
movsd 0x68(%rsi), %xmm3
movsd 0x70(%rsi), %xmm4
movsd 0x78(%rsi), %xmm5
movl 0x110(%rsi), %ebp
leaq 0x145d68(%rip), %rdi    # 0x14d100
movapd %xmm0, 0x80(%rsp)
movsd %xmm1, 0xd0(%rsp)
movapd %xmm2, 0x60(%rsp)
movsd %xmm3, 0xc8(%rsp)
movsd %xmm4, 0xc0(%rsp)
movsd %xmm5, 0x30(%rsp)
movb $0x6, %al
callq 0x4060
testl %ebp, %ebp
jle 0x78ea
movsd 0x145c29(%rip), %xmm0   # 0x14d008
movapd 0x80(%rsp), %xmm2
ucomisd %xmm2, %xmm0
ja 0x78fe
ucomisd 0x60(%rsp), %xmm0
ja 0x78fe
ucomisd 0x30(%rsp), %xmm0
ja 0x78fe
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd 0x145bf7(%rip), %xmm1   # 0x14d010
mulsd %xmm2, %xmm1
mulsd 0x60(%rsp), %xmm1
mulsd 0x30(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0x145be3(%rip), %xmm1   # 0x14d018
callq 0x4770
movapd 0x80(%rsp), %xmm2
unpcklpd 0x60(%rsp), %xmm2       # xmm2 = xmm2[0],mem[0]
movsd 0x30(%rsp), %xmm1
mulsd %xmm0, %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm2, %xmm0
addpd 0x145c2d(%rip), %xmm0   # 0x14d090
addsd 0x145bb5(%rip), %xmm1   # 0x14d020
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
movapd %xmm0, %xmm3
mulsd %xmm2, %xmm3
mulsd %xmm1, %xmm3
ucomisd 0x145ba1(%rip), %xmm3   # 0x14d028
ja 0x7903
leaq 0x38(%r12), %rax
movq %rax, 0x98(%rsp)
leaq 0x50(%r12), %rax
movq %rax, 0xa0(%rsp)
leaq 0x68(%r12), %rax
movq %rax, 0xa8(%rsp)
cvttsd2si %xmm0, %edx
cvttsd2si %xmm2, %ecx
cvttsd2si %xmm1, %r8d
leaq 0x145e0a(%rip), %rdi    # 0x14d2d2
xorl %r13d, %r13d
movl %ebp, %esi
movl %edx, 0x48(%rsp)
movl %ecx, 0x44(%rsp)
movl %r8d, 0x40(%rsp)
xorl %eax, %eax
callq 0x4060
movzbl (%r12), %eax
movl %eax, 0x38(%rsp)
movzbl %bl, %eax
movl %eax, 0x3c(%rsp)
leaq 0x54(%rsp), %r14
leaq 0x50(%rsp), %r15
cmpb $0x0, 0x7c(%rsp)
movl %r13d, 0x4c(%rsp)
je 0x76bb
leaq 0x145df8(%rip), %rdi    # 0x14d30a
callq 0x4660
movl $0x298, %edi            # imm = 0x298
callq 0x4460
movq %rax, %rbp
movq %rax, %rdi
movapd 0x80(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm1
movapd 0x60(%rsp), %xmm2
movsd 0xc8(%rsp), %xmm3
movsd 0xc0(%rsp), %xmm4
movsd 0x30(%rsp), %xmm5
movl 0x48(%rsp), %esi
movl 0x44(%rsp), %edx
movl 0x40(%rsp), %ecx
movl $0x10, %r8d
callq 0x148a6a
movq 0x2f0(%r12), %r13
cmpq 0x2f8(%r12), %r13
je 0x7653
xorl %ebx, %ebx
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movsd 0x10(%r13), %xmm2
movsd 0x30(%r13), %xmm3
movq %rbp, %rdi
movl %ebx, %esi
leaq 0xc(%rsp), %rdx
movq %r14, %rcx
movq %r15, %r8
callq 0x14927a
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rax
cmpq %rax, %rsi
je 0x75d3
movl 0xc(%rsp), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x75ec
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0xf864
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rax
cmpq %rax, %rsi
je 0x7602
movl 0x54(%rsp), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x7619
leaq 0x10(%rsp), %rdi
movq %r14, %rdx
callq 0xf864
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rax
cmpq %rax, %rsi
je 0x762f
movl 0x50(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x763c
leaq 0x10(%rsp), %rdi
movq %r15, %rdx
callq 0xf864
addq $0x98, %r13
incl %ebx
cmpq 0x2f8(%r12), %r13
jne 0x7586
movq 0xb0(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq %r12, %rsi
movq 0x58(%rsp), %rdx
movapd 0x80(%rsp), %xmm0
movapd 0x60(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movq 0xb8(%rsp), %rcx
leaq 0x10(%rsp), %r8
movl 0x3c(%rsp), %r9d
callq 0xca5a
movl 0x4c(%rsp), %r13d
testb %al, %al
jne 0x7864
cmpl 0x38(%rsp), %r13d
je 0x78d9
movq %r12, %rdi
callq 0x51744
jmp 0x785c
leaq 0x145c6f(%rip), %rdi    # 0x14d331
callq 0x4660
movl $0x270, %edi            # imm = 0x270
callq 0x4460
movq %rax, %rbp
movq %rax, %rdi
movapd 0x80(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm1
movapd 0x60(%rsp), %xmm2
movsd 0xc8(%rsp), %xmm3
movsd 0xc0(%rsp), %xmm4
movsd 0x30(%rsp), %xmm5
movl 0x48(%rsp), %esi
movl 0x44(%rsp), %edx
movl 0x40(%rsp), %ecx
movl $0x10, %r8d
callq 0x148a0a
movq 0x2f0(%r12), %r13
cmpq 0x2f8(%r12), %r13
je 0x77fd
xorl %ebx, %ebx
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movsd 0x10(%r13), %xmm2
movq %rbp, %rdi
movl %ebx, %esi
leaq 0xc(%rsp), %rdx
movq %r14, %rcx
movq %r15, %r8
callq 0x148f34
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rax
cmpq %rax, %rsi
je 0x777d
movl 0xc(%rsp), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x7796
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0xf864
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rax
cmpq %rax, %rsi
je 0x77ac
movl 0x54(%rsp), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x77c3
leaq 0x10(%rsp), %rdi
movq %r14, %rdx
callq 0xf864
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rax
cmpq %rax, %rsi
je 0x77d9
movl 0x50(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x77e6
leaq 0x10(%rsp), %rdi
movq %r15, %rdx
callq 0xf864
addq $0x98, %r13
incl %ebx
cmpq 0x2f8(%r12), %r13
jne 0x7736
movq 0xb0(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq %r12, %rsi
movq 0x58(%rsp), %rdx
movapd 0x80(%rsp), %xmm0
movapd 0x60(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movq 0xb8(%rsp), %rcx
leaq 0x10(%rsp), %r8
movl 0x3c(%rsp), %r9d
callq 0xd961
movl 0x4c(%rsp), %r13d
testb %al, %al
jne 0x7864
cmpl 0x38(%rsp), %r13d
je 0x78d9
movq %r12, %rdi
callq 0x51744
incl %r13d
jmp 0x74fb
movq 0x98(%rsp), %rdx
movq 0x10(%rdx), %rax
movq 0x58(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups (%rdx), %xmm0
movups %xmm0, (%rcx)
movq 0xa0(%rsp), %rax
movups (%rax), %xmm0
movups %xmm0, 0x18(%rcx)
movq 0x10(%rax), %rax
movq %rax, 0x28(%rcx)
movq 0xa8(%rsp), %rax
movups (%rax), %xmm0
movups %xmm0, 0x30(%rcx)
movq 0x10(%rax), %rax
movq %rax, 0x40(%rcx)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x78c4
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %rbp, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14cdea(%rip), %rdi    # 0x1546ca
callq 0x4660
callq 0x72bc
movl $0x12c, %edi            # imm = 0x12C
callq 0x4030
movq %rax, %rdi
movl %ebp, %esi
callq 0x48bf
callq 0x48a3
callq 0x4880
jmp 0x7930
jmp 0x7930
jmp 0x7930
movq %rax, %rbx
movl $0x298, %esi            # imm = 0x298
jmp 0x7920
movq %rax, %rbx
movl $0x270, %esi            # imm = 0x270
movq %rbp, %rdi
callq 0x4470
jmp 0x7933
jmp 0x7930
jmp 0x7930
jmp 0x7930
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x794a
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %rbx, %rdi
callq 0x4780