// File: gjoni[P]iPot/src/RRCE.cpp
// Function: RRCE::GetEnergy(Complex const&)
double RRCE::GetEnergy(const Complex &C) {

	double E = 0;

	for (int i = 0; i < C.Receptor.nRes; i++) {

		char ti = C.Receptor.residue[i].type;
		if (ti < 0 || ti > 19) {
			continue;
		}

		Residue *R = &(C.Receptor.residue[i]);
		double *ci = R->centroid;

		/* check whether the current atom is within the bounding box
		 * of the Receptor */
		kdtree *kd = C.Ligand.kdCent;
		if (ci[0] < kd->rect->min[0] - dmax || ci[1] < kd->rect->min[1] - dmax
				|| ci[2] < kd->rect->min[2] - dmax
				|| ci[0] > kd->rect->max[0] + dmax
				|| ci[1] > kd->rect->max[1] + dmax
				|| ci[2] > kd->rect->max[2] + dmax) {

			continue;

		}

		kdres *res = kd_nearest_range3f(kd, ci[0], ci[1], ci[2], dmax);
		double pos[3];

		while (!kd_res_end(res)) {

			Atom *Aj = (Atom*) kd_res_item(res, pos);
			char tj = Aj->residue->type;

			if (tj >= 0 && tj < 20) {
				E += J[(int) ti][(int) tj];
			}
			kd_res_next(res);
		}
		kd_res_free(res);
	}

	return E;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x78(%rcx), %eax
jge 0x27992
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x188, %rcx, %rcx      # imm = 0x188
addq %rcx, %rax
movb (%rax), %al
movb %al, -0x1d(%rbp)
movsbl -0x1d(%rbp), %eax
cmpl $0x0, %eax
jl 0x27787
movsbl -0x1d(%rbp), %eax
cmpl $0x13, %eax
jle 0x2778c
jmp 0x27984
movq -0x78(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x70(%rcx), %rcx
movslq -0x1c(%rbp), %rdx
imulq $0x188, %rdx, %rdx      # imm = 0x188
addq %rdx, %rcx
movq %rcx, -0x28(%rbp)
movq -0x28(%rbp), %rcx
addq $0x20, %rcx
movq %rcx, -0x30(%rbp)
movq -0x10(%rbp), %rcx
movq 0x100(%rcx), %rcx
movq %rcx, -0x38(%rbp)
movq -0x30(%rbp), %rcx
movsd (%rcx), %xmm1
movq -0x38(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq 0x8(%rcx), %rcx
movsd (%rcx), %xmm0
subsd 0x10(%rax), %xmm0
ucomisd %xmm1, %xmm0
ja 0x278bb
movq -0x78(%rbp), %rax
movq -0x30(%rbp), %rcx
movsd 0x8(%rcx), %xmm1
movq -0x38(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq 0x8(%rcx), %rcx
movsd 0x8(%rcx), %xmm0
subsd 0x10(%rax), %xmm0
ucomisd %xmm1, %xmm0
ja 0x278bb
movq -0x78(%rbp), %rax
movq -0x30(%rbp), %rcx
movsd 0x10(%rcx), %xmm1
movq -0x38(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq 0x8(%rcx), %rcx
movsd 0x10(%rcx), %xmm0
subsd 0x10(%rax), %xmm0
ucomisd %xmm1, %xmm0
ja 0x278bb
movq -0x78(%rbp), %rax
movq -0x30(%rbp), %rcx
movsd (%rcx), %xmm0
movq -0x38(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq 0x10(%rcx), %rcx
movsd (%rcx), %xmm1
addsd 0x10(%rax), %xmm1
ucomisd %xmm1, %xmm0
ja 0x278bb
movq -0x78(%rbp), %rax
movq -0x30(%rbp), %rcx
movsd 0x8(%rcx), %xmm0
movq -0x38(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq 0x10(%rcx), %rcx
movsd 0x8(%rcx), %xmm1
addsd 0x10(%rax), %xmm1
ucomisd %xmm1, %xmm0
ja 0x278bb
movq -0x78(%rbp), %rax
movq -0x30(%rbp), %rcx
movsd 0x10(%rcx), %xmm0
movq -0x38(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq 0x10(%rcx), %rcx
movsd 0x10(%rcx), %xmm1
addsd 0x10(%rax), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x278c0
jmp 0x27984
movq -0x78(%rbp), %rax
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rcx
movsd (%rcx), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x30(%rbp), %rcx
movsd 0x8(%rcx), %xmm1
cvtsd2ss %xmm1, %xmm1
movq -0x30(%rbp), %rcx
movsd 0x10(%rcx), %xmm2
cvtsd2ss %xmm2, %xmm2
movsd 0x10(%rax), %xmm3
cvtsd2ss %xmm3, %xmm3
callq 0x32730
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x32820
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x27917
jmp 0x2797b
movq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x32870
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq 0x40(%rax), %rax
movb (%rax), %al
movb %al, -0x69(%rbp)
movsbl -0x69(%rbp), %eax
cmpl $0x0, %eax
jl 0x27970
movsbl -0x69(%rbp), %eax
cmpl $0x14, %eax
jge 0x27970
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rax
movsbl -0x1d(%rbp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movsbl -0x69(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x18(%rbp)
movq -0x40(%rbp), %rdi
callq 0x32840
jmp 0x27900
movq -0x40(%rbp), %rdi
callq 0x31d50
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2774a
movsd -0x18(%rbp), %xmm0
addq $0x80, %rsp
popq %rbp
retq