// File: Luthaf[P]zeopp/networkstorage.cc
// Function: create_unit_cell_from_vectors(std::vector<XYZ, std::allocator<XYZ>>*, ATOM_NETWORK*)
void create_unit_cell_from_vectors(vector<XYZ> *vecs, ATOM_NETWORK *cell) {
  bool verbose = false;
  //DEBUG: complex version - attempts selection of appropriate vectors for each axis
  int num_vecs = vecs->size();
  if(num_vecs!=3) {
    printf("ERROR: create_unit_cell_from_vectors() called with !=3 (%d) vectors\n", num_vecs);
    exit(EXIT_FAILURE);
  }
  vector<int> assignments, scales;
  vector<bool> is_assigned;
  for(int i=0; i<num_vecs; i++) { 
    if(i<2) {
      assignments.push_back(-1);
      scales.push_back(1);
    }
    is_assigned.push_back(false);
  }
  //1) find which vector is closest to the x axis, and which way around it is; repeat for y
  for(int ax=0; ax<2; ax++) {
    XYZ axis(0,0,0);
    if(ax==0) axis.x=1;
    if(ax==1) axis.y=1;
    int index_closest_to_axis = -1;
    int scale = 1;
    double best_angle = -1;
    for(int i=0; i<num_vecs; i++) {
      if(!is_assigned.at(i)) { //candidate
        XYZ cand = vecs->at(i);
        XYZ reverse_cand = cand.scale(-1);
        double angle = axis.angle_between(cand);
        if(angle<best_angle || best_angle<0) {
          best_angle = angle;
          index_closest_to_axis = i;
          scale = 1;
        }
        angle = axis.angle_between(reverse_cand);
        if(angle<best_angle || best_angle<0) {
          best_angle = angle;
          index_closest_to_axis = i;
          scale = -1;
        }
      }
    }
    assignments.at(ax) = index_closest_to_axis;
    scales.at(ax) = scale;
    is_assigned.at(index_closest_to_axis) = true;
    string axis_name("ERROR");
    if(ax==0) axis_name = "x";
    if(ax==1) axis_name = "y";
    if(verbose) {
      if(scale==-1) printf("DEBUG: reversed vector %d is closest to the %s axis\n", index_closest_to_axis, axis_name.c_str());
      else printf("DEBUG: vector %d is closest to the %s axis\n", index_closest_to_axis, axis_name.c_str());
    }
  }
  //2) z axis is different - we know x and y, and want to find z to satisfy the right hand rule!
  XYZ x = vecs->at(assignments.at(0)).scale(scales.at(0));
  XYZ y = vecs->at(assignments.at(1)).scale(scales.at(1));
  XYZ z(0,0,1);
  bool found_z = false;
  for(int i=0; i<num_vecs; i++) {
    if(!is_assigned.at(i)) { //found z
      if(found_z) {
        printf("ERROR: after setting x and y vectors, more than one vector remains to be assigned to z\n");
        exit(EXIT_FAILURE);
      }
      z = vecs->at(i);
      found_z = true;
    }
  }
  XYZ cross = x.cross(y);
  if(verbose) printf("DEBUG: x = %.3f %.3f %.3f, y = %.3f %.3f %.3f, cross = %.3f %.3f %.3f; need to know which orientation of z = %.3f %.3f %.3f satisfies right hand rule\n", x.x, x.y, x.z, y.x, y.y, y.z, cross.x, cross.y, cross.z, z.x, z.y, z.z);
  int z_scale = 1;
  if(cross.angle_between(z)>cross.angle_between(z.scale(-1))) z_scale = -1;
  z = z.scale(z_scale);
  if(verbose) printf("DEBUG: selected z to be %.3f %.3f %.3f\n", z.x, z.y, z.z);
  //3) build the cell
  cell->make(x,y,z);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x370, %rsp            # imm = 0x370
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
movq -0x8(%rbp), %rdi
callq 0xae590
movl %eax, -0x18(%rbp)
cmpl $0x3, -0x18(%rbp)
je 0xa0854
movl -0x18(%rbp), %esi
leaq 0x15800d(%rip), %rdi    # 0x1f8850
movb $0x0, %al
callq 0x5070
movl $0x1, %edi
callq 0x67c0
leaq -0x30(%rbp), %rdi
callq 0xd0d0
leaq -0x48(%rbp), %rdi
callq 0xd0d0
leaq -0x70(%rbp), %rdi
callq 0xf670
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0xa08e6
cmpl $0x2, -0x74(%rbp)
jge 0xa08cc
movl $0xffffffff, -0x78(%rbp) # imm = 0xFFFFFFFF
leaq -0x30(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xffc0
jmp 0xa089a
movl $0x1, -0x88(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0xffc0
jmp 0xa08b6
jmp 0xa08cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0xa11a2
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
callq 0xf700
jmp 0xa08d9
jmp 0xa08db
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0xa0876
movl $0x0, -0x8c(%rbp)
cmpl $0x2, -0x8c(%rbp)
jge 0xa0d33
leaq -0xa8(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xea8e0
jmp 0xa0914
cmpl $0x0, -0x8c(%rbp)
jne 0xa092d
movsd 0x15172b(%rip), %xmm0   # 0x1f2050
movsd %xmm0, -0xa8(%rbp)
cmpl $0x1, -0x8c(%rbp)
jne 0xa0946
movsd 0x151712(%rip), %xmm0   # 0x1f2050
movsd %xmm0, -0xa0(%rbp)
movl $0xffffffff, -0xac(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0xb0(%rbp)
movsd 0x151e16(%rip), %xmm0   # 0x1f2778
movsd %xmm0, -0xb8(%rbp)
movl $0x0, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0xa0b2c
movslq -0xbc(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0xfff0
movq %rdx, -0x260(%rbp)
movq %rax, -0x258(%rbp)
jmp 0xa09a3
movq -0x260(%rbp), %rax
movq -0x258(%rbp), %rcx
movq %rcx, -0xd0(%rbp)
movq %rax, -0xc8(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x101a0
xorb $-0x1, %al
testb $0x1, %al
jne 0xa09d6
jmp 0xa0b16
movq -0x8(%rbp), %rdi
movslq -0xbc(%rbp), %rsi
callq 0xae5c0
movq %rax, -0x268(%rbp)
jmp 0xa09ef
movq -0x268(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0xe0(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0xf0(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movsd 0x151d57(%rip), %xmm0   # 0x1f2778
callq 0xea9f0
jmp 0xa0a28
leaq -0xa8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0xeac60
movsd %xmm0, -0x270(%rbp)
jmp 0xa0a45
movsd -0x270(%rbp), %xmm0
movsd %xmm0, -0x110(%rbp)
movsd -0x110(%rbp), %xmm1
movsd -0xb8(%rbp), %xmm0
ucomisd %xmm1, %xmm0
ja 0xa0a78
xorps %xmm0, %xmm0
ucomisd -0xb8(%rbp), %xmm0
jbe 0xa0a9e
movsd -0x110(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, -0xac(%rbp)
movl $0x1, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0xeac60
movsd %xmm0, -0x278(%rbp)
jmp 0xa0abb
movsd -0x278(%rbp), %xmm0
movsd %xmm0, -0x110(%rbp)
movsd -0x110(%rbp), %xmm1
movsd -0xb8(%rbp), %xmm0
ucomisd %xmm1, %xmm0
ja 0xa0aee
xorps %xmm0, %xmm0
ucomisd -0xb8(%rbp), %xmm0
jbe 0xa0b14
movsd -0x110(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, -0xac(%rbp)
movl $0xffffffff, -0xb0(%rbp) # imm = 0xFFFFFFFF
jmp 0xa0b16
jmp 0xa0b18
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0xa0974
movl -0xac(%rbp), %eax
movl %eax, -0x284(%rbp)
movslq -0x8c(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x101d0
movq %rax, -0x280(%rbp)
jmp 0xa0b51
movq -0x280(%rbp), %rax
movl -0x284(%rbp), %ecx
movl %ecx, (%rax)
movl -0xb0(%rbp), %eax
movl %eax, -0x294(%rbp)
movslq -0x8c(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x101d0
movq %rax, -0x290(%rbp)
jmp 0xa0b85
movq -0x290(%rbp), %rax
movl -0x294(%rbp), %ecx
movl %ecx, (%rax)
movslq -0xac(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0xfff0
movq %rdx, -0x2a8(%rbp)
movq %rax, -0x2a0(%rbp)
jmp 0xa0bb4
movq -0x2a8(%rbp), %rax
movq -0x2a0(%rbp), %rcx
movq %rcx, -0x120(%rbp)
movq %rax, -0x118(%rbp)
leaq -0x120(%rbp), %rdi
movl $0x1, %esi
callq 0x10040
leaq -0x141(%rbp), %rdi
movq %rdi, -0x2b0(%rbp)
callq 0x5900
movq -0x2b0(%rbp), %rdx
leaq 0x157c93(%rip), %rsi    # 0x1f8895
leaq -0x140(%rbp), %rdi
callq 0x47530
jmp 0xa0c10
leaq -0x141(%rbp), %rdi
callq 0x55d0
cmpl $0x0, -0x8c(%rbp)
jne 0xa0c7c
leaq 0x15216f(%rip), %rsi    # 0x1f2d9b
leaq -0x140(%rbp), %rdi
callq 0x5710
jmp 0xa0c3a
jmp 0xa0c7c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x141(%rbp), %rdi
callq 0x55d0
jmp 0xa11a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x5300
jmp 0xa11a2
cmpl $0x1, -0x8c(%rbp)
jne 0xa0c9c
leaq 0x151b97(%rip), %rsi    # 0x1f2823
leaq -0x140(%rbp), %rdi
callq 0x5710
jmp 0xa0c9a
jmp 0xa0c9c
testb $0x1, -0x11(%rbp)
je 0xa0d13
cmpl $-0x1, -0xb0(%rbp)
jne 0xa0cde
movl -0xac(%rbp), %eax
movl %eax, -0x2b4(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x5200
movl -0x2b4(%rbp), %esi
movq %rax, %rdx
leaq 0x157bc8(%rip), %rdi    # 0x1f889b
xorl %eax, %eax
callq 0x5070
jmp 0xa0cdc
jmp 0xa0d11
movl -0xac(%rbp), %eax
movl %eax, -0x2b8(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x5200
movl -0x2b8(%rbp), %esi
movq %rax, %rdx
leaq 0x157bca(%rip), %rdi    # 0x1f88d0
xorl %eax, %eax
callq 0x5070
jmp 0xa0d0f
jmp 0xa0d11
jmp 0xa0d13
leaq -0x140(%rbp), %rdi
callq 0x5300
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xa08f0
movq -0x8(%rbp), %rax
movq %rax, -0x2c8(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x30(%rbp), %rdi
callq 0x101d0
movq %rax, -0x2c0(%rbp)
jmp 0xa0d54
movq -0x2c8(%rbp), %rdi
movq -0x2c0(%rbp), %rax
movslq (%rax), %rsi
callq 0xae5c0
movq %rax, -0x2d0(%rbp)
jmp 0xa0d73
xorl %eax, %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x101d0
movq %rax, -0x2d8(%rbp)
jmp 0xa0d89
movq -0x2d0(%rbp), %rsi
movq -0x2d8(%rbp), %rax
cvtsi2sdl (%rax), %xmm0
leaq -0x160(%rbp), %rdi
callq 0xea9f0
jmp 0xa0da9
movq -0x8(%rbp), %rax
movq %rax, -0x2e8(%rbp)
leaq -0x30(%rbp), %rdi
movl $0x1, %esi
callq 0x101d0
movq %rax, -0x2e0(%rbp)
jmp 0xa0dcb
movq -0x2e8(%rbp), %rdi
movq -0x2e0(%rbp), %rax
movslq (%rax), %rsi
callq 0xae5c0
movq %rax, -0x2f0(%rbp)
jmp 0xa0dea
leaq -0x48(%rbp), %rdi
movl $0x1, %esi
callq 0x101d0
movq %rax, -0x2f8(%rbp)
jmp 0xa0e01
movq -0x2f0(%rbp), %rsi
movq -0x2f8(%rbp), %rax
cvtsi2sdl (%rax), %xmm0
leaq -0x178(%rbp), %rdi
callq 0xea9f0
jmp 0xa0e21
leaq -0x190(%rbp), %rdi
xorps %xmm1, %xmm1
movsd 0x15121d(%rip), %xmm2   # 0x1f2050
movaps %xmm1, %xmm0
callq 0xea8e0
jmp 0xa0e3d
movb $0x0, -0x191(%rbp)
movl $0x0, -0x198(%rbp)
movl -0x198(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0xa0f2d
movslq -0x198(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0xfff0
movq %rdx, -0x308(%rbp)
movq %rax, -0x300(%rbp)
jmp 0xa0e7d
movq -0x308(%rbp), %rax
movq -0x300(%rbp), %rcx
movq %rcx, -0x1a8(%rbp)
movq %rax, -0x1a0(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x101a0
xorb $-0x1, %al
testb $0x1, %al
jne 0xa0ead
jmp 0xa0f17
testb $0x1, -0x191(%rbp)
je 0xa0ed0
leaq 0x157a3f(%rip), %rdi    # 0x1f88fc
xorl %eax, %eax
callq 0x5070
jmp 0xa0ec6
movl $0x1, %edi
callq 0x67c0
movq -0x8(%rbp), %rdi
movslq -0x198(%rbp), %rsi
callq 0xae5c0
movq %rax, -0x310(%rbp)
jmp 0xa0ee9
movq -0x310(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x190(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x188(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x180(%rbp)
movb $0x1, -0x191(%rbp)
jmp 0xa0f19
movl -0x198(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x198(%rbp)
jmp 0xa0e4e
leaq -0x1c0(%rbp), %rdi
leaq -0x160(%rbp), %rsi
leaq -0x178(%rbp), %rdx
callq 0xeab30
jmp 0xa0f49
testb $0x1, -0x11(%rbp)
je 0xa0fe3
movsd -0x160(%rbp), %xmm0
movsd -0x158(%rbp), %xmm1
movsd -0x150(%rbp), %xmm2
movsd -0x178(%rbp), %xmm3
movsd -0x170(%rbp), %xmm4
movsd -0x168(%rbp), %xmm5
movsd -0x1c0(%rbp), %xmm6
movsd -0x1b8(%rbp), %xmm7
movsd -0x1b0(%rbp), %xmm8
movsd -0x190(%rbp), %xmm9
movsd -0x188(%rbp), %xmm10
movsd -0x180(%rbp), %xmm11
movq %rsp, %rax
movsd %xmm11, 0x18(%rax)
movsd %xmm10, 0x10(%rax)
movsd %xmm9, 0x8(%rax)
movsd %xmm8, (%rax)
leaq 0x15797c(%rip), %rdi    # 0x1f8954
movb $0x8, %al
callq 0x5070
jmp 0xa0fe1
jmp 0xa0fe3
movl $0x1, -0x1c4(%rbp)
leaq -0x1c0(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0xeac60
movsd %xmm0, -0x318(%rbp)
jmp 0xa100a
leaq -0x1e0(%rbp), %rdi
leaq -0x190(%rbp), %rsi
movsd 0x151758(%rip), %xmm0   # 0x1f2778
callq 0xea9f0
jmp 0xa1027
leaq -0x1c0(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0xeac60
movsd %xmm0, -0x320(%rbp)
jmp 0xa1044
movsd -0x318(%rbp), %xmm0
movsd -0x320(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0xa1064
movl $0xffffffff, -0x1c4(%rbp) # imm = 0xFFFFFFFF
cvtsi2sdl -0x1c4(%rbp), %xmm0
leaq -0x1f8(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0xea9f0
jmp 0xa1081
movq -0x1f8(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x1f0(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0x1e8(%rbp), %rax
movq %rax, -0x180(%rbp)
testb $0x1, -0x11(%rbp)
je 0xa10db
movsd -0x190(%rbp), %xmm0
movsd -0x188(%rbp), %xmm1
movsd -0x180(%rbp), %xmm2
leaq 0x15791b(%rip), %rdi    # 0x1f89eb
movb $0x3, %al
callq 0x5070
jmp 0xa10d9
jmp 0xa10db
movq -0x10(%rbp), %rdi
movq -0x150(%rbp), %rax
movq %rax, -0x200(%rbp)
movups -0x160(%rbp), %xmm0
movaps %xmm0, -0x210(%rbp)
movq -0x168(%rbp), %rax
movq %rax, -0x220(%rbp)
movups -0x178(%rbp), %xmm0
movaps %xmm0, -0x230(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x240(%rbp)
movups -0x190(%rbp), %xmm0
movaps %xmm0, -0x250(%rbp)
movq -0x240(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x40(%rax)
movaps -0x250(%rbp), %xmm0
movups %xmm0, 0x30(%rax)
movq -0x220(%rbp), %rcx
movq %rcx, 0x28(%rax)
movaps -0x230(%rbp), %xmm0
movups %xmm0, 0x18(%rax)
movq -0x200(%rbp), %rcx
movq %rcx, 0x10(%rax)
movaps -0x210(%rbp), %xmm0
movups %xmm0, (%rax)
callq 0x96500
jmp 0xa117e
leaq -0x70(%rbp), %rdi
callq 0xf9b0
leaq -0x48(%rbp), %rdi
callq 0xec80
leaq -0x30(%rbp), %rdi
callq 0xec80
addq $0x370, %rsp            # imm = 0x370
popq %rbp
retq
leaq -0x70(%rbp), %rdi
callq 0xf9b0
leaq -0x48(%rbp), %rdi
callq 0xec80
leaq -0x30(%rbp), %rdi
callq 0xec80
movq -0x80(%rbp), %rdi
callq 0x58f0
nopw %cs:(%rax,%rax)