// File: gjoni[P]iPot/src/Chain.cpp
// Function: Chain::SetKD()
void Chain::SetKD() {

	if (kd != NULL) {
		kd_free(kd);
	}

	if (kdCA != NULL) {
		kd_free(kdCA);
	}

	if (kdCent != NULL) {
		kd_free(kdCent);
	}

	kd = kd_create(3);
	kdCA = kd_create(3);
	kdCent = kd_create(3);

	Atom *A;
	int idx = 0;
	for (int i = 0; i < nRes; i++) {

		/* kd */
		for (int j = 0; j < residue[i].nAtoms; j++) {
			A = &(residue[i].atom[j]);

			kd_insert3(kd, A->x, A->y, A->z, atom + idx);
			idx++;
		}

		A = residue[i].CA;
		if (A != NULL) {

			/* kdCA */
			kd_insert3(kdCA, A->x, A->y, A->z, A);

			/* kdCent - GLY */
			if (residue[i].type == 7 /* GLY */) {
				kd_insert3(kdCent, A->x, A->y, A->z, residue[i].atom);
			}

		} else {
			//printf("WARNING: Missing CA atom in %s-%d\n", residues[i].name,
			//		residues[i].seqNum);
		}

		/* kdCent - all other residues */
		if (residue[i].type != 7) {
			kd_insert(kdCent, residue[i].centroid, residue[i].atom);
		}

	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, 0x50(%rax)
je 0x1a0e8
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rdi
callq 0x31450
movq -0x28(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x1a100
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x31450
movq -0x28(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x1a118
movq -0x28(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x31450
movl $0x3, %edi
callq 0x313e0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x50(%rax)
movl $0x3, %edi
callq 0x313e0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x58(%rax)
movl $0x3, %edi
callq 0x313e0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x60(%rax)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movq -0x28(%rbp), %rcx
movl -0x18(%rbp), %eax
cmpl 0x48(%rcx), %eax
jge 0x1a32c
movl $0x0, -0x1c(%rbp)
movq -0x28(%rbp), %rcx
movl -0x1c(%rbp), %eax
movq 0x40(%rcx), %rcx
movslq -0x18(%rbp), %rdx
imulq $0x188, %rdx, %rdx      # imm = 0x188
addq %rdx, %rcx
cmpl 0x18(%rcx), %eax
jge 0x1a20d
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rcx
movslq -0x18(%rbp), %rdx
imulq $0x188, %rdx, %rdx      # imm = 0x188
addq %rdx, %rcx
movq 0x10(%rcx), %rcx
movslq -0x1c(%rbp), %rdx
imulq $0x60, %rdx, %rdx
addq %rdx, %rcx
movq %rcx, -0x10(%rbp)
movq 0x50(%rax), %rdi
movq -0x10(%rbp), %rcx
movsd 0x10(%rcx), %xmm0
movq -0x10(%rbp), %rcx
movsd 0x18(%rcx), %xmm1
movq -0x10(%rbp), %rcx
movsd 0x20(%rcx), %xmm2
movq 0x30(%rax), %rsi
movslq -0x14(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x31a70
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1a17c
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x18(%rbp), %rcx
imulq $0x188, %rcx, %rcx      # imm = 0x188
addq %rcx, %rax
movq 0x168(%rax), %rax
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x1a2c3
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x10(%rbp), %rax
movsd 0x10(%rax), %xmm0
movq -0x10(%rbp), %rax
movsd 0x18(%rax), %xmm1
movq -0x10(%rbp), %rax
movsd 0x20(%rax), %xmm2
movq -0x10(%rbp), %rsi
callq 0x31a70
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x18(%rbp), %rcx
imulq $0x188, %rcx, %rcx      # imm = 0x188
addq %rcx, %rax
movsbl (%rax), %eax
cmpl $0x7, %eax
jne 0x1a2c1
movq -0x28(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x10(%rbp), %rcx
movsd 0x10(%rcx), %xmm0
movq -0x10(%rbp), %rcx
movsd 0x18(%rcx), %xmm1
movq -0x10(%rbp), %rcx
movsd 0x20(%rcx), %xmm2
movq 0x40(%rax), %rax
movslq -0x18(%rbp), %rcx
imulq $0x188, %rcx, %rcx      # imm = 0x188
addq %rcx, %rax
movq 0x10(%rax), %rsi
callq 0x31a70
jmp 0x1a2c5
jmp 0x1a2c5
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x18(%rbp), %rcx
imulq $0x188, %rcx, %rcx      # imm = 0x188
addq %rcx, %rax
movsbl (%rax), %eax
cmpl $0x7, %eax
je 0x1a31c
movq -0x28(%rbp), %rax
movq 0x60(%rax), %rdi
movq 0x40(%rax), %rsi
movslq -0x18(%rbp), %rcx
imulq $0x188, %rcx, %rcx      # imm = 0x188
addq %rcx, %rsi
addq $0x20, %rsi
movq 0x40(%rax), %rax
movslq -0x18(%rbp), %rcx
imulq $0x188, %rcx, %rcx      # imm = 0x188
addq %rcx, %rax
movq 0x10(%rax), %rdx
callq 0x315b0
jmp 0x1a31e
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1a165
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)