// File: Luthaf[P]zeopp/network.cc
// Function: extendVorNet(VORONOI_NETWORK*, VORONOI_NETWORK*, DELTA_POS, std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int>>>*, std::set<int, std::less<int>, std::allocator<int>>*)
void extendVorNet(VORONOI_NETWORK *vornet, VORONOI_NETWORK *newNet, DELTA_POS direction, map<int,int> *idAliases, set<int> *sourceNodes){
  idAliases->clear();
  sourceNodes->clear();

  DELTA_POS filter = DELTA_POS(1,1,1) - direction;
  int numIDs = vornet->nodes.size();
  
  for(unsigned int i = 0; i < vornet->edges.size(); i++){
    VOR_EDGE curEdge = vornet->edges.at(i);
    DELTA_POS dirComp = DELTA_POS(curEdge.delta_uc_x, curEdge.delta_uc_y, curEdge.delta_uc_z)*direction;
    if(dirComp.isZero())
      continue;
    else if ((dirComp.x < 0) || (dirComp.y < 0) || (dirComp.z < 0)){
      sourceNodes->insert(curEdge.to);
    }
  }

  newNet->nodes.clear();
  newNet->edges.clear();
  int factor = 10;
  newNet->v_a = vornet->v_a.scale(direction.x * factor);
  newNet->v_b = vornet->v_b.scale(direction.y * factor);
  newNet->v_c = vornet->v_c.scale(direction.z * factor);

  //Repeat copy process for each additional unit cell
  int idCount = 0;
  for(int i = 0; i <= factor; i++){
    
    //Translate all of the Voronoi nodes and assign them new id's
    for(int j = 0; j < numIDs; j++){
      VOR_NODE oldNode = vornet->nodes.at(j);
      VOR_NODE newNode; 
      newNode.x = oldNode.x + i*direction.x*vornet->v_a.x + i*direction.y*vornet->v_b.x + i*direction.z*vornet->v_c.x; 
      newNode.y = oldNode.y + i*direction.x*vornet->v_a.y + i*direction.y*vornet->v_b.y + i*direction.z*vornet->v_c.y; 
      newNode.z = oldNode.z + i*direction.x*vornet->v_a.z + i*direction.y*vornet->v_b.z + i*direction.z*vornet->v_c.z; 
      newNode.rad_stat_sphere = oldNode.rad_stat_sphere;
      newNet->nodes.push_back(newNode);
      if(sourceNodes->find(j) != sourceNodes->end()){
	sourceNodes->insert(idCount);
	idAliases->insert(pair<int,int> (idCount,j));
      }
      idCount++;
    }

    //Translate all of the Voronoi edges, modifying the connectivity information as is appropriate
    for(unsigned int j = 0; j < vornet->edges.size(); j++){
      VOR_EDGE oldEdge = vornet->edges.at(j);
      DELTA_POS oldDirection = DELTA_POS(oldEdge.delta_uc_x, oldEdge.delta_uc_y, oldEdge.delta_uc_z);
      DELTA_POS newDirection = oldDirection*filter;
      DELTA_POS dirComp = oldDirection*direction;

      int changeInTo;
      if(dirComp.isZero())
	changeInTo = 0;
      else if ((dirComp.x < 0) || (dirComp.y < 0) || (dirComp.z < 0))
	changeInTo = -1;
      else
	changeInTo = 1;
     
      int newTo = i + changeInTo;
      if(newTo < 0){
	newDirection = newDirection + (direction*(-1));
	newTo = factor;
      }
      else if (newTo > factor){
	newDirection = newDirection + direction;
	newTo = 0;
      }
 
      VOR_EDGE newEdge;
      newEdge.from = oldEdge.from + i*numIDs; 
      newEdge.to = oldEdge.to + newTo*numIDs; 
      newEdge.rad_moving_sphere = oldEdge.rad_moving_sphere; 
    
      newEdge.delta_uc_x = newDirection.x;
      newEdge.delta_uc_y = newDirection.y; 
      newEdge.delta_uc_z = newDirection.z;
      newEdge.length = oldEdge.length;
      newNet->edges.push_back(newEdge);
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x360, %rsp            # imm = 0x360
movq %rdx, -0x20(%rbp)
movl %ecx, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x10(%rbp)
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0xf230
movq -0x40(%rbp), %rdi
callq 0xf250
leaq -0x58(%rbp), %rdi
movq %rdi, -0x308(%rbp)
movl $0x1, %ecx
movl %ecx, %esi
movl %ecx, %edx
callq 0x94010
movq -0x308(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x941b0
movl %edx, -0x60(%rbp)
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x4c(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x44(%rbp)
movq -0x28(%rbp), %rdi
addq $0x48, %rdi
callq 0xf270
movl %eax, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
movq %rax, -0x310(%rbp)
movq -0x28(%rbp), %rdi
addq $0x60, %rdi
callq 0xf290
movq %rax, %rcx
movq -0x310(%rbp), %rax
cmpq %rcx, %rax
jae 0x7c1d
movq -0x28(%rbp), %rdi
addq $0x60, %rdi
movl -0x70(%rbp), %eax
movl %eax, %esi
callq 0xf2c0
movq %rax, %rsi
leaq -0x98(%rbp), %rdi
callq 0x99a90
movl -0x88(%rbp), %esi
movl -0x84(%rbp), %edx
movl -0x80(%rbp), %ecx
leaq -0xb0(%rbp), %rdi
movq %rdi, -0x318(%rbp)
callq 0x94010
movq -0x318(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x940f0
movl %edx, -0xb8(%rbp)
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0xa4(%rbp)
movl -0xb8(%rbp), %eax
movl %eax, -0x9c(%rbp)
leaq -0xa4(%rbp), %rdi
callq 0x94270
testb $0x1, %al
jne 0x7bcd
jmp 0x7bcf
jmp 0x7c0f
cmpl $0x0, -0xa4(%rbp)
jl 0x7bea
cmpl $0x0, -0xa0(%rbp)
jl 0x7bea
cmpl $0x0, -0x9c(%rbp)
jge 0x7c0b
movq -0x40(%rbp), %rdi
leaq -0x98(%rbp), %rsi
addq $0x4, %rsi
callq 0xf300
movq %rax, -0xd0(%rbp)
movb %dl, -0xc8(%rbp)
jmp 0x7c0d
jmp 0x7c0f
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x7b17
movq -0x30(%rbp), %rdi
addq $0x48, %rdi
callq 0xf350
movq -0x30(%rbp), %rdi
addq $0x60, %rdi
callq 0xf370
movl $0xa, -0xd4(%rbp)
movq -0x28(%rbp), %rsi
movl -0x10(%rbp), %eax
imull -0xd4(%rbp), %eax
cvtsi2sd %eax, %xmm0
leaq -0xf0(%rbp), %rdi
callq 0xea9f0
movq -0x30(%rbp), %rax
movq -0xf0(%rbp), %rcx
movq %rcx, (%rax)
movq -0xe8(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0xe0(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rsi
addq $0x18, %rsi
movl -0xc(%rbp), %eax
imull -0xd4(%rbp), %eax
cvtsi2sd %eax, %xmm0
leaq -0x108(%rbp), %rdi
callq 0xea9f0
movq -0x30(%rbp), %rax
movq -0x108(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq -0x100(%rbp), %rcx
movq %rcx, 0x20(%rax)
movq -0xf8(%rbp), %rcx
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rsi
addq $0x30, %rsi
movl -0x8(%rbp), %eax
imull -0xd4(%rbp), %eax
cvtsi2sd %eax, %xmm0
leaq -0x120(%rbp), %rdi
callq 0xea9f0
movq -0x30(%rbp), %rax
movq -0x120(%rbp), %rcx
movq %rcx, 0x30(%rax)
movq -0x118(%rbp), %rcx
movq %rcx, 0x38(%rax)
movq -0x110(%rbp), %rcx
movq %rcx, 0x40(%rax)
movl $0x0, -0x124(%rbp)
movl $0x0, -0x128(%rbp)
movl -0x128(%rbp), %eax
cmpl -0xd4(%rbp), %eax
jg 0x836a
movl $0x0, -0x12c(%rbp)
movl -0x12c(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jge 0x8040
movq -0x28(%rbp), %rdi
addq $0x48, %rdi
movslq -0x12c(%rbp), %rsi
callq 0xf390
movq %rax, %rsi
leaq -0x170(%rbp), %rdi
callq 0xf3d0
leaq -0x1b0(%rbp), %rdi
callq 0x99ae0
jmp 0x7d81
movsd -0x170(%rbp), %xmm1
movl -0x128(%rbp), %ecx
movl -0x10(%rbp), %edx
movl -0xc(%rbp), %esi
movl %ecx, %eax
imull %edx, %eax
cvtsi2sd %eax, %xmm0
movq -0x28(%rbp), %rax
movsd (%rax), %xmm3
movsd 0x18(%rax), %xmm2
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
movl %ecx, %edx
imull %esi, %edx
cvtsi2sd %edx, %xmm1
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movl -0x8(%rbp), %edx
imull %edx, %ecx
cvtsi2sd %ecx, %xmm0
movsd 0x30(%rax), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x1b0(%rbp)
movsd -0x168(%rbp), %xmm1
movl -0x128(%rbp), %ecx
movl -0x10(%rbp), %edx
movl -0xc(%rbp), %esi
movl %ecx, %eax
imull %edx, %eax
cvtsi2sd %eax, %xmm0
movq -0x28(%rbp), %rax
movsd 0x8(%rax), %xmm3
movsd 0x20(%rax), %xmm2
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
movl %ecx, %edx
imull %esi, %edx
cvtsi2sd %edx, %xmm1
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movl -0x8(%rbp), %edx
imull %edx, %ecx
cvtsi2sd %ecx, %xmm0
movsd 0x38(%rax), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x1a8(%rbp)
movsd -0x160(%rbp), %xmm1
movl -0x128(%rbp), %ecx
movl -0x10(%rbp), %edx
movl -0xc(%rbp), %esi
movl %ecx, %eax
imull %edx, %eax
cvtsi2sd %eax, %xmm0
movq -0x28(%rbp), %rax
movsd 0x10(%rax), %xmm3
movsd 0x28(%rax), %xmm2
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
movl %ecx, %edx
imull %esi, %edx
cvtsi2sd %edx, %xmm1
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movl -0x8(%rbp), %edx
imull %edx, %ecx
cvtsi2sd %ecx, %xmm0
movsd 0x40(%rax), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x1a0(%rbp)
movsd -0x158(%rbp), %xmm0
movsd %xmm0, -0x198(%rbp)
movq -0x30(%rbp), %rdi
addq $0x48, %rdi
leaq -0x1b0(%rbp), %rsi
callq 0xf440
jmp 0x7ecf
movq -0x40(%rbp), %rdi
leaq -0x12c(%rbp), %rsi
callq 0xf4e0
movq %rax, -0x320(%rbp)
jmp 0x7ee8
movq -0x320(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq -0x40(%rbp), %rdi
callq 0xf520
movq %rax, -0x1d0(%rbp)
leaq -0x1c8(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0xf4b0
testb $0x1, %al
jne 0x7f22
jmp 0x7ff4
movq -0x40(%rbp), %rdi
leaq -0x124(%rbp), %rsi
callq 0xf300
movb %dl, -0x329(%rbp)
movq %rax, -0x328(%rbp)
jmp 0x7f41
movb -0x329(%rbp), %al
movq -0x328(%rbp), %rcx
movq %rcx, -0x1e0(%rbp)
movb %al, -0x1d8(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x338(%rbp)
leaq -0x1e8(%rbp), %rdi
leaq -0x124(%rbp), %rsi
leaq -0x12c(%rbp), %rdx
callq 0xf590
jmp 0x7f82
movq -0x338(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0xf550
movb %dl, -0x341(%rbp)
movq %rax, -0x340(%rbp)
jmp 0x7fa4
movb -0x341(%rbp), %al
movq -0x340(%rbp), %rcx
movq %rcx, -0x1f8(%rbp)
movb %al, -0x1f0(%rbp)
jmp 0x7ff4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
jmp 0x802f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0xf5c0
jmp 0x802f
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0xf5c0
leaq -0x170(%rbp), %rdi
callq 0xf5c0
movl -0x12c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12c(%rbp)
jmp 0x7d41
leaq -0x170(%rbp), %rdi
callq 0xf5c0
jmp 0x8373
movl $0x0, -0x1fc(%rbp)
movl -0x1fc(%rbp), %eax
movq %rax, -0x350(%rbp)
movq -0x28(%rbp), %rdi
addq $0x60, %rdi
callq 0xf290
movq %rax, %rcx
movq -0x350(%rbp), %rax
cmpq %rcx, %rax
jae 0x8354
movq -0x28(%rbp), %rdi
addq $0x60, %rdi
movl -0x1fc(%rbp), %eax
movl %eax, %esi
callq 0xf2c0
movq %rax, %rsi
leaq -0x228(%rbp), %rdi
callq 0x99a90
movl -0x218(%rbp), %esi
movl -0x214(%rbp), %edx
movl -0x210(%rbp), %ecx
leaq -0x234(%rbp), %rdi
movq %rdi, -0x358(%rbp)
callq 0x94010
movq -0x358(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
callq 0x940f0
movq -0x358(%rbp), %rdi
movl %edx, -0x248(%rbp)
movq %rax, -0x250(%rbp)
movl -0x248(%rbp), %eax
movl %eax, -0x238(%rbp)
movq -0x250(%rbp), %rax
movq %rax, -0x240(%rbp)
leaq -0x10(%rbp), %rsi
callq 0x940f0
movl %edx, -0x268(%rbp)
movq %rax, -0x270(%rbp)
movq -0x270(%rbp), %rax
movq %rax, -0x25c(%rbp)
movl -0x268(%rbp), %eax
movl %eax, -0x254(%rbp)
leaq -0x25c(%rbp), %rdi
callq 0x94270
testb $0x1, %al
jne 0x8140
jmp 0x814c
movl $0x0, -0x274(%rbp)
jmp 0x817f
cmpl $0x0, -0x25c(%rbp)
jl 0x8167
cmpl $0x0, -0x258(%rbp)
jl 0x8167
cmpl $0x0, -0x254(%rbp)
jge 0x8173
movl $0xffffffff, -0x274(%rbp) # imm = 0xFFFFFFFF
jmp 0x817d
movl $0x1, -0x274(%rbp)
jmp 0x817f
movl -0x128(%rbp), %eax
addl -0x274(%rbp), %eax
movl %eax, -0x278(%rbp)
cmpl $0x0, -0x278(%rbp)
jge 0x8241
movl $0xffffffff, -0x294(%rbp) # imm = 0xFFFFFFFF
leaq -0x10(%rbp), %rdi
leaq -0x294(%rbp), %rsi
callq 0x940a0
movl %edx, -0x2a0(%rbp)
movq %rax, -0x2a8(%rbp)
movl -0x2a0(%rbp), %eax
movl %eax, -0x288(%rbp)
movq -0x2a8(%rbp), %rax
movq %rax, -0x290(%rbp)
leaq -0x240(%rbp), %rdi
leaq -0x290(%rbp), %rsi
callq 0x94150
movl %edx, -0x2b0(%rbp)
movq %rax, -0x2b8(%rbp)
movq -0x2b8(%rbp), %rax
movq %rax, -0x284(%rbp)
movl -0x2b0(%rbp), %eax
movl %eax, -0x27c(%rbp)
movq -0x284(%rbp), %rax
movq %rax, -0x240(%rbp)
movl -0x27c(%rbp), %eax
movl %eax, -0x238(%rbp)
movl -0xd4(%rbp), %eax
movl %eax, -0x278(%rbp)
jmp 0x82ac
movl -0x278(%rbp), %eax
cmpl -0xd4(%rbp), %eax
jle 0x82aa
leaq -0x240(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x94150
movl %edx, -0x2d0(%rbp)
movq %rax, -0x2d8(%rbp)
movq -0x2d8(%rbp), %rax
movq %rax, -0x2c4(%rbp)
movl -0x2d0(%rbp), %eax
movl %eax, -0x2bc(%rbp)
movq -0x2c4(%rbp), %rax
movq %rax, -0x240(%rbp)
movl -0x2bc(%rbp), %eax
movl %eax, -0x238(%rbp)
movl $0x0, -0x278(%rbp)
jmp 0x82ac
leaq -0x300(%rbp), %rdi
callq 0x99a20
movl -0x228(%rbp), %eax
movl -0x128(%rbp), %ecx
imull -0x6c(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x300(%rbp)
movl -0x224(%rbp), %eax
movl -0x278(%rbp), %ecx
imull -0x6c(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x2fc(%rbp)
movsd -0x220(%rbp), %xmm0
movsd %xmm0, -0x2f8(%rbp)
movl -0x240(%rbp), %eax
movl %eax, -0x2f0(%rbp)
movl -0x23c(%rbp), %eax
movl %eax, -0x2ec(%rbp)
movl -0x238(%rbp), %eax
movl %eax, -0x2e8(%rbp)
movsd -0x208(%rbp), %xmm0
movsd %xmm0, -0x2e0(%rbp)
movq -0x30(%rbp), %rdi
addq $0x60, %rdi
leaq -0x300(%rbp), %rsi
callq 0xf5e0
movl -0x1fc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1fc(%rbp)
jmp 0x804a
jmp 0x8356
movl -0x128(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x128(%rbp)
jmp 0x7d25
addq $0x360, %rsp            # imm = 0x360
popq %rbp
retq
movq -0x1b8(%rbp), %rdi
callq 0x58f0
nop