// File: /workspace/github_cpp_makefile_star2_O1/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, 0x48(%rsp)
movq %rsi, %r12
movl %edi, 0x74(%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 0x147ec8(%rip), %rdi    # 0x14f0b0
movsd %xmm0, 0x40(%rsp)
movsd %xmm1, 0xd0(%rsp)
movsd %xmm2, 0x38(%rsp)
movsd %xmm3, 0xc8(%rsp)
movsd %xmm4, 0xc0(%rsp)
movsd %xmm5, 0x30(%rsp)
movb $0x6, %al
callq 0x4060
testl %ebp, %ebp
jle 0x775c
movsd 0x147ddc(%rip), %xmm0   # 0x14f008
movsd 0x40(%rsp), %xmm2
ucomisd %xmm2, %xmm0
ja 0x7770
ucomisd 0x38(%rsp), %xmm0
ja 0x7770
ucomisd 0x30(%rsp), %xmm0
ja 0x7770
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd 0x147dad(%rip), %xmm1   # 0x14f010
mulsd %xmm2, %xmm1
mulsd 0x38(%rsp), %xmm1
mulsd 0x30(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0x147d99(%rip), %xmm1   # 0x14f018
callq 0x4770
movsd 0x40(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd 0x147d8a(%rip), %xmm3   # 0x14f020
addsd %xmm3, %xmm1
movsd 0x38(%rsp), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
mulsd 0x30(%rsp), %xmm0
addsd %xmm3, %xmm0
movapd %xmm1, %xmm3
mulsd %xmm2, %xmm3
mulsd %xmm0, %xmm3
ucomisd 0x147d62(%rip), %xmm3   # 0x14f028
ja 0x7775
leaq 0x38(%r12), %rax
movq %rax, 0xa8(%rsp)
leaq 0x50(%r12), %rax
movq %rax, 0xa0(%rsp)
leaq 0x68(%r12), %rax
movq %rax, 0x98(%rsp)
cvttsd2si %xmm1, %edx
cvttsd2si %xmm2, %ecx
cvttsd2si %xmm0, %r8d
leaq 0x147f7b(%rip), %rdi    # 0x14f282
xorl %r13d, %r13d
movl %ebp, %esi
movl %edx, 0x60(%rsp)
movl %ecx, 0x5c(%rsp)
movl %r8d, 0x58(%rsp)
xorl %eax, %eax
callq 0x4060
movzbl (%r12), %eax
movq %rax, 0x90(%rsp)
incl %eax
movl %eax, 0x70(%rsp)
movq 0x48(%rsp), %rax
leaq 0x18(%rax), %rcx
movq %rcx, 0x88(%rsp)
addq $0x30, %rax
movq %rax, 0x80(%rsp)
movzbl %bl, %eax
movl %eax, 0x54(%rsp)
leaq 0x6c(%rsp), %rbx
leaq 0x68(%rsp), %r14
cmpb $0x0, 0x74(%rsp)
movl %r13d, 0x64(%rsp)
je 0x7517
leaq 0x147f42(%rip), %rdi    # 0x14f2ba
callq 0x4660
movl $0x298, %edi            # imm = 0x298
callq 0x4460
movq %rax, %r15
movq %rax, %rdi
movsd 0x40(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm1
movsd 0x38(%rsp), %xmm2
movsd 0xc8(%rsp), %xmm3
movsd 0xc0(%rsp), %xmm4
movsd 0x30(%rsp), %xmm5
movl 0x60(%rsp), %esi
movl 0x5c(%rsp), %edx
movl 0x58(%rsp), %ecx
movl $0x10, %r8d
callq 0x149fd6
movq 0x2f0(%r12), %r13
cmpq 0x2f8(%r12), %r13
je 0x74ad
xorl %ebp, %ebp
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movsd 0x10(%r13), %xmm2
movsd 0x30(%r13), %xmm3
movq %r15, %rdi
movl %ebp, %esi
leaq 0xc(%rsp), %rdx
movq %rbx, %rcx
movq %r14, %r8
callq 0x14a7ba
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x7433
movl 0xc(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x7442
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0xf7b4
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x745f
movl 0x6c(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x746c
leaq 0x10(%rsp), %rdi
movq %rbx, %rdx
callq 0xf7b4
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x7489
movl 0x68(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x7496
leaq 0x10(%rsp), %rdi
movq %r14, %rdx
callq 0xf7b4
addq $0x98, %r13
incl %ebp
cmpq 0x2f8(%r12), %r13
jne 0x73e9
movq 0xb0(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq 0x48(%rsp), %rdx
movsd 0x40(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movq 0xb8(%rsp), %rcx
leaq 0x10(%rsp), %r8
movl 0x54(%rsp), %r9d
callq 0xca29
movl 0x64(%rsp), %r13d
movl %eax, %ebp
testb %al, %al
jne 0x7698
cmpl 0x90(%rsp), %r13d
je 0x774b
movq %r12, %rdi
callq 0x51b32
jmp 0x770a
leaq 0x147dc3(%rip), %rdi    # 0x14f2e1
callq 0x4660
movl $0x270, %edi            # imm = 0x270
callq 0x4460
movq %rax, %r15
movq %rax, %rdi
movsd 0x40(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm1
movsd 0x38(%rsp), %xmm2
movsd 0xc8(%rsp), %xmm3
movsd 0xc0(%rsp), %xmm4
movsd 0x30(%rsp), %xmm5
movl 0x60(%rsp), %esi
movl 0x5c(%rsp), %edx
movl 0x58(%rsp), %ecx
movl $0x10, %r8d
callq 0x149f76
movq 0x2f0(%r12), %r13
cmpq 0x2f8(%r12), %r13
je 0x764d
xorl %ebp, %ebp
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movsd 0x10(%r13), %xmm2
movq %r15, %rdi
movl %ebp, %esi
leaq 0xc(%rsp), %rdx
movq %rbx, %rcx
movq %r14, %r8
callq 0x14a480
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x75d3
movl 0xc(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x75e2
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0xf7b4
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x75ff
movl 0x6c(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x760c
leaq 0x10(%rsp), %rdi
movq %rbx, %rdx
callq 0xf7b4
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x7629
movl 0x68(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x7636
leaq 0x10(%rsp), %rdi
movq %r14, %rdx
callq 0xf7b4
addq $0x98, %r13
incl %ebp
cmpq 0x2f8(%r12), %r13
jne 0x758f
movq 0xb0(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq 0x48(%rsp), %rdx
movsd 0x40(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movq 0xb8(%rsp), %rcx
leaq 0x10(%rsp), %r8
movl 0x54(%rsp), %r9d
callq 0xd90f
movl 0x64(%rsp), %r13d
movl %eax, %ebp
testb %al, %al
je 0x76f8
movq 0xa8(%rsp), %rdx
movq 0x10(%rdx), %rax
movq 0x48(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups (%rdx), %xmm0
movups %xmm0, (%rcx)
movq 0xa0(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0x88(%rsp), %rdx
movq %rax, 0x10(%rdx)
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq 0x98(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0x80(%rsp), %rdx
movq %rax, 0x10(%rdx)
movupd (%rcx), %xmm0
movupd %xmm0, (%rdx)
movq %r15, 0x78(%rsp)
jmp 0x770a
cmpl 0x90(%rsp), %r13d
je 0x774b
movq %r12, %rdi
callq 0x51b32
testb %bpl, %bpl
jne 0x771d
incl %r13d
cmpl 0x70(%rsp), %r13d
jne 0x7361
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7734
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x78(%rsp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14ee60(%rip), %rdi    # 0x1565b2
callq 0x4660
callq 0x710c
movl $0x12c, %edi            # imm = 0x12C
callq 0x4030
movq %rax, %rdi
movl %ebp, %esi
callq 0x48bf
callq 0x48a3
callq 0x4880
jmp 0x77a2
movq %rax, %rbx
movl $0x270, %esi            # imm = 0x270
jmp 0x778e
movq %rax, %rbx
movl $0x298, %esi            # imm = 0x298
movq %r15, %rdi
callq 0x4470
jmp 0x77a5
jmp 0x77a2
jmp 0x77a2
jmp 0x77a2
jmp 0x77a2
jmp 0x77a2
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x77bc
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %rbx, %rdi
callq 0x4780