// File: /workspace/github_cpp_makefile_star2_O3/gjoni[P]iPot/src/Chain.cpp
// Function: Chain::InitChain(std::vector<AtomRecord, std::allocator<AtomRecord>>&)
void Chain::InitChain(std::vector<AtomRecord> &atomRecordVector) {

	/* define boundaries for all residues : insCode + resNum */
	std::vector<int> first, last; /* residues boundaries */
	first.push_back(0); /* first atom of the first residue */
	nAtoms = atomRecordVector.size();
	for (int i = 1; i < nAtoms; i++) {
		if (atomRecordVector[i].insCode != atomRecordVector[i - 1].insCode
				|| atomRecordVector[i].resNum
						!= atomRecordVector[i - 1].resNum) {
			first.push_back(i);
			last.push_back(i - 1);
		}
	}
	last.push_back(nAtoms - 1); /* last atom of the last residue */

	assert(first.size() == last.size()); /* unexpectedly :-) */

	/*
	 * TODO: temp. solution for eliminating incomplete residues
	 */
	bool *fl = (bool*) calloc(first.size(), sizeof(bool));
	nRes = 0;
	for (unsigned i = 0; i < first.size(); i++) {
		for (int j = first[i]; j <= last[i]; j++) {
			if (strcmp(atomRecordVector[j].atomName, "CA") == 0) {
				fl[i] = true;
			}
		}
		nRes += fl[i];
	}

	/* initialize residues */
	residue = new Residue[nRes];
	int idx = 0;
	nAtoms = 0;
	for (unsigned i = 0; i < first.size(); i++) {
		if (fl[i]) {
			residue[idx] = Residue(atomRecordVector.begin() + first[i],
					atomRecordVector.begin() + last[i]);
			//nAtoms += last[i] - first[i] + 1;
			nAtoms += residue[idx].nAtoms;
			idx++;
		}
	}
	free(fl);

	/* initialize pointers to atoms */
	atom = new Atom*[nAtoms]; /* array of pointers to atoms */
	SetAtomPointers();

	SetKD();

	/* initialize CA trace */
	ca_trace = (double**) malloc(nRes * sizeof(double*));
	for (int i = 0; i < nRes; i++) {
		ca_trace[i] = (double*) malloc(3 * sizeof(double));
		ca_trace[i][0] = residue[i].CA->x;
		ca_trace[i][1] = residue[i].CA->y;
		ca_trace[i][2] = residue[i].CA->z;
	}

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rsi, %r14
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
movq %rax, 0x10(%rsp)
movaps %xmm0, (%rsp)
leaq 0x60(%rsp), %rdx
movl $0x0, (%rdx)
xorl %esi, %esi
callq 0x1058a
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x5, %rax
imull $0xaaaaaaab, %eax, %eax # imm = 0xAAAAAAAB
movl %eax, 0x38(%rbx)
movl $0x1, 0x60(%rsp)
cmpl $0x2, %eax
jl 0xebcb
movl $0x1, %ecx
leaq 0x20(%rsp), %r15
leaq 0x60(%rsp), %r12
movq %rsp, %r13
leaq 0x4c(%rsp), %rbp
movslq %ecx, %rsi
movq (%r14), %rdx
leaq (%rsi,%rsi,2), %rsi
shlq $0x5, %rsi
movb 0x14(%rdx,%rsi), %dil
cmpb -0x4c(%rdx,%rsi), %dil
jne 0xeb64
addq %rsi, %rdx
movl 0x10(%rdx), %esi
cmpl -0x50(%rdx), %esi
je 0xebbd
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0xeb7d
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rsp)
jmp 0xeb88
movq %r15, %rdi
movq %r12, %rdx
callq 0x106c6
movl 0x60(%rsp), %eax
decl %eax
movl %eax, 0x4c(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0xebab
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rsp)
jmp 0xebb6
movq %r13, %rdi
movq %rbp, %rdx
callq 0x1058a
movl 0x60(%rsp), %ecx
movl 0x38(%rbx), %eax
incl %ecx
movl %ecx, 0x60(%rsp)
cmpl %eax, %ecx
jl 0xeb3f
decl %eax
movl %eax, 0x60(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
movq %r14, 0x58(%rsp)
je 0xebef
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rsp)
jmp 0xec01
movq %rsp, %rdi
leaq 0x60(%rsp), %rdx
callq 0x1058a
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %r14
movq %r14, %r12
subq %r13, %r12
movq (%rsp), %rbp
subq %rbp, %rsi
cmpq %rsi, %r12
jne 0xeecf
sarq $0x2, %r12
movl $0x1, %esi
movq %r12, %rdi
callq 0x2170
movq %rax, %r15
movl $0x0, 0x48(%rbx)
movq %r14, 0x40(%rsp)
cmpq %r13, %r14
je 0xecad
movq 0x58(%rsp), %r14
movq (%r14), %rax
addq $0x4, %rax
xorl %edx, %edx
xorl %ecx, %ecx
movslq (%r13,%rdx,4), %r8
movl (%rbp,%rdx,4), %esi
cmpl %esi, %r8d
jg 0xec97
leaq (%r8,%r8,2), %rdi
shlq $0x5, %rdi
addq %rax, %rdi
subl %r8d, %esi
incl %esi
movzwl (%rdi), %r8d
xorl $0x4143, %r8d           # imm = 0x4143
movzbl 0x2(%rdi), %r9d
orw %r8w, %r9w
jne 0xec8f
movb $0x1, (%r15,%rdx)
addq $0x60, %rdi
decl %esi
jne 0xec74
movzbl (%r15,%rdx), %esi
addl %esi, %ecx
movl %ecx, 0x48(%rbx)
incq %rdx
movl %edx, %esi
cmpq %rsi, %r12
ja 0xec56
jmp 0xecaf
xorl %ecx, %ecx
movslq %ecx, %rbp
movl $0x188, %ecx            # imm = 0x188
movq %rbp, %rax
mulq %rcx
movq %rax, %r12
seto %al
addq $0x8, %r12
movq $-0x1, %rcx
cmovbq %rcx, %r12
testb %al, %al
cmovneq %rcx, %r12
movq %r12, %rdi
callq 0x2030
movq %rbp, (%rax)
movq %rax, 0x50(%rsp)
addq $0x8, %rax
testl %ebp, %ebp
movq 0x40(%rsp), %rdx
je 0xed31
imulq $-0x188, %rbp, %r13     # imm = 0xFE78
xorl %r14d, %r14d
movq %rax, 0x40(%rsp)
movq %rax, %rbp
movq %rbp, %rdi
callq 0x13ae8
addq $-0x188, %r14           # imm = 0xFE78
addq $0x188, %rbp            # imm = 0x188
cmpq %r14, %r13
jne 0xed07
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rdx
movq 0x40(%rsp), %rax
movq %rax, 0x40(%rbx)
movl $0x0, 0x38(%rbx)
xorl %r14d, %r14d
cmpq %r13, %rdx
je 0xede7
movl $0x1, %ebp
xorl %eax, %eax
xorl %r12d, %r12d
cmpb $0x1, (%r15,%rax)
jne 0xedd0
movq 0x58(%rsp), %rcx
movq (%rcx), %rcx
movslq (%r13,%rax,4), %rdx
leaq (%rdx,%rdx,2), %rsi
shlq $0x5, %rsi
addq %rcx, %rsi
movq (%rsp), %rdx
movslq (%rdx,%rax,4), %rax
leaq (%rax,%rax,2), %rdx
shlq $0x5, %rdx
addq %rcx, %rdx
leaq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x14288
movslq %r12d, %rax
imulq $0x188, %rax, %r13      # imm = 0x188
movq 0x40(%rbx), %rdi
addq %r13, %rdi
movq %r14, %rsi
callq 0x148ee
movq %r14, %rdi
callq 0x1486e
movq 0x40(%rbx), %rax
movl 0x38(%rbx), %r14d
addl 0x18(%rax,%r13), %r14d
movl %r14d, 0x38(%rbx)
incl %r12d
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rdx
movl %ebp, %eax
movq %rdx, %rcx
subq %r13, %rcx
sarq $0x2, %rcx
incl %ebp
cmpq %rax, %rcx
ja 0xed52
movq %r15, %rdi
callq 0x2260
movslq %r14d, %rax
leaq (,%rax,8), %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x2030
movq %rax, 0x30(%rbx)
movq %rbx, %rdi
callq 0xf12a
movq %rbx, %rdi
callq 0xf18e
movslq 0x48(%rbx), %r15
leaq (,%r15,8), %rdi
callq 0x22c0
movq %rax, 0x68(%rbx)
testq %r15, %r15
jle 0xee90
movq %rax, %r14
movl $0x168, %r12d           # imm = 0x168
xorl %r13d, %r13d
movl $0x18, %edi
callq 0x22c0
movq %rax, (%r14,%r13,8)
movq 0x40(%rbx), %rax
movq 0x68(%rbx), %r14
movq (%rax,%r12), %rax
movsd 0x10(%rax), %xmm0
movq (%r14,%r13,8), %rcx
movsd %xmm0, (%rcx)
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x8(%rcx)
movsd 0x20(%rax), %xmm0
movsd %xmm0, 0x10(%rcx)
incq %r13
addq $0x188, %r12            # imm = 0x188
cmpq %r13, %r15
jne 0xee46
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xeea6
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xeebd
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbc9c(%rip), %rdi      # 0x1ab72
leaq 0xbcb1(%rip), %rsi      # 0x1ab8e
leaq 0xbcb8(%rip), %rcx      # 0x1ab9c
movl $0x71, %edx
callq 0x2160
jmp 0xeefa
jmp 0xeefa
jmp 0xeefa
jmp 0xeefa
jmp 0xeefa
jmp 0xeefa
movq %rax, %rbx
jmp 0xef44
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x1486e
jmp 0xef44
movq %rax, %rbx
testq %r14, %r14
je 0xef37
negq %r14
movq 0x50(%rsp), %r15
addq $-0x180, %r15           # imm = 0xFE80
leaq (%r15,%r14), %rdi
callq 0x1486e
addq $-0x188, %r14           # imm = 0xFE78
jne 0xef25
movq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x23c0
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xef5a
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xef71
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
movq %rbx, %rdi
callq 0x2380
nop