// File: CK7[P]abawaca/src/abawaca-build.cpp
// Function: Links::add_pair(std::shared_ptr<Bio::ReadMapping>, std::shared_ptr<Bio::ReadMapping>)
void Links::add_pair(Bio::ReadMappingPtr mapping1, Bio::ReadMappingPtr mapping2)
{
	auto scafit1 = mapping1->unmapped()? scafs.end() : scafs.find(mapping1->ref_name());
	auto scafit2 = mapping2->unmapped()? scafs.end() : scafs.find(mapping2->ref_name());

	// Either both are unmapped or both are mapped to the same sequence
	if((scafit1 == scafs.end()) && (scafit2 == scafs.end()))
		return;

	size_t end1 = 0;
	if(scafit1 != scafs.end()) {
		if((mapping1->ref_pos() < this->insert_size) && (mapping1->direction() == Bio::ReadMapping::reverse))
			end1 = 5;
		else if((mapping1->ref_pos() > scafit1->second->get_seq().size()-this->insert_size) && (mapping1->direction() == Bio::ReadMapping::forward))
			end1 = 3;
	}

	size_t end2 = 0;
	if(scafit2 != scafs.end()) {
		if((mapping2->ref_pos() < this->insert_size) && (mapping2->direction() == Bio::ReadMapping::reverse))
			end2 = 5;
		else if((mapping2->ref_pos() > scafit2->second->get_seq().size()-this->insert_size) && (mapping2->direction() == Bio::ReadMapping::forward))
			end2 = 3;
	}

	// If both reads fall into the same read this is only interesting if they connect opposite ends
	if((scafit1 == scafit2) && ((end1 == end2) || (end1 == 0) || (end2 == 0)))
		return;

	// None of the reads is mapped to the ends
	if((end1 == 0) && (end2 == 0))
		return;

	Links::Side	side1((end1 != 0)? scafit1->first : "", end1), side2((end2 != 0)? scafit2->first : "", end2);
	if((end1 != 0) && (links.find(side1) == links.end())) {
		links.insert(pair<Links::Side, map<Links::Side, Links::Link*> >(side1, map<Links::Side, Links::Link*>()));
	}
	if((end2 != 0) && (links.find(side2) == links.end())) {
		links.insert(pair<Links::Side, map<Links::Side, Links::Link*> >(side2, map<Links::Side, Links::Link*>()));
	}

	auto		it = (end1 == 0)? links.find(side2) : links.find(side1);
	auto		itm = (end1 == 0)? it->second.find(side1) : it->second.find(side2);

	if(itm == it->second.end()) {
		Links::Link* link = new Links::Link(side1, side2);
		if(end1 != 0) {
			it = links.find(side1);
			it->second.insert(pair<Links::Side, Links::Link*>(side2, link));
			itm = it->second.find(side2);
		}
		if(end2 != 0) {
			it = links.find(side2);
			it->second.insert(pair<Links::Side, Links::Link*>(side1, link));
			itm = it->second.find(side1);
		}
	}
	itm->second->count++;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x450, %rsp            # imm = 0x450
movq %rsi, -0x340(%rbp)
movq %rdi, %rax
movq -0x340(%rbp), %rdi
movq %rdi, -0x338(%rbp)
movq %rdx, -0x330(%rbp)
movq %rax, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x328(%rbp)
callq 0x111b0
movq %rax, %rdi
callq 0x111d0
testb $0x1, %al
jne 0xca34
jmp 0xca4a
movq -0x328(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x111f0
movq %rax, -0x20(%rbp)
jmp 0xca83
movq -0x338(%rbp), %rdi
movq -0x328(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x348(%rbp)
callq 0x111b0
movq %rax, %rdi
callq 0x11250
movq -0x348(%rbp), %rdi
movq %rax, %rsi
callq 0x11220
movq %rax, -0x20(%rbp)
movq -0x330(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x111d0
testb $0x1, %al
jne 0xca9d
jmp 0xcab3
movq -0x328(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x111f0
movq %rax, -0x28(%rbp)
jmp 0xcaec
movq -0x330(%rbp), %rdi
movq -0x328(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x350(%rbp)
callq 0x111b0
movq %rax, %rdi
callq 0x11250
movq -0x350(%rbp), %rdi
movq %rax, %rsi
callq 0x11220
movq %rax, -0x28(%rbp)
movq -0x328(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x111f0
movq %rax, -0x30(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x11270
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x351(%rbp)
jne 0xcb1e
jmp 0xcb45
movq -0x328(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x111f0
movq %rax, -0x38(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x11270
movb %al, -0x351(%rbp)
movb -0x351(%rbp), %al
testb $0x1, %al
jne 0xcb51
jmp 0xcb56
jmp 0xd632
movq -0x328(%rbp), %rax
movq $0x0, -0x40(%rbp)
movq 0x38(%rax), %rdi
callq 0x111f0
movq %rax, -0x48(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x112a0
testb $0x1, %al
jne 0xcb88
jmp 0xcc3e
movq -0x338(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x10680
movq -0x328(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xcbcb
movq -0x338(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x112d0
cmpl $0x1, %eax
jne 0xcbcb
movq $0x5, -0x40(%rbp)
jmp 0xcc3c
movq -0x338(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x10680
movq %rax, -0x360(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x11300
movq 0x20(%rax), %rdi
callq 0x11320
movq %rax, %rdi
callq 0x7020
movq -0x328(%rbp), %rdx
movq %rax, %rcx
movq -0x360(%rbp), %rax
subq (%rdx), %rcx
cmpq %rcx, %rax
jbe 0xcc3a
movq -0x338(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x112d0
cmpl $0x0, %eax
jne 0xcc3a
movq $0x3, -0x40(%rbp)
jmp 0xcc3c
jmp 0xcc3e
movq -0x328(%rbp), %rax
movq $0x0, -0x50(%rbp)
movq 0x38(%rax), %rdi
callq 0x111f0
movq %rax, -0x58(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x112a0
testb $0x1, %al
jne 0xcc70
jmp 0xcd26
movq -0x330(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x10680
movq -0x328(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xccb3
movq -0x330(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x112d0
cmpl $0x1, %eax
jne 0xccb3
movq $0x5, -0x50(%rbp)
jmp 0xcd24
movq -0x330(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x10680
movq %rax, -0x368(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x11300
movq 0x20(%rax), %rdi
callq 0x11320
movq %rax, %rdi
callq 0x7020
movq -0x328(%rbp), %rdx
movq %rax, %rcx
movq -0x368(%rbp), %rax
subq (%rdx), %rcx
cmpq %rcx, %rax
jbe 0xcd22
movq -0x330(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x112d0
cmpl $0x0, %eax
jne 0xcd22
movq $0x3, -0x50(%rbp)
jmp 0xcd24
jmp 0xcd26
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x11270
testb $0x1, %al
jne 0xcd39
jmp 0xcd56
movq -0x40(%rbp), %rax
cmpq -0x50(%rbp), %rax
je 0xcd51
cmpq $0x0, -0x40(%rbp)
je 0xcd51
cmpq $0x0, -0x50(%rbp)
jne 0xcd56
jmp 0xd632
cmpq $0x0, -0x40(%rbp)
jne 0xcd69
cmpq $0x0, -0x50(%rbp)
jne 0xcd69
jmp 0xd632
movq -0x40(%rbp), %rax
movb $0x0, -0xa2(%rbp)
cmpq $0x0, %rax
je 0xcd94
leaq -0x20(%rbp), %rdi
callq 0x11300
movq %rax, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x4140
jmp 0xcdcc
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x370(%rbp)
callq 0x45d0
movq -0x370(%rbp), %rdx
movb $0x1, -0xa2(%rbp)
leaq 0x154b0(%rip), %rsi     # 0x2226c
leaq -0xa0(%rbp), %rdi
callq 0x4470
jmp 0xcdca
jmp 0xcdcc
movq -0x40(%rbp), %rdx
leaq -0x80(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x11340
jmp 0xcde2
leaq -0xa0(%rbp), %rdi
callq 0x41c0
testb $0x1, -0xa2(%rbp)
jne 0xcdf9
jmp 0xce05
leaq -0xa1(%rbp), %rdi
callq 0x4370
movq -0x50(%rbp), %rax
movb $0x0, -0x102(%rbp)
cmpq $0x0, %rax
je 0xce32
leaq -0x28(%rbp), %rdi
callq 0x11300
movq %rax, %rsi
leaq -0x100(%rbp), %rdi
callq 0x4140
jmp 0xce30
jmp 0xce6a
leaq -0x101(%rbp), %rdi
movq %rdi, -0x378(%rbp)
callq 0x45d0
movq -0x378(%rbp), %rdx
movb $0x1, -0x102(%rbp)
leaq 0x15412(%rip), %rsi     # 0x2226c
leaq -0x100(%rbp), %rdi
callq 0x4470
jmp 0xce68
jmp 0xce6a
movq -0x50(%rbp), %rdx
leaq -0xe0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x11340
jmp 0xce83
leaq -0x100(%rbp), %rdi
callq 0x41c0
testb $0x1, -0x102(%rbp)
jne 0xce9a
jmp 0xcea6
leaq -0x101(%rbp), %rdi
callq 0x4370
xorl %eax, %eax
cmpq $0x0, -0x40(%rbp)
movb %al, -0x379(%rbp)
je 0xcf10
movq -0x328(%rbp), %rdi
addq $0x8, %rdi
leaq -0x80(%rbp), %rsi
callq 0x113b0
movq %rax, -0x388(%rbp)
jmp 0xced2
movq -0x328(%rbp), %rdi
movq -0x388(%rbp), %rax
movq %rax, -0x110(%rbp)
addq $0x8, %rdi
callq 0x10c80
movq %rax, -0x118(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x11380
movb %al, -0x379(%rbp)
movb -0x379(%rbp), %al
testb $0x1, %al
jne 0xcf1f
jmp 0xd0db
movq -0x328(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x398(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x180(%rbp)
movaps %xmm0, -0x190(%rbp)
movaps %xmm0, -0x1a0(%rbp)
leaq -0x1a0(%rbp), %rdi
movq %rdi, -0x390(%rbp)
callq 0x11420
movq -0x390(%rbp), %rdx
leaq -0x170(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x11440
jmp 0xcf75
movq -0x398(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x113e0
movb %dl, -0x3a1(%rbp)
movq %rax, -0x3a0(%rbp)
jmp 0xcf97
movb -0x3a1(%rbp), %al
movq -0x3a0(%rbp), %rcx
movq %rcx, -0x1b0(%rbp)
movb %al, -0x1a8(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x11480
leaq -0x1a0(%rbp), %rdi
callq 0x114b0
jmp 0xd0db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0xd000
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x41c0
testb $0x1, -0xa2(%rbp)
jne 0xd00b
jmp 0xd017
leaq -0xa1(%rbp), %rdi
callq 0x4370
jmp 0xd650
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0xd647
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0xd065
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x41c0
testb $0x1, -0x102(%rbp)
jne 0xd070
jmp 0xd07c
leaq -0x101(%rbp), %rdi
callq 0x4370
jmp 0xd647
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0xd63b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0xd0ca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x11480
leaq -0x1a0(%rbp), %rdi
callq 0x114b0
jmp 0xd63b
xorl %eax, %eax
cmpq $0x0, -0x50(%rbp)
movb %al, -0x3a2(%rbp)
je 0xd148
movq -0x328(%rbp), %rdi
addq $0x8, %rdi
leaq -0xe0(%rbp), %rsi
callq 0x113b0
movq %rax, -0x3b0(%rbp)
jmp 0xd10a
movq -0x328(%rbp), %rdi
movq -0x3b0(%rbp), %rax
movq %rax, -0x1b8(%rbp)
addq $0x8, %rdi
callq 0x10c80
movq %rax, -0x1c0(%rbp)
leaq -0x1b8(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x11380
movb %al, -0x3a2(%rbp)
movb -0x3a2(%rbp), %al
testb $0x1, %al
jne 0xd157
jmp 0xd249
movq -0x328(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x3c0(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x230(%rbp)
movaps %xmm0, -0x240(%rbp)
movaps %xmm0, -0x250(%rbp)
leaq -0x250(%rbp), %rdi
movq %rdi, -0x3b8(%rbp)
callq 0x11420
movq -0x3b8(%rbp), %rdx
leaq -0x218(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x11440
jmp 0xd1b0
movq -0x3c0(%rbp), %rdi
leaq -0x218(%rbp), %rsi
callq 0x113e0
movb %dl, -0x3c9(%rbp)
movq %rax, -0x3c8(%rbp)
jmp 0xd1d2
movb -0x3c9(%rbp), %al
movq -0x3c8(%rbp), %rcx
movq %rcx, -0x260(%rbp)
movb %al, -0x258(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x11480
leaq -0x250(%rbp), %rdi
callq 0x114b0
jmp 0xd249
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0xd238
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x11480
leaq -0x250(%rbp), %rdi
callq 0x114b0
jmp 0xd63b
cmpq $0x0, -0x40(%rbp)
jne 0xd280
movq -0x328(%rbp), %rdi
addq $0x8, %rdi
leaq -0xe0(%rbp), %rsi
callq 0x113b0
movq %rax, -0x3d8(%rbp)
jmp 0xd270
movq -0x3d8(%rbp), %rax
movq %rax, -0x268(%rbp)
jmp 0xd2ab
movq -0x328(%rbp), %rdi
addq $0x8, %rdi
leaq -0x80(%rbp), %rsi
callq 0x113b0
movq %rax, -0x3e0(%rbp)
jmp 0xd29d
movq -0x3e0(%rbp), %rax
movq %rax, -0x268(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0xd2e7
leaq -0x268(%rbp), %rdi
callq 0x10cb0
movq %rax, %rdi
addq $0x28, %rdi
leaq -0x80(%rbp), %rsi
callq 0x114d0
movq %rax, -0x3e8(%rbp)
jmp 0xd2d7
movq -0x3e8(%rbp), %rax
movq %rax, -0x270(%rbp)
jmp 0xd31d
leaq -0x268(%rbp), %rdi
callq 0x10cb0
movq %rax, %rdi
addq $0x28, %rdi
leaq -0xe0(%rbp), %rsi
callq 0x114d0
movq %rax, -0x3f0(%rbp)
jmp 0xd30f
movq -0x3f0(%rbp), %rax
movq %rax, -0x270(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x10cb0
movq %rax, %rdi
addq $0x28, %rdi
callq 0x10d30
movq %rax, -0x278(%rbp)
leaq -0x270(%rbp), %rdi
leaq -0x278(%rbp), %rsi
callq 0x11500
testb $0x1, %al
jne 0xd358
jmp 0xd601
movl $0x58, %edi
callq 0x4310
movq %rax, -0x3f8(%rbp)
jmp 0xd36b
movq -0x3f8(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
callq 0x11530
jmp 0xd384
movq -0x3f8(%rbp), %rax
movq %rax, -0x280(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0xd4dd
movq -0x328(%rbp), %rdi
addq $0x8, %rdi
leaq -0x80(%rbp), %rsi
callq 0x113b0
movq %rax, -0x400(%rbp)
jmp 0xd3ba
movq -0x400(%rbp), %rax
movq %rax, -0x288(%rbp)
movq -0x288(%rbp), %rax
movq %rax, -0x268(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x10cb0
addq $0x28, %rax
movq %rax, -0x408(%rbp)
leaq -0x2b8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
leaq -0x280(%rbp), %rdx
callq 0x11640
jmp 0xd409
movq -0x408(%rbp), %rdi
leaq -0x2b8(%rbp), %rsi
callq 0x11600
movb %dl, -0x411(%rbp)
movq %rax, -0x410(%rbp)
jmp 0xd42b
movb -0x411(%rbp), %al
movq -0x410(%rbp), %rcx
movq %rcx, -0x2c8(%rbp)
movb %al, -0x2c0(%rbp)
leaq -0x2b8(%rbp), %rdi
callq 0x11680
leaq -0x268(%rbp), %rdi
callq 0x10cb0
movq %rax, %rdi
addq $0x28, %rdi
leaq -0xe0(%rbp), %rsi
callq 0x114d0
movq %rax, -0x420(%rbp)
jmp 0xd479
movq -0x420(%rbp), %rax
movq %rax, -0x2d0(%rbp)
movq -0x2d0(%rbp), %rax
movq %rax, -0x270(%rbp)
jmp 0xd4dd
movq -0x3f8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
callq 0x42c0
jmp 0xd63b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0x2b8(%rbp), %rdi
callq 0x11680
jmp 0xd63b
cmpq $0x0, -0x50(%rbp)
je 0xd5ff
movq -0x328(%rbp), %rdi
addq $0x8, %rdi
leaq -0xe0(%rbp), %rsi
callq 0x113b0
movq %rax, -0x428(%rbp)
jmp 0xd508
movq -0x428(%rbp), %rax
movq %rax, -0x2d8(%rbp)
movq -0x2d8(%rbp), %rax
movq %rax, -0x268(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x10cb0
addq $0x28, %rax
movq %rax, -0x430(%rbp)
leaq -0x308(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0x280(%rbp), %rdx
callq 0x11640
jmp 0xd554
movq -0x430(%rbp), %rdi
leaq -0x308(%rbp), %rsi
callq 0x11600
movb %dl, -0x439(%rbp)
movq %rax, -0x438(%rbp)
jmp 0xd576
movb -0x439(%rbp), %al
movq -0x438(%rbp), %rcx
movq %rcx, -0x318(%rbp)
movb %al, -0x310(%rbp)
leaq -0x308(%rbp), %rdi
callq 0x11680
leaq -0x268(%rbp), %rdi
callq 0x10cb0
movq %rax, %rdi
addq $0x28, %rdi
leaq -0x80(%rbp), %rsi
callq 0x114d0
movq %rax, -0x448(%rbp)
jmp 0xd5c1
movq -0x448(%rbp), %rax
movq %rax, -0x320(%rbp)
movq -0x320(%rbp), %rax
movq %rax, -0x270(%rbp)
jmp 0xd5ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0x308(%rbp), %rdi
callq 0x11680
jmp 0xd63b
jmp 0xd601
leaq -0x270(%rbp), %rdi
callq 0x10db0
movq 0x28(%rax), %rax
movq 0x50(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x50(%rax)
leaq -0xe0(%rbp), %rdi
callq 0x116a0
leaq -0x80(%rbp), %rdi
callq 0x116a0
addq $0x450, %rsp            # imm = 0x450
popq %rbp
retq
leaq -0xe0(%rbp), %rdi
callq 0x116a0
leaq -0x80(%rbp), %rdi
callq 0x116a0
movq -0xb0(%rbp), %rdi
callq 0x45c0
nopl (%rax)