// File: /workspace/github_cpp_makefile_star2_O3/CK7[P]abawaca/src/abawaca-build.cpp
// Function: Scaf_segment::Scaf_segment(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned long, unsigned long, Bio::DNAString const&)
Scaf_segment::Scaf_segment(string _display_id, string _desc, size_t s, size_t e, const Bio::DNAString& _seq) : start(s), end(e), Sequence(_display_id, _seq), desc(_desc)
{
	int k = 4;
	if(dimension2index.size() == 0) {
		prepare_mer_indexes(k, dimension2index);
	}
	dimensions.resize(dimension_order.size());

	// Compute 1,2,3 and 4-mer frequencies
	int factor = 10;
	int N_limit = 1;
	for(auto i=1; i<=k; i++, N_limit*=10, factor*=10) {
		// Shouldn't happen...
		if(seq.size() < i)
			continue;

		unordered_map<size_t, size_t>	counts;
		int 	key=0, total=0;
		auto	it_start = seq.begin(), it_end = seq.begin();

		// Add first mer
		while(it_end-it_start < i) {
			key *= 10;	// 0=00000, 2=00010, 6=00110, 19=10011
			switch(*(it_end++)) {
				case 'A':	key += 2;	break;
				case 'C':	key += 3;	break;
				case 'G':	key += 4;	break;
				case 'T':	key += 5;	break;
				default:	key = 0;
			}
		}
		if(key > N_limit) {
			counts[key]++;
 			total++;
		}
		// Rest of the mers
		while(it_end != seq.end()) {
			key *= 10;
			key = key%factor;
			switch(*(it_end++)) {
				case 'A':	key += 2;	break;
				case 'C':	key += 3;	break;
				case 'G':	key += 4;	break;
				case 'T':	key += 5;	break;
				default:	key = 0;
			}

			// The following means that there are no N's in mer)
			if(key > N_limit) {
				counts[key]++;
				total++;
			}
		}

		for(auto it=counts.begin(); it!=counts.end(); it++) {
			string mer;
			for(int i=factor; i>0; i/=10)
			switch((it->first/i)%10) {
				case 2:	mer.push_back('A');	break;
				case 3:	mer.push_back('C');	break;
				case 4:	mer.push_back('G');	break;
				case 5:	mer.push_back('T');	break;
			}
			auto sit = dimension2index.find(mer);
			if(sit == dimension2index.end()) {
				cerr << "Error: could not find index for " << mer << endl;
				exit(-1);
			}
			dimensions[sit->second] += (total != 0)? (double(it->second)/total): 0;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r9, %r13
movq %r8, -0x40(%rbp)
movq %rcx, %r12
movq %rdx, %r14
movq %rdi, %rbx
leaq -0xa0(%rbp), %rax
movq %rax, -0x10(%rax)
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
addq %rax, %rdx
leaq -0xb0(%rbp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x63a2
movq %rbx, -0x38(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0xbb1c
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x8897
callq 0x4280
leaq 0xf25a(%rip), %rax      # 0x17af8
movq -0x38(%rbp), %r13
movq %rax, (%r13)
movq %r12, 0x48(%r13)
movq -0x40(%rbp), %rax
movq %rax, 0x50(%r13)
leaq 0x58(%r13), %rdi
leaq 0x68(%r13), %rax
movq %rax, -0x80(%rbp)
movq %rax, 0x58(%r13)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
movq %rdi, -0x88(%rbp)
callq 0x63a2
leaq 0x78(%r13), %rax
movq %rax, -0x50(%rbp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x78(%r13)
movq $0x0, 0x88(%r13)
cmpq $0x0, 0xfa53(%rip)      # 0x18350
jne 0x8910
leaq 0xfa22(%rip), %rsi      # 0x18328
movl $0x4, %edi
callq 0x8783
movq 0xfa49(%rip), %rsi      # 0x18360
subq 0xfa3a(%rip), %rsi      # 0x18358
sarq $0x5, %rsi
movq -0x50(%rbp), %rdi
callq 0xbb94
movl $0xa, %eax
movq %rax, -0x40(%rbp)
movl $0x1, %eax
movq %rax, -0x48(%rbp)
movl $0x1, %edx
movabsq $-0x3333333333333333, %r15 # imm = 0xCCCCCCCCCCCCCCCD
movl $0xcccccccd, %r14d      # imm = 0xCCCCCCCD
movq 0x30(%r13), %rcx
cmpq %rdx, %rcx
jb 0x8bdb
movq %rdx, -0x58(%rbp)
leaq -0xb8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq $0x1, -0xe0(%rbp)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, -0xc8(%rbp) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
movq 0x28(%r13), %rbx
xorl %eax, %eax
xorl %r12d, %r12d
addl %r12d, %r12d
leal (%r12,%r12,4), %edx
movzbl (%rbx,%rax), %esi
cmpl $0x46, %esi
jg 0x89c2
cmpl $0x41, %esi
je 0x89d7
movl $0x0, %r12d
cmpl $0x43, %esi
jne 0x89e2
addl $0x3, %edx
jmp 0x89df
cmpl $0x47, %esi
je 0x89dc
movl $0x0, %r12d
cmpl $0x54, %esi
jne 0x89e2
addl $0x5, %edx
jmp 0x89df
addl $0x2, %edx
jmp 0x89df
addl $0x4, %edx
movl %edx, %r12d
incq %rax
cmpq -0x58(%rbp), %rax
jb 0x899d
movl $0x0, -0x2c(%rbp)
movq %rbx, %rsi
cmpl -0x48(%rbp), %r12d
jle 0x8a24
movslq %r12d, %rax
movq %rax, -0x78(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xd5a2
incq (%rax)
movq 0x28(%r13), %rsi
movq 0x30(%r13), %rcx
movl $0x1, -0x2c(%rbp)
addq -0x58(%rbp), %rbx
leaq (%rsi,%rcx), %rax
cmpq %rax, %rbx
je 0x8ab0
addl %r12d, %r12d
leal (%r12,%r12,4), %eax
cltd
idivl -0x40(%rbp)
movzbl (%rbx), %eax
movl %edx, %r12d
cmpl $0x46, %eax
jg 0x8a58
cmpl $0x41, %eax
je 0x8a6e
cmpl $0x43, %eax
jne 0x8a69
movl $0x3, %eax
jmp 0x8a7a
cmpl $0x47, %eax
je 0x8a75
cmpl $0x54, %eax
jne 0x8a69
movl $0x5, %eax
jmp 0x8a7a
xorl %r12d, %r12d
jmp 0x8aa8
movl $0x2, %eax
jmp 0x8a7a
movl $0x4, %eax
addl %eax, %r12d
cmpl -0x48(%rbp), %r12d
jle 0x8aa8
movslq %r12d, %rax
movq %rax, -0x78(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xd5a2
incq (%rax)
incl -0x2c(%rbp)
movq 0x28(%r13), %rsi
movq 0x30(%r13), %rcx
incq %rbx
jmp 0x8a28
movq -0xd8(%rbp), %rbx
testq %rbx, %rbx
je 0x8bcb
xorps %xmm0, %xmm0
cvtsi2sdl -0x2c(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
leaq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
movb $0x0, -0x68(%rbp)
movq -0x40(%rbp), %rax
movl %eax, %r12d
movq 0x8(%rbx), %rax
movl %r12d, %r13d
xorl %edx, %edx
divq %r13
movq %rax, %rcx
mulq %r15
shrq $0x2, %rdx
andq $-0x2, %rdx
leaq (%rdx,%rdx,4), %rax
negq %rax
addq %rax, %rcx
addq $-0x2, %rcx
cmpq $0x4, %rcx
jae 0x8b2f
shll $0x3, %ecx
movl $0x54474341, %eax       # imm = 0x54474341
shrl %cl, %eax
movzbl %al, %esi
leaq -0x78(%rbp), %rdi
callq 0x4240
imulq %r14, %r13
shrq $0x23, %r13
cmpl $0xa, %r12d
movl %r13d, %r12d
jae 0x8aeb
leaq 0xf7e1(%rip), %rdi      # 0x18328
leaq -0x78(%rbp), %rsi
callq 0xcf92
leaq 0xf7d9(%rip), %rcx      # 0x18330
cmpq %rcx, %rax
je 0x8c14
cmpl $0x0, -0x2c(%rbp)
je 0x8b90
movsd 0x10(%rbx), %xmm1
unpcklps 0x87ce(%rip), %xmm1     # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x87d6(%rip), %xmm1     # 0x11350
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
divsd -0x90(%rbp), %xmm0
jmp 0x8b94
xorpd %xmm0, %xmm0
movq -0x38(%rbp), %r13
movq 0x40(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rcx), %rcx
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x8bbf
callq 0x4280
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x8ad0
leaq -0xe8(%rbp), %rdi
callq 0xc68c
movq -0x58(%rbp), %rdx
incq %rdx
movq -0x48(%rbp), %rax
addl %eax, %eax
leal (%rax,%rax,4), %eax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
addl %eax, %eax
leal (%rax,%rax,4), %eax
movq %rax, -0x40(%rbp)
cmpq $0x5, %rdx
jne 0x8952
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xf3d5(%rip), %rdi      # 0x17ff0
leaq 0x89ab(%rip), %rsi      # 0x115cd
movl $0x20, %edx
callq 0x4320
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq 0xf3b5(%rip), %rdi      # 0x17ff0
callq 0x4320
movq %rax, %rdi
callq 0x4140
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4370
movq %rax, %r12
jmp 0x8ccf
movq %rax, %r12
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x8cd7
callq 0x4280
jmp 0x8cd7
movq %rax, %r12
jmp 0x8ca9
jmp 0x8c7b
movq %rax, %r12
jmp 0x8c9d
jmp 0x8c84
jmp 0x8c84
movq %rax, %r12
movq -0x38(%rbp), %r13
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x8c9d
callq 0x4280
leaq -0xe8(%rbp), %rdi
callq 0xc68c
movq -0x50(%rbp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x8cba
callq 0x4280
movq -0x88(%rbp), %rax
movq (%rax), %rdi
cmpq -0x80(%rbp), %rdi
je 0x8ccf
callq 0x4280
movq %r13, %rdi
callq 0xc630
movq %r12, %rdi
callq 0x44f0
nop