// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1f8, %rsp            # imm = 0x1F8
movq %rsi, %r14
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
leaq 0x70(%rsp), %rsi
andl $0x0, (%rsi)
callq 0xfdba
movq 0x8(%r14), %rax
subq (%r14), %rax
pushq $0x60
popq %rcx
cqto
idivq %rcx
movl %eax, 0x38(%rbx)
pushq $0x1
popq %rcx
leaq 0x40(%rsp), %r15
leaq 0x70(%rsp), %r12
leaq 0x20(%rsp), %r13
leaq 0x1c(%rsp), %rbp
movl %ecx, 0x70(%rsp)
cmpl %eax, %ecx
jge 0xe3ac
movslq %ecx, %rsi
movq (%r14), %rdx
imulq $0x60, %rsi, %rsi
movb 0x14(%rdx,%rsi), %dil
cmpb -0x4c(%rdx,%rsi), %dil
jne 0xe381
addq %rsi, %rdx
movl 0x10(%rdx), %esi
cmpl -0x50(%rdx), %esi
je 0xe3a8
movq %r15, %rdi
movq %r12, %rsi
callq 0xf51c
movl 0x70(%rsp), %eax
decl %eax
movl %eax, 0x1c(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0xfdba
movl 0x70(%rsp), %ecx
movl 0x38(%rbx), %eax
incl %ecx
jmp 0xe358
decl %eax
leaq 0x70(%rsp), %rsi
movl %eax, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0xfdba
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %r15
movq %rax, 0x68(%rsp)
subq %rax, %r15
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x60(%rsp)
subq %rcx, %rax
cmpq %rax, %r15
jne 0xe67b
sarq $0x2, %r15
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x2170
movq %rax, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
andl $0x0, 0x48(%rbx)
xorl %r12d, %r12d
leaq 0xc25b(%rip), %r13      # 0x1a66e
pushq $0x3
popq %rbp
xorl %eax, %eax
movq %r14, 0x38(%rsp)
movq %r15, 0x58(%rsp)
movl %eax, %ebx
cmpq %rbx, %r15
jbe 0xe4a1
movl %r12d, 0x18(%rsp)
movq 0x68(%rsp), %rax
movslq (%rax,%rbx,4), %r12
movq 0x60(%rsp), %rax
movslq (%rax,%rbx,4), %r15
movq (%r14), %rax
imulq $0x60, %r12, %rcx
leaq (%rax,%rcx), %r14
addq $0x4, %r14
cmpq %r15, %r12
jg 0xe478
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x2230
testl %eax, %eax
jne 0xe46f
movq 0x10(%rsp), %rax
movb $0x1, (%rax,%rbx)
incq %r12
addq $0x60, %r14
jmp 0xe44f
movq 0x10(%rsp), %rax
movzbl (%rax,%rbx), %eax
movl 0x18(%rsp), %r12d
addl %eax, %r12d
movq 0x8(%rsp), %rax
movl %r12d, 0x48(%rax)
leal 0x1(%rbx), %eax
movq 0x38(%rsp), %r14
movq 0x58(%rsp), %r15
jmp 0xe422
movl %r12d, %r14d
imulq $0x188, %r14, %rbp      # imm = 0x188
addq $0x8, %rbp
movq %rbp, %rdi
callq 0x2030
movq %rax, %r13
movq %r14, (%rax)
leaq 0x8(%rax), %rbx
testl %r12d, %r12d
je 0xe4ee
imulq $-0x188, %r14, %r15     # imm = 0xFE78
xorl %r12d, %r12d
movq %rbx, %r14
movq %r14, %rdi
callq 0x12d0c
addq $-0x188, %r12           # imm = 0xFE78
addq $0x188, %r14            # imm = 0x188
cmpq %r12, %r15
jne 0xe4d3
movq 0x8(%rsp), %rax
movq %rbx, 0x40(%rax)
movq %rax, %rbx
andl $0x0, 0x38(%rax)
xorl %r13d, %r13d
leaq 0x70(%rsp), %r12
xorl %ebp, %ebp
xorl %r14d, %r14d
movq 0x8(%rsp), %r15
movl %r14d, %r14d
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r14, %rcx
jbe 0xe59f
movq 0x10(%rsp), %rcx
cmpb $0x1, (%rcx,%r14)
jne 0xe597
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
movslq (%rax,%r14,4), %rax
imulq $0x60, %rax, %rsi
addq %rcx, %rsi
movq 0x20(%rsp), %rax
movslq (%rax,%r14,4), %rax
imulq $0x60, %rax, %rdx
addq %rcx, %rdx
movq %r12, %rdi
callq 0x133fc
movslq %ebp, %rax
imulq $0x188, %rax, %rbx      # imm = 0x188
movq 0x40(%r15), %rdi
addq %rbx, %rdi
movq %r12, %rsi
callq 0x138e4
movq %r12, %rdi
callq 0x13872
movq 0x40(%r15), %rax
movl 0x38(%r15), %r13d
addl 0x18(%rax,%rbx), %r13d
movl %r13d, 0x38(%r15)
incl %ebp
movq %r15, %rbx
incl %r14d
jmp 0xe510
movq 0x10(%rsp), %rdi
callq 0x2260
movslq %r13d, %rax
leaq (,%rax,8), %rcx
testl %eax, %eax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
callq 0x2030
movq %rax, 0x30(%rbx)
movq %rbx, %rdi
callq 0xe884
movq %rbx, %rdi
callq 0xe8e4
movslq 0x48(%rbx), %r15
movq %r15, %rdi
shlq $0x3, %rdi
callq 0x22d0
movq %rax, %r14
movq %rax, 0x68(%rbx)
xorl %ebx, %ebx
testl %r15d, %r15d
movl $0x0, %r12d
cmovgl %r15d, %r12d
movl $0x168, %r13d           # imm = 0x168
pushq $0x18
popq %r15
cmpq %rbx, %r12
je 0xe655
movq %r15, %rdi
callq 0x22d0
movq %rax, (%r14,%rbx,8)
movq 0x8(%rsp), %rcx
movq 0x40(%rcx), %rax
movq 0x68(%rcx), %r14
movq (%rax,%r13), %rax
movsd 0x10(%rax), %xmm0
movq (%r14,%rbx,8), %rcx
movsd %xmm0, (%rcx)
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x8(%rcx)
movsd 0x20(%rax), %xmm0
movsd %xmm0, 0x10(%rcx)
incq %rbx
addq $0x188, %r13            # imm = 0x188
jmp 0xe606
leaq 0x20(%rsp), %rdi
callq 0xfd86
leaq 0x40(%rsp), %rdi
callq 0xfd86
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa4f0(%rip), %rdi      # 0x18b72
leaq 0xa505(%rip), %rsi      # 0x18b8e
leaq 0xa50c(%rip), %rcx      # 0x18b9c
pushq $0x71
popq %rdx
callq 0x2160
jmp 0xe6e7
jmp 0xe6e7
jmp 0xe6e7
jmp 0xe6e7
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x13872
jmp 0xe6ea
jmp 0xe6e7
movq %rax, %rbx
testq %r12, %r12
je 0xe6d8
negq %r12
movq %r13, %r14
addq $-0x180, %r14           # imm = 0xFE80
leaq (%r14,%r12), %rdi
callq 0x13872
addq $-0x188, %r12           # imm = 0xFE78
jne 0xe6c6
movq %r13, %rdi
movq %rbp, %rsi
callq 0x23f0
jmp 0xe6ea
jmp 0xe6e7
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0xfd86
leaq 0x40(%rsp), %rdi
callq 0xfd86
movq %rbx, %rdi
callq 0x23a0