// File: CK7[P]abawaca/src/abawaca-build.cpp
// Function: calc_insert_size(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
size_t calc_insert_size(string sam_file)
{
	Bio::SAMReader  reader(sam_file);
	size_t s = 0;
	size_t	n = 0;

	while(reader.good() && (n<20000)) {
		Bio::ReadMappingPtr mapping(reader.next_mapping());
		if(mapping == NULL) {
			break;
		}

		// Note: bowtie2 computes the wrong TLEN for inserts connecting the two ends of a circular scaffold
		if(mapping->mapped() && mapping->pair_mapped() && (mapping->insert_size() > 0) && (mapping->direction() == Bio::ReadMapping::forward)) {
			s += mapping->insert_size();
			n++;
		}
	}

	return (n>0)? s/n : 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, %rsi
movq %rsi, -0x8(%rbp)
leaq -0x68(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x4140
movq -0xa8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x116c0
jmp 0xd704
leaq -0x68(%rbp), %rdi
callq 0x41c0
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x9ee0
movb %al, -0xa9(%rbp)
jmp 0xd731
movb -0xa9(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0xaa(%rbp)
jne 0xd746
jmp 0xd75a
cmpq $0x4e20, -0x88(%rbp) # imm = 0x4E20
setb %al
movb %al, -0xaa(%rbp)
movb -0xaa(%rbp), %al
testb $0x1, %al
jne 0xd769
jmp 0xd90e
leaq -0x48(%rbp), %rdi
callq 0x8b30
movq %rax, -0xb8(%rbp)
jmp 0xd77b
movq -0xb8(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x11760
jmp 0xd790
leaq -0x98(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x11790
testb $0x1, %al
jne 0xd7a6
jmp 0xd7e0
movl $0x3, -0x9c(%rbp)
jmp 0xd8ef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x41c0
jmp 0xd975
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0xd96c
leaq -0x98(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x117c0
movb %al, -0xb9(%rbp)
jmp 0xd7fc
movb -0xb9(%rbp), %al
testb $0x1, %al
jne 0xd80b
jmp 0xd8e5
leaq -0x98(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x117f0
movb %al, -0xba(%rbp)
jmp 0xd827
movb -0xba(%rbp), %al
testb $0x1, %al
jne 0xd836
jmp 0xd8e5
leaq -0x98(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x11820
movl %eax, -0xc0(%rbp)
jmp 0xd852
movl -0xc0(%rbp), %eax
cmpl $0x0, %eax
jle 0xd8e5
leaq -0x98(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x112d0
movl %eax, -0xc4(%rbp)
jmp 0xd87d
movl -0xc4(%rbp), %eax
cmpl $0x0, %eax
jne 0xd8e5
leaq -0x98(%rbp), %rdi
callq 0x111b0
movq %rax, %rdi
callq 0x11820
movl %eax, -0xc8(%rbp)
jmp 0xd8a4
movl -0xc8(%rbp), %eax
cltq
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0xd8e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x11840
jmp 0xd96c
movl $0x0, -0x9c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x11840
movl -0x9c(%rbp), %eax
testl %eax, %eax
je 0xd909
jmp 0xd907
jmp 0xd90e
jmp 0xd720
cmpq $0x0, -0x88(%rbp)
jbe 0xd930
movq -0x80(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x88(%rbp)
movq %rax, -0xd0(%rbp)
jmp 0xd93b
xorl %eax, %eax
movq %rax, -0xd0(%rbp)
jmp 0xd93b
movq -0xd0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl $0x1, -0x9c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x9f90
movq -0xd8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x9f90
movq -0x70(%rbp), %rdi
callq 0x45c0
nop