// File: 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
movq %rsp, %rbp
subq $0x2b0, %rsp            # imm = 0x2B0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x238(%rbp)
leaq -0x28(%rbp), %rdi
movq %rdi, -0x230(%rbp)
callq 0x1bab0
leaq -0x40(%rbp), %rdi
callq 0x1bab0
movq -0x230(%rbp), %rdi
movl $0x0, -0x44(%rbp)
leaq -0x44(%rbp), %rsi
callq 0x1bad0
jmp 0x19700
movq -0x10(%rbp), %rdi
callq 0x1b8f0
movq %rax, %rcx
movq -0x238(%rbp), %rax
movl %ecx, 0x38(%rax)
movl $0x1, -0x58(%rbp)
movq -0x238(%rbp), %rcx
movl -0x58(%rbp), %eax
cmpl 0x38(%rcx), %eax
jge 0x197e9
movq -0x10(%rbp), %rdi
movslq -0x58(%rbp), %rsi
callq 0x1ba90
movsbl 0x14(%rax), %eax
movl %eax, -0x23c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x58(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x1ba90
movq %rax, %rcx
movl -0x23c(%rbp), %eax
movsbl 0x14(%rcx), %ecx
cmpl %ecx, %eax
jne 0x197a0
movq -0x10(%rbp), %rdi
movslq -0x58(%rbp), %rsi
callq 0x1ba90
movl 0x10(%rax), %eax
movl %eax, -0x240(%rbp)
movq -0x10(%rbp), %rdi
movl -0x58(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x1ba90
movq %rax, %rcx
movl -0x240(%rbp), %eax
cmpl 0x10(%rcx), %eax
je 0x197d9
leaq -0x28(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x1bb00
jmp 0x197af
movl -0x58(%rbp), %eax
decl %eax
movl %eax, -0x5c(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x1bad0
jmp 0x197c6
jmp 0x197d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x19d84
jmp 0x197db
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x1971d
movq -0x238(%rbp), %rax
movl 0x38(%rax), %eax
decl %eax
movl %eax, -0x60(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1bad0
jmp 0x19807
leaq -0x28(%rbp), %rdi
callq 0x1bb70
movq %rax, -0x248(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x1bb70
movq %rax, %rcx
movq -0x248(%rbp), %rax
cmpq %rcx, %rax
jne 0x19831
jmp 0x19850
leaq 0x1b33a(%rip), %rdi     # 0x34b72
leaq 0x1b34f(%rip), %rsi     # 0x34b8e
movl $0x71, %edx
leaq 0x1b351(%rip), %rcx     # 0x34b9c
callq 0x31b0
leaq -0x28(%rbp), %rdi
callq 0x1bb70
movq %rax, %rdi
movl $0x1, %esi
callq 0x31c0
movq %rax, %rcx
movq -0x238(%rbp), %rax
movq %rcx, -0x68(%rbp)
movl $0x0, 0x48(%rax)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq %rax, -0x250(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1bb70
movq %rax, %rcx
movq -0x250(%rbp), %rax
cmpq %rcx, %rax
jae 0x19947
movl -0x6c(%rbp), %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
callq 0x1bb90
movl (%rax), %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
movl %eax, -0x254(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0x1bb90
movq %rax, %rcx
movl -0x254(%rbp), %eax
cmpl (%rcx), %eax
jg 0x1991c
movq -0x10(%rbp), %rdi
movslq -0x70(%rbp), %rsi
callq 0x1ba90
movq %rax, %rdi
addq $0x4, %rdi
leaq 0x1cd50(%rip), %rsi     # 0x3664a
callq 0x33c0
cmpl $0x0, %eax
jne 0x1990f
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
movb $0x1, (%rax,%rcx)
jmp 0x19911
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x198bb
movq -0x238(%rbp), %rax
movq -0x68(%rbp), %rcx
movl -0x6c(%rbp), %edx
movb (%rcx,%rdx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
addl 0x48(%rax), %ecx
movl %ecx, 0x48(%rax)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x19882
movq -0x238(%rbp), %rax
movslq 0x48(%rax), %rax
movq %rax, -0x270(%rbp)
movl $0x188, %ecx            # imm = 0x188
mulq %rcx
movq %rax, %rdi
seto %cl
addq $0x8, %rdi
movq $-0x1, %rax
cmovbq %rax, %rdi
testb %cl, %cl
cmovneq %rax, %rdi
movq %rdi, -0x268(%rbp)
callq 0x3030
movq %rax, -0x260(%rbp)
jmp 0x19991
movq -0x270(%rbp), %rax
movq -0x260(%rbp), %rcx
movq %rax, (%rcx)
addq $0x8, %rcx
movq %rcx, -0x278(%rbp)
cmpq $0x0, %rax
je 0x19a11
movq -0x278(%rbp), %rax
movq -0x270(%rbp), %rcx
imulq $0x188, %rcx, %rdx      # imm = 0x188
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x288(%rbp)
movq %rax, -0x280(%rbp)
movq -0x280(%rbp), %rdi
movq %rdi, -0x290(%rbp)
callq 0x279a0
jmp 0x199f1
movq -0x288(%rbp), %rcx
movq -0x290(%rbp), %rax
addq $0x188, %rax            # imm = 0x188
cmpq %rcx, %rax
movq %rax, -0x280(%rbp)
jne 0x199dc
movq -0x238(%rbp), %rax
movq -0x278(%rbp), %rcx
movq %rcx, 0x40(%rax)
movl $0x0, -0x74(%rbp)
movl $0x0, 0x38(%rax)
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
movq %rax, -0x298(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1bb70
movq %rax, %rcx
movq -0x298(%rbp), %rax
cmpq %rcx, %rax
jae 0x19bf3
movq -0x68(%rbp), %rax
movl -0x78(%rbp), %ecx
testb $0x1, (%rax,%rcx)
je 0x19be3
movq -0x10(%rbp), %rdi
callq 0x1bbb0
movq %rax, -0x210(%rbp)
movl -0x78(%rbp), %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
callq 0x1bb90
movslq (%rax), %rsi
leaq -0x210(%rbp), %rdi
callq 0x1bbe0
movq %rax, -0x208(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1bbb0
movq %rax, -0x220(%rbp)
movl -0x78(%rbp), %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0x1bb90
movslq (%rax), %rsi
leaq -0x220(%rbp), %rdi
callq 0x1bbe0
movq %rax, -0x218(%rbp)
movq -0x208(%rbp), %rsi
movq -0x218(%rbp), %rdx
leaq -0x200(%rbp), %rdi
callq 0x28780
jmp 0x19af3
movq -0x238(%rbp), %rax
movq 0x40(%rax), %rdi
movslq -0x74(%rbp), %rax
imulq $0x188, %rax, %rax      # imm = 0x188
addq %rax, %rdi
leaq -0x200(%rbp), %rsi
callq 0x29350
jmp 0x19b1a
leaq -0x200(%rbp), %rdi
callq 0x29280
movq -0x238(%rbp), %rax
movq 0x40(%rax), %rcx
movslq -0x74(%rbp), %rdx
imulq $0x188, %rdx, %rdx      # imm = 0x188
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
addl 0x38(%rax), %ecx
movl %ecx, 0x38(%rax)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x19be3
movq -0x278(%rbp), %rcx
movq %rax, %rsi
movq -0x290(%rbp), %rax
movq %rsi, -0x50(%rbp)
movl %edx, -0x54(%rbp)
cmpq %rax, %rcx
movq %rax, -0x2a0(%rbp)
je 0x19bae
movq -0x2a0(%rbp), %rdi
addq $-0x188, %rdi           # imm = 0xFE78
movq %rdi, -0x2a8(%rbp)
callq 0x29280
movq -0x278(%rbp), %rcx
movq -0x2a8(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x2a0(%rbp)
jne 0x19b7a
movq -0x268(%rbp), %rsi
movq -0x260(%rbp), %rdi
callq 0x34a0
jmp 0x19d84
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x29280
jmp 0x19d84
jmp 0x19be5
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x19a38
movq -0x68(%rbp), %rdi
callq 0x3320
movq -0x238(%rbp), %rax
movslq 0x38(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x3030
movq %rax, -0x2b0(%rbp)
jmp 0x19c2b
movq -0x238(%rbp), %rdi
movq -0x2b0(%rbp), %rax
movq %rax, 0x30(%rdi)
callq 0x1a010
jmp 0x19c44
movq -0x238(%rbp), %rdi
callq 0x1a0c0
jmp 0x19c52
movq -0x238(%rbp), %rax
movslq 0x48(%rax), %rdi
shlq $0x3, %rdi
callq 0x3390
movq %rax, %rcx
movq -0x238(%rbp), %rax
movq %rcx, 0x68(%rax)
movl $0x0, -0x224(%rbp)
movq -0x238(%rbp), %rcx
movl -0x224(%rbp), %eax
cmpl 0x48(%rcx), %eax
jge 0x19d69
movl $0x18, %edi
callq 0x3390
movq %rax, %rsi
movq -0x238(%rbp), %rax
movq 0x68(%rax), %rcx
movslq -0x224(%rbp), %rdx
movq %rsi, (%rcx,%rdx,8)
movq 0x40(%rax), %rcx
movslq -0x224(%rbp), %rdx
imulq $0x188, %rdx, %rdx      # imm = 0x188
addq %rdx, %rcx
movq 0x168(%rcx), %rcx
movsd 0x10(%rcx), %xmm0
movq 0x68(%rax), %rcx
movslq -0x224(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsd %xmm0, (%rcx)
movq 0x40(%rax), %rcx
movslq -0x224(%rbp), %rdx
imulq $0x188, %rdx, %rdx      # imm = 0x188
addq %rdx, %rcx
movq 0x168(%rcx), %rcx
movsd 0x18(%rcx), %xmm0
movq 0x68(%rax), %rcx
movslq -0x224(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsd %xmm0, 0x8(%rcx)
movq 0x40(%rax), %rcx
movslq -0x224(%rbp), %rdx
imulq $0x188, %rdx, %rdx      # imm = 0x188
addq %rdx, %rcx
movq 0x168(%rcx), %rcx
movsd 0x20(%rcx), %xmm0
movq 0x68(%rax), %rax
movslq -0x224(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsd %xmm0, 0x10(%rax)
movl -0x224(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x224(%rbp)
jmp 0x19c7e
leaq -0x40(%rbp), %rdi
callq 0x1bc20
leaq -0x28(%rbp), %rdi
callq 0x1bc20
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x1bc20
leaq -0x28(%rbp), %rdi
callq 0x1bc20
movq -0x50(%rbp), %rdi
callq 0x3460
nop