// File: /workspace/github_cpp_makefile_star2_O2/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, 0x98(%rsp)
movl %r8d, %ebx
movq %rcx, 0x90(%rsp)
movq %rdx, 0x38(%rsp)
movl %edi, 0x5c(%rsp)
xorps %xmm0, %xmm0
andq $0x0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%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
movq %rsi, 0x8(%rsp)
movl 0x110(%rsi), %ebp
leaq 0xf6dd1(%rip), %rdi     # 0xfc0f0
movapd %xmm0, 0x60(%rsp)
movsd %xmm1, 0xb0(%rsp)
movapd %xmm2, 0x40(%rsp)
movsd %xmm3, 0xa8(%rsp)
movsd %xmm4, 0xa0(%rsp)
movsd %xmm5, 0x10(%rsp)
movb $0x6, %al
callq 0x4050
testl %ebp, %ebp
jle 0x577b
movsd 0xf6ca5(%rip), %xmm0    # 0xfc008
movapd 0x60(%rsp), %xmm2
ucomisd %xmm2, %xmm0
ja 0x57aa
ucomisd 0x40(%rsp), %xmm0
ja 0x57aa
ucomisd 0x10(%rsp), %xmm0
ja 0x57aa
cvtsi2sd %ebp, %xmm0
movsd 0xf6c79(%rip), %xmm1    # 0xfc010
mulsd %xmm2, %xmm1
mulsd 0x40(%rsp), %xmm1
mulsd 0x10(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0xf6c65(%rip), %xmm1    # 0xfc018
callq 0x4770
movapd 0x60(%rsp), %xmm2
unpcklpd 0x40(%rsp), %xmm2       # xmm2 = xmm2[0],mem[0]
movsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm2, %xmm0
addpd 0xf6cb2(%rip), %xmm0    # 0xfc090
addsd 0xf6c3a(%rip), %xmm1    # 0xfc020
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
movapd %xmm0, %xmm3
mulsd %xmm2, %xmm3
mulsd %xmm1, %xmm3
ucomisd 0xf6c26(%rip), %xmm3    # 0xfc028
ja 0x57c5
movq 0x8(%rsp), %r14
leaq 0x38(%r14), %rax
movq %rax, 0x78(%rsp)
leaq 0x50(%r14), %rax
movq %rax, 0x80(%rsp)
leaq 0x68(%r14), %rax
movq %rax, 0x88(%rsp)
cvttsd2si %xmm0, %edx
cvttsd2si %xmm2, %ecx
cvttsd2si %xmm1, %r8d
leaq 0xf6e80(%rip), %rdi     # 0xfc2c2
xorl %r12d, %r12d
movl %ebp, %esi
movl %edx, 0x30(%rsp)
movl %ecx, 0x2c(%rsp)
movl %r8d, 0x28(%rsp)
xorl %eax, %eax
callq 0x4050
movzbl (%r14), %eax
movl %eax, 0x20(%rsp)
movzbl %bl, %eax
movl %eax, 0x24(%rsp)
leaq 0xc0(%rsp), %rbx
leaq 0xbc(%rsp), %r14
leaq 0xb8(%rsp), %r15
cmpb $0x0, 0x5c(%rsp)
movl %r12d, 0x34(%rsp)
je 0x556c
leaq 0xf6e61(%rip), %rdi     # 0xfc2fa
callq 0x4670
movl $0x298, %edi            # imm = 0x298
callq 0x44a0
movq %rax, %rbp
movq %rax, %rdi
movaps 0x60(%rsp), %xmm0
movsd 0xb0(%rsp), %xmm1
movaps 0x40(%rsp), %xmm2
movsd 0xa8(%rsp), %xmm3
movsd 0xa0(%rsp), %xmm4
movsd 0x10(%rsp), %xmm5
movl 0x30(%rsp), %esi
movl 0x2c(%rsp), %edx
movl 0x28(%rsp), %ecx
pushq $0x10
popq %r8
callq 0xf8750
movq 0x8(%rsp), %rax
movq 0x2f0(%rax), %r13
xorl %r12d, %r12d
movq 0x8(%rsp), %rax
cmpq 0x2f8(%rax), %r13
je 0x5640
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movsd 0x10(%r13), %xmm2
movsd 0x30(%r13), %xmm3
movq %rbp, %rdi
movl %r12d, %esi
leaq 0x1c(%rsp), %rdx
movq %r14, %rcx
movq %r15, %r8
callq 0xf8f16
movq %rbx, %rdi
leaq 0x1c(%rsp), %rsi
callq 0x9434
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9434
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9434
addq $0x98, %r13
incl %r12d
jmp 0x54fd
leaq 0xf6dae(%rip), %rdi     # 0xfc321
callq 0x4670
movl $0x270, %edi            # imm = 0x270
callq 0x44a0
movq %rax, %rbp
movq %rax, %rdi
movaps 0x60(%rsp), %xmm0
movsd 0xb0(%rsp), %xmm1
movaps 0x40(%rsp), %xmm2
movsd 0xa8(%rsp), %xmm3
movsd 0xa0(%rsp), %xmm4
movsd 0x10(%rsp), %xmm5
movl 0x30(%rsp), %esi
movl 0x2c(%rsp), %edx
movl 0x28(%rsp), %ecx
pushq $0x10
popq %r8
callq 0xf86f2
movq 0x8(%rsp), %rax
movq 0x2f0(%rax), %r13
xorl %r12d, %r12d
movq 0x8(%rsp), %rax
cmpq 0x2f8(%rax), %r13
je 0x56a0
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movsd 0x10(%r13), %xmm2
movq %rbp, %rdi
movl %r12d, %esi
leaq 0x1c(%rsp), %rdx
movq %r14, %rcx
movq %r15, %r8
callq 0xf8bf0
movq %rbx, %rdi
leaq 0x1c(%rsp), %rsi
callq 0x9434
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9434
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9434
addq $0x98, %r13
incl %r12d
jmp 0x55d7
movq 0x90(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
movq 0x38(%rsp), %rdx
movapd 0x60(%rsp), %xmm0
movapd 0x40(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movq 0x98(%rsp), %rcx
movq %rbx, %r8
movl 0x24(%rsp), %r9d
callq 0x9452
movl 0x34(%rsp), %r12d
testb %al, %al
jne 0x5702
cmpl 0x20(%rsp), %r12d
je 0x576a
movq 0x8(%rsp), %rdi
callq 0x3e09e
jmp 0x56fa
movq 0x90(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
movq 0x38(%rsp), %rdx
movapd 0x60(%rsp), %xmm0
movapd 0x40(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movq 0x98(%rsp), %rcx
movq %rbx, %r8
movl 0x24(%rsp), %r9d
callq 0x9d46
movl 0x34(%rsp), %r12d
testb %al, %al
jne 0x5702
cmpl 0x20(%rsp), %r12d
je 0x576a
movq 0x8(%rsp), %rdi
callq 0x3e09e
incl %r12d
jmp 0x5482
movq 0x78(%rsp), %rdx
movq 0x10(%rdx), %rax
movq 0x38(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups (%rdx), %xmm0
movups %xmm0, (%rcx)
movq 0x80(%rsp), %rax
movups (%rax), %xmm0
movups %xmm0, 0x18(%rcx)
movq 0x10(%rax), %rax
movq %rax, 0x28(%rcx)
movq 0x88(%rsp), %rax
movups (%rax), %xmm0
movups %xmm0, 0x30(%rcx)
movq 0x10(%rax), %rax
movq %rax, 0x40(%rcx)
leaq 0xc0(%rsp), %rdi
callq 0xb290
movq %rbp, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xfdeb1(%rip), %rdi     # 0x103622
callq 0x4670
callq 0x524c
movl $0x12c, %edi            # imm = 0x12C
callq 0x4030
movq %rax, %rbx
leaq 0xf69a0(%rip), %rsi     # 0xfc12f
movq %rax, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x40d0
movq 0x14a82e(%rip), %rax    # 0x14ffd0
movq (%rax), %rsi
movq %rbx, %rdi
jmp 0x57bb
movq 0x14a81f(%rip), %rax    # 0x14ffd0
movq (%rax), %rsi
leaq 0xf69f7(%rip), %rdi     # 0xfc1b2
callq 0x44f0
callq 0x524c
movq 0x14a804(%rip), %rax    # 0x14ffd0
movq (%rax), %rdi
leaq 0xf6a50(%rip), %rsi     # 0xfc226
movl $0x1000000, %edx        # imm = 0x1000000
xorl %eax, %eax
callq 0x4610
callq 0x524c
jmp 0x580f
jmp 0x580f
movq %rax, %rbx
movl $0x270, %esi            # imm = 0x270
jmp 0x57fd
movq %rax, %rbx
movl $0x298, %esi            # imm = 0x298
movq %rbp, %rdi
callq 0x44b0
jmp 0x5812
jmp 0x580f
jmp 0x580f
jmp 0x580f
jmp 0x580f
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0xb290
movq %rbx, %rdi
callq 0x4780